podbc.h

Go to the documentation of this file.
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: 19008 $
00031  * $Author: rjongbloed $
00032  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
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 #ifdef P_ODBC
00102 
00103 namespace PTODBC {
00104 
00105 #include <sql.h> 
00106 #include <sqlext.h>
00107 #include <odbcinst.h>
00108 
00109 #ifdef _MSC_VER
00110  #include <tchar.h>
00111  #pragma comment(lib,"odbc32.lib")
00112  #pragma comment(lib,"odbcCP32.lib")
00113  typedef ::LPCTSTR LPCTSTR;
00114  typedef ::LPVOID LPVOID;
00115  typedef ::DWORD DWORD;
00116  typedef ::LONG LONG;
00117  typedef ::ULONG ULONG;
00118  typedef ::USHORT USHORT;
00119 #else
00120  typedef const char *LPCTSTR;
00121  typedef void *LPVOID;
00122  typedef long long _int64;
00123  typedef long LONG;
00124  #define _T(x) x
00125 #endif // _MSC_VER
00126 
00127 
00128 // Max SQL String Data Length
00129 #define MAX_DATA_LEN 1024
00130 
00137 }; // PTODBC
00138 
00139 class PODBC;
00140 class PODBCRecord;
00141 class PODBCStmt : public PObject
00142 {
00143   PCLASSINFO(PODBCStmt, PObject);
00144 
00145 PTODBC::HSTMT m_hStmt;
00146 
00147 public:
00148 
00154           PODBCStmt(PODBC * odbc);
00155 
00160           ~PODBCStmt();
00162 
00167           operator PTODBC::HSTMT() { return m_hStmt; };
00169 
00170 
00176           PBoolean IsValid();
00177 
00181           PTODBC::DWORD GetChangedRowCount(void);
00182 
00187           PBoolean Query(PString strSQL);
00189 
00194           PBoolean Fetch();
00195 
00199           PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1);
00200 
00203           PBoolean FetchPrevious();
00204 
00207           PBoolean FetchNext();
00208 
00211           PBoolean FetchFirst();
00212 
00215           PBoolean FetchLast();
00216 
00219           PBoolean Cancel();
00221 
00229           PStringArray TableList(PString option = "");
00230 
00231 
00236           PBoolean SQL_OK(PTODBC::SQLRETURN res);
00237 
00241           void GetLastError();
00243 
00244           PODBC * odbclink;             
00245 
00246           int dbase; 
00247 
00248 };
00249 
00250 
00251 
00262 class PODBCRecord;
00263 class PODBC  : public PObject
00264 {
00265   PCLASSINFO(PODBC, PObject);
00266 
00267 public:
00268 
00273         PODBC();
00274 
00277         ~PODBC();
00279 
00288           enum FieldTypes
00289           {
00290                 LongVarChar     =-1,     
00291                 Binary                  =-2,
00292                 VarBinary       =-3,
00293                 LongVarBinary   =-4,
00294                 BigInt                  =-5,
00295                 TinyInt             =-6,
00296                 Bit             =-7,     
00297                 Guid                    =-11,
00298                 Unknown                 = 0,
00299                 Char                    = 1,
00300                 Numeric                 = 2,
00301                 Decimal                 = 3,
00302                 Integer                 = 4,
00303                 SmallInt                = 5,
00304                 Float                   = 6,
00305                 Real                    = 7,
00306                 Double                  = 8,
00307                 DateTime                = 9,
00308                 VarChar                 =12,            
00309                 Date                    =91,            
00310                 Time                    =92,            
00311                 TimeStamp               =93                     
00312           };
00313 
00319         enum PwType
00320           { 
00321                 oPString,       // String Value
00322                 oBOOL,          // Boolean 
00323                 ochar,          // Character
00324                 oshort,         // Short        
00325                 oint,           // Integer  use .AsInteger()
00326                 olong,          // long         
00327                 odouble,        // Double   use .AsReal()
00328                 oPBYTEArray,// Binary Data
00329                 oPInt64,        // BigInt       use .AsInt64()
00330                 oPTime,         // Time         use  PTime( "Value" ) 
00331                 oPGUID          // GUID         use  PGUID( "Value" ) To Be Implemented...?
00332         };
00333 
00338         enum DataSources
00339         {
00340                 mySQL,                  
00341                 MSSQL,                  
00342                 Oracle,                 
00343                 IBM_DB2,
00344                 DBASE,
00345                 Paradox,
00346                 Excel,
00347                 Ascii,
00348                 Foxpro,
00349                 MSAccess,
00350                 postgreSQL
00351         };
00352 
00355         enum MSSQLProtocols
00356         {
00357            MSSQLNamedPipes,
00358            MSSQLWinSock,
00359            MSSQLIPX,
00360            MSSQLBanyan,
00361            MSSQLRPC
00362          };
00363 
00365 
00375          class ConnectData
00376           {
00377                 public:
00378                   PFilePath DBPath;    
00379                   PString DefDir;          
00380                   PString User;            
00381                   PString Pass;            
00382                   PBoolean Excl_Trust;     
00383                   PString Host;            
00384                   int Port;                        
00385                   int opt;                         
00386           };
00388 
00389 
00394           class Row;
00395           class Field : public PObject
00396           {
00397                   PCLASSINFO(Field, PObject);
00398           public:
00399 
00405                   class Bind
00406                   {
00407                   public:
00408                            PString                              sbin;           
00409                            PString                              sbinlong;   
00410                            short int                    ssint;          
00411                            long int                             slint;          
00412                            double                               sdoub;          
00413                            unsigned char                sbit;           
00414                            unsigned char *              suchar;         
00415 #if _WIN32
00416                            _int64                               sbint;          
00417 #else
00418          PTODBC::_int64                         sbint;          
00419 #endif
00420                            PTODBC::DATE_STRUCT                  date;       
00421                            PTODBC::TIME_STRUCT                  time;           
00422                            PTODBC::TIMESTAMP_STRUCT             timestamp;      
00423                            PTODBC::SQLGUID                              guid;           
00424                            PTODBC::SQLINTEGER                   dataLen;        
00425                   };
00426 
00429                   PBoolean Post();
00430 
00433                   PString operator=(const PString & str);
00434 
00437                   PString AsString();   
00438 
00442                   void SetValue(PString value);  
00443 
00446                   void SetDefaultValues();
00447 
00451                   PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, PTODBC::SQLINTEGER & size);
00452 
00455 
00456                           Bind  Data;                    
00457                           PwType  Type;                  
00458                           FieldTypes ODBCType;   
00459 
00461                           PString Name;                  
00462                           PINDEX col;                    
00463 
00465                           PBoolean isReadOnly;           
00466                           PBoolean isNullable;           
00467                           PBoolean isAutoInc;            
00468                           int Decimals;                  
00469                           PBoolean LongData;             
00470 
00472                           Row * row;                     
00473           };
00475 
00476 
00485           class Row : public PObject
00486           {
00487           public:
00488 
00494                   Row(PODBCStmt * stmt);
00495 
00499                   Field & Column(PINDEX col);
00500 
00503                   Field & Column(PString name);
00504 
00507                   PStringArray ColumnNames();
00508 
00511                   PINDEX Columns();
00512 
00515                   PINDEX Rows();
00516 
00519                   Field & operator[] (PINDEX col);
00520 
00523                   Field & operator[] (PString col);
00524 
00527                   PBoolean Navigate(PINDEX row);
00528 
00531                   void SetNewRow();
00532 
00539                   PBoolean Post();
00540 
00544                   PBoolean Delete(PINDEX row =0);
00545 
00546                   PODBCRecord * rec;      
00547 
00548                   PINDEX CurRow;          
00549                   PBoolean NewRow;                        
00550                   PINDEX RowCount;                
00551 
00552           protected:
00553                   PArray<Field> Fields;   
00554           };
00556 
00562   class Table : public PObject
00563   {
00564   public:
00565 
00572           Table(PODBC * odbc, PString Query);
00573 
00576           ~Table();
00578 
00583           Row NewRow();
00584 
00587           PBoolean DeleteRow(PINDEX row = 0);
00588 
00591           PBoolean Post();
00593 
00598           PINDEX Rows();
00599 
00602           PINDEX Columns();
00603 
00606           PStringArray ColumnNames();
00607 
00612           Row & RecordHandler();
00613 
00616           Row & operator[] (PINDEX row);
00617 
00622           Field & operator() (PINDEX row, PINDEX col);
00623 
00627           Field & Column(PINDEX col);
00628 
00631           Field & Column(PString Name);
00633 
00634   protected:
00635           PODBCStmt stmt;                       
00636           PString tableName;            
00637           Row * RowHandler;                     
00638   };
00639 
00648           Table LoadTable(PString table);
00649 
00655           PBoolean Query(PString Query);
00657 
00658 
00668           PBoolean DataSource(DataSources Source, ConnectData Data);
00669   
00674           virtual PBoolean Connect(PTODBC::LPCTSTR svSource);
00675 
00678           PBoolean Connect_DB2(PFilePath DBPath);
00679 
00682           PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = "");
00683 
00686           PBoolean Connect_TXT(PFilePath TXTPath);
00687 
00690           PBoolean Connect_FOX(PFilePath DBPath,PString User = "",
00691                           PString Pass = "",PString Type= "DBF",
00692                           PBoolean Exclusive=PFalse);
00693 
00696           PBoolean Connect_MDB(PFilePath MDBPath,PString User ="",
00697                                 PString Pass = "",PBoolean Exclusive=PFalse);
00698 
00701           PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
00702                                 int version =5);
00703 
00706           PBoolean Connect_Oracle(PString Server,PString User="", PString Pass="");
00707 
00710           PBoolean Connect_DBASE(PDirectory DBPath);
00711 
00714           PBoolean Connect_MSSQL(PString User="",PString Pass="", 
00715                          PString Host ="(local)",PBoolean Trusted = PTrue, 
00716                          MSSQLProtocols Proto=MSSQLNamedPipes);
00717 
00720           PBoolean Connect_mySQL(PString User="",PString Pass="",
00721                          PString Host= "localhost",
00722                          int Port=3306,int Option=0);
00723 
00726           PBoolean ConnectDB_mySQL(PString DB,PString User="",
00727                         PString Pass="",PString Host= "localhost",
00728                         int Port=3306,int Option=0);
00729 
00732           PBoolean Connect_postgreSQL(PString DB,PString User,
00733         PString Pass,PString Host, int Port=5432,int Option=0);
00734 
00737           void Disconnect();
00739 
00746           PStringArray TableList(PString option = "");
00747 
00751           PBoolean NeedLongDataLen();
00752 
00755           virtual void OnSQLError(PString RetCode, PString RetString) {};
00756 
00757 
00764           void SetPrecision(int Digit);
00765 
00768           void SetTimeFormat(PTime::TimeFormat tformat);
00769 
00772           operator PTODBC::HDBC() { return m_hDBC; };
00774                 
00775          PODBC::DataSources  dbase; 
00776 
00777 protected:
00778 
00779   PTODBC::SQLRETURN       m_nReturn;      // Internal SQL Error code
00780   PTODBC::HENV            m_hEnv;         // Handle to environment
00781   PTODBC::HDBC            m_hDBC;         // Handle to database connection
00782 };
00783 
00792 class PDSNConnection : public PODBC
00793 {
00794   PCLASSINFO(PDSNConnection, PODBC);
00795 
00796 public:
00797 
00800           PDSNConnection();
00801           ~PDSNConnection();
00803 
00810           PBoolean Connect( PString Source ,PString Username, PString Password);
00811 
00812 };
00813 
00814  //--
00821 class PODBCRecord : public PObject
00822 {
00823   
00824   PCLASSINFO(PODBCRecord, PObject);
00825 
00826 PTODBC::HSTMT m_hStmt;
00827 
00828 public:
00829 
00834            PODBCRecord(PODBCStmt * hStmt);
00835 
00838           ~PODBCRecord(){};
00840 
00846           void Data(PINDEX Column, PODBC::Field & field);
00847 
00852           PBoolean InternalGetData(
00853                 PTODBC::USHORT Column,
00854                 PTODBC::LPVOID pBuffer, 
00855                 PTODBC::ULONG pBufLen,
00856                 PTODBC::SQLINTEGER * dataLen=NULL,
00857                 int Type=SQL_C_DEFAULT
00858           );
00859 
00860           /* Get Long Character Data. Long Data fields cannot be bound
00861                         and Data must be Got from the RecordSet.
00862           */
00863           PString GetLongData(PINDEX Column);
00864   
00867           PBoolean PostNew(PODBC::Row & rec);
00868 
00871           PBoolean PostUpdate(PODBC::Row & rec);
00872 
00876           PBoolean PostDelete(PINDEX row= 1);
00877 
00880           PBoolean InternalSaveLongData(PTODBC::SQLRETURN nRet,PODBC::Row & rec);
00881 
00884           PBoolean InternalBindColumn(
00885             PTODBC::USHORT Column,PTODBC::LPVOID pBuffer,
00886             PTODBC::ULONG pBufferSize,
00887             PTODBC::LONG * pReturnedBufferSize=NULL,
00888             PTODBC::USHORT nType=SQL_C_TCHAR
00889           );
00891 
00897           PINDEX ColumnByName(PString Column);
00898 
00901           PINDEX ColumnCount();
00902 
00905           PODBC::FieldTypes ColumnType(PINDEX Column );
00906 
00909           PTODBC::DWORD ColumnSize( PINDEX Column );
00910 
00913           PTODBC::DWORD ColumnScale( PINDEX Column );
00914 
00917           PString ColumnName( PINDEX Column);
00918 
00923           unsigned int ColumnPrecision( PINDEX Column );
00924 
00927           PBoolean IsColumnNullable( PINDEX Column );
00928 
00931           PBoolean IsColumnUpdatable( PINDEX Column );
00932 
00935           PBoolean IsColumnAutoIndex( PINDEX Column );
00936 
00938 
00943           static unsigned int Precision;      
00944           static int MaxCharSize;                     
00945           static PTime::TimeFormat TimeFormat;
00946 
00947 
00948           PODBCStmt * Stmt;                                     
00949           PODBC::DataSources dbase;                     
00950 
00951 
00952 };
00953 
00954 #endif // PTODBC
00955 
00956 #endif // !defined(PODBC_H)
00957 

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1