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
00088
00089
00090 #if !defined(PODBC_H)
00091 #define PODBC_H
00092
00093 #if _MSC_VER > 1000
00094 #pragma once
00095 #endif // _MSC_VER > 1000
00096
00097 #ifndef _PTLIB_H
00098 #include <ptlib.h>
00099 #endif
00100
00101 #if defined(P_ODBC) && !defined(_WIN32_WCE)
00102
00103 #include <sql.h>
00104 #include <sqlext.h>
00105 #include <odbcinst.h>
00106
00107 #ifdef _MSC_VER
00108 #include <tchar.h>
00109 #pragma comment(lib,"odbc32.lib")
00110 #pragma comment(lib,"odbcCP32.lib")
00111 #else
00112
00113 #ifdef UNICODE
00114 typedef WCHAR TCHAR;
00115 typedef LPWSTR LPTSTR;
00116 typedef LPCWSTR LPCTSTR;
00117
00118 #else
00119 typedef CHAR TCHAR;
00120 typedef LPSTR LPTSTR;
00121 typedef LPCSTR LPCTSTR;
00122 #define _T(x) x
00123 #endif
00124
00125 #endif // _MSC_VER
00126
00127
00128 #define MAX_DATA_LEN 1024
00129
00137 class PODBC;
00138 class PODBCRecord;
00139
00140
00141 class PODBCStmt : public PObject
00142 {
00143 PCLASSINFO(PODBCStmt, PObject);
00144
00145 public:
00151 PODBCStmt(PODBC * odbc);
00152
00157 ~PODBCStmt();
00159
00164 operator HSTMT() { return m_hStmt; };
00166
00167
00173 PBoolean IsValid();
00174
00178 DWORD GetChangedRowCount(void);
00179
00184 PBoolean Query(PString strSQL);
00186
00191 PBoolean Fetch();
00192
00196 PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1);
00197
00200 PBoolean FetchPrevious();
00201
00204 PBoolean FetchNext();
00205
00208 PBoolean FetchFirst();
00209
00212 PBoolean FetchLast();
00213
00216 PBoolean Cancel();
00218
00226 PStringArray TableList(PString option = "");
00227
00228
00233 PBoolean SQL_OK(SQLRETURN res);
00234
00238 void GetLastError();
00239
00240 PODBC * GetLink() const { return odbclink; }
00241 int GetDBase() const { return dbase; }
00243
00244 protected:
00245 HSTMT m_hStmt;
00246 PODBC * odbclink;
00247 int dbase;
00248 };
00249
00250
00251
00262 class PODBC : public PObject
00263 {
00264 PCLASSINFO(PODBC, PObject);
00265
00266 public:
00271 PODBC();
00272
00275 ~PODBC();
00277
00286 enum FieldTypes
00287 {
00288 LongVarChar =-1,
00289 Binary =-2,
00290 VarBinary =-3,
00291 LongVarBinary =-4,
00292 BigInt =-5,
00293 TinyInt =-6,
00294 Bit =-7,
00295 Guid =-11,
00296 Unknown = 0,
00297 Char = 1,
00298 Numeric = 2,
00299 Decimal = 3,
00300 Integer = 4,
00301 SmallInt = 5,
00302 Float = 6,
00303 Real = 7,
00304 Double = 8,
00305 DateTime = 9,
00306 VarChar =12,
00307 Date =91,
00308 Time =92,
00309 TimeStamp =93
00310 };
00311
00317 enum PwType
00318 {
00319 oPString,
00320 oBOOL,
00321 ochar,
00322 oshort,
00323 oint,
00324 olong,
00325 odouble,
00326 oPBYTEArray,
00327 oPInt64,
00328 oPTime,
00329 oPGUID
00330 };
00331
00336 enum DataSources
00337 {
00338 mySQL,
00339 MSSQL,
00340 Oracle,
00341 IBM_DB2,
00342 DBASE,
00343 Paradox,
00344 Excel,
00345 Ascii,
00346 Foxpro,
00347 MSAccess,
00348 postgreSQL
00349 };
00350
00353 enum MSSQLProtocols
00354 {
00355 MSSQLNamedPipes,
00356 MSSQLWinSock,
00357 MSSQLIPX,
00358 MSSQLBanyan,
00359 MSSQLRPC
00360 };
00361
00363
00373 class ConnectData
00374 {
00375 public:
00376 PFilePath DBPath;
00377 PString DefDir;
00378 PString User;
00379 PString Pass;
00380 PBoolean Excl_Trust;
00381 PString Host;
00382 int Port;
00383 int opt;
00384 };
00386
00387
00392 class Row;
00393 class Field : public PObject
00394 {
00395 PCLASSINFO(Field, PObject);
00396 public:
00397
00403 class Bind
00404 {
00405 public:
00406 PString sbin;
00407 PString sbinlong;
00408 short int ssint;
00409 long int slint;
00410 double sdoub;
00411 unsigned char sbit;
00412 unsigned char * suchar;
00413 PInt64 sbint;
00414 DATE_STRUCT date;
00415 TIME_STRUCT time;
00416 TIMESTAMP_STRUCT timestamp;
00417 SQLGUID guid;
00418 SQLLEN dataLen;
00419 };
00420
00423 PBoolean Post();
00424
00427 PString operator=(const PString & str);
00428
00431 PString AsString();
00432
00436 void SetValue(PString value);
00437
00440 void SetDefaultValues();
00441
00445 PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, SQLINTEGER & size);
00446
00449
00450 Bind Data;
00451 PwType Type;
00452 FieldTypes ODBCType;
00453
00455 PString Name;
00456 PINDEX col;
00457
00459 PBoolean isReadOnly;
00460 PBoolean isNullable;
00461 PBoolean isAutoInc;
00462 int Decimals;
00463 PBoolean LongData;
00464
00466 Row * row;
00467 };
00469
00470
00479 class Row : public PObject
00480 {
00481 public:
00482
00488 Row(PODBCStmt * stmt);
00489
00493 Field & Column(PINDEX col);
00494
00497 Field & Column(PString name);
00498
00501 PStringArray ColumnNames();
00502
00505 PINDEX Columns();
00506
00509 PINDEX Rows();
00510
00513 Field & operator[] (PINDEX col);
00514
00517 Field & operator[] (PString col);
00518
00521 PBoolean Navigate(PINDEX row);
00522
00525 void SetNewRow();
00526
00533 PBoolean Post();
00534
00538 PBoolean Delete(PINDEX row =0);
00539
00540 PODBCRecord * rec;
00541
00542 PINDEX CurRow;
00543 PBoolean NewRow;
00544 PINDEX RowCount;
00545
00546 protected:
00547 PArray<Field> Fields;
00548 };
00550
00556 class Table : public PObject
00557 {
00558 public:
00559
00566 Table(PODBC * odbc, PString Query);
00567
00570 ~Table();
00572
00577 Row NewRow();
00578
00581 PBoolean DeleteRow(PINDEX row = 0);
00582
00585 PBoolean Post();
00587
00592 PINDEX Rows();
00593
00596 PINDEX Columns();
00597
00600 PStringArray ColumnNames();
00601
00606 Row & RecordHandler();
00607
00610 Row & operator[] (PINDEX row);
00611
00616 Field & operator() (PINDEX row, PINDEX col);
00617
00621 Field & Column(PINDEX col);
00622
00625 Field & Column(PString Name);
00627
00628 protected:
00629 PODBCStmt stmt;
00630 PString tableName;
00631 Row * RowHandler;
00632 };
00633
00642 Table LoadTable(PString table);
00643
00649 PBoolean Query(PString Query);
00651
00652
00662 PBoolean DataSource(DataSources Source, ConnectData Data);
00663
00668 virtual PBoolean Connect(LPCTSTR svSource);
00669
00672 PBoolean Connect_DB2(PFilePath DBPath);
00673
00676 PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = "");
00677
00680 PBoolean Connect_TXT(PFilePath TXTPath);
00681
00684 PBoolean Connect_FOX(PFilePath DBPath,PString User = "",
00685 PString Pass = "",PString Type= "DBF",
00686 PBoolean Exclusive=PFalse);
00687
00690 PBoolean Connect_MDB(PFilePath MDBPath,PString User ="",
00691 PString Pass = "",PBoolean Exclusive=PFalse);
00692
00695 PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
00696 int version =5);
00697
00700 PBoolean Connect_Oracle(PString Server,PString User="", PString Pass="");
00701
00704 PBoolean Connect_DBASE(PDirectory DBPath);
00705
00708 PBoolean Connect_MSSQL(PString User="",PString Pass="",
00709 PString Host ="(local)",PBoolean Trusted = PTrue,
00710 MSSQLProtocols Proto=MSSQLNamedPipes);
00711
00714 PBoolean Connect_mySQL(PString User="",PString Pass="",
00715 PString Host= "localhost",
00716 int Port=3306,int Option=0);
00717
00720 PBoolean ConnectDB_mySQL(PString DB,PString User="",
00721 PString Pass="",PString Host= "localhost",
00722 int Port=3306,int Option=0);
00723
00726 PBoolean Connect_postgreSQL(PString DB,PString User,
00727 PString Pass,PString Host, int Port=5432,int Option=0);
00728
00731 void Disconnect();
00733
00740 PStringArray TableList(PString option = "");
00741
00745 PBoolean NeedLongDataLen();
00746
00749 virtual void OnSQLError(PString RetCode, PString RetString) {};
00750
00751
00758 void SetPrecision(int Digit);
00759
00762 void SetTimeFormat(PTime::TimeFormat tformat);
00763
00766 operator HDBC() { return m_hDBC; };
00768
00769 PODBC::DataSources dbase;
00770
00771 protected:
00772 SQLRETURN m_nReturn;
00773 HENV m_hEnv;
00774 HDBC m_hDBC;
00775 };
00776
00777
00784 class PDSNConnection : public PODBC
00785 {
00786 PCLASSINFO(PDSNConnection, PODBC);
00787
00788 public:
00791 PDSNConnection();
00792 ~PDSNConnection();
00794
00801 PBoolean Connect( PString Source ,PString Username, PString Password);
00802 };
00803
00804
00805
00812 class PODBCRecord : public PObject
00813 {
00814 PCLASSINFO(PODBCRecord, PObject);
00815
00816 public:
00821 PODBCRecord(PODBCStmt * hStmt);
00822
00825 ~PODBCRecord(){};
00827
00833 void Data(PINDEX Column, PODBC::Field & field);
00834
00839 PBoolean InternalGetData(
00840 USHORT Column,
00841 LPVOID pBuffer,
00842 ULONG pBufLen,
00843 SQLINTEGER * dataLen=NULL,
00844 int Type=SQL_C_DEFAULT
00845 );
00846
00847
00848
00849
00850 PString GetLongData(PINDEX Column);
00851
00854 PBoolean PostNew(PODBC::Row & rec);
00855
00858 PBoolean PostUpdate(PODBC::Row & rec);
00859
00863 PBoolean PostDelete(PINDEX row= 1);
00864
00867 PBoolean InternalSaveLongData(SQLRETURN nRet,PODBC::Row & rec);
00868
00871 PBoolean InternalBindColumn(
00872 USHORT Column,LPVOID pBuffer,
00873 ULONG pBufferSize,
00874 LONG * pReturnedBufferSize=NULL,
00875 USHORT nType=SQL_C_TCHAR
00876 );
00878
00884 PINDEX ColumnByName(PString Column);
00885
00888 PINDEX ColumnCount();
00889
00892 PODBC::FieldTypes ColumnType(PINDEX Column );
00893
00896 DWORD ColumnSize( PINDEX Column );
00897
00900 DWORD ColumnScale( PINDEX Column );
00901
00904 PString ColumnName( PINDEX Column);
00905
00910 unsigned int ColumnPrecision( PINDEX Column );
00911
00914 PBoolean IsColumnNullable( PINDEX Column );
00915
00918 PBoolean IsColumnUpdatable( PINDEX Column );
00919
00922 PBoolean IsColumnAutoIndex( PINDEX Column );
00923
00925
00930 static unsigned int Precision;
00931 static int MaxCharSize;
00932 static PTime::TimeFormat TimeFormat;
00933
00934
00935 protected:
00936 HSTMT m_hStmt;
00937 PODBCStmt * Stmt;
00938 PODBC::DataSources dbase;
00939
00940 friend class PODBC::Field;
00941 friend class PODBC::Row;
00942 };
00943
00944 #endif // P_ODBC
00945
00946 #endif // !defined(PODBC_H)
00947