cpl_odbc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /******************************************************************************
  2. * $Id: cpl_odbc.h 29025 2015-04-26 11:50:20Z tamas $
  3. *
  4. * Project: OGR ODBC Driver
  5. * Purpose: Declarations for ODBC Access Cover API.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2003, Frank Warmerdam
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. ****************************************************************************/
  29. #ifndef CPL_ODBC_H_INCLUDED
  30. #define CPL_ODBC_H_INCLUDED
  31. #include "cpl_port.h"
  32. #ifndef WIN32CE /* ODBC is not supported on Windows CE. */
  33. #ifdef WIN32
  34. # include <windows.h>
  35. #endif
  36. #include <sql.h>
  37. #include <sqlext.h>
  38. #include <odbcinst.h>
  39. #include "cpl_string.h"
  40. #ifdef PATH_MAX
  41. # define ODBC_FILENAME_MAX PATH_MAX
  42. #else
  43. # define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
  44. #endif
  45. /**
  46. * \file cpl_odbc.h
  47. *
  48. * ODBC Abstraction Layer (C++).
  49. */
  50. /**
  51. * A class providing functions to install or remove ODBC driver.
  52. */
  53. class CPL_DLL CPLODBCDriverInstaller
  54. {
  55. char m_szPathOut[ODBC_FILENAME_MAX];
  56. char m_szError[SQL_MAX_MESSAGE_LENGTH];
  57. DWORD m_nErrorCode;
  58. DWORD m_nUsageCount;
  59. public:
  60. // Default constructor.
  61. CPLODBCDriverInstaller();
  62. /**
  63. * Installs ODBC driver or updates definition of already installed driver.
  64. * Interanally, it calls ODBC's SQLInstallDriverEx function.
  65. *
  66. * @param pszDriver - The driver definition as a list of keyword-value
  67. * pairs describing the driver (See ODBC API Reference).
  68. *
  69. * @param pszPathIn - Full path of the target directory of the installation,
  70. * or a null pointer (for unixODBC, NULL is passed).
  71. *
  72. * @param fRequest - The fRequest argument must contain one of
  73. * the following values:
  74. * ODBC_INSTALL_COMPLETE - (default) complete the installation request
  75. * ODBC_INSTALL_INQUIRY - inquire about where a driver can be installed
  76. *
  77. * @return TRUE indicates success, FALSE if it fails.
  78. */
  79. int InstallDriver( const char* pszDriver, const char* pszPathIn,
  80. WORD fRequest = ODBC_INSTALL_COMPLETE );
  81. /**
  82. * Removes or changes information about the driver from
  83. * the Odbcinst.ini entry in the system information.
  84. *
  85. * @param pszDriverName - The name of the driver as registered in
  86. * the Odbcinst.ini key of the system information.
  87. *
  88. * @param fRemoveDSN - TRUE: Remove DSNs associated with the driver
  89. * specified in lpszDriver. FALSE: Do not remove DSNs associated
  90. * with the driver specified in lpszDriver.
  91. *
  92. * @return The function returns TRUE if it is successful,
  93. * FALSE if it fails. If no entry exists in the system information
  94. * when this function is called, the function returns FALSE.
  95. * In order to obtain usage count value, call GetUsageCount().
  96. */
  97. int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
  98. // The usage count of the driver after this function has been called
  99. int GetUsageCount() const { return m_nUsageCount; }
  100. // Path of the target directory where the driver should be installed.
  101. // For details, see ODBC API Reference and lpszPathOut
  102. // parameter of SQLInstallDriverEx
  103. const char* GetPathOut() const { return m_szPathOut; }
  104. // If InstallDriver returns FALSE, then GetLastError then
  105. // error message can be obtained by calling this function.
  106. // Internally, it calls ODBC's SQLInstallerError function.
  107. const char* GetLastError() const { return m_szError; }
  108. // If InstallDriver returns FALSE, then GetLastErrorCode then
  109. // error code can be obtained by calling this function.
  110. // Internally, it calls ODBC's SQLInstallerError function.
  111. // See ODBC API Reference for possible error flags.
  112. DWORD GetLastErrorCode() const { return m_nErrorCode; }
  113. };
  114. class CPLODBCStatement;
  115. /* On MSVC SQLULEN is missing in some cases (ie. VC6)
  116. ** but it is always a #define so test this way. On Unix
  117. ** it is a typedef so we can't always do this.
  118. */
  119. #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
  120. # define MISSING_SQLULEN
  121. #endif
  122. #if !defined(MISSING_SQLULEN)
  123. /* ODBC types to support 64 bit compilation */
  124. # define _SQLULEN SQLULEN
  125. # define _SQLLEN SQLLEN
  126. #else
  127. # define _SQLULEN SQLUINTEGER
  128. # define _SQLLEN SQLINTEGER
  129. #endif /* ifdef SQLULEN */
  130. /**
  131. * A class representing an ODBC database session.
  132. *
  133. * Includes error collection services.
  134. */
  135. class CPL_DLL CPLODBCSession {
  136. char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
  137. HENV m_hEnv;
  138. HDBC m_hDBC;
  139. int m_bInTransaction;
  140. int m_bAutoCommit;
  141. public:
  142. CPLODBCSession();
  143. ~CPLODBCSession();
  144. int EstablishSession( const char *pszDSN,
  145. const char *pszUserid,
  146. const char *pszPassword );
  147. const char *GetLastError();
  148. // Transaction handling
  149. int ClearTransaction();
  150. int BeginTransaction();
  151. int CommitTransaction();
  152. int RollbackTransaction();
  153. int IsInTransaction() { return m_bInTransaction; }
  154. // Essentially internal.
  155. int CloseSession();
  156. int Failed( int, HSTMT = NULL );
  157. HDBC GetConnection() { return m_hDBC; }
  158. HENV GetEnvironment() { return m_hEnv; }
  159. };
  160. /**
  161. * Abstraction for statement, and resultset.
  162. *
  163. * Includes methods for executing an SQL statement, and for accessing the
  164. * resultset from that statement. Also provides for executing other ODBC
  165. * requests that produce results sets such as SQLColumns() and SQLTables()
  166. * requests.
  167. */
  168. class CPL_DLL CPLODBCStatement {
  169. CPLODBCSession *m_poSession;
  170. HSTMT m_hStmt;
  171. SQLSMALLINT m_nColCount;
  172. char **m_papszColNames;
  173. SQLSMALLINT *m_panColType;
  174. char **m_papszColTypeNames;
  175. _SQLULEN *m_panColSize;
  176. SQLSMALLINT *m_panColPrecision;
  177. SQLSMALLINT *m_panColNullable;
  178. char **m_papszColColumnDef;
  179. char **m_papszColValues;
  180. _SQLLEN *m_panColValueLengths;
  181. int Failed( int );
  182. char *m_pszStatement;
  183. size_t m_nStatementMax;
  184. size_t m_nStatementLen;
  185. public:
  186. CPLODBCStatement( CPLODBCSession * );
  187. ~CPLODBCStatement();
  188. HSTMT GetStatement() { return m_hStmt; }
  189. // Command buffer related.
  190. void Clear();
  191. void AppendEscaped( const char * );
  192. void Append( const char * );
  193. void Append( int );
  194. void Append( double );
  195. int Appendf( const char *, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
  196. const char *GetCommand() { return m_pszStatement; }
  197. int ExecuteSQL( const char * = NULL );
  198. // Results fetching
  199. int Fetch( int nOrientation = SQL_FETCH_NEXT,
  200. int nOffset = 0 );
  201. void ClearColumnData();
  202. int GetColCount();
  203. const char *GetColName( int );
  204. short GetColType( int );
  205. const char *GetColTypeName( int );
  206. short GetColSize( int );
  207. short GetColPrecision( int );
  208. short GetColNullable( int );
  209. const char *GetColColumnDef( int );
  210. int GetColId( const char * );
  211. const char *GetColData( int, const char * = NULL );
  212. const char *GetColData( const char *, const char * = NULL );
  213. int GetColDataLength( int );
  214. int GetRowCountAffected();
  215. // Fetch special metadata.
  216. int GetColumns( const char *pszTable,
  217. const char *pszCatalog = NULL,
  218. const char *pszSchema = NULL );
  219. int GetPrimaryKeys( const char *pszTable,
  220. const char *pszCatalog = NULL,
  221. const char *pszSchema = NULL );
  222. int GetTables( const char *pszCatalog = NULL,
  223. const char *pszSchema = NULL );
  224. void DumpResult( FILE *fp, int bShowSchema = FALSE );
  225. static CPLString GetTypeName( int );
  226. static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
  227. int CollectResultsInfo();
  228. };
  229. #endif /* #ifndef WIN32CE */
  230. #endif