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
61 class 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 // Default constructor.
73 CPLODBCDriverInstaller();
74
92 int InstallDriver(const char *pszDriver, const char *pszPathIn,
93 WORD fRequest = ODBC_INSTALL_COMPLETE);
94
102 static void InstallMdbToolsDriver();
103
120 int RemoveDriver(const char *pszDriverName, int fRemoveDSN = FALSE);
121
123 int GetUsageCount() const
124 {
125 return m_nUsageCount;
126 }
127
132 const char *GetPathOut() const
133 {
134 return m_szPathOut;
135 }
136
141 const char *GetLastError() const
142 {
143 return m_szError;
144 }
145
151 DWORD GetLastErrorCode() const
152 {
153 return m_nErrorCode;
154 }
155 };
156
157 class CPLODBCStatement;
158
159 /* On MSVC SQLULEN is missing in some cases (i.e. VC6)
160 ** but it is always a #define so test this way. On Unix
161 ** it is a typedef so we can't always do this.
162 */
163 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
164 #define MISSING_SQLULEN
165 #endif
166
168 #if !defined(MISSING_SQLULEN)
169 /* ODBC types to support 64 bit compilation */
170 #define CPL_SQLULEN SQLULEN
171 #define CPL_SQLLEN SQLLEN
172 #else
173 #define CPL_SQLULEN SQLUINTEGER
174 #define CPL_SQLLEN SQLINTEGER
175 #endif /* ifdef SQLULEN */
176
184 class CPL_DLL CPLODBCSession
185 {
186
187 CPL_DISALLOW_COPY_ASSIGN(CPLODBCSession)
188
189
190 protected:
191 CPLString m_osLastError{};
192 HENV m_hEnv = nullptr;
193 HDBC m_hDBC = nullptr;
194 int m_bInTransaction = false;
195 int m_bAutoCommit = true;
198 public:
199 CPLODBCSession();
200 ~CPLODBCSession();
201
202 int EstablishSession(const char *pszDSN, const char *pszUserid,
203 const char *pszPassword);
204 const char *GetLastError();
205
206 // Transaction handling
207
208 int ClearTransaction();
209 int BeginTransaction();
210 int CommitTransaction();
211 int RollbackTransaction();
213 int IsInTransaction()
214 {
215 return m_bInTransaction;
216 }
217
218 // Essentially internal.
219
220 int CloseSession();
221
222 int Failed(int, HSTMT = nullptr);
224 HDBC GetConnection()
225 {
226 return m_hDBC;
227 }
229 HENV GetEnvironment()
230 {
231 return m_hEnv;
232 }
233
234 bool ConnectToMsAccess(const char *pszName,
235 const char *pszDSNStringTemplate);
236 };
237
247 class CPL_DLL CPLODBCStatement
248 {
249
250 CPL_DISALLOW_COPY_ASSIGN(CPLODBCStatement)
251
252
253 protected:
254 int m_nFlags = 0;
255
256 CPLODBCSession *m_poSession = nullptr;
257 HSTMT m_hStmt = nullptr;
258
259 SQLSMALLINT m_nColCount = 0;
260 char **m_papszColNames = nullptr;
261 SQLSMALLINT *m_panColType = nullptr;
262 char **m_papszColTypeNames = nullptr;
263 CPL_SQLULEN *m_panColSize = nullptr;
264 SQLSMALLINT *m_panColPrecision = nullptr;
265 SQLSMALLINT *m_panColNullable = nullptr;
266 char **m_papszColColumnDef = nullptr;
267
268 char **m_papszColValues = nullptr;
269 CPL_SQLLEN *m_panColValueLengths = nullptr;
270 double *m_padColValuesAsDouble = nullptr;
271
272 int Failed(int);
273
274 char *m_pszStatement = nullptr;
275 size_t m_nStatementMax = 0;
276 size_t m_nStatementLen = 0;
279 public:
283 enum Flag
284 {
299 RetrieveNumericColumnsAsDouble = 1 << 0,
300 };
301
302 explicit CPLODBCStatement(CPLODBCSession *, int flags = 0);
303 ~CPLODBCStatement();
304
306 HSTMT GetStatement()
307 {
308 return m_hStmt;
309 }
310
314 int Flags() const
315 {
316 return m_nFlags;
317 }
318
319 // Command buffer related.
320 void Clear();
321 void AppendEscaped(const char *);
322 void Append(const char *);
323 void Append(int);
324 void Append(double);
325 int Appendf(CPL_FORMAT_STRING(const char *), ...)
326 CPL_PRINT_FUNC_FORMAT(2, 3);
328 const char *GetCommand()
329 {
330 return m_pszStatement;
331 }
332
333 int ExecuteSQL(const char * = nullptr);
334
335 // Results fetching
336 int Fetch(int nOrientation = SQL_FETCH_NEXT, int nOffset = 0);
337 void ClearColumnData();
338
339 int GetColCount();
340 const char *GetColName(int);
341 short GetColType(int);
342 const char *GetColTypeName(int);
343 short GetColSize(int);
344 short GetColPrecision(int);
345 short GetColNullable(int);
346 const char *GetColColumnDef(int);
347
348 int GetColId(const char *) const;
349 const char *GetColData(int, const char * = nullptr);
350 const char *GetColData(const char *, const char * = nullptr);
351 int GetColDataLength(int);
352
353 double GetColDataAsDouble(int) const;
354 double GetColDataAsDouble(const char *) const;
355
356 int GetRowCountAffected();
357
358 // Fetch special metadata.
359 int GetColumns(const char *pszTable, const char *pszCatalog = nullptr,
360 const char *pszSchema = nullptr);
361 int GetPrimaryKeys(const char *pszTable, const char *pszCatalog = nullptr,
362 const char *pszSchema = nullptr);
363
364 int GetTables(const char *pszCatalog = nullptr,
365 const char *pszSchema = nullptr);
366
367 void DumpResult(FILE *fp, int bShowSchema = FALSE);
368
369 static CPLString GetTypeName(int);
370 static SQLSMALLINT GetTypeMapping(SQLSMALLINT);
371
372 int CollectResultsInfo();
373 };
374
375 #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:959
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:311
CPLODBCStatement::GetStatement
HSTMT GetStatement()
Return statement handle.
Definition: cpl_odbc.h:306
CPLODBCSession::GetConnection
HDBC GetConnection()
Return connection handle.
Definition: cpl_odbc.h:224
CPLODBCSession::GetEnvironment
HENV GetEnvironment()
Return GetEnvironment handle.
Definition: cpl_odbc.h:229
CPLODBCStatement
Abstraction for statement, and resultset.
Definition: cpl_odbc.h:247
cpl_string.h
CPLODBCSession
A class representing an ODBC database session.
Definition: cpl_odbc.h:184
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:141
CPLODBCDriverInstaller::GetPathOut
const char * GetPathOut() const
Path of the target directory where the driver should be installed.
Definition: cpl_odbc.h:132
CPLODBCDriverInstaller::GetLastErrorCode
DWORD GetLastErrorCode() const
If InstallDriver returns FALSE, then GetLastErrorCode then error code can be obtained by calling this...
Definition: cpl_odbc.h:151
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:975
cpl_port.h
CPLODBCDriverInstaller::GetUsageCount
int GetUsageCount() const
The usage count of the driver after this function has been called.
Definition: cpl_odbc.h:123
CPLODBCStatement::Flags
int Flags() const
Returns statement flags.
Definition: cpl_odbc.h:314
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:213
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:1051
CPLODBCStatement::Flag
Flag
Flags which control ODBC statement behavior.
Definition: cpl_odbc.h:283