Go to the documentation of this file.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 #ifndef WIN32CE
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)
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
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
00115 int GetUsageCount() const { return m_nUsageCount; }
00116
00117
00118
00119
00120
00121 const char* GetPathOut() const { return m_szPathOut; }
00122
00123
00124
00125
00126
00127 const char* GetLastError() const { return m_szError; }
00128
00129
00130
00131
00132
00133
00134 DWORD GetLastErrorCode() const { return m_nErrorCode; }
00135 };
00136
00137 class CPLODBCStatement;
00138
00139
00140
00141
00142
00143 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
00144 # define MISSING_SQLULEN
00145 #endif
00146
00147 #if !defined(MISSING_SQLULEN)
00148
00149 # define _SQLULEN SQLULEN
00150 # define _SQLLEN SQLLEN
00151 #else
00152 # define _SQLULEN SQLUINTEGER
00153 # define _SQLLEN SQLINTEGER
00154 #endif
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 int m_bInTransaction;
00168 int m_bAutoCommit;
00169
00170 public:
00171 CPLODBCSession();
00172 ~CPLODBCSession();
00173
00174 int EstablishSession( const char *pszDSN,
00175 const char *pszUserid,
00176 const char *pszPassword );
00177 const char *GetLastError();
00178
00179
00180
00181 int ClearTransaction();
00182 int BeginTransaction();
00183 int CommitTransaction();
00184 int RollbackTransaction();
00185 int IsInTransaction() { return m_bInTransaction; }
00186
00187
00188
00189 int CloseSession();
00190
00191 int Failed( int, HSTMT = NULL );
00192 HDBC GetConnection() { return m_hDBC; }
00193 HENV GetEnvironment() { return m_hEnv; }
00194 };
00195
00205 class CPL_DLL CPLODBCStatement {
00206
00207 CPLODBCSession *m_poSession;
00208 HSTMT m_hStmt;
00209
00210 SQLSMALLINT m_nColCount;
00211 char **m_papszColNames;
00212 SQLSMALLINT *m_panColType;
00213 char **m_papszColTypeNames;
00214 _SQLULEN *m_panColSize;
00215 SQLSMALLINT *m_panColPrecision;
00216 SQLSMALLINT *m_panColNullable;
00217 char **m_papszColColumnDef;
00218
00219 char **m_papszColValues;
00220 _SQLLEN *m_panColValueLengths;
00221
00222 int Failed( int );
00223
00224 char *m_pszStatement;
00225 size_t m_nStatementMax;
00226 size_t m_nStatementLen;
00227
00228 public:
00229 CPLODBCStatement( CPLODBCSession * );
00230 ~CPLODBCStatement();
00231
00232 HSTMT GetStatement() { return m_hStmt; }
00233
00234
00235 void Clear();
00236 void AppendEscaped( const char * );
00237 void Append( const char * );
00238 void Append( int );
00239 void Append( double );
00240 int Appendf( const char *, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00241 const char *GetCommand() { return m_pszStatement; }
00242
00243 int ExecuteSQL( const char * = NULL );
00244
00245
00246 int Fetch( int nOrientation = SQL_FETCH_NEXT,
00247 int nOffset = 0 );
00248 void ClearColumnData();
00249
00250 int GetColCount();
00251 const char *GetColName( int );
00252 short GetColType( int );
00253 const char *GetColTypeName( int );
00254 short GetColSize( int );
00255 short GetColPrecision( int );
00256 short GetColNullable( int );
00257 const char *GetColColumnDef( int );
00258
00259 int GetColId( const char * );
00260 const char *GetColData( int, const char * = NULL );
00261 const char *GetColData( const char *, const char * = NULL );
00262 int GetColDataLength( int );
00263 int GetRowCountAffected();
00264
00265
00266 int GetColumns( const char *pszTable,
00267 const char *pszCatalog = NULL,
00268 const char *pszSchema = NULL );
00269 int GetPrimaryKeys( const char *pszTable,
00270 const char *pszCatalog = NULL,
00271 const char *pszSchema = NULL );
00272
00273 int GetTables( const char *pszCatalog = NULL,
00274 const char *pszSchema = NULL );
00275
00276 void DumpResult( FILE *fp, int bShowSchema = FALSE );
00277
00278 static CPLString GetTypeName( int );
00279 static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
00280
00281 int CollectResultsInfo();
00282 };
00283
00284 #endif
00285
00286 #endif
00287
00288