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 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef CPL_ODBC_H_INCLUDED
15#define CPL_ODBC_H_INCLUDED
16
17#include "cpl_port.h"
18
19#ifdef _WIN32
20#include <windows.h>
21#endif
22
23#include <sql.h>
24#include <sqlext.h>
25#include <odbcinst.h>
26#include "cpl_string.h"
27
29#ifdef PATH_MAX
30#define ODBC_FILENAME_MAX PATH_MAX
31#else
32#define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
33#endif
46{
47 char m_szPathOut[ODBC_FILENAME_MAX];
48 char m_szError[SQL_MAX_MESSAGE_LENGTH];
49 DWORD m_nErrorCode;
50 DWORD m_nUsageCount;
51
52 static bool FindMdbToolsDriverLib(CPLString &osDriverFile);
53 static bool LibraryExists(const char *pszLibPath);
54
55 public:
56 // Default constructor.
58
76 int InstallDriver(const char *pszDriver, const char *pszPathIn,
77 WORD fRequest = ODBC_INSTALL_COMPLETE);
78
86 static void InstallMdbToolsDriver();
87
104 int RemoveDriver(const char *pszDriverName, int fRemoveDSN = FALSE);
105
107 int GetUsageCount() const
108 {
109 return m_nUsageCount;
110 }
111
116 const char *GetPathOut() const
117 {
118 return m_szPathOut;
119 }
120
125 const char *GetLastError() const
126 {
127 return m_szError;
128 }
129
135 DWORD GetLastErrorCode() const
136 {
137 return m_nErrorCode;
138 }
139};
140
141class CPLODBCStatement;
142
143/* On MSVC SQLULEN is missing in some cases (i.e. VC6)
144** but it is always a #define so test this way. On Unix
145** it is a typedef so we can't always do this.
146*/
147#if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
148#define MISSING_SQLULEN
149#endif
150
152#if !defined(MISSING_SQLULEN)
153/* ODBC types to support 64 bit compilation */
154#define CPL_SQLULEN SQLULEN
155#define CPL_SQLLEN SQLLEN
156#else
157#define CPL_SQLULEN SQLUINTEGER
158#define CPL_SQLLEN SQLINTEGER
159#endif /* ifdef SQLULEN */
168class CPL_DLL CPLODBCSession
169{
170
172
173
174 protected:
175 CPLString m_osLastError{};
176 HENV m_hEnv = nullptr;
177 HDBC m_hDBC = nullptr;
178 int m_bInTransaction = false;
179 int m_bAutoCommit = true;
182 public:
185
186 int EstablishSession(const char *pszDSN, const char *pszUserid,
187 const char *pszPassword);
188 const char *GetLastError();
189
190 // Transaction handling
191
192 int ClearTransaction();
193 int BeginTransaction();
194 int CommitTransaction();
195 int RollbackTransaction();
196
199 {
200 return m_bInTransaction;
201 }
202
203 // Essentially internal.
204
205 int CloseSession();
206
207 int Failed(int, HSTMT = nullptr);
208
211 {
212 return m_hDBC;
213 }
214
217 {
218 return m_hEnv;
219 }
220
221 bool ConnectToMsAccess(const char *pszName,
222 const char *pszDSNStringTemplate);
223};
224
234class CPL_DLL CPLODBCStatement
235{
236
238
239
240 protected:
241 int m_nFlags = 0;
242
243 CPLODBCSession *m_poSession = nullptr;
244 HSTMT m_hStmt = nullptr;
245
246 SQLSMALLINT m_nColCount = 0;
247 char **m_papszColNames = nullptr;
248 SQLSMALLINT *m_panColType = nullptr;
249 char **m_papszColTypeNames = nullptr;
250 CPL_SQLULEN *m_panColSize = nullptr;
251 SQLSMALLINT *m_panColPrecision = nullptr;
252 SQLSMALLINT *m_panColNullable = nullptr;
253 char **m_papszColColumnDef = nullptr;
254
255 char **m_papszColValues = nullptr;
256 CPL_SQLLEN *m_panColValueLengths = nullptr;
257 double *m_padColValuesAsDouble = nullptr;
258
259 int Failed(int);
260
261 char *m_pszStatement = nullptr;
262 size_t m_nStatementMax = 0;
263 size_t m_nStatementLen = 0;
266 public:
270 enum Flag
271 {
286 RetrieveNumericColumnsAsDouble = 1 << 0,
287 };
288
289 explicit CPLODBCStatement(CPLODBCSession *, int flags = 0);
291
294 {
295 return m_hStmt;
296 }
297
301 int Flags() const
302 {
303 return m_nFlags;
304 }
305
306 // Command buffer related.
307 void Clear();
308 void AppendEscaped(const char *);
309 void Append(const char *);
310 void Append(const std::string &);
311 // cppcheck-suppress functionStatic
312 void Append(int);
313 void Append(double);
314 int Appendf(CPL_FORMAT_STRING(const char *), ...)
316
318 const char *GetCommand()
319 {
320 return m_pszStatement;
321 }
322
323 int ExecuteSQL(const char * = nullptr);
324
325 // Results fetching
326 int Fetch(int nOrientation = SQL_FETCH_NEXT, int nOffset = 0);
327 void ClearColumnData();
328
329 int GetColCount();
330 const char *GetColName(int);
331 short GetColType(int);
332 const char *GetColTypeName(int);
333 short GetColSize(int);
334 short GetColPrecision(int);
335 short GetColNullable(int);
336 const char *GetColColumnDef(int);
337
338 int GetColId(const char *) const;
339 const char *GetColData(int, const char * = nullptr);
340 const char *GetColData(const char *, const char * = nullptr);
341 int GetColDataLength(int);
342
343 double GetColDataAsDouble(int) const;
344 double GetColDataAsDouble(const char *) const;
345
346 int GetRowCountAffected();
347
348 // Fetch special metadata.
349 int GetColumns(const char *pszTable, const char *pszCatalog = nullptr,
350 const char *pszSchema = nullptr);
351 int GetPrimaryKeys(const char *pszTable, const char *pszCatalog = nullptr,
352 const char *pszSchema = nullptr);
353
354 int GetTables(const char *pszCatalog = nullptr,
355 const char *pszSchema = nullptr);
356
357 void DumpResult(FILE *fp, int bShowSchema = FALSE);
358
359 static CPLString GetTypeName(int);
360 static SQLSMALLINT GetTypeMapping(SQLSMALLINT);
361
362 int CollectResultsInfo();
363};
364
365#endif
A class providing functions to install or remove ODBC driver.
Definition: cpl_odbc.h:46
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition: cpl_odbc.h:107
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition: cpl_odbc.h:116
const char * GetLastError() const
If InstallDriver returns FALSE, then GetLastError then error message can be obtained by calling this ...
Definition: cpl_odbc.h:125
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition: cpl_odbc.h:135
A class representing an ODBC database session.
Definition: cpl_odbc.h:169
int IsInTransaction()
Returns whether a transaction is active.
Definition: cpl_odbc.h:198
HDBC GetConnection()
Return connection handle.
Definition: cpl_odbc.h:210
HENV GetEnvironment()
Return GetEnvironment handle.
Definition: cpl_odbc.h:216
Abstraction for statement, and resultset.
Definition: cpl_odbc.h:235
int Flags() const
Returns statement flags.
Definition: cpl_odbc.h:301
HSTMT GetStatement()
Return statement handle.
Definition: cpl_odbc.h:293
Flag
Flags which control ODBC statement behavior.
Definition: cpl_odbc.h:271
Convenient string class based on std::string.
Definition: cpl_string.h:307
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:954
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:938
#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:1030
Various convenience functions for working with strings and string lists.