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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #ifndef CPL_ODBC_H_INCLUDED
00098 #define CPL_ODBC_H_INCLUDED
00099
00100 #include "cpl_port.h"
00101
00102 #ifndef WIN32CE
00103
00104 #ifdef WIN32
00105 # include <windows.h>
00106 #endif
00107
00108 #include <sql.h>
00109 #include <sqlext.h>
00110 #include <odbcinst.h>
00111 #include "cpl_string.h"
00112
00113 #ifdef PATH_MAX
00114 # define ODBC_FILENAME_MAX PATH_MAX
00115 #else
00116 # define ODBC_FILENAME_MAX (255 + 1)
00117 #endif
00118
00119
00129 class CPL_DLL CPLODBCDriverInstaller
00130 {
00131 char m_szPathOut[ODBC_FILENAME_MAX];
00132 char m_szError[SQL_MAX_MESSAGE_LENGTH];
00133 DWORD m_nErrorCode;
00134 DWORD m_nUsageCount;
00135
00136 public:
00137
00138
00139 CPLODBCDriverInstaller();
00140
00141
00159 int InstallDriver( const char* pszDriver, const char* pszPathIn,
00160 WORD fRequest = ODBC_INSTALL_COMPLETE );
00161
00178 int RemoveDriver( const char* pszDriverName, int fRemoveDSN = FALSE );
00179
00180
00181
00182 int GetUsageCount() const { return m_nUsageCount; }
00183
00184
00185
00186
00187
00188 const char* GetPathOut() const { return m_szPathOut; }
00189
00190
00191
00192
00193
00194 const char* GetLastError() const { return m_szError; }
00195
00196
00197
00198
00199
00200
00201 DWORD GetLastErrorCode() const { return m_nErrorCode; }
00202 };
00203
00204 class CPLODBCStatement;
00205
00206
00207
00208
00209
00210 #if defined(_MSC_VER) && !defined(SQLULEN) && !defined(_WIN64)
00211 # define MISSING_SQLULEN
00212 #endif
00213
00214 #if !defined(MISSING_SQLULEN)
00215
00216 # define _SQLULEN SQLULEN
00217 # define _SQLLEN SQLLEN
00218 #else
00219 # define _SQLULEN SQLUINTEGER
00220 # define _SQLLEN SQLINTEGER
00221 #endif
00222
00223
00230 class CPL_DLL CPLODBCSession {
00231 char m_szLastError[SQL_MAX_MESSAGE_LENGTH + 1];
00232 HENV m_hEnv;
00233 HDBC m_hDBC;
00234
00235 public:
00236 CPLODBCSession();
00237 ~CPLODBCSession();
00238
00239 int EstablishSession( const char *pszDSN,
00240 const char *pszUserid,
00241 const char *pszPassword );
00242 const char *GetLastError();
00243
00244
00245
00246 int CloseSession();
00247
00248 int Failed( int, HSTMT = NULL );
00249 HDBC GetConnection() { return m_hDBC; }
00250 HENV GetEnvironment() { return m_hEnv; }
00251 };
00252
00262 class CPL_DLL CPLODBCStatement {
00263
00264 CPLODBCSession *m_poSession;
00265 HSTMT m_hStmt;
00266
00267 SQLSMALLINT m_nColCount;
00268 char **m_papszColNames;
00269 SQLSMALLINT *m_panColType;
00270 char **m_papszColTypeNames;
00271 _SQLULEN *m_panColSize;
00272 SQLSMALLINT *m_panColPrecision;
00273 SQLSMALLINT *m_panColNullable;
00274
00275 char **m_papszColValues;
00276 _SQLLEN *m_panColValueLengths;
00277
00278 int Failed( int );
00279
00280 char *m_pszStatement;
00281 size_t m_nStatementMax;
00282 size_t m_nStatementLen;
00283
00284 public:
00285 CPLODBCStatement( CPLODBCSession * );
00286 ~CPLODBCStatement();
00287
00288 HSTMT GetStatement() { return m_hStmt; }
00289
00290
00291 void Clear();
00292 void AppendEscaped( const char * );
00293 void Append( const char * );
00294 void Append( int );
00295 void Append( double );
00296 int Appendf( const char *, ... );
00297 const char *GetCommand() { return m_pszStatement; }
00298
00299 int ExecuteSQL( const char * = NULL );
00300
00301
00302 int Fetch( int nOrientation = SQL_FETCH_NEXT,
00303 int nOffset = 0 );
00304 void ClearColumnData();
00305
00306 int GetColCount();
00307 const char *GetColName( int );
00308 short GetColType( int );
00309 const char *GetColTypeName( int );
00310 short GetColSize( int );
00311 short GetColPrecision( int );
00312 short GetColNullable( int );
00313
00314 int GetColId( const char * );
00315 const char *GetColData( int, const char * = NULL );
00316 const char *GetColData( const char *, const char * = NULL );
00317 int GetColDataLength( int );
00318
00319
00320 int GetColumns( const char *pszTable,
00321 const char *pszCatalog = NULL,
00322 const char *pszSchema = NULL );
00323 int GetPrimaryKeys( const char *pszTable,
00324 const char *pszCatalog = NULL,
00325 const char *pszSchema = NULL );
00326
00327 int GetTables( const char *pszCatalog = NULL,
00328 const char *pszSchema = NULL );
00329
00330 void DumpResult( FILE *fp, int bShowSchema = FALSE );
00331
00332 static CPLString GetTypeName( int );
00333 static SQLSMALLINT GetTypeMapping( SQLSMALLINT );
00334
00335 int CollectResultsInfo();
00336 };
00337
00338 #endif
00339
00340 #endif
00341
00342