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  * $Log: podbc.h,v $
00031  * Revision 1.4  2007/08/25 06:37:39  csoutheren
00032  * Fixed compilation on 64bit Linux
00033  *
00034  * Revision 1.3  2007/04/30 00:07:26  csoutheren
00035  * Fix problems with PODBC on Windows
00036  *
00037  * Revision 1.2  2007/04/25 09:21:20  csoutheren
00038  * Move unixODBC includes to a seperate namespace to avoid namespace conflicts
00039  *
00040  * Revision 1.1  2007/04/13 18:17:57  shorne
00041  * added ODBC support for linux thx Michal Z
00042  *
00043  * Revision 1.3  2006/06/27 04:28:16  shorne
00044  * Removed debuging code
00045  *
00046  * Revision 1.2  2006/06/25 11:20:24  csoutheren
00047  * Add detection of ODBC on Windows
00048  *
00049  * Revision 1.1  2006/01/27 06:35:30  shorne
00050  * added ODBC support
00051  *
00052  *
00053 */
00054 
00108 //--
00109 
00110 #if !defined(PODBC_H)
00111 #define PODBC_H
00112 
00113 #if _MSC_VER > 1000
00114 #pragma once
00115 #endif // _MSC_VER > 1000
00116 
00117 #ifndef _PTLIB_H
00118 #include <ptlib.h>
00119 #endif
00120 
00121 #ifdef P_ODBC
00122 
00123 namespace PTODBC {
00124 
00125 #include <sql.h> 
00126 #include <sqlext.h>
00127 #include <odbcinst.h>
00128 
00129 #ifdef _MSC_VER
00130  #include <tchar.h>
00131  #pragma comment(lib,"odbc32.lib")
00132  #pragma comment(lib,"odbcCP32.lib")
00133  typedef ::LPCTSTR LPCTSTR;
00134  typedef ::LPVOID LPVOID;
00135  typedef ::DWORD DWORD;
00136  typedef ::LONG LONG;
00137  typedef ::ULONG ULONG;
00138  typedef ::USHORT USHORT;
00139 #else
00140  typedef const char *LPCTSTR;
00141  typedef void *LPVOID;
00142  typedef long long _int64;
00143  typedef long LONG;
00144  #define _T(x) x
00145 #endif // _MSC_VER
00146 
00147 
00148 // Max SQL String Data Length
00149 #define MAX_DATA_LEN 1024
00150 
00157 }; // PTODBC
00158 
00159 class PODBC;
00160 class PODBCRecord;
00161 class PODBCStmt : public PObject
00162 {
00163   PCLASSINFO(PODBCStmt, PObject);
00164 
00165 PTODBC::HSTMT m_hStmt;
00166 
00167 public:
00168 
00174           PODBCStmt(PODBC * odbc);
00175 
00180           ~PODBCStmt();
00182 
00187           operator PTODBC::HSTMT() { return m_hStmt; };
00189 
00190 
00196           BOOL IsValid();
00197 
00201           PTODBC::DWORD GetChangedRowCount(void);
00202 
00207           BOOL Query(PString strSQL);
00209 
00214           BOOL Fetch();
00215 
00219           BOOL FetchRow(PINDEX nRow,BOOL Absolute=1);
00220 
00223           BOOL FetchPrevious();
00224 
00227           BOOL FetchNext();
00228 
00231           BOOL FetchFirst();
00232 
00235           BOOL FetchLast();
00236 
00239           BOOL Cancel();
00241 
00249           PStringArray TableList(PString option = "");
00250 
00251 
00256           BOOL SQL_OK(PTODBC::SQLRETURN res);
00257 
00261           void GetLastError();
00263 
00264           PODBC * odbclink;             
00265 
00266           int dbase; 
00267 
00268 };
00269 
00270 
00271 
00282 class PODBCRecord;
00283 class PODBC  : public PObject
00284 {
00285   PCLASSINFO(PODBC, PObject);
00286 
00287 public:
00288 
00293         PODBC();
00294 
00297         ~PODBC();
00299 
00308           enum FieldTypes
00309           {
00310                 LongVarChar     =-1,     
00311                 Binary                  =-2,
00312                 VarBinary       =-3,
00313                 LongVarBinary   =-4,
00314                 BigInt                  =-5,
00315                 TinyInt             =-6,
00316                 Bit             =-7,     
00317                 Guid                    =-11,
00318                 Unknown                 = 0,
00319                 Char                    = 1,
00320                 Numeric                 = 2,
00321                 Decimal                 = 3,
00322                 Integer                 = 4,
00323                 SmallInt                = 5,
00324                 Float                   = 6,
00325                 Real                    = 7,
00326                 Double                  = 8,
00327                 DateTime                = 9,
00328                 VarChar                 =12,            
00329                 Date                    =91,            
00330                 Time                    =92,            
00331                 TimeStamp               =93                     
00332           };
00333 
00339         enum PwType
00340           { 
00341                 oPString,       // String Value
00342                 oBOOL,          // Boolean 
00343                 ochar,          // Character
00344                 oshort,         // Short        
00345                 oint,           // Integer  use .AsInteger()
00346                 olong,          // long         
00347                 odouble,        // Double   use .AsReal()
00348                 oPBYTEArray,// Binary Data
00349                 oPInt64,        // BigInt       use .AsInt64()
00350                 oPTime,         // Time         use  PTime( "Value" ) 
00351                 oPGUID          // GUID         use  PGUID( "Value" ) To Be Implemented...?
00352         };
00353 
00358         enum DataSources
00359         {
00360                 mySQL,                  
00361                 MSSQL,                  
00362                 Oracle,                 
00363                 IBM_DB2,
00364                 DBASE,
00365                 Paradox,
00366                 Excel,
00367                 Ascii,
00368                 Foxpro,
00369                 MSAccess,
00370                 postgreSQL
00371         };
00372 
00375         enum MSSQLProtocols
00376         {
00377            MSSQLNamedPipes,
00378            MSSQLWinSock,
00379            MSSQLIPX,
00380            MSSQLBanyan,
00381            MSSQLRPC
00382          };
00383 
00385 
00395          class ConnectData
00396           {
00397                 public:
00398                   PFilePath DBPath;    
00399                   PString DefDir;          
00400                   PString User;            
00401                   PString Pass;            
00402                   BOOL Excl_Trust;         
00403                   PString Host;            
00404                   int Port;                        
00405                   int opt;                         
00406           };
00408 
00409 
00414           class Row;
00415           class Field : public PObject
00416           {
00417                   PCLASSINFO(Field, PObject);
00418           public:
00419 
00425                   class Bind
00426                   {
00427                   public:
00428                            PString                              sbin;           
00429                            PString                              sbinlong;   
00430                            short int                    ssint;          
00431                            long int                             slint;          
00432                            double                               sdoub;          
00433                            unsigned char                sbit;           
00434                            unsigned char *              suchar;         
00435 #if _WIN32
00436                            _int64                               sbint;          
00437 #else
00438          PTODBC::_int64                         sbint;          
00439 #endif
00440                            PTODBC::DATE_STRUCT                  date;       
00441                            PTODBC::TIME_STRUCT                  time;           
00442                            PTODBC::TIMESTAMP_STRUCT             timestamp;      
00443                            PTODBC::SQLGUID                              guid;           
00444                            PTODBC::SQLINTEGER                   dataLen;        
00445                   };
00446 
00449                   BOOL Post();
00450 
00453                   PString operator=(const PString & str);
00454 
00457                   PString AsString();   
00458 
00462                   void SetValue(PString value);  
00463 
00466                   void SetDefaultValues();
00467 
00471                   BOOL DataFragment(PString & Buffer ,PINDEX & fragment, PTODBC::SQLINTEGER & size);
00472 
00475 
00476                           Bind  Data;                    
00477                           PwType  Type;                  
00478                           FieldTypes ODBCType;   
00479 
00481                           PString Name;                  
00482                           PINDEX col;                    
00483 
00485                           BOOL isReadOnly;               
00486                           BOOL isNullable;               
00487                           BOOL isAutoInc;                
00488                           int Decimals;                  
00489                           BOOL LongData;                 
00490 
00492                           Row * row;                     
00493           };
00495 
00496 
00505           class Row : public PObject
00506           {
00507           public:
00508 
00514                   Row(PODBCStmt * stmt);
00515 
00519                   Field & Column(PINDEX col);
00520 
00523                   Field & Column(PString name);
00524 
00527                   PStringArray ColumnNames();
00528 
00531                   PINDEX Columns();
00532 
00535                   PINDEX Rows();
00536 
00539                   Field & operator[] (PINDEX col);
00540 
00543                   Field & operator[] (PString col);
00544 
00547                   BOOL Navigate(PINDEX row);
00548 
00551                   void SetNewRow();
00552 
00559                   BOOL Post();
00560 
00564                   BOOL Delete(PINDEX row =0);
00565 
00566                   PODBCRecord * rec;      
00567 
00568                   PINDEX CurRow;          
00569                   BOOL NewRow;                    
00570                   PINDEX RowCount;                
00571 
00572           protected:
00573                   PArray<Field> Fields;   
00574           };
00576 
00582   class Table : public PObject
00583   {
00584   public:
00585 
00592           Table(PODBC * odbc, PString Query);
00593 
00596           ~Table();
00598 
00603           Row NewRow();
00604 
00607           BOOL DeleteRow(PINDEX row = 0);
00608 
00611           BOOL Post();
00613 
00618           PINDEX Rows();
00619 
00622           PINDEX Columns();
00623 
00626           PStringArray ColumnNames();
00627 
00632           Row & RecordHandler();
00633 
00636           Row & operator[] (PINDEX row);
00637 
00642           Field & operator() (PINDEX row, PINDEX col);
00643 
00647           Field & Column(PINDEX col);
00648 
00651           Field & Column(PString Name);
00653 
00654   protected:
00655           PODBCStmt stmt;                       
00656           PString tableName;            
00657           Row * RowHandler;                     
00658   };
00659 
00668           Table LoadTable(PString table);
00669 
00675           BOOL Query(PString Query);
00677 
00678 
00688           BOOL DataSource(DataSources Source, ConnectData Data);
00689   
00694           virtual BOOL Connect(PTODBC::LPCTSTR svSource);
00695 
00698           BOOL Connect_DB2(PFilePath DBPath);
00699 
00702           BOOL Connect_XLS(PFilePath XLSPath,PString DefDir = "");
00703 
00706           BOOL Connect_TXT(PFilePath TXTPath);
00707 
00710           BOOL Connect_FOX(PFilePath DBPath,PString User = "",
00711                           PString Pass = "",PString Type= "DBF",
00712                           BOOL Exclusive=FALSE);
00713 
00716           BOOL Connect_MDB(PFilePath MDBPath,PString User ="",
00717                                 PString Pass = "",BOOL Exclusive=FALSE);
00718 
00721           BOOL Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
00722                                 int version =5);
00723 
00726           BOOL Connect_Oracle(PString Server,PString User="", PString Pass="");
00727 
00730           BOOL Connect_DBASE(PDirectory DBPath);
00731 
00734           BOOL Connect_MSSQL(PString User="",PString Pass="", 
00735                          PString Host ="(local)",BOOL Trusted = TRUE, 
00736                          MSSQLProtocols Proto=MSSQLNamedPipes);
00737 
00740           BOOL Connect_mySQL(PString User="",PString Pass="",
00741                          PString Host= "localhost",
00742                          int Port=3306,int Option=0);
00743 
00746           BOOL ConnectDB_mySQL(PString DB,PString User="",
00747                         PString Pass="",PString Host= "localhost",
00748                         int Port=3306,int Option=0);
00749 
00752           BOOL Connect_postgreSQL(PString DB,PString User,
00753         PString Pass,PString Host, int Port=5432,int Option=0);
00754 
00757           void Disconnect();
00759 
00766           PStringArray TableList(PString option = "");
00767 
00771           BOOL NeedLongDataLen();
00772 
00775           virtual void OnSQLError(PString RetCode, PString RetString) {};
00776 
00777 
00784           void SetPrecision(int Digit);
00785 
00788           void SetTimeFormat(PTime::TimeFormat tformat);
00789 
00792           operator PTODBC::HDBC() { return m_hDBC; };
00794                 
00795          PODBC::DataSources  dbase; 
00796 
00797 protected:
00798 
00799   PTODBC::SQLRETURN       m_nReturn;      // Internal SQL Error code
00800   PTODBC::HENV            m_hEnv;         // Handle to environment
00801   PTODBC::HDBC            m_hDBC;         // Handle to database connection
00802 };
00803 
00812 class PDSNConnection : public PODBC
00813 {
00814   PCLASSINFO(PDSNConnection, PODBC);
00815 
00816 public:
00817 
00820           PDSNConnection();
00821           ~PDSNConnection();
00823 
00830           BOOL Connect( PString Source ,PString Username, PString Password);
00831 
00832 };
00833 
00834  //--
00841 class PODBCRecord : public PObject
00842 {
00843   
00844   PCLASSINFO(PODBCRecord, PObject);
00845 
00846 PTODBC::HSTMT m_hStmt;
00847 
00848 public:
00849 
00854            PODBCRecord(PODBCStmt * hStmt);
00855 
00858           ~PODBCRecord(){};
00860 
00866           void Data(PINDEX Column, PODBC::Field & field);
00867 
00872           BOOL InternalGetData(
00873                 PTODBC::USHORT Column,
00874                 PTODBC::LPVOID pBuffer, 
00875                 PTODBC::ULONG pBufLen,
00876                 PTODBC::SQLINTEGER * dataLen=NULL,
00877                 int Type=SQL_C_DEFAULT
00878           );
00879 
00880           /* Get Long Character Data. Long Data fields cannot be bound
00881                         and Data must be Got from the RecordSet.
00882           */
00883           PString GetLongData(PINDEX Column);
00884   
00887           BOOL PostNew(PODBC::Row & rec);
00888 
00891           BOOL PostUpdate(PODBC::Row & rec);
00892 
00896           BOOL PostDelete(PINDEX row= 1);
00897 
00900           BOOL InternalSaveLongData(PTODBC::SQLRETURN nRet,PODBC::Row & rec);
00901 
00904           BOOL InternalBindColumn(
00905             PTODBC::USHORT Column,PTODBC::LPVOID pBuffer,
00906             PTODBC::ULONG pBufferSize,
00907             PTODBC::LONG * pReturnedBufferSize=NULL,
00908             PTODBC::USHORT nType=SQL_C_TCHAR
00909           );
00911 
00917           PINDEX ColumnByName(PString Column);
00918 
00921           PINDEX ColumnCount();
00922 
00925           PODBC::FieldTypes ColumnType(PINDEX Column );
00926 
00929           PTODBC::DWORD ColumnSize( PINDEX Column );
00930 
00933           PTODBC::DWORD ColumnScale( PINDEX Column );
00934 
00937           PString ColumnName( PINDEX Column);
00938 
00943           unsigned int ColumnPrecision( PINDEX Column );
00944 
00947           BOOL IsColumnNullable( PINDEX Column );
00948 
00951           BOOL IsColumnUpdatable( PINDEX Column );
00952 
00955           BOOL IsColumnAutoIndex( PINDEX Column );
00956 
00958 
00963           static unsigned int Precision;      
00964           static int MaxCharSize;                     
00965           static PTime::TimeFormat TimeFormat;
00966 
00967 
00968           PODBCStmt * Stmt;                                     
00969           PODBC::DataSources dbase;                     
00970 
00971 
00972 };
00973 
00974 #endif // PTODBC
00975 
00976 #endif // !defined(PODBC_H)
00977 

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1