GDAL
cpl_odbc.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
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 
164 
165  char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1] = { '\0' };
166  HENV m_hEnv = nullptr;
167  HDBC m_hDBC = nullptr;
168  int m_bInTransaction = false;
169  int m_bAutoCommit = true;
170 
171  public:
172  CPLODBCSession();
173  ~CPLODBCSession();
174 
175  int EstablishSession( const char *pszDSN,
176  const char *pszUserid,
177  const char *pszPassword );
178  const char *GetLastError();
179 
180  // Transaction handling
181 
182  int ClearTransaction();
183  int BeginTransaction();
184  int CommitTransaction();
185  int RollbackTransaction();
187  int IsInTransaction() { return m_bInTransaction; }
188 
189  // Essentially internal.
190 
191  int CloseSession();
192 
193  int Failed( int, HSTMT = nullptr );
195  HDBC GetConnection() { return m_hDBC; }
197  HENV GetEnvironment() { return m_hEnv; }
198 };
199 
209 class CPL_DLL CPLODBCStatement {
210 
212 
213  CPLODBCSession *m_poSession = nullptr;
214  HSTMT m_hStmt = nullptr;
215 
216  SQLSMALLINT m_nColCount = 0;
217  char **m_papszColNames = nullptr;
218  SQLSMALLINT *m_panColType = nullptr;
219  char **m_papszColTypeNames = nullptr;
220  CPL_SQLULEN *m_panColSize = nullptr;
221  SQLSMALLINT *m_panColPrecision = nullptr;
222  SQLSMALLINT *m_panColNullable = nullptr;
223  char **m_papszColColumnDef = nullptr;
224 
225  char **m_papszColValues = nullptr;
226  CPL_SQLLEN *m_panColValueLengths = nullptr;
227 
228  int Failed( int );
229 
230  char *m_pszStatement = nullptr;
231  size_t m_nStatementMax = 0;
232  size_t m_nStatementLen = 0;
233 
234  public:
235  explicit CPLODBCStatement( CPLODBCSession * );
236  ~CPLODBCStatement();
237 
239  HSTMT GetStatement() { return m_hStmt; }
240 
241  // Command buffer related.
242  void Clear();
243  void AppendEscaped( const char * );
244  void Append( const char * );
245  void Append( int );
246  void Append( double );
247  int Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
249  const char *GetCommand() { return m_pszStatement; }
250 
251  int ExecuteSQL( const char * = nullptr );
252 
253  // Results fetching
254  int Fetch( int nOrientation = SQL_FETCH_NEXT,
255  int nOffset = 0 );
256  void ClearColumnData();
257 
258  int GetColCount();
259  const char *GetColName( int );
260  short GetColType( int );
261  const char *GetColTypeName( int );
262  short GetColSize( int );
263  short GetColPrecision( int );
264  short GetColNullable( int );
265  const char *GetColColumnDef( int );
266 
267  int GetColId( const char * );
268  const char *GetColData( int, const char * = nullptr );
269  const char *GetColData( const char *, const char * = nullptr );
270  int GetColDataLength( int );
271  int GetRowCountAffected();
272 
273  // Fetch special metadata.
274  int GetColumns( const char *pszTable,
275  const char *pszCatalog = nullptr,
276  const char *pszSchema = nullptr );
277  int GetPrimaryKeys( const char *pszTable,
278  const char *pszCatalog = nullptr,
279  const char *pszSchema = nullptr );
280 
281  int GetTables( const char *pszCatalog = nullptr,
282  const char *pszSchema = nullptr );
283 
284  void DumpResult( FILE *fp, int bShowSchema = FALSE );
285 
286  static CPLString GetTypeName( int );
287  static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
288 
289  int CollectResultsInfo();
290 };
291 
292 #endif
int IsInTransaction()
Returns whether a transaction is active.
Definition: cpl_odbc.h:187
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:195
Convenient string class based on std::string.
Definition: cpl_string.h:327
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:904
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:209
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:197
HSTMT GetStatement()
Return statement handle.
Definition: cpl_odbc.h:239
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:919
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition: cpl_odbc.h:118
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:987

Generated for GDAL by doxygen 1.8.8.