GDAL
cpl_odbc.h
Go to the documentation of this file.
1/******************************************************************************
2 * $Id: cpl_odbc.h 658d4f6254f467214d228ee9513838e9b1f878f9 2021-08-27 15:00:59 +1000 Nyall Dawson $
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
51
57
61class CPL_DLL CPLODBCDriverInstaller
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.
74 CPLODBCDriverInstaller();
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
146class 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 */
166
172
173class CPL_DLL CPLODBCSession {
174
176
177 CPLString m_osLastError{};
178 HENV m_hEnv = nullptr;
179 HDBC m_hDBC = nullptr;
180 int m_bInTransaction = false;
181 int m_bAutoCommit = true;
182
183 public:
186
187 int EstablishSession( const char *pszDSN,
188 const char *pszUserid,
189 const char *pszPassword );
190 const char *GetLastError();
191
192 // Transaction handling
193
194 int ClearTransaction();
195 int BeginTransaction();
196 int CommitTransaction();
199 int IsInTransaction() { return m_bInTransaction; }
200
201 // Essentially internal.
202
203 int CloseSession();
204
205 int Failed( int, HSTMT = nullptr );
207 HDBC GetConnection() { return m_hDBC; }
209 HENV GetEnvironment() { return m_hEnv; }
210
211 bool ConnectToMsAccess( const char * pszName, const char* pszDSNStringTemplate );
212
213};
214
223
224class CPL_DLL CPLODBCStatement {
225
227
228 CPLODBCSession *m_poSession = nullptr;
229 HSTMT m_hStmt = nullptr;
230
231 SQLSMALLINT m_nColCount = 0;
232 char **m_papszColNames = nullptr;
233 SQLSMALLINT *m_panColType = nullptr;
234 char **m_papszColTypeNames = nullptr;
235 CPL_SQLULEN *m_panColSize = nullptr;
236 SQLSMALLINT *m_panColPrecision = nullptr;
237 SQLSMALLINT *m_panColNullable = nullptr;
238 char **m_papszColColumnDef = nullptr;
239
240 char **m_papszColValues = nullptr;
241 CPL_SQLLEN *m_panColValueLengths = nullptr;
242
243 int Failed( int );
244
245 char *m_pszStatement = nullptr;
246 size_t m_nStatementMax = 0;
247 size_t m_nStatementLen = 0;
248
249 public:
250 explicit CPLODBCStatement( CPLODBCSession * );
252
254 HSTMT GetStatement() { return m_hStmt; }
255
256 // Command buffer related.
257 void Clear();
258 void AppendEscaped( const char * );
259 void Append( const char * );
260 void Append( int );
261 void Append( double );
262 int Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
264 const char *GetCommand() { return m_pszStatement; }
265
266 int ExecuteSQL( const char * = nullptr );
267
268 // Results fetching
269 int Fetch( int nOrientation = SQL_FETCH_NEXT,
270 int nOffset = 0 );
271 void ClearColumnData();
272
273 int GetColCount();
274 const char *GetColName( int );
275 short GetColType( int );
276 const char *GetColTypeName( int );
277 short GetColSize( int );
278 short GetColPrecision( int );
279 short GetColNullable( int );
280 const char *GetColColumnDef( int );
281
282 int GetColId( const char * );
283 const char *GetColData( int, const char * = nullptr );
284 const char *GetColData( const char *, const char * = nullptr );
285 int GetColDataLength( int );
286 int GetRowCountAffected();
287
288 // Fetch special metadata.
289 int GetColumns( const char *pszTable,
290 const char *pszCatalog = nullptr,
291 const char *pszSchema = nullptr );
292 int GetPrimaryKeys( const char *pszTable,
293 const char *pszCatalog = nullptr,
294 const char *pszSchema = nullptr );
295
296 int GetTables( const char *pszCatalog = nullptr,
297 const char *pszSchema = nullptr );
298
299 void DumpResult( FILE *fp, int bShowSchema = FALSE );
300
301 static CPLString GetTypeName( int );
302 static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
303
304 int CollectResultsInfo();
305};
306
307#endif
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition cpl_odbc.h:124
static void InstallMdbToolsDriver()
Attempts to install the MDB Tools driver for Microsoft Access databases.
Definition cpl_odbc.cpp:275
int InstallDriver(const char *pszDriver, const char *pszPathIn, WORD fRequest=ODBC_INSTALL_COMPLETE)
Installs ODBC driver or updates definition of already installed driver.
Definition cpl_odbc.cpp:78
int RemoveDriver(const char *pszDriverName, int fRemoveDSN=FALSE)
Removes or changes information about the driver from the Odbcinst.ini entry in the system information...
Definition cpl_odbc.cpp:329
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition cpl_odbc.h:130
const char * GetLastError() const
If InstallDriver returns FALSE, then GetLastError then error message can be obtained by calling this ...
Definition cpl_odbc.h:136
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition cpl_odbc.h:143
A class representing an ODBC database session.
Definition cpl_odbc.h:173
int IsInTransaction()
Returns whether a transaction is active.
Definition cpl_odbc.h:199
HDBC GetConnection()
Return connection handle.
Definition cpl_odbc.h:207
const char * GetLastError()
Returns the last ODBC error message.
Definition cpl_odbc.cpp:740
int EstablishSession(const char *pszDSN, const char *pszUserid, const char *pszPassword)
Connect to database and logon.
Definition cpl_odbc.cpp:663
int ClearTransaction()
Clear transaction.
Definition cpl_odbc.cpp:402
HENV GetEnvironment()
Return GetEnvironment handle.
Definition cpl_odbc.h:209
int RollbackTransaction()
Rollback transaction.
Definition cpl_odbc.cpp:497
CPLODBCSession()
Constructor.
Definition cpl_odbc.cpp:354
int BeginTransaction()
Begin transaction.
Definition cpl_odbc.cpp:436
int CommitTransaction()
Commit transaction.
Definition cpl_odbc.cpp:474
Abstraction for statement, and resultset.
Definition cpl_odbc.h:224
const char * GetCommand()
Return statement string.
Definition cpl_odbc.h:264
HSTMT GetStatement()
Return statement handle.
Definition cpl_odbc.h:254
CPLODBCStatement(CPLODBCSession *)
Constructor.
Definition cpl_odbc.cpp:757
Convenient string class based on std::string.
Definition cpl_string.h:333
Core portability definitions for CPL.
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition cpl_port.h:884
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition cpl_port.h:869
#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:955
Various convenience functions for working with strings and string lists.