GDAL
cpl_odbc.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_odbc.h 36675 2016-12-04 11:10:10Z rouault $
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 #ifdef WIN32
00036 #  include <windows.h>
00037 #endif
00038 
00039 #include <sql.h>
00040 #include <sqlext.h>
00041 #include <odbcinst.h>
00042 #include "cpl_string.h"
00043 
00045 #ifdef PATH_MAX
00046 #  define ODBC_FILENAME_MAX PATH_MAX
00047 #else
00048 #  define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
00049 #endif
00050 
00061 class CPL_DLL CPLODBCDriverInstaller
00062 {
00063     char m_szPathOut[ODBC_FILENAME_MAX];
00064     char m_szError[SQL_MAX_MESSAGE_LENGTH];
00065     DWORD m_nErrorCode;
00066     DWORD m_nUsageCount;
00067 
00068   public:
00069 
00070     // Default constructor.
00071     CPLODBCDriverInstaller();
00072 
00090     int InstallDriver( const char* pszDriver, const char* pszPathIn,
00091             WORD fRequest = ODBC_INSTALL_COMPLETE );
00092 
00109     int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
00110 
00112     int GetUsageCount() const {  return m_nUsageCount; }
00113 
00118     const char* GetPathOut() const { return m_szPathOut; }
00119 
00124     const char* GetLastError() const { return m_szError; }
00125 
00131     DWORD GetLastErrorCode() const { return m_nErrorCode; }
00132 };
00133 
00134 class CPLODBCStatement;
00135 
00136 /* On MSVC SQLULEN is missing in some cases (i.e. VC6)
00137 ** but it is always a #define so test this way.   On Unix
00138 ** it is a typedef so we can't always do this.
00139 */
00140 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
00141 #  define MISSING_SQLULEN
00142 #endif
00143 
00145 #if !defined(MISSING_SQLULEN)
00146 /* ODBC types to support 64 bit compilation */
00147 #  define CPL_SQLULEN SQLULEN
00148 #  define CPL_SQLLEN  SQLLEN
00149 #else
00150 #  define CPL_SQLULEN SQLUINTEGER
00151 #  define CPL_SQLLEN  SQLINTEGER
00152 #endif  /* ifdef SQLULEN */
00153 
00161 class CPL_DLL CPLODBCSession {
00162     char      m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
00163     HENV      m_hEnv;
00164     HDBC      m_hDBC;
00165     int       m_bInTransaction;
00166     int       m_bAutoCommit;
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     // Transaction handling
00178 
00179     int         ClearTransaction();
00180     int         BeginTransaction();
00181     int         CommitTransaction();
00182     int         RollbackTransaction();
00184     int         IsInTransaction() { return m_bInTransaction; }
00185 
00186     // Essentially internal.
00187 
00188     int         CloseSession();
00189 
00190     int         Failed( int, HSTMT = NULL );
00192     HDBC        GetConnection() { return m_hDBC; }
00194     HENV        GetEnvironment()  { return m_hEnv; }
00195 };
00196 
00206 class CPL_DLL CPLODBCStatement {
00207 
00208     CPLODBCSession     *m_poSession;
00209     HSTMT               m_hStmt;
00210 
00211     SQLSMALLINT    m_nColCount;
00212     char         **m_papszColNames;
00213     SQLSMALLINT   *m_panColType;
00214     char         **m_papszColTypeNames;
00215     CPL_SQLULEN      *m_panColSize;
00216     SQLSMALLINT   *m_panColPrecision;
00217     SQLSMALLINT   *m_panColNullable;
00218     char         **m_papszColColumnDef;
00219 
00220     char         **m_papszColValues;
00221     CPL_SQLLEN       *m_panColValueLengths;
00222 
00223     int            Failed( int );
00224 
00225     char          *m_pszStatement;
00226     size_t         m_nStatementMax;
00227     size_t         m_nStatementLen;
00228 
00229   public:
00230     explicit CPLODBCStatement( CPLODBCSession * );
00231     ~CPLODBCStatement();
00232 
00234     HSTMT          GetStatement() { return m_hStmt; }
00235 
00236     // Command buffer related.
00237     void           Clear();
00238     void           AppendEscaped( const char * );
00239     void           Append( const char * );
00240     void           Append( int );
00241     void           Append( double );
00242     int            Appendf( CPL_FORMAT_STRING(const char *), ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00244     const char    *GetCommand() { return m_pszStatement; }
00245 
00246     int            ExecuteSQL( const char * = NULL );
00247 
00248     // Results fetching
00249     int            Fetch( int nOrientation = SQL_FETCH_NEXT,
00250                           int nOffset = 0 );
00251     void           ClearColumnData();
00252 
00253     int            GetColCount();
00254     const char    *GetColName( int );
00255     short          GetColType( int );
00256     const char    *GetColTypeName( int );
00257     short          GetColSize( int );
00258     short          GetColPrecision( int );
00259     short          GetColNullable( int );
00260     const char    *GetColColumnDef( int );
00261 
00262     int            GetColId( const char * );
00263     const char    *GetColData( int, const char * = NULL );
00264     const char    *GetColData( const char *, const char * = NULL );
00265     int            GetColDataLength( int );
00266     int            GetRowCountAffected();
00267 
00268     // Fetch special metadata.
00269     int            GetColumns( const char *pszTable,
00270                                const char *pszCatalog = NULL,
00271                                const char *pszSchema = NULL );
00272     int            GetPrimaryKeys( const char *pszTable,
00273                                    const char *pszCatalog = NULL,
00274                                    const char *pszSchema = NULL );
00275 
00276     int            GetTables( const char *pszCatalog = NULL,
00277                               const char *pszSchema = NULL );
00278 
00279     void           DumpResult( FILE *fp, int bShowSchema = FALSE );
00280 
00281     static CPLString GetTypeName( int );
00282     static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
00283 
00284     int            CollectResultsInfo();
00285 };
00286 
00287 #endif

Generated for GDAL by doxygen 1.7.6.1.