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  static bool FindMdbToolsDriverLib( CPLString& osDriverFile );
69  static bool LibraryExists( const char* pszLibPath );
70 
71  public:
72 
73  // Default constructor.
75 
93  int InstallDriver( const char* pszDriver, const char* pszPathIn,
94  WORD fRequest = ODBC_INSTALL_COMPLETE );
95 
103  static void InstallMdbToolsDriver();
104 
121  int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
122 
124  int GetUsageCount() const { return m_nUsageCount; }
125 
130  const char* GetPathOut() const { return m_szPathOut; }
131 
136  const char* GetLastError() const { return m_szError; }
137 
143  DWORD GetLastErrorCode() const { return m_nErrorCode; }
144 };
145 
146 class CPLODBCStatement;
147 
148 /* On MSVC SQLULEN is missing in some cases (i.e. VC6)
149 ** but it is always a #define so test this way. On Unix
150 ** it is a typedef so we can't always do this.
151 */
152 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
153 # define MISSING_SQLULEN
154 #endif
155 
157 #if !defined(MISSING_SQLULEN)
158 /* ODBC types to support 64 bit compilation */
159 # define CPL_SQLULEN SQLULEN
160 # define CPL_SQLLEN SQLLEN
161 #else
162 # define CPL_SQLULEN SQLUINTEGER
163 # define CPL_SQLLEN SQLINTEGER
164 #endif /* ifdef SQLULEN */
165 
173 class CPL_DLL CPLODBCSession {
174 
176 
177 
178  protected:
179  CPLString m_osLastError{};
180  HENV m_hEnv = nullptr;
181  HDBC m_hDBC = nullptr;
182  int m_bInTransaction = false;
183  int m_bAutoCommit = true;
186  public:
187  CPLODBCSession();
188  ~CPLODBCSession();
189 
190  int EstablishSession( const char *pszDSN,
191  const char *pszUserid,
192  const char *pszPassword );
193  const char *GetLastError();
194 
195  // Transaction handling
196 
197  int ClearTransaction();
198  int BeginTransaction();
199  int CommitTransaction();
200  int RollbackTransaction();
202  int IsInTransaction() { return m_bInTransaction; }
203 
204  // Essentially internal.
205 
206  int CloseSession();
207 
208  int Failed( int, HSTMT = nullptr );
210  HDBC GetConnection() { return m_hDBC; }
212  HENV GetEnvironment() { return m_hEnv; }
213 
214  bool ConnectToMsAccess( const char * pszName, const char* pszDSNStringTemplate );
215 
216 };
217 
227 class CPL_DLL CPLODBCStatement {
228 
230 
231 
232  protected:
233  int m_nFlags = 0;
234 
235  CPLODBCSession *m_poSession = nullptr;
236  HSTMT m_hStmt = nullptr;
237 
238  SQLSMALLINT m_nColCount = 0;
239  char **m_papszColNames = nullptr;
240  SQLSMALLINT *m_panColType = nullptr;
241  char **m_papszColTypeNames = nullptr;
242  CPL_SQLULEN *m_panColSize = nullptr;
243  SQLSMALLINT *m_panColPrecision = nullptr;
244  SQLSMALLINT *m_panColNullable = nullptr;
245  char **m_papszColColumnDef = nullptr;
246 
247  char **m_papszColValues = nullptr;
248  CPL_SQLLEN *m_panColValueLengths = nullptr;
249  double *m_padColValuesAsDouble = nullptr;
250 
251  int Failed( int );
252 
253  char *m_pszStatement = nullptr;
254  size_t m_nStatementMax = 0;
255  size_t m_nStatementLen = 0;
258  public:
259 
263  enum Flag
264  {
275  RetrieveNumericColumnsAsDouble = 1 << 0,
276  };
277 
278  explicit CPLODBCStatement( CPLODBCSession *, int flags = 0 );
279  ~CPLODBCStatement();
280 
282  HSTMT GetStatement() { return m_hStmt; }
283 
287  int Flags() const { return m_nFlags; }
288 
289  // Command buffer related.
290  void Clear();
291  void AppendEscaped( const char * );
292  void Append( const char * );
293  void Append( int );
294  void Append( double );
295  int Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
297  const char *GetCommand() { return m_pszStatement; }
298 
299  int ExecuteSQL( const char * = nullptr );
300 
301  // Results fetching
302  int Fetch( int nOrientation = SQL_FETCH_NEXT,
303  int nOffset = 0 );
304  void ClearColumnData();
305 
306  int GetColCount();
307  const char *GetColName( int );
308  short GetColType( int );
309  const char *GetColTypeName( int );
310  short GetColSize( int );
311  short GetColPrecision( int );
312  short GetColNullable( int );
313  const char *GetColColumnDef( int );
314 
315  int GetColId( const char * ) const;
316  const char *GetColData( int, const char * = nullptr );
317  const char *GetColData( const char *, const char * = nullptr );
318  int GetColDataLength( int );
319 
320  double GetColDataAsDouble( int ) const;
321  double GetColDataAsDouble( const char * ) const;
322 
323  int GetRowCountAffected();
324 
325  // Fetch special metadata.
326  int GetColumns( const char *pszTable,
327  const char *pszCatalog = nullptr,
328  const char *pszSchema = nullptr );
329  int GetPrimaryKeys( const char *pszTable,
330  const char *pszCatalog = nullptr,
331  const char *pszSchema = nullptr );
332 
333  int GetTables( const char *pszCatalog = nullptr,
334  const char *pszSchema = nullptr );
335 
336  void DumpResult( FILE *fp, int bShowSchema = FALSE );
337 
338  static CPLString GetTypeName( int );
339  static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
340 
341  int CollectResultsInfo();
342 };
343 
344 #endif
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:844
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:320
CPLODBCStatement::GetStatement
HSTMT GetStatement()
Return statement handle.
Definition: cpl_odbc.h:282
CPLODBCSession::GetConnection
HDBC GetConnection()
Return connection handle.
Definition: cpl_odbc.h:210
CPLODBCSession::GetEnvironment
HENV GetEnvironment()
Return GetEnvironment handle.
Definition: cpl_odbc.h:212
CPLODBCStatement
Abstraction for statement, and resultset.
Definition: cpl_odbc.h:227
cpl_string.h
CPLODBCSession
A class representing an ODBC database session.
Definition: cpl_odbc.h:173
CPLODBCDriverInstaller::GetLastError
const char * GetLastError() const
If InstallDriver returns FALSE, then GetLastError then error message can be obtained by calling this ...
Definition: cpl_odbc.h:136
CPLODBCDriverInstaller::GetPathOut
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition: cpl_odbc.h:130
CPLODBCDriverInstaller::GetLastErrorCode
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition: cpl_odbc.h:143
CPL_FORMAT_STRING
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:859
cpl_port.h
CPLODBCDriverInstaller::GetUsageCount
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition: cpl_odbc.h:124
CPLODBCStatement::Flags
int Flags() const
Returns statement flags.
Definition: cpl_odbc.h:287
CPLODBCDriverInstaller
A class providing functions to install or remove ODBC driver.
Definition: cpl_odbc.h:61
CPLODBCSession::IsInTransaction
int IsInTransaction()
Returns whether a transaction is active.
Definition: cpl_odbc.h:202
CPL_DISALLOW_COPY_ASSIGN
#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:930
CPLODBCStatement::Flag
Flag
Flags which control ODBC statement behavior.
Definition: cpl_odbc.h:263