gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
9.8KB

  1. /******************************************************************************
  2. * $Id: cpl_odbc.h 97890c6814b40588defbe147a3189f056d50cf97 2018-05-14 20:12:48 +0200 KovBal $
  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. #ifdef WIN32
  33. # include <windows.h>
  34. #endif
  35. #include <sql.h>
  36. #include <sqlext.h>
  37. #include <odbcinst.h>
  38. #include "cpl_string.h"
  39. /*! @cond Doxygen_Suppress */
  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. /*! @endcond */
  46. /**
  47. * \file cpl_odbc.h
  48. *
  49. * ODBC Abstraction Layer (C++).
  50. */
  51. /**
  52. * A class providing functions to install or remove ODBC driver.
  53. */
  54. class CPL_DLL CPLODBCDriverInstaller
  55. {
  56. char m_szPathOut[ODBC_FILENAME_MAX];
  57. char m_szError[SQL_MAX_MESSAGE_LENGTH];
  58. DWORD m_nErrorCode;
  59. DWORD m_nUsageCount;
  60. public:
  61. // Default constructor.
  62. CPLODBCDriverInstaller();
  63. /**
  64. * Installs ODBC driver or updates definition of already installed driver.
  65. * Interanally, it calls ODBC's SQLInstallDriverEx function.
  66. *
  67. * @param pszDriver - The driver definition as a list of keyword-value
  68. * pairs describing the driver (See ODBC API Reference).
  69. *
  70. * @param pszPathIn - Full path of the target directory of the installation,
  71. * or a null pointer (for unixODBC, NULL is passed).
  72. *
  73. * @param fRequest - The fRequest argument must contain one of
  74. * the following values:
  75. * ODBC_INSTALL_COMPLETE - (default) complete the installation request
  76. * ODBC_INSTALL_INQUIRY - inquire about where a driver can be installed
  77. *
  78. * @return TRUE indicates success, FALSE if it fails.
  79. */
  80. int InstallDriver( const char* pszDriver, const char* pszPathIn,
  81. WORD fRequest = ODBC_INSTALL_COMPLETE );
  82. /**
  83. * Removes or changes information about the driver from
  84. * the Odbcinst.ini entry in the system information.
  85. *
  86. * @param pszDriverName - The name of the driver as registered in
  87. * the Odbcinst.ini key of the system information.
  88. *
  89. * @param fRemoveDSN - TRUE: Remove DSNs associated with the driver
  90. * specified in lpszDriver. FALSE: Do not remove DSNs associated
  91. * with the driver specified in lpszDriver.
  92. *
  93. * @return The function returns TRUE if it is successful,
  94. * FALSE if it fails. If no entry exists in the system information
  95. * when this function is called, the function returns FALSE.
  96. * In order to obtain usage count value, call GetUsageCount().
  97. */
  98. int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
  99. /** The usage count of the driver after this function has been called */
  100. int GetUsageCount() const { return m_nUsageCount; }
  101. /** Path of the target directory where the driver should be installed.
  102. * For details, see ODBC API Reference and lpszPathOut
  103. * parameter of SQLInstallDriverEx
  104. */
  105. const char* GetPathOut() const { return m_szPathOut; }
  106. /** If InstallDriver returns FALSE, then GetLastError then
  107. * error message can be obtained by calling this function.
  108. * Internally, it calls ODBC's SQLInstallerError function.
  109. */
  110. const char* GetLastError() const { return m_szError; }
  111. /** If InstallDriver returns FALSE, then GetLastErrorCode then
  112. * error code can be obtained by calling this function.
  113. * Internally, it calls ODBC's SQLInstallerError function.
  114. * See ODBC API Reference for possible error flags.
  115. */
  116. DWORD GetLastErrorCode() const { return m_nErrorCode; }
  117. };
  118. class CPLODBCStatement;
  119. /* On MSVC SQLULEN is missing in some cases (i.e. VC6)
  120. ** but it is always a #define so test this way. On Unix
  121. ** it is a typedef so we can't always do this.
  122. */
  123. #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
  124. # define MISSING_SQLULEN
  125. #endif
  126. /*! @cond Doxygen_Suppress */
  127. #if !defined(MISSING_SQLULEN)
  128. /* ODBC types to support 64 bit compilation */
  129. # define CPL_SQLULEN SQLULEN
  130. # define CPL_SQLLEN SQLLEN
  131. #else
  132. # define CPL_SQLULEN SQLUINTEGER
  133. # define CPL_SQLLEN SQLINTEGER
  134. #endif /* ifdef SQLULEN */
  135. /*! @endcond */
  136. /**
  137. * A class representing an ODBC database session.
  138. *
  139. * Includes error collection services.
  140. */
  141. class CPL_DLL CPLODBCSession {
  142. CPL_DISALLOW_COPY_ASSIGN(CPLODBCSession)
  143. CPLString m_osLastError{};
  144. HENV m_hEnv = nullptr;
  145. HDBC m_hDBC = nullptr;
  146. int m_bInTransaction = false;
  147. int m_bAutoCommit = true;
  148. public:
  149. CPLODBCSession();
  150. ~CPLODBCSession();
  151. int EstablishSession( const char *pszDSN,
  152. const char *pszUserid,
  153. const char *pszPassword );
  154. const char *GetLastError();
  155. // Transaction handling
  156. int ClearTransaction();
  157. int BeginTransaction();
  158. int CommitTransaction();
  159. int RollbackTransaction();
  160. /** Returns whether a transaction is active */
  161. int IsInTransaction() { return m_bInTransaction; }
  162. // Essentially internal.
  163. int CloseSession();
  164. int Failed( int, HSTMT = nullptr );
  165. /** Return connection handle */
  166. HDBC GetConnection() { return m_hDBC; }
  167. /** Return GetEnvironment handle */
  168. HENV GetEnvironment() { return m_hEnv; }
  169. };
  170. /**
  171. * Abstraction for statement, and resultset.
  172. *
  173. * Includes methods for executing an SQL statement, and for accessing the
  174. * resultset from that statement. Also provides for executing other ODBC
  175. * requests that produce results sets such as SQLColumns() and SQLTables()
  176. * requests.
  177. */
  178. class CPL_DLL CPLODBCStatement {
  179. CPL_DISALLOW_COPY_ASSIGN(CPLODBCStatement)
  180. CPLODBCSession *m_poSession = nullptr;
  181. HSTMT m_hStmt = nullptr;
  182. SQLSMALLINT m_nColCount = 0;
  183. char **m_papszColNames = nullptr;
  184. SQLSMALLINT *m_panColType = nullptr;
  185. char **m_papszColTypeNames = nullptr;
  186. CPL_SQLULEN *m_panColSize = nullptr;
  187. SQLSMALLINT *m_panColPrecision = nullptr;
  188. SQLSMALLINT *m_panColNullable = nullptr;
  189. char **m_papszColColumnDef = nullptr;
  190. char **m_papszColValues = nullptr;
  191. CPL_SQLLEN *m_panColValueLengths = nullptr;
  192. int Failed( int );
  193. char *m_pszStatement = nullptr;
  194. size_t m_nStatementMax = 0;
  195. size_t m_nStatementLen = 0;
  196. public:
  197. explicit CPLODBCStatement( CPLODBCSession * );
  198. ~CPLODBCStatement();
  199. /** Return statement handle */
  200. HSTMT GetStatement() { return m_hStmt; }
  201. // Command buffer related.
  202. void Clear();
  203. void AppendEscaped( const char * );
  204. void Append( const char * );
  205. void Append( int );
  206. void Append( double );
  207. int Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
  208. /** Return statement string */
  209. const char *GetCommand() { return m_pszStatement; }
  210. int ExecuteSQL( const char * = nullptr );
  211. // Results fetching
  212. int Fetch( int nOrientation = SQL_FETCH_NEXT,
  213. int nOffset = 0 );
  214. void ClearColumnData();
  215. int GetColCount();
  216. const char *GetColName( int );
  217. short GetColType( int );
  218. const char *GetColTypeName( int );
  219. short GetColSize( int );
  220. short GetColPrecision( int );
  221. short GetColNullable( int );
  222. const char *GetColColumnDef( int );
  223. int GetColId( const char * );
  224. const char *GetColData( int, const char * = nullptr );
  225. const char *GetColData( const char *, const char * = nullptr );
  226. int GetColDataLength( int );
  227. int GetRowCountAffected();
  228. // Fetch special metadata.
  229. int GetColumns( const char *pszTable,
  230. const char *pszCatalog = nullptr,
  231. const char *pszSchema = nullptr );
  232. int GetPrimaryKeys( const char *pszTable,
  233. const char *pszCatalog = nullptr,
  234. const char *pszSchema = nullptr );
  235. int GetTables( const char *pszCatalog = nullptr,
  236. const char *pszSchema = nullptr );
  237. void DumpResult( FILE *fp, int bShowSchema = FALSE );
  238. static CPLString GetTypeName( int );
  239. static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
  240. int CollectResultsInfo();
  241. };
  242. #endif
上海开阖软件有限公司 沪ICP备12045867号-1