1 : /******************************************************************************
2 : * $Id: cpl_odbc.h 20579 2010-09-12 11:43:35Z rouault $
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 : #ifndef WIN32CE /* ODBC is not supported on Windows CE. */
36 :
37 : #ifdef WIN32
38 : # include <windows.h>
39 : #endif
40 :
41 : #include <sql.h>
42 : #include <sqlext.h>
43 : #include <odbcinst.h>
44 : #include "cpl_string.h"
45 :
46 : #ifdef PATH_MAX
47 : # define ODBC_FILENAME_MAX PATH_MAX
48 : #else
49 : # define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
50 : #endif
51 :
52 :
53 : /**
54 : * \file cpl_odbc.h
55 : *
56 : * ODBC Abstraction Layer (C++).
57 : */
58 :
59 : /**
60 : * A class providing functions to install or remove ODBC driver.
61 : */
62 : class CPL_DLL CPLODBCDriverInstaller
63 : {
64 : char m_szPathOut[ODBC_FILENAME_MAX];
65 : char m_szError[SQL_MAX_MESSAGE_LENGTH];
66 : DWORD m_nErrorCode;
67 : DWORD m_nUsageCount;
68 :
69 : public:
70 :
71 : // Default constructor.
72 : CPLODBCDriverInstaller();
73 :
74 :
75 : /**
76 : * Installs ODBC driver or updates definition of already installed driver.
77 : * Interanally, it calls ODBC's SQLInstallDriverEx function.
78 : *
79 : * @param pszDriver - The driver definition as a list of keyword-value
80 : * pairs describing the driver (See ODBC API Reference).
81 : *
82 : * @param pszPathIn - Full path of the target directory of the installation,
83 : * or a null pointer (for unixODBC, NULL is passed).
84 : *
85 : * @param fRequest - The fRequest argument must contain one of
86 : * the following values:
87 : * ODBC_INSTALL_COMPLETE - (default) complete the installation request
88 : * ODBC_INSTALL_INQUIRY - inquire about where a driver can be installed
89 : *
90 : * @return TRUE indicates success, FALSE if it fails.
91 : */
92 : int InstallDriver( const char* pszDriver, const char* pszPathIn,
93 : WORD fRequest = ODBC_INSTALL_COMPLETE );
94 :
95 : /**
96 : * Removes or changes information about the driver from
97 : * the Odbcinst.ini entry in the system information.
98 : *
99 : * @param pszDriverName - The name of the driver as registered in
100 : * the Odbcinst.ini key of the system information.
101 : *
102 : * @param fRemoveDSN - TRUE: Remove DSNs associated with the driver
103 : * specified in lpszDriver. FALSE: Do not remove DSNs associated
104 : * with the driver specified in lpszDriver.
105 : *
106 : * @return The function returns TRUE if it is successful,
107 : * FALSE if it fails. If no entry exists in the system information
108 : * when this function is called, the function returns FALSE.
109 : * In order to obtain usage count value, call GetUsageCount().
110 : */
111 : int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
112 :
113 :
114 : // The usage count of the driver after this function has been called
115 : int GetUsageCount() const { return m_nUsageCount; }
116 :
117 :
118 : // Path of the target directory where the driver should be installed.
119 : // For details, see ODBC API Reference and lpszPathOut
120 : // parameter of SQLInstallDriverEx
121 : const char* GetPathOut() const { return m_szPathOut; }
122 :
123 :
124 : // If InstallDriver returns FALSE, then GetLastError then
125 : // error message can be obtained by calling this function.
126 : // Internally, it calls ODBC's SQLInstallerError function.
127 0 : const char* GetLastError() const { return m_szError; }
128 :
129 :
130 : // If InstallDriver returns FALSE, then GetLastErrorCode then
131 : // error code can be obtained by calling this function.
132 : // Internally, it calls ODBC's SQLInstallerError function.
133 : // See ODBC API Reference for possible error flags.
134 : DWORD GetLastErrorCode() const { return m_nErrorCode; }
135 : };
136 :
137 : class CPLODBCStatement;
138 :
139 : /* On MSVC SQLULEN is missing in some cases (ie. VC6)
140 : ** but it is always a #define so test this way. On Unix
141 : ** it is a typedef so we can't always do this.
142 : */
143 : #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
144 : # define MISSING_SQLULEN
145 : #endif
146 :
147 : #if !defined(MISSING_SQLULEN)
148 : /* ODBC types to support 64 bit compilation */
149 : # define _SQLULEN SQLULEN
150 : # define _SQLLEN SQLLEN
151 : #else
152 : # define _SQLULEN SQLUINTEGER
153 : # define _SQLLEN SQLINTEGER
154 : #endif /* ifdef SQLULEN */
155 :
156 :
157 : /**
158 : * A class representing an ODBC database session.
159 : *
160 : * Includes error collection services.
161 : */
162 :
163 : class CPL_DLL CPLODBCSession {
164 : char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
165 : HENV m_hEnv;
166 : HDBC m_hDBC;
167 : int m_bInTransaction;
168 : int m_bAutoCommit;
169 :
170 : public:
171 : CPLODBCSession();
172 : ~CPLODBCSession();
173 :
174 : int EstablishSession( const char *pszDSN,
175 : const char *pszUserid,
176 : const char *pszPassword );
177 : const char *GetLastError();
178 :
179 : // Transaction handling
180 :
181 : int ClearTransaction();
182 : int BeginTransaction();
183 : int CommitTransaction();
184 : int RollbackTransaction();
185 0 : int IsInTransaction() { return m_bInTransaction; }
186 :
187 : // Essentially internal.
188 :
189 : int CloseSession();
190 :
191 : int Failed( int, HSTMT = NULL );
192 0 : HDBC GetConnection() { return m_hDBC; }
193 : HENV GetEnvironment() { return m_hEnv; }
194 : };
195 :
196 : /**
197 : * Abstraction for statement, and resultset.
198 : *
199 : * Includes methods for executing an SQL statement, and for accessing the
200 : * resultset from that statement. Also provides for executing other ODBC
201 : * requests that produce results sets such as SQLColumns() and SQLTables()
202 : * requests.
203 : */
204 :
205 : class CPL_DLL CPLODBCStatement {
206 :
207 : CPLODBCSession *m_poSession;
208 : HSTMT m_hStmt;
209 :
210 : SQLSMALLINT m_nColCount;
211 : char **m_papszColNames;
212 : SQLSMALLINT *m_panColType;
213 : char **m_papszColTypeNames;
214 : _SQLULEN *m_panColSize;
215 : SQLSMALLINT *m_panColPrecision;
216 : SQLSMALLINT *m_panColNullable;
217 :
218 : char **m_papszColValues;
219 : _SQLLEN *m_panColValueLengths;
220 :
221 : int Failed( int );
222 :
223 : char *m_pszStatement;
224 : size_t m_nStatementMax;
225 : size_t m_nStatementLen;
226 :
227 : public:
228 : CPLODBCStatement( CPLODBCSession * );
229 : ~CPLODBCStatement();
230 :
231 : HSTMT GetStatement() { return m_hStmt; }
232 :
233 : // Command buffer related.
234 : void Clear();
235 : void AppendEscaped( const char * );
236 : void Append( const char * );
237 : void Append( int );
238 : void Append( double );
239 : int Appendf( const char *, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
240 0 : const char *GetCommand() { return m_pszStatement; }
241 :
242 : int ExecuteSQL( const char * = NULL );
243 :
244 : // Results fetching
245 : int Fetch( int nOrientation = SQL_FETCH_NEXT,
246 : int nOffset = 0 );
247 : void ClearColumnData();
248 :
249 : int GetColCount();
250 : const char *GetColName( int );
251 : short GetColType( int );
252 : const char *GetColTypeName( int );
253 : short GetColSize( int );
254 : short GetColPrecision( int );
255 : short GetColNullable( int );
256 :
257 : int GetColId( const char * );
258 : const char *GetColData( int, const char * = NULL );
259 : const char *GetColData( const char *, const char * = NULL );
260 : int GetColDataLength( int );
261 :
262 : // Fetch special metadata.
263 : int GetColumns( const char *pszTable,
264 : const char *pszCatalog = NULL,
265 : const char *pszSchema = NULL );
266 : int GetPrimaryKeys( const char *pszTable,
267 : const char *pszCatalog = NULL,
268 : const char *pszSchema = NULL );
269 :
270 : int GetTables( const char *pszCatalog = NULL,
271 : const char *pszSchema = NULL );
272 :
273 : void DumpResult( FILE *fp, int bShowSchema = FALSE );
274 :
275 : static CPLString GetTypeName( int );
276 : static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
277 :
278 : int CollectResultsInfo();
279 : };
280 :
281 : #endif /* #ifndef WIN32CE */
282 :
283 : #endif
284 :
285 :
|