GDAL
cpl_odbc.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_odbc.h 36675 2016-12-04 11:10:10Z rouault $
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 
45 #ifdef PATH_MAX
46 # define ODBC_FILENAME_MAX PATH_MAX
47 #else
48 # define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
49 #endif
50 
62 {
63  char m_szPathOut[ODBC_FILENAME_MAX];
64  char m_szError[SQL_MAX_MESSAGE_LENGTH];
65  DWORD m_nErrorCode;
66  DWORD m_nUsageCount;
67 
68  public:
69 
70  // Default constructor.
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 
112  int GetUsageCount() const { return m_nUsageCount; }
113 
118  const char* GetPathOut() const { return m_szPathOut; }
119 
124  const char* GetLastError() const { return m_szError; }
125 
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 
145 #if !defined(MISSING_SQLULEN)
146 /* ODBC types to support 64 bit compilation */
147 # define CPL_SQLULEN SQLULEN
148 # define CPL_SQLLEN SQLLEN
149 #else
150 # define CPL_SQLULEN SQLUINTEGER
151 # define CPL_SQLLEN SQLINTEGER
152 #endif /* ifdef SQLULEN */
153 
161 class CPL_DLL CPLODBCSession {
162  char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
163  HENV m_hEnv;
164  HDBC m_hDBC;
165  int m_bInTransaction;
166  int m_bAutoCommit;
167 
168  public:
169  CPLODBCSession();
170  ~CPLODBCSession();
171 
172  int EstablishSession( const char *pszDSN,
173  const char *pszUserid,
174  const char *pszPassword );
175  const char *GetLastError();
176 
177  // Transaction handling
178 
179  int ClearTransaction();
180  int BeginTransaction();
181  int CommitTransaction();
182  int RollbackTransaction();
184  int IsInTransaction() { return m_bInTransaction; }
185 
186  // Essentially internal.
187 
188  int CloseSession();
189 
190  int Failed( int, HSTMT = NULL );
192  HDBC GetConnection() { return m_hDBC; }
194  HENV GetEnvironment() { return m_hEnv; }
195 };
196 
206 class CPL_DLL CPLODBCStatement {
207 
208  CPLODBCSession *m_poSession;
209  HSTMT m_hStmt;
210 
211  SQLSMALLINT m_nColCount;
212  char **m_papszColNames;
213  SQLSMALLINT *m_panColType;
214  char **m_papszColTypeNames;
215  CPL_SQLULEN *m_panColSize;
216  SQLSMALLINT *m_panColPrecision;
217  SQLSMALLINT *m_panColNullable;
218  char **m_papszColColumnDef;
219 
220  char **m_papszColValues;
221  CPL_SQLLEN *m_panColValueLengths;
222 
223  int Failed( int );
224 
225  char *m_pszStatement;
226  size_t m_nStatementMax;
227  size_t m_nStatementLen;
228 
229  public:
230  explicit CPLODBCStatement( CPLODBCSession * );
231  ~CPLODBCStatement();
232 
234  HSTMT GetStatement() { return m_hStmt; }
235 
236  // Command buffer related.
237  void Clear();
238  void AppendEscaped( const char * );
239  void Append( const char * );
240  void Append( int );
241  void Append( double );
242  int Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
244  const char *GetCommand() { return m_pszStatement; }
245 
246  int ExecuteSQL( const char * = NULL );
247 
248  // Results fetching
249  int Fetch( int nOrientation = SQL_FETCH_NEXT,
250  int nOffset = 0 );
251  void ClearColumnData();
252 
253  int GetColCount();
254  const char *GetColName( int );
255  short GetColType( int );
256  const char *GetColTypeName( int );
257  short GetColSize( int );
258  short GetColPrecision( int );
259  short GetColNullable( int );
260  const char *GetColColumnDef( int );
261 
262  int GetColId( const char * );
263  const char *GetColData( int, const char * = NULL );
264  const char *GetColData( const char *, const char * = NULL );
265  int GetColDataLength( int );
266  int GetRowCountAffected();
267 
268  // Fetch special metadata.
269  int GetColumns( const char *pszTable,
270  const char *pszCatalog = NULL,
271  const char *pszSchema = NULL );
272  int GetPrimaryKeys( const char *pszTable,
273  const char *pszCatalog = NULL,
274  const char *pszSchema = NULL );
275 
276  int GetTables( const char *pszCatalog = NULL,
277  const char *pszSchema = NULL );
278 
279  void DumpResult( FILE *fp, int bShowSchema = FALSE );
280 
281  static CPLString GetTypeName( int );
282  static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
283 
284  int CollectResultsInfo();
285 };
286 
287 #endif
int IsInTransaction()
Returns whether a transaction is active.
Definition: cpl_odbc.h:184
Core portability definitions for CPL.
A class representing an ODBC database session.
Definition: cpl_odbc.h:161
const char * GetLastError() const
If InstallDriver returns FALSE, then GetLastError then error message can be obtained by calling this ...
Definition: cpl_odbc.h:124
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition: cpl_odbc.h:112
HDBC GetConnection()
Return connection handle.
Definition: cpl_odbc.h:192
Convenient string class based on std::string.
Definition: cpl_string.h:338
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:966
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition: cpl_odbc.h:131
Abstraction for statement, and resultset.
Definition: cpl_odbc.h:206
Various convenience functions for working with strings and string lists.
A class providing functions to install or remove ODBC driver.
Definition: cpl_odbc.h:61
HENV GetEnvironment()
Return GetEnvironment handle.
Definition: cpl_odbc.h:194
HSTMT GetStatement()
Return statement handle.
Definition: cpl_odbc.h:234
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:988
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition: cpl_odbc.h:118

Generated for GDAL by doxygen 1.8.8.