# cpl_odbc.h

Go to the documentation of this file.

    00001 /******************************************************************************
    00002  * $Id: cpl_odbc.h 10645 2007-01-18 02:22:39Z warmerdam $
    00003  *
    00004  * Project:  OGR ODBC Driver
    00005  * Purpose:  Declarations for ODBC Access Cover API.
    00006  * Author:   Frank Warmerdam, warmerdam@pobox.com
    00007  *
    00008  ******************************************************************************
    00009  * Copyright (c) 2003, Frank Warmerdam
    00010  *
    00011  * Permission is hereby granted, free of charge, to any person obtaining a
    00012  * copy of this software and associated documentation files (the "Software"),
    00013  * to deal in the Software without restriction, including without limitation
    00014  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00015  * and/or sell copies of the Software, and to permit persons to whom the
    00016  * Software is furnished to do so, subject to the following conditions:
    00017  *
    00018  * The above copyright notice and this permission notice shall be included
    00019  * in all copies or substantial portions of the Software.
    00020  *
    00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    00022  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    00024  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    00026  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    00027  * DEALINGS IN THE SOFTWARE.
    00028  ****************************************************************************/
    00029
    00030 #ifndef CPL_ODBC_H_INCLUDED
    00031 #define CPL_ODBC_H_INCLUDED
    00032
    00033 #include "cpl_port.h"
    00034
    00035 #ifndef WIN32CE /* ODBC is not supported on Windows CE. */
    00036
    00037 #ifdef WIN32
    00038 #  include <windows.h>
    00039 #endif
    00040
    00041 #include <sql.h>
    00042 #include <sqlext.h>
    00043 #include <odbcinst.h>
    00044 #include "cpl_string.h"
    00045
    00046 #ifdef PATH_MAX
    00047 #  define ODBC_FILENAME_MAX PATH_MAX
    00048 #else
    00049 #  define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
    00050 #endif
    00051
    00052
    00062 class CPL_DLL CPLODBCDriverInstaller
    00063 {
    00064     char m_szPathOut[ODBC_FILENAME_MAX];
    00065     char m_szError[SQL_MAX_MESSAGE_LENGTH];
    00066     DWORD m_nErrorCode;
    00067     DWORD m_nUsageCount;
    00068
    00069   public:
    00070
    00071     // Default constructor.
    00072     CPLODBCDriverInstaller();
    00073
    00074
    00092     int InstallDriver( const char* pszDriver, const char* pszPathIn,
    00093             WORD fRequest = ODBC_INSTALL_COMPLETE );
    00094
    00111     int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
    00112
    00113
    00114     // The usage count of the driver after this function has been called
    00115     int GetUsageCount() const {  return m_nUsageCount; }
    00116
    00117
    00118     // Path of the target directory where the driver should be installed.
    00119     // For details, see ODBC API Reference and lpszPathOut
    00120     // parameter of SQLInstallDriverEx
    00121     const char* GetPathOut() const { return m_szPathOut; }
    00122
    00123
    00124     // If InstallDriver returns FALSE, then GetLastError then
    00125     // error message can be obtained by calling this function.
    00126     // Internally, it calls ODBC's SQLInstallerError function.
    00127     const char* GetLastError() const { return m_szError; }
    00128
    00129
    00130     // If InstallDriver returns FALSE, then GetLastErrorCode then
    00131     // error code can be obtained by calling this function.
    00132     // Internally, it calls ODBC's SQLInstallerError function.
    00133     // See ODBC API Reference for possible error flags.
    00134     DWORD GetLastErrorCode() const { return m_nErrorCode; }
    00135 };
    00136
    00137 class CPLODBCStatement;
    00138
    00139 /* On MSVC SQLULEN is missing in some cases (ie. VC6)
    00140 ** but it is always a #define so test this way.   On Unix
    00141 ** it is a typedef so we can't always do this.
    00142 */
    00143 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
    00144 #  define MISSING_SQLULEN
    00145 #endif
    00146
    00147 #if !defined(MISSING_SQLULEN)
    00148 /* ODBC types to support 64 bit compilation */
    00149 #  define _SQLULEN SQLULEN
    00150 #  define _SQLLEN  SQLLEN
    00151 #else
    00152 #  define _SQLULEN SQLUINTEGER
    00153 #  define _SQLLEN  SQLINTEGER
    00154 #endif  /* ifdef SQLULEN */
    00155
    00156
    00163 class CPL_DLL CPLODBCSession {
    00164     char      m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
    00165     HENV      m_hEnv;
    00166     HDBC      m_hDBC;
    00167
    00168   public:
    00169     CPLODBCSession();
    00170     ~CPLODBCSession();
    00171
    00172     int         EstablishSession( const char *pszDSN,
    00173                                   const char *pszUserid,
    00174                                   const char *pszPassword );
    00175     const char  *GetLastError();
    00176
    00177     // Essentially internal.
    00178
    00179     int         CloseSession();
    00180
    00181     int         Failed( int, HSTMT = NULL );
    00182     HDBC        GetConnection() { return m_hDBC; }
    00183     HENV        GetEnvironment()  { return m_hEnv; }
    00184 };
    00185
    00195 class CPL_DLL CPLODBCStatement {
    00196
    00197     CPLODBCSession     *m_poSession;
    00198     HSTMT               m_hStmt;
    00199
    00200     SQLSMALLINT    m_nColCount;
    00201     char         **m_papszColNames;
    00202     SQLSMALLINT   *m_panColType;
    00203     char         **m_papszColTypeNames;
    00204     _SQLULEN      *m_panColSize;
    00205     SQLSMALLINT   *m_panColPrecision;
    00206     SQLSMALLINT   *m_panColNullable;
    00207
    00208     char         **m_papszColValues;
    00209     _SQLLEN       *m_panColValueLengths;
    00210
    00211     int            Failed( int );
    00212
    00213     char          *m_pszStatement;
    00214     size_t         m_nStatementMax;
    00215     size_t         m_nStatementLen;
    00216
    00217   public:
    00218     CPLODBCStatement( CPLODBCSession * );
    00219     ~CPLODBCStatement();
    00220
    00221     HSTMT          GetStatement() { return m_hStmt; }
    00222
    00223     // Command buffer related.
    00224     void           Clear();
    00225     void           AppendEscaped( const char * );
    00226     void           Append( const char * );
    00227     void           Append( int );
    00228     void           Append( double );
    00229     int            Appendf( const char *, ... );
    00230     const char    *GetCommand() { return m_pszStatement; }
    00231
    00232     int            ExecuteSQL( const char * = NULL );
    00233
    00234     // Results fetching
    00235     int            Fetch( int nOrientation = SQL_FETCH_NEXT,
    00236                           int nOffset = 0 );
    00237     void           ClearColumnData();
    00238
    00239     int            GetColCount();
    00240     const char    *GetColName( int );
    00241     short          GetColType( int );
    00242     const char    *GetColTypeName( int );
    00243     short          GetColSize( int );
    00244     short          GetColPrecision( int );
    00245     short          GetColNullable( int );
    00246
    00247     int            GetColId( const char * );
    00248     const char    *GetColData( int, const char * = NULL );
    00249     const char    *GetColData( const char *, const char * = NULL );
    00250     int            GetColDataLength( int );
    00251
    00252     // Fetch special metadata.
    00253     int            GetColumns( const char *pszTable,
    00254                                const char *pszCatalog = NULL,
    00255                                const char *pszSchema = NULL );
    00256     int            GetPrimaryKeys( const char *pszTable,
    00257                                    const char *pszCatalog = NULL,
    00258                                    const char *pszSchema = NULL );
    00259
    00260     int            GetTables( const char *pszCatalog = NULL,
    00261                               const char *pszSchema = NULL );
    00262
    00263     void           DumpResult( FILE *fp, int bShowSchema = FALSE );
    00264
    00265     static CPLString GetTypeName( int );
    00266     static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
    00267
    00268     int            CollectResultsInfo();
    00269 };
    00270
    00271 #endif /* #ifndef WIN32CE */
    00272
    00273 #endif
    00274
    00275

Generated for GDAL by 1.5.7.1.