GDAL
cpl_odbc.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Project: OGR ODBC Driver
4 * Purpose: Declarations for ODBC Access Cover API.
5 * Author: Frank Warmerdam, warmerdam@pobox.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2003, Frank Warmerdam
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef CPL_ODBC_H_INCLUDED
14#define CPL_ODBC_H_INCLUDED
15
16#include "cpl_port.h"
17
18#ifdef _WIN32
19#include <windows.h>
20#endif
21
22#include <sql.h>
23#include <sqlext.h>
24#include <odbcinst.h>
25#include "cpl_string.h"
26
28#ifdef PATH_MAX
29#define ODBC_FILENAME_MAX PATH_MAX
30#else
31#define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
32#endif
34
40
44class CPL_DLL CPLODBCDriverInstaller
45{
46 char m_szPathOut[ODBC_FILENAME_MAX];
47 char m_szError[SQL_MAX_MESSAGE_LENGTH];
48 DWORD m_nErrorCode;
49 DWORD m_nUsageCount;
50
51 static bool FindMdbToolsDriverLib(CPLString &osDriverFile);
52 static bool LibraryExists(const char *pszLibPath);
53
54 public:
55 // Default constructor.
56 CPLODBCDriverInstaller();
57
75 int InstallDriver(const char *pszDriver, const char *pszPathIn,
76 WORD fRequest = ODBC_INSTALL_COMPLETE);
77
85 static void InstallMdbToolsDriver();
86
103 int RemoveDriver(const char *pszDriverName, int fRemoveDSN = FALSE);
104
106 int GetUsageCount() const
107 {
108 return m_nUsageCount;
109 }
110
115 const char *GetPathOut() const
116 {
117 return m_szPathOut;
118 }
119
124 const char *GetLastError() const
125 {
126 return m_szError;
127 }
128
134 DWORD GetLastErrorCode() const
135 {
136 return m_nErrorCode;
137 }
138};
139
140class CPLODBCStatement;
141
142/* On MSVC SQLULEN is missing in some cases (i.e. VC6)
143** but it is always a #define so test this way. On Unix
144** it is a typedef so we can't always do this.
145*/
146#if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
147#define MISSING_SQLULEN
148#endif
149
151#if !defined(MISSING_SQLULEN)
152/* ODBC types to support 64 bit compilation */
153#define CPL_SQLULEN SQLULEN
154#define CPL_SQLLEN SQLLEN
155#else
156#define CPL_SQLULEN SQLUINTEGER
157#define CPL_SQLLEN SQLINTEGER
158#endif /* ifdef SQLULEN */
160
166
167class CPL_DLL CPLODBCSession
168{
169
171
172
173 protected:
174 CPLString m_osLastError{};
175 HENV m_hEnv = nullptr;
176 HDBC m_hDBC = nullptr;
177 int m_bInTransaction = false;
178 int m_bAutoCommit = true;
180
181 public:
184
185 int EstablishSession(const char *pszDSN, const char *pszUserid,
186 const char *pszPassword);
187 const char *GetLastError();
188
189 // Transaction handling
190
191 int ClearTransaction();
192 int BeginTransaction();
193 int CommitTransaction();
195
198 {
199 return m_bInTransaction;
200 }
201
202 // Essentially internal.
203
204 int CloseSession();
205
206 int Failed(int, HSTMT = nullptr);
207
210 {
211 return m_hDBC;
212 }
213
216 {
217 return m_hEnv;
218 }
219
220 bool ConnectToMsAccess(const char *pszName,
221 const char *pszDSNStringTemplate);
222};
223
232
233class CPL_DLL CPLODBCStatement
234{
235
237
238
239 protected:
240 int m_nFlags = 0;
241
242 CPLODBCSession *m_poSession = nullptr;
243 HSTMT m_hStmt = nullptr;
244
245 SQLSMALLINT m_nColCount = 0;
246 char **m_papszColNames = nullptr;
247 SQLSMALLINT *m_panColType = nullptr;
248 char **m_papszColTypeNames = nullptr;
249 CPL_SQLULEN *m_panColSize = nullptr;
250 SQLSMALLINT *m_panColPrecision = nullptr;
251 SQLSMALLINT *m_panColNullable = nullptr;
252 char **m_papszColColumnDef = nullptr;
253
254 char **m_papszColValues = nullptr;
255 CPL_SQLLEN *m_panColValueLengths = nullptr;
256 double *m_padColValuesAsDouble = nullptr;
257
258 int Failed(int);
259
260 char *m_pszStatement = nullptr;
261 size_t m_nStatementMax = 0;
262 size_t m_nStatementLen = 0;
264
265 public:
269 enum Flag
270 {
286 };
287
288 explicit CPLODBCStatement(CPLODBCSession *, int flags = 0);
290
293 {
294 return m_hStmt;
295 }
296
300 int Flags() const
301 {
302 return m_nFlags;
303 }
304
305 // Command buffer related.
306 void Clear();
307 void AppendEscaped(const char *);
308 void Append(const char *);
309 void Append(const std::string &);
310 // cppcheck-suppress functionStatic
311 void Append(int);
312 void Append(double);
313 int Appendf(CPL_FORMAT_STRING(const char *), ...)
315
317 const char *GetCommand()
318 {
319 return m_pszStatement;
320 }
321
322 int ExecuteSQL(const char * = nullptr);
323
324 // Results fetching
325 int Fetch(int nOrientation = SQL_FETCH_NEXT, int nOffset = 0);
326 void ClearColumnData();
327
328 int GetColCount();
329 const char *GetColName(int);
330 short GetColType(int);
331 const char *GetColTypeName(int);
332 short GetColSize(int);
333 short GetColPrecision(int);
334 short GetColNullable(int);
335 const char *GetColColumnDef(int);
336
337 int GetColId(const char *) const;
338 const char *GetColData(int, const char * = nullptr);
339 const char *GetColData(const char *, const char * = nullptr);
340 int GetColDataLength(int);
341
342 double GetColDataAsDouble(int) const;
343 double GetColDataAsDouble(const char *) const;
344
345 int GetRowCountAffected();
346
347 // Fetch special metadata.
348 int GetColumns(const char *pszTable, const char *pszCatalog = nullptr,
349 const char *pszSchema = nullptr);
350 int GetPrimaryKeys(const char *pszTable, const char *pszCatalog = nullptr,
351 const char *pszSchema = nullptr);
352
353 int GetTables(const char *pszCatalog = nullptr,
354 const char *pszSchema = nullptr);
355
356 void DumpResult(FILE *fp, int bShowSchema = FALSE);
357
358 static CPLString GetTypeName(int);
359 static SQLSMALLINT GetTypeMapping(SQLSMALLINT);
360
361 int CollectResultsInfo();
362};
363
364#endif
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition cpl_odbc.h:106
static void InstallMdbToolsDriver()
Attempts to install the MDB Tools driver for Microsoft Access databases.
Definition cpl_odbc.cpp:258
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:60
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:318
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition cpl_odbc.h:115
const char * GetLastError() const
If InstallDriver returns FALSE, then GetLastError then error message can be obtained by calling this ...
Definition cpl_odbc.h:124
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition cpl_odbc.h:134
A class representing an ODBC database session.
Definition cpl_odbc.h:168
int IsInTransaction()
Returns whether a transaction is active.
Definition cpl_odbc.h:197
HDBC GetConnection()
Return connection handle.
Definition cpl_odbc.h:209
const char * GetLastError()
Returns the last ODBC error message.
Definition cpl_odbc.cpp:756
int EstablishSession(const char *pszDSN, const char *pszUserid, const char *pszPassword)
Connect to database and logon.
Definition cpl_odbc.cpp:668
int ClearTransaction()
Clear transaction.
Definition cpl_odbc.cpp:391
HENV GetEnvironment()
Return GetEnvironment handle.
Definition cpl_odbc.h:215
int RollbackTransaction()
Rollback transaction.
Definition cpl_odbc.cpp:488
CPLODBCSession()
Constructor.
Definition cpl_odbc.cpp:343
int BeginTransaction()
Begin transaction.
Definition cpl_odbc.cpp:426
int CommitTransaction()
Commit transaction.
Definition cpl_odbc.cpp:465
Abstraction for statement, and resultset.
Definition cpl_odbc.h:234
int Flags() const
Returns statement flags.
Definition cpl_odbc.h:300
const char * GetCommand()
Return statement string.
Definition cpl_odbc.h:317
HSTMT GetStatement()
Return statement handle.
Definition cpl_odbc.h:292
CPLODBCStatement(CPLODBCSession *, int flags=0)
Constructor.
Definition cpl_odbc.cpp:778
Flag
Flags which control ODBC statement behavior.
Definition cpl_odbc.h:270
@ RetrieveNumericColumnsAsDouble
Numeric column values should be retrieved as doubles, using either the SQL_C_DOUBLE or SQL_C_FLOAT ty...
Definition cpl_odbc.h:285
Convenient string class based on std::string.
Definition cpl_string.h:320
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:860
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition cpl_port.h:844
#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:936
Various convenience functions for working with strings and string lists.