cpl_odbc.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_odbc.h 33666 2016-03-07 05:21:07Z goatbar $
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 
00044 #ifdef PATH_MAX
00045 #  define ODBC_FILENAME_MAX PATH_MAX
00046 #else
00047 #  define ODBC_FILENAME_MAX (255 + 1) /* Max path length */
00048 #endif
00049 
00050 
00060 class CPL_DLL CPLODBCDriverInstaller
00061 {
00062     char m_szPathOut[ODBC_FILENAME_MAX];
00063     char m_szError[SQL_MAX_MESSAGE_LENGTH];
00064     DWORD m_nErrorCode;
00065     DWORD m_nUsageCount;
00066 
00067   public:
00068 
00069     // Default constructor.
00070     CPLODBCDriverInstaller();
00071 
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 
00111 
00112     // The usage count of the driver after this function has been called
00113     int GetUsageCount() const {  return m_nUsageCount; }
00114 
00115 
00116     // Path of the target directory where the driver should be installed.
00117     // For details, see ODBC API Reference and lpszPathOut
00118     // parameter of SQLInstallDriverEx
00119     const char* GetPathOut() const { return m_szPathOut; }
00120 
00121 
00122     // If InstallDriver returns FALSE, then GetLastError then
00123     // error message can be obtained by calling this function.
00124     // Internally, it calls ODBC's SQLInstallerError function.
00125     const char* GetLastError() const { return m_szError; }
00126 
00127     // If InstallDriver returns FALSE, then GetLastErrorCode then
00128     // error code can be obtained by calling this function.
00129     // Internally, it calls ODBC's SQLInstallerError function.
00130     // See ODBC API Reference for possible error flags.
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 
00144 #if !defined(MISSING_SQLULEN)
00145 /* ODBC types to support 64 bit compilation */
00146 #  define CPL_SQLULEN SQLULEN
00147 #  define CPL_SQLLEN  SQLLEN
00148 #else
00149 #  define CPL_SQLULEN SQLUINTEGER
00150 #  define CPL_SQLLEN  SQLINTEGER
00151 #endif  /* ifdef SQLULEN */
00152 
00153 
00160 class CPL_DLL CPLODBCSession {
00161     char      m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
00162     HENV      m_hEnv;
00163     HDBC      m_hDBC;
00164     int       m_bInTransaction;
00165     int       m_bAutoCommit;
00166 
00167   public:
00168     CPLODBCSession();
00169     ~CPLODBCSession();
00170 
00171     int         EstablishSession( const char *pszDSN,
00172                                   const char *pszUserid,
00173                                   const char *pszPassword );
00174     const char  *GetLastError();
00175 
00176     // Transaction handling
00177 
00178     int         ClearTransaction();
00179     int         BeginTransaction();
00180     int         CommitTransaction();
00181     int         RollbackTransaction();
00182     int         IsInTransaction() { return m_bInTransaction; }
00183 
00184     // Essentially internal.
00185 
00186     int         CloseSession();
00187 
00188     int         Failed( int, HSTMT = NULL );
00189     HDBC        GetConnection() { return m_hDBC; }
00190     HENV        GetEnvironment()  { return m_hEnv; }
00191 };
00192 
00202 class CPL_DLL CPLODBCStatement {
00203 
00204     CPLODBCSession     *m_poSession;
00205     HSTMT               m_hStmt;
00206 
00207     SQLSMALLINT    m_nColCount;
00208     char         **m_papszColNames;
00209     SQLSMALLINT   *m_panColType;
00210     char         **m_papszColTypeNames;
00211     CPL_SQLULEN      *m_panColSize;
00212     SQLSMALLINT   *m_panColPrecision;
00213     SQLSMALLINT   *m_panColNullable;
00214     char         **m_papszColColumnDef;
00215 
00216     char         **m_papszColValues;
00217     CPL_SQLLEN       *m_panColValueLengths;
00218 
00219     int            Failed( int );
00220 
00221     char          *m_pszStatement;
00222     size_t         m_nStatementMax;
00223     size_t         m_nStatementLen;
00224 
00225   public:
00226     CPLODBCStatement( CPLODBCSession * );
00227     ~CPLODBCStatement();
00228 
00229     HSTMT          GetStatement() { return m_hStmt; }
00230 
00231     // Command buffer related.
00232     void           Clear();
00233     void           AppendEscaped( const char * );
00234     void           Append( const char * );
00235     void           Append( int );
00236     void           Append( double );
00237     int            Appendf( const char *, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00238     const char    *GetCommand() { return m_pszStatement; }
00239 
00240     int            ExecuteSQL( const char * = NULL );
00241 
00242     // Results fetching
00243     int            Fetch( int nOrientation = SQL_FETCH_NEXT,
00244                           int nOffset = 0 );
00245     void           ClearColumnData();
00246 
00247     int            GetColCount();
00248     const char    *GetColName( int );
00249     short          GetColType( int );
00250     const char    *GetColTypeName( int );
00251     short          GetColSize( int );
00252     short          GetColPrecision( int );
00253     short          GetColNullable( int );
00254     const char    *GetColColumnDef( int );
00255 
00256     int            GetColId( const char * );
00257     const char    *GetColData( int, const char * = NULL );
00258     const char    *GetColData( const char *, const char * = NULL );
00259     int            GetColDataLength( int );
00260     int            GetRowCountAffected();
00261 
00262     // Fetch special metadata.
00263     int            GetColumns( const char *pszTable,
00264                                const char *pszCatalog = NULL,
00265                                const char *pszSchema = NULL );
00266     int            GetPrimaryKeys( const char *pszTable,
00267                                    const char *pszCatalog = NULL,
00268                                    const char *pszSchema = NULL );
00269 
00270     int            GetTables( const char *pszCatalog = NULL,
00271                               const char *pszSchema = NULL );
00272 
00273     void           DumpResult( FILE *fp, int bShowSchema = FALSE );
00274 
00275     static CPLString GetTypeName( int );
00276     static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
00277 
00278     int            CollectResultsInfo();
00279 };
00280 
00281 #endif

Generated for GDAL by doxygen 1.6.1.