1 : /******************************************************************************
2 : * $Id: cpl_odbc.h 10645 2007-01-18 02:22:39Z warmerdam $
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 :
168 : public:
169 : CPLODBCSession();
170 : ~CPLODBCSession();
171 :
172 : int EstablishSession( const char *pszDSN,
173 : const char *pszUserid,
174 : const char *pszPassword );
175 : const char *GetLastError();
176 :
177 : // Essentially internal.
178 :
179 : int CloseSession();
180 :
181 : int Failed( int, HSTMT = NULL );
182 0 : HDBC GetConnection() { return m_hDBC; }
183 : HENV GetEnvironment() { return m_hEnv; }
184 : };
185 :
186 : /**
187 : * Abstraction for statement, and resultset.
188 : *
189 : * Includes methods for executing an SQL statement, and for accessing the
190 : * resultset from that statement. Also provides for executing other ODBC
191 : * requests that produce results sets such as SQLColumns() and SQLTables()
192 : * requests.
193 : */
194 :
195 : class CPL_DLL CPLODBCStatement {
196 :
197 : CPLODBCSession *m_poSession;
198 : HSTMT m_hStmt;
199 :
200 : SQLSMALLINT m_nColCount;
201 : char **m_papszColNames;
202 : SQLSMALLINT *m_panColType;
203 : char **m_papszColTypeNames;
204 : _SQLULEN *m_panColSize;
205 : SQLSMALLINT *m_panColPrecision;
206 : SQLSMALLINT *m_panColNullable;
207 :
208 : char **m_papszColValues;
209 : _SQLLEN *m_panColValueLengths;
210 :
211 : int Failed( int );
212 :
213 : char *m_pszStatement;
214 : size_t m_nStatementMax;
215 : size_t m_nStatementLen;
216 :
217 : public:
218 : CPLODBCStatement( CPLODBCSession * );
219 : ~CPLODBCStatement();
220 :
221 : HSTMT GetStatement() { return m_hStmt; }
222 :
223 : // Command buffer related.
224 : void Clear();
225 : void AppendEscaped( const char * );
226 : void Append( const char * );
227 : void Append( int );
228 : void Append( double );
229 : int Appendf( const char *, ... );
230 0 : const char *GetCommand() { return m_pszStatement; }
231 :
232 : int ExecuteSQL( const char * = NULL );
233 :
234 : // Results fetching
235 : int Fetch( int nOrientation = SQL_FETCH_NEXT,
236 : int nOffset = 0 );
237 : void ClearColumnData();
238 :
239 : int GetColCount();
240 : const char *GetColName( int );
241 : short GetColType( int );
242 : const char *GetColTypeName( int );
243 : short GetColSize( int );
244 : short GetColPrecision( int );
245 : short GetColNullable( int );
246 :
247 : int GetColId( const char * );
248 : const char *GetColData( int, const char * = NULL );
249 : const char *GetColData( const char *, const char * = NULL );
250 : int GetColDataLength( int );
251 :
252 : // Fetch special metadata.
253 : int GetColumns( const char *pszTable,
254 : const char *pszCatalog = NULL,
255 : const char *pszSchema = NULL );
256 : int GetPrimaryKeys( const char *pszTable,
257 : const char *pszCatalog = NULL,
258 : const char *pszSchema = NULL );
259 :
260 : int GetTables( const char *pszCatalog = NULL,
261 : const char *pszSchema = NULL );
262 :
263 : void DumpResult( FILE *fp, int bShowSchema = FALSE );
264 :
265 : static CPLString GetTypeName( int );
266 : static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
267 :
268 : int CollectResultsInfo();
269 : };
270 :
271 : #endif /* #ifndef WIN32CE */
272 :
273 : #endif
274 :
275 :
|