PTLib
Version 2.10.4
|
00001 /* 00002 * podbc.h 00003 * 00004 * Virteos ODBC Implementation for PWLib Library. 00005 * 00006 * Virteos is a Trade Mark of ISVO (Asia) Pte Ltd. 00007 * 00008 * Copyright (c) 2005 ISVO (Asia) Pte Ltd. All Rights Reserved. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * 00021 * The Original Code is derived from and used in conjunction with the 00022 * pwlib Libaray of the OpenH323 Project (www.openh323.org/) 00023 * 00024 * The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd. 00025 * 00026 * Portions: Simple ODBC Wrapper Article www.codeproject.com 00027 * 00028 * Contributor(s): ______________________________________. 00029 * 00030 * $Revision: 24177 $ 00031 * $Author: rjongbloed $ 00032 * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $ 00033 */ 00034 00088 //-- 00089 00090 #ifndef PTLIB_PODBC_H 00091 #define PTLIB_PODBC_H 00092 00093 #if _MSC_VER > 1000 00094 #pragma once 00095 #endif // _MSC_VER > 1000 00096 00097 00098 #if defined(P_ODBC) && !defined(_WIN32_WCE) 00099 00100 #include <odbcinst.h> 00101 #include <sql.h> 00102 #include <sqlext.h> 00103 00104 #ifdef _MSC_VER 00105 #include <tchar.h> 00106 #pragma comment(lib,"odbc32.lib") 00107 #pragma comment(lib,"odbcCP32.lib") 00108 #else 00109 00110 #ifdef UNICODE 00111 typedef WCHAR TCHAR; 00112 typedef LPWSTR LPTSTR; 00113 typedef LPCWSTR LPCTSTR; 00114 // Needs a definition one day ... #define _T(x) 00115 #else 00116 typedef CHAR TCHAR; 00117 typedef LPSTR LPTSTR; 00118 typedef LPCSTR LPCTSTR; 00119 #define _T(x) x 00120 #endif 00121 00122 #endif // _MSC_VER 00123 00124 // Max SQL String Data Length 00125 #define MAX_DATA_LEN 1024 00126 00134 class PODBC; 00135 class PODBCRecord; 00136 00137 00138 class PODBCStmt : public PObject 00139 { 00140 PCLASSINFO(PODBCStmt, PObject); 00141 00142 public: 00148 PODBCStmt(PODBC * odbc); 00149 00154 ~PODBCStmt(); 00156 00161 operator HSTMT() { return m_hStmt; }; 00163 00164 00170 PBoolean IsValid(); 00171 00175 DWORD GetChangedRowCount(void); 00176 00181 PBoolean Query(PString strSQL); 00183 00188 PBoolean Fetch(); 00189 00193 PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1); 00194 00197 PBoolean FetchPrevious(); 00198 00201 PBoolean FetchNext(); 00202 00205 PBoolean FetchFirst(); 00206 00209 PBoolean FetchLast(); 00210 00213 PBoolean Cancel(); 00215 00223 PStringArray TableList(PString option = ""); 00224 00225 00230 PBoolean SQL_OK(SQLRETURN res); 00231 00235 void GetLastError(); 00236 00237 PODBC * GetLink() const { return odbclink; } 00238 int GetDBase() const { return dbase; } 00240 00241 protected: 00242 HSTMT m_hStmt; 00243 PODBC * odbclink; 00244 int dbase; 00245 }; 00246 00247 00248 00259 class PODBC : public PObject 00260 { 00261 PCLASSINFO(PODBC, PObject); 00262 00263 public: 00268 PODBC(); 00269 00272 ~PODBC(); 00274 00283 enum FieldTypes 00284 { 00285 LongVarChar =-1, 00286 Binary =-2, 00287 VarBinary =-3, 00288 LongVarBinary =-4, 00289 BigInt =-5, 00290 TinyInt =-6, 00291 Bit =-7, 00292 Guid =-11, 00293 Unknown = 0, 00294 Char = 1, 00295 Numeric = 2, 00296 Decimal = 3, 00297 Integer = 4, 00298 SmallInt = 5, 00299 Float = 6, 00300 Real = 7, 00301 Double = 8, 00302 DateTime = 9, 00303 VarChar =12, 00304 Date =91, 00305 Time =92, 00306 TimeStamp =93 00307 }; 00308 00314 enum PwType 00315 { 00316 oPString, // String Value 00317 oBOOL, // Boolean 00318 ochar, // Character 00319 oshort, // Short 00320 oint, // Integer use .AsInteger() 00321 olong, // long 00322 odouble, // Double use .AsReal() 00323 oPBYTEArray,// Binary Data 00324 oPInt64, // BigInt use .AsInt64() 00325 oPTime, // Time use PTime( "Value" ) 00326 oPGUID // GUID use PGUID( "Value" ) To Be Implemented...? 00327 }; 00328 00333 enum DataSources 00334 { 00335 mySQL, 00336 MSSQL, 00337 Oracle, 00338 IBM_DB2, 00339 DBASE, 00340 Paradox, 00341 Excel, 00342 Ascii, 00343 Foxpro, 00344 MSAccess, 00345 postgreSQL 00346 }; 00347 00350 enum MSSQLProtocols 00351 { 00352 MSSQLNamedPipes, 00353 MSSQLWinSock, 00354 MSSQLIPX, 00355 MSSQLBanyan, 00356 MSSQLRPC 00357 }; 00358 00360 00370 class ConnectData 00371 { 00372 public: 00373 PFilePath DBPath; 00374 PString DefDir; 00375 PString User; 00376 PString Pass; 00377 PBoolean Excl_Trust; 00378 PString Host; 00379 int Port; 00380 int opt; 00381 }; 00383 00384 00389 class Row; 00390 class Field : public PObject 00391 { 00392 PCLASSINFO(Field, PObject); 00393 public: 00394 00400 class Bind 00401 { 00402 public: 00403 PString sbin; 00404 PString sbinlong; 00405 short int ssint; 00406 long int slint; 00407 double sdoub; 00408 unsigned char sbit; 00409 unsigned char * suchar; 00410 PInt64 sbint; 00411 DATE_STRUCT date; 00412 TIME_STRUCT time; 00413 TIMESTAMP_STRUCT timestamp; 00414 SQLGUID guid; 00415 SQLLEN dataLen; 00416 }; 00417 00420 PBoolean Post(); 00421 00424 PString operator=(const PString & str); 00425 00428 PString AsString(); 00429 00433 void SetValue(PString value); 00434 00437 void SetDefaultValues(); 00438 00442 PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, SQLINTEGER & size); 00443 00446 00447 Bind Data; 00448 PwType Type; 00449 FieldTypes ODBCType; 00450 00452 PString Name; 00453 PINDEX col; 00454 00456 PBoolean isReadOnly; 00457 PBoolean isNullable; 00458 PBoolean isAutoInc; 00459 int Decimals; 00460 PBoolean LongData; 00461 00463 Row * row; 00464 }; 00466 00467 00476 class Row : public PObject 00477 { 00478 public: 00479 00485 Row(PODBCStmt * stmt); 00486 00490 Field & Column(PINDEX col); 00491 00494 Field & Column(PString name); 00495 00498 PStringArray ColumnNames(); 00499 00502 PINDEX Columns(); 00503 00506 PINDEX Rows(); 00507 00510 Field & operator[] (PINDEX col); 00511 00514 Field & operator[] (PString col); 00515 00518 PBoolean Navigate(PINDEX row); 00519 00522 void SetNewRow(); 00523 00530 PBoolean Post(); 00531 00535 PBoolean Delete(PINDEX row =0); 00536 00537 PODBCRecord * rec; 00538 00539 PINDEX CurRow; 00540 PBoolean NewRow; 00541 PINDEX RowCount; 00542 00543 protected: 00544 PArray<Field> Fields; 00545 }; 00547 00553 class Table : public PObject 00554 { 00555 public: 00556 00563 Table(PODBC * odbc, PString Query); 00564 00567 ~Table(); 00569 00574 Row NewRow(); 00575 00578 PBoolean DeleteRow(PINDEX row = 0); 00579 00582 PBoolean Post(); 00584 00589 PINDEX Rows(); 00590 00593 PINDEX Columns(); 00594 00597 PStringArray ColumnNames(); 00598 00603 Row & RecordHandler(); 00604 00607 Row & operator[] (PINDEX row); 00608 00613 Field & operator() (PINDEX row, PINDEX col); 00614 00618 Field & Column(PINDEX col); 00619 00622 Field & Column(PString Name); 00624 00625 protected: 00626 PODBCStmt stmt; 00627 PString tableName; 00628 Row * RowHandler; 00629 }; 00630 00639 Table LoadTable(PString table); 00640 00646 PBoolean Query(PString Query); 00648 00649 00659 PBoolean DataSource(DataSources Source, ConnectData Data); 00660 00665 virtual PBoolean Connect(LPCTSTR svSource); 00666 00669 PBoolean Connect_DB2(PFilePath DBPath); 00670 00673 PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = ""); 00674 00677 PBoolean Connect_TXT(PFilePath TXTPath); 00678 00681 PBoolean Connect_FOX(PFilePath DBPath,PString User = "", 00682 PString Pass = "",PString Type= "DBF", 00683 PBoolean Exclusive=false); 00684 00687 PBoolean Connect_MDB(PFilePath MDBPath,PString User ="", 00688 PString Pass = "",PBoolean Exclusive=false); 00689 00692 PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir, 00693 int version =5); 00694 00697 PBoolean Connect_Oracle(PString Server,PString User="", PString Pass=""); 00698 00701 PBoolean Connect_DBASE(PDirectory DBPath); 00702 00705 PBoolean Connect_MSSQL(PString User="",PString Pass="", 00706 PString Host ="(local)",PBoolean Trusted = true, 00707 MSSQLProtocols Proto=MSSQLNamedPipes); 00708 00711 PBoolean Connect_mySQL(PString User="",PString Pass="", 00712 PString Host= "localhost", 00713 int Port=3306,int Option=0); 00714 00717 PBoolean ConnectDB_mySQL(PString DB,PString User="", 00718 PString Pass="",PString Host= "localhost", 00719 int Port=3306,int Option=0); 00720 00723 PBoolean Connect_postgreSQL(PString DB,PString User, 00724 PString Pass,PString Host, int Port=5432,int Option=0); 00725 00728 void Disconnect(); 00730 00737 PStringArray TableList(PString option = ""); 00738 00742 PBoolean NeedLongDataLen(); 00743 00746 virtual void OnSQLError(PString RetCode, PString RetString) {}; 00747 00748 00755 void SetPrecision(int Digit); 00756 00759 void SetTimeFormat(PTime::TimeFormat tformat); 00760 00763 operator HDBC() { return m_hDBC; }; 00765 00766 PODBC::DataSources dbase; 00767 00768 protected: 00769 SQLRETURN m_nReturn; // Internal SQL Error code 00770 HENV m_hEnv; // Handle to environment 00771 HDBC m_hDBC; // Handle to database connection 00772 }; 00773 00774 00781 class PDSNConnection : public PODBC 00782 { 00783 PCLASSINFO(PDSNConnection, PODBC); 00784 00785 public: 00788 PDSNConnection(); 00789 ~PDSNConnection(); 00791 00798 PBoolean Connect( PString Source ,PString Username, PString Password); 00799 }; 00800 00801 00802 //-- 00809 class PODBCRecord : public PObject 00810 { 00811 PCLASSINFO(PODBCRecord, PObject); 00812 00813 public: 00818 PODBCRecord(PODBCStmt * hStmt); 00819 00822 ~PODBCRecord(){}; 00824 00830 void Data(PINDEX Column, PODBC::Field & field); 00831 00836 PBoolean InternalGetData( 00837 USHORT Column, 00838 LPVOID pBuffer, 00839 ULONG pBufLen, 00840 SQLINTEGER * dataLen=NULL, 00841 int Type=SQL_C_DEFAULT 00842 ); 00843 00844 /* Get Long Character Data. Long Data fields cannot be bound 00845 and Data must be Got from the RecordSet. 00846 */ 00847 PString GetLongData(PINDEX Column); 00848 00851 PBoolean PostNew(PODBC::Row & rec); 00852 00855 PBoolean PostUpdate(PODBC::Row & rec); 00856 00860 PBoolean PostDelete(PINDEX row= 1); 00861 00864 PBoolean InternalSaveLongData(SQLRETURN nRet,PODBC::Row & rec); 00865 00868 PBoolean InternalBindColumn( 00869 USHORT Column,LPVOID pBuffer, 00870 ULONG pBufferSize, 00871 LONG * pReturnedBufferSize=NULL, 00872 USHORT nType=SQL_C_TCHAR 00873 ); 00875 00881 PINDEX ColumnByName(PString Column); 00882 00885 PINDEX ColumnCount(); 00886 00889 PODBC::FieldTypes ColumnType(PINDEX Column ); 00890 00893 DWORD ColumnSize( PINDEX Column ); 00894 00897 DWORD ColumnScale( PINDEX Column ); 00898 00901 PString ColumnName( PINDEX Column); 00902 00907 unsigned int ColumnPrecision( PINDEX Column ); 00908 00911 PBoolean IsColumnNullable( PINDEX Column ); 00912 00915 PBoolean IsColumnUpdatable( PINDEX Column ); 00916 00919 PBoolean IsColumnAutoIndex( PINDEX Column ); 00920 00922 00927 static unsigned int Precision; 00928 static int MaxCharSize; 00929 static PTime::TimeFormat TimeFormat; 00930 00931 00932 protected: 00933 HSTMT m_hStmt; 00934 PODBCStmt * Stmt; 00935 PODBC::DataSources dbase; 00936 00937 friend class PODBC::Field; 00938 friend class PODBC::Row; 00939 }; 00940 00941 #endif // P_ODBC 00942 00943 #endif // PTLIB_PODBC_H 00944 00945 00946 // End Of File ///////////////////////////////////////////////////////////////