GDAL
cpl_odbc.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_odbc.h 33666 2016-03-07 05:21:07Z goatbar $
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 
30 #ifndef CPL_ODBC_H_INCLUDED
31 #define CPL_ODBC_H_INCLUDED
32 
33 #include "cpl_port.h"
34 
35 #ifdef WIN32
36 # include <windows.h>
37 #endif
38 
39 #include <sql.h>
40 #include <sqlext.h>
41 #include <odbcinst.h>
42 #include "cpl_string.h"
43 
44 #ifdef PATH_MAX
45 # define ODBC_FILENAME_MAX PATH_MAX
46 #else
47 # define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
48 #endif
49 
50 
61 {
62  char m_szPathOut[ODBC_FILENAME_MAX];
63  char m_szError[SQL_MAX_MESSAGE_LENGTH];
64  DWORD m_nErrorCode;
65  DWORD m_nUsageCount;
66 
67  public:
68 
69  // Default constructor.
71 
72 
90  int InstallDriver( const char* pszDriver, const char* pszPathIn,
91  WORD fRequest = ODBC_INSTALL_COMPLETE );
92 
109  int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
110 
111 
112  // The usage count of the driver after this function has been called
113  int GetUsageCount() const { return m_nUsageCount; }
114 
115 
116  // Path of the target directory where the driver should be installed.
117  // For details, see ODBC API Reference and lpszPathOut
118  // parameter of SQLInstallDriverEx
119  const char* GetPathOut() const { return m_szPathOut; }
120 
121 
122  // If InstallDriver returns FALSE, then GetLastError then
123  // error message can be obtained by calling this function.
124  // Internally, it calls ODBC's SQLInstallerError function.
125  const char* GetLastError() const { return m_szError; }
126 
127  // If InstallDriver returns FALSE, then GetLastErrorCode then
128  // error code can be obtained by calling this function.
129  // Internally, it calls ODBC's SQLInstallerError function.
130  // See ODBC API Reference for possible error flags.
131  DWORD GetLastErrorCode() const { return m_nErrorCode; }
132 };
133 
134 class CPLODBCStatement;
135 
136 /* On MSVC SQLULEN is missing in some cases (i.e. VC6)
137 ** but it is always a #define so test this way. On Unix
138 ** it is a typedef so we can't always do this.
139 */
140 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
141 # define MISSING_SQLULEN
142 #endif
143 
144 #if !defined(MISSING_SQLULEN)
145 /* ODBC types to support 64 bit compilation */
146 # define CPL_SQLULEN SQLULEN
147 # define CPL_SQLLEN SQLLEN
148 #else
149 # define CPL_SQLULEN SQLUINTEGER
150 # define CPL_SQLLEN SQLINTEGER
151 #endif /* ifdef SQLULEN */
152 
153 
160 class CPL_DLL CPLODBCSession {
161  char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
162  HENV m_hEnv;
163  HDBC m_hDBC;
164  int m_bInTransaction;
165  int m_bAutoCommit;
166 
167  public:
168  CPLODBCSession();
169  ~CPLODBCSession();
170 
171  int EstablishSession( const char *pszDSN,
172  const char *pszUserid,
173  const char *pszPassword );
174  const char *GetLastError();
175 
176  // Transaction handling
177 
178  int ClearTransaction();
179  int BeginTransaction();
180  int CommitTransaction();
181  int RollbackTransaction();
182  int IsInTransaction() { return m_bInTransaction; }
183 
184  // Essentially internal.
185 
186  int CloseSession();
187 
188  int Failed( int, HSTMT = NULL );
189  HDBC GetConnection() { return m_hDBC; }
190  HENV GetEnvironment() { return m_hEnv; }
191 };
192 
202 class CPL_DLL CPLODBCStatement {
203 
204  CPLODBCSession *m_poSession;
205  HSTMT m_hStmt;
206 
207  SQLSMALLINT m_nColCount;
208  char **m_papszColNames;
209  SQLSMALLINT *m_panColType;
210  char **m_papszColTypeNames;
211  CPL_SQLULEN *m_panColSize;
212  SQLSMALLINT *m_panColPrecision;
213  SQLSMALLINT *m_panColNullable;
214  char **m_papszColColumnDef;
215 
216  char **m_papszColValues;
217  CPL_SQLLEN *m_panColValueLengths;
218 
219  int Failed( int );
220 
221  char *m_pszStatement;
222  size_t m_nStatementMax;
223  size_t m_nStatementLen;
224 
225  public:
227  ~CPLODBCStatement();
228 
229  HSTMT GetStatement() { return m_hStmt; }
230 
231  // Command buffer related.
232  void Clear();
233  void AppendEscaped( const char * );
234  void Append( const char * );
235  void Append( int );
236  void Append( double );
237  int Appendf( const char *, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
238  const char *GetCommand() { return m_pszStatement; }
239 
240  int ExecuteSQL( const char * = NULL );
241 
242  // Results fetching
243  int Fetch( int nOrientation = SQL_FETCH_NEXT,
244  int nOffset = 0 );
245  void ClearColumnData();
246 
247  int GetColCount();
248  const char *GetColName( int );
249  short GetColType( int );
250  const char *GetColTypeName( int );
251  short GetColSize( int );
252  short GetColPrecision( int );
253  short GetColNullable( int );
254  const char *GetColColumnDef( int );
255 
256  int GetColId( const char * );
257  const char *GetColData( int, const char * = NULL );
258  const char *GetColData( const char *, const char * = NULL );
259  int GetColDataLength( int );
260  int GetRowCountAffected();
261 
262  // Fetch special metadata.
263  int GetColumns( const char *pszTable,
264  const char *pszCatalog = NULL,
265  const char *pszSchema = NULL );
266  int GetPrimaryKeys( const char *pszTable,
267  const char *pszCatalog = NULL,
268  const char *pszSchema = NULL );
269 
270  int GetTables( const char *pszCatalog = NULL,
271  const char *pszSchema = NULL );
272 
273  void DumpResult( FILE *fp, int bShowSchema = FALSE );
274 
275  static CPLString GetTypeName( int );
276  static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
277 
278  int CollectResultsInfo();
279 };
280 
281 #endif
Core portability definitions for CPL.
A class representing an ODBC database session.
Definition: cpl_odbc.h:160
Convenient string class based on std::string.
Definition: cpl_string.h:283
Abstraction for statement, and resultset.
Definition: cpl_odbc.h:202
Various convenience functions for working with strings and string lists.
A class providing functions to install or remove ODBC driver.
Definition: cpl_odbc.h:60

Generated for GDAL by doxygen 1.8.11.