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

Generated for GDAL by doxygen 1.8.11.