00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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)
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
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
00113 int GetUsageCount() const { return m_nUsageCount; }
00114
00115
00116
00117
00118
00119 const char* GetPathOut() const { return m_szPathOut; }
00120
00121
00122
00123
00124
00125 const char* GetLastError() const { return m_szError; }
00126
00127
00128
00129
00130
00131 DWORD GetLastErrorCode() const { return m_nErrorCode; }
00132 };
00133
00134 class CPLODBCStatement;
00135
00136
00137
00138
00139
00140 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
00141 # define MISSING_SQLULEN
00142 #endif
00143
00144 #if !defined(MISSING_SQLULEN)
00145
00146 # define CPL_SQLULEN SQLULEN
00147 # define CPL_SQLLEN SQLLEN
00148 #else
00149 # define CPL_SQLULEN SQLUINTEGER
00150 # define CPL_SQLLEN SQLINTEGER
00151 #endif
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
00177
00178 int ClearTransaction();
00179 int BeginTransaction();
00180 int CommitTransaction();
00181 int RollbackTransaction();
00182 int IsInTransaction() { return m_bInTransaction; }
00183
00184
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
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
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
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