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 #endif // _MSC_VER
00112
00113
00114
00115 #define MAX_DATA_LEN 1024
00116
00124 class PODBC;
00125 class PODBCRecord;
00126
00127
00128 class PODBCStmt : public PObject
00129 {
00130 PCLASSINFO(PODBCStmt, PObject);
00131
00132 public:
00138 PODBCStmt(PODBC * odbc);
00139
00144 ~PODBCStmt();
00146
00151 operator HSTMT() { return m_hStmt; };
00153
00154
00160 PBoolean IsValid();
00161
00165 DWORD GetChangedRowCount(void);
00166
00171 PBoolean Query(PString strSQL);
00173
00178 PBoolean Fetch();
00179
00183 PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1);
00184
00187 PBoolean FetchPrevious();
00188
00191 PBoolean FetchNext();
00192
00195 PBoolean FetchFirst();
00196
00199 PBoolean FetchLast();
00200
00203 PBoolean Cancel();
00205
00213 PStringArray TableList(PString option = "");
00214
00215
00220 PBoolean SQL_OK(SQLRETURN res);
00221
00225 void GetLastError();
00226
00227 PODBC * GetLink() const { return odbclink; }
00228 int GetDBase() const { return dbase; }
00230
00231 protected:
00232 HSTMT m_hStmt;
00233 PODBC * odbclink;
00234 int dbase;
00235 };
00236
00237
00238
00249 class PODBC : public PObject
00250 {
00251 PCLASSINFO(PODBC, PObject);
00252
00253 public:
00258 PODBC();
00259
00262 ~PODBC();
00264
00273 enum FieldTypes
00274 {
00275 LongVarChar =-1,
00276 Binary =-2,
00277 VarBinary =-3,
00278 LongVarBinary =-4,
00279 BigInt =-5,
00280 TinyInt =-6,
00281 Bit =-7,
00282 Guid =-11,
00283 Unknown = 0,
00284 Char = 1,
00285 Numeric = 2,
00286 Decimal = 3,
00287 Integer = 4,
00288 SmallInt = 5,
00289 Float = 6,
00290 Real = 7,
00291 Double = 8,
00292 DateTime = 9,
00293 VarChar =12,
00294 Date =91,
00295 Time =92,
00296 TimeStamp =93
00297 };
00298
00304 enum PwType
00305 {
00306 oPString,
00307 oBOOL,
00308 ochar,
00309 oshort,
00310 oint,
00311 olong,
00312 odouble,
00313 oPBYTEArray,
00314 oPInt64,
00315 oPTime,
00316 oPGUID
00317 };
00318
00323 enum DataSources
00324 {
00325 mySQL,
00326 MSSQL,
00327 Oracle,
00328 IBM_DB2,
00329 DBASE,
00330 Paradox,
00331 Excel,
00332 Ascii,
00333 Foxpro,
00334 MSAccess,
00335 postgreSQL
00336 };
00337
00340 enum MSSQLProtocols
00341 {
00342 MSSQLNamedPipes,
00343 MSSQLWinSock,
00344 MSSQLIPX,
00345 MSSQLBanyan,
00346 MSSQLRPC
00347 };
00348
00350
00360 class ConnectData
00361 {
00362 public:
00363 PFilePath DBPath;
00364 PString DefDir;
00365 PString User;
00366 PString Pass;
00367 PBoolean Excl_Trust;
00368 PString Host;
00369 int Port;
00370 int opt;
00371 };
00373
00374
00379 class Row;
00380 class Field : public PObject
00381 {
00382 PCLASSINFO(Field, PObject);
00383 public:
00384
00390 class Bind
00391 {
00392 public:
00393 PString sbin;
00394 PString sbinlong;
00395 short int ssint;
00396 long int slint;
00397 double sdoub;
00398 unsigned char sbit;
00399 unsigned char * suchar;
00400 PInt64 sbint;
00401 DATE_STRUCT date;
00402 TIME_STRUCT time;
00403 TIMESTAMP_STRUCT timestamp;
00404 SQLGUID guid;
00405 SQLLEN dataLen;
00406 };
00407
00410 PBoolean Post();
00411
00414 PString operator=(const PString & str);
00415
00418 PString AsString();
00419
00423 void SetValue(PString value);
00424
00427 void SetDefaultValues();
00428
00432 PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, SQLINTEGER & size);
00433
00436
00437 Bind Data;
00438 PwType Type;
00439 FieldTypes ODBCType;
00440
00442 PString Name;
00443 PINDEX col;
00444
00446 PBoolean isReadOnly;
00447 PBoolean isNullable;
00448 PBoolean isAutoInc;
00449 int Decimals;
00450 PBoolean LongData;
00451
00453 Row * row;
00454 };
00456
00457
00466 class Row : public PObject
00467 {
00468 public:
00469
00475 Row(PODBCStmt * stmt);
00476
00480 Field & Column(PINDEX col);
00481
00484 Field & Column(PString name);
00485
00488 PStringArray ColumnNames();
00489
00492 PINDEX Columns();
00493
00496 PINDEX Rows();
00497
00500 Field & operator[] (PINDEX col);
00501
00504 Field & operator[] (PString col);
00505
00508 PBoolean Navigate(PINDEX row);
00509
00512 void SetNewRow();
00513
00520 PBoolean Post();
00521
00525 PBoolean Delete(PINDEX row =0);
00526
00527 PODBCRecord * rec;
00528
00529 PINDEX CurRow;
00530 PBoolean NewRow;
00531 PINDEX RowCount;
00532
00533 protected:
00534 PArray<Field> Fields;
00535 };
00537
00543 class Table : public PObject
00544 {
00545 public:
00546
00553 Table(PODBC * odbc, PString Query);
00554
00557 ~Table();
00559
00564 Row NewRow();
00565
00568 PBoolean DeleteRow(PINDEX row = 0);
00569
00572 PBoolean Post();
00574
00579 PINDEX Rows();
00580
00583 PINDEX Columns();
00584
00587 PStringArray ColumnNames();
00588
00593 Row & RecordHandler();
00594
00597 Row & operator[] (PINDEX row);
00598
00603 Field & operator() (PINDEX row, PINDEX col);
00604
00608 Field & Column(PINDEX col);
00609
00612 Field & Column(PString Name);
00614
00615 protected:
00616 PODBCStmt stmt;
00617 PString tableName;
00618 Row * RowHandler;
00619 };
00620
00629 Table LoadTable(PString table);
00630
00636 PBoolean Query(PString Query);
00638
00639
00649 PBoolean DataSource(DataSources Source, ConnectData Data);
00650
00655 virtual PBoolean Connect(LPCTSTR svSource);
00656
00659 PBoolean Connect_DB2(PFilePath DBPath);
00660
00663 PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = "");
00664
00667 PBoolean Connect_TXT(PFilePath TXTPath);
00668
00671 PBoolean Connect_FOX(PFilePath DBPath,PString User = "",
00672 PString Pass = "",PString Type= "DBF",
00673 PBoolean Exclusive=PFalse);
00674
00677 PBoolean Connect_MDB(PFilePath MDBPath,PString User ="",
00678 PString Pass = "",PBoolean Exclusive=PFalse);
00679
00682 PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
00683 int version =5);
00684
00687 PBoolean Connect_Oracle(PString Server,PString User="", PString Pass="");
00688
00691 PBoolean Connect_DBASE(PDirectory DBPath);
00692
00695 PBoolean Connect_MSSQL(PString User="",PString Pass="",
00696 PString Host ="(local)",PBoolean Trusted = PTrue,
00697 MSSQLProtocols Proto=MSSQLNamedPipes);
00698
00701 PBoolean Connect_mySQL(PString User="",PString Pass="",
00702 PString Host= "localhost",
00703 int Port=3306,int Option=0);
00704
00707 PBoolean ConnectDB_mySQL(PString DB,PString User="",
00708 PString Pass="",PString Host= "localhost",
00709 int Port=3306,int Option=0);
00710
00713 PBoolean Connect_postgreSQL(PString DB,PString User,
00714 PString Pass,PString Host, int Port=5432,int Option=0);
00715
00718 void Disconnect();
00720
00727 PStringArray TableList(PString option = "");
00728
00732 PBoolean NeedLongDataLen();
00733
00736 virtual void OnSQLError(PString RetCode, PString RetString) {};
00737
00738
00745 void SetPrecision(int Digit);
00746
00749 void SetTimeFormat(PTime::TimeFormat tformat);
00750
00753 operator HDBC() { return m_hDBC; };
00755
00756 PODBC::DataSources dbase;
00757
00758 protected:
00759 SQLRETURN m_nReturn;
00760 HENV m_hEnv;
00761 HDBC m_hDBC;
00762 };
00763
00764
00771 class PDSNConnection : public PODBC
00772 {
00773 PCLASSINFO(PDSNConnection, PODBC);
00774
00775 public:
00778 PDSNConnection();
00779 ~PDSNConnection();
00781
00788 PBoolean Connect( PString Source ,PString Username, PString Password);
00789 };
00790
00791
00792
00799 class PODBCRecord : public PObject
00800 {
00801 PCLASSINFO(PODBCRecord, PObject);
00802
00803 public:
00808 PODBCRecord(PODBCStmt * hStmt);
00809
00812 ~PODBCRecord(){};
00814
00820 void Data(PINDEX Column, PODBC::Field & field);
00821
00826 PBoolean InternalGetData(
00827 USHORT Column,
00828 LPVOID pBuffer,
00829 ULONG pBufLen,
00830 SQLINTEGER * dataLen=NULL,
00831 int Type=SQL_C_DEFAULT
00832 );
00833
00834
00835
00836
00837 PString GetLongData(PINDEX Column);
00838
00841 PBoolean PostNew(PODBC::Row & rec);
00842
00845 PBoolean PostUpdate(PODBC::Row & rec);
00846
00850 PBoolean PostDelete(PINDEX row= 1);
00851
00854 PBoolean InternalSaveLongData(SQLRETURN nRet,PODBC::Row & rec);
00855
00858 PBoolean InternalBindColumn(
00859 USHORT Column,LPVOID pBuffer,
00860 ULONG pBufferSize,
00861 LONG * pReturnedBufferSize=NULL,
00862 USHORT nType=SQL_C_TCHAR
00863 );
00865
00871 PINDEX ColumnByName(PString Column);
00872
00875 PINDEX ColumnCount();
00876
00879 PODBC::FieldTypes ColumnType(PINDEX Column );
00880
00883 DWORD ColumnSize( PINDEX Column );
00884
00887 DWORD ColumnScale( PINDEX Column );
00888
00891 PString ColumnName( PINDEX Column);
00892
00897 unsigned int ColumnPrecision( PINDEX Column );
00898
00901 PBoolean IsColumnNullable( PINDEX Column );
00902
00905 PBoolean IsColumnUpdatable( PINDEX Column );
00906
00909 PBoolean IsColumnAutoIndex( PINDEX Column );
00910
00912
00917 static unsigned int Precision;
00918 static int MaxCharSize;
00919 static PTime::TimeFormat TimeFormat;
00920
00921
00922 protected:
00923 HSTMT m_hStmt;
00924 PODBCStmt * Stmt;
00925 PODBC::DataSources dbase;
00926
00927 friend class PODBC::Field;
00928 friend class PODBC::Row;
00929 };
00930
00931 #endif // P_ODBC
00932
00933 #endif // !defined(PODBC_H)
00934