PTLib  Version 2.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
podbc.h
Go to the documentation of this file.
1 /*
2  * podbc.h
3  *
4  * Virteos ODBC Implementation for PWLib Library.
5  *
6  * Virteos is a Trade Mark of ISVO (Asia) Pte Ltd.
7  *
8  * Copyright (c) 2005 ISVO (Asia) Pte Ltd. All Rights Reserved.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  *
21  * The Original Code is derived from and used in conjunction with the
22  * pwlib Libaray of the OpenH323 Project (www.openh323.org/)
23  *
24  * The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
25  *
26  * Portions: Simple ODBC Wrapper Article www.codeproject.com
27  *
28  * Contributor(s): ______________________________________.
29  *
30  * $Revision: 24177 $
31  * $Author: rjongbloed $
32  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
33  */
34 
88 //--
89 
90 #ifndef PTLIB_PODBC_H
91 #define PTLIB_PODBC_H
92 
93 #if _MSC_VER > 1000
94 #pragma once
95 #endif // _MSC_VER > 1000
96 
97 
98 #if defined(P_ODBC) && !defined(_WIN32_WCE)
99 
100 #include <odbcinst.h>
101 #include <sql.h>
102 #include <sqlext.h>
103 
104 #ifdef _MSC_VER
105  #include <tchar.h>
106  #pragma comment(lib,"odbc32.lib")
107  #pragma comment(lib,"odbcCP32.lib")
108 #else
109 
110  #ifdef UNICODE
111  typedef WCHAR TCHAR;
112  typedef LPWSTR LPTSTR;
113  typedef LPCWSTR LPCTSTR;
114  // Needs a definition one day ... #define _T(x)
115  #else
116  typedef CHAR TCHAR;
117  typedef LPSTR LPTSTR;
118  typedef LPCSTR LPCTSTR;
119  #define _T(x) x
120  #endif
121 
122 #endif // _MSC_VER
123 
124 // Max SQL String Data Length
125 #define MAX_DATA_LEN 1024
126 
134 class PODBC;
135 class PODBCRecord;
136 
137 
138 class PODBCStmt : public PObject
139 {
140  PCLASSINFO(PODBCStmt, PObject);
141 
142  public:
148  PODBCStmt(PODBC * odbc);
149 
154  ~PODBCStmt();
156 
161  operator HSTMT() { return m_hStmt; };
163 
164 
170  PBoolean IsValid();
171 
175  DWORD GetChangedRowCount(void);
176 
181  PBoolean Query(PString strSQL);
183 
188  PBoolean Fetch();
189 
193  PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1);
194 
198 
202 
206 
210 
213  PBoolean Cancel();
215 
223  PStringArray TableList(PString option = "");
224 
225 
230  PBoolean SQL_OK(SQLRETURN res);
231 
235  void GetLastError();
236 
237  PODBC * GetLink() const { return odbclink; }
238  int GetDBase() const { return dbase; }
240 
241  protected:
242  HSTMT m_hStmt;
244  int dbase;
245 };
246 
247 
248 
259 class PODBC : public PObject
260 {
261  PCLASSINFO(PODBC, PObject);
262 
263  public:
268  PODBC();
269 
272  ~PODBC();
274 
284  {
286  Binary =-2,
289  BigInt =-5,
290  TinyInt =-6,
291  Bit =-7,
292  Guid =-11,
293  Unknown = 0,
294  Char = 1,
295  Numeric = 2,
296  Decimal = 3,
297  Integer = 4,
298  SmallInt = 5,
299  Float = 6,
300  Real = 7,
301  Double = 8,
302  DateTime = 9,
303  VarChar =12,
304  Date =91,
305  Time =92,
307  };
308 
314  enum PwType
315  {
316  oPString, // String Value
317  oBOOL, // Boolean
318  ochar, // Character
319  oshort, // Short
320  oint, // Integer use .AsInteger()
321  olong, // long
322  odouble, // Double use .AsReal()
323  oPBYTEArray,// Binary Data
324  oPInt64, // BigInt use .AsInt64()
325  oPTime, // Time use PTime( "Value" )
326  oPGUID // GUID use PGUID( "Value" ) To Be Implemented...?
327  };
328 
334  {
346  };
347 
351  {
357  };
358 
360 
371  {
372  public:
379  int Port;
380  int opt;
381  };
383 
384 
389  class Row;
390  class Field : public PObject
391  {
392  PCLASSINFO(Field, PObject);
393  public:
394 
400  class Bind
401  {
402  public:
405  short int ssint;
406  long int slint;
407  double sdoub;
408  unsigned char sbit;
409  unsigned char * suchar;
410  PInt64 sbint;
411  DATE_STRUCT date;
412  TIME_STRUCT time;
413  TIMESTAMP_STRUCT timestamp;
414  SQLGUID guid;
415  SQLLEN dataLen;
416  };
417 
420  PBoolean Post();
421 
424  PString operator=(const PString & str);
425 
428  PString AsString();
429 
433  void SetValue(PString value);
434 
437  void SetDefaultValues();
438 
442  PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, SQLINTEGER & size);
443 
446 
450 
453  PINDEX col;
454 
459  int Decimals;
461 
463  Row * row;
464  };
466 
467 
476  class Row : public PObject
477  {
478  public:
479 
485  Row(PODBCStmt * stmt);
486 
490  Field & Column(PINDEX col);
491 
494  Field & Column(PString name);
495 
499 
502  PINDEX Columns();
503 
506  PINDEX Rows();
507 
510  Field & operator[] (PINDEX col);
511 
514  Field & operator[] (PString col);
515 
518  PBoolean Navigate(PINDEX row);
519 
522  void SetNewRow();
523 
530  PBoolean Post();
531 
535  PBoolean Delete(PINDEX row =0);
536 
538 
539  PINDEX CurRow;
541  PINDEX RowCount;
542 
543  protected:
545  };
547 
553  class Table : public PObject
554  {
555  public:
556 
563  Table(PODBC * odbc, PString Query);
564 
567  ~Table();
569 
574  Row NewRow();
575 
578  PBoolean DeleteRow(PINDEX row = 0);
579 
582  PBoolean Post();
584 
589  PINDEX Rows();
590 
593  PINDEX Columns();
594 
598 
603  Row & RecordHandler();
604 
607  Row & operator[] (PINDEX row);
608 
613  Field & operator() (PINDEX row, PINDEX col);
614 
618  Field & Column(PINDEX col);
619 
622  Field & Column(PString Name);
624 
625  protected:
629  };
630 
639  Table LoadTable(PString table);
640 
648 
649 
660 
665  virtual PBoolean Connect(LPCTSTR svSource);
666 
670 
673  PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = "");
674 
677  PBoolean Connect_TXT(PFilePath TXTPath);
678 
681  PBoolean Connect_FOX(PFilePath DBPath,PString User = "",
682  PString Pass = "",PString Type= "DBF",
683  PBoolean Exclusive=false);
684 
687  PBoolean Connect_MDB(PFilePath MDBPath,PString User ="",
688  PString Pass = "",PBoolean Exclusive=false);
689 
692  PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
693  int version =5);
694 
697  PBoolean Connect_Oracle(PString Server,PString User="", PString Pass="");
698 
702 
705  PBoolean Connect_MSSQL(PString User="",PString Pass="",
706  PString Host ="(local)",PBoolean Trusted = true,
708 
711  PBoolean Connect_mySQL(PString User="",PString Pass="",
712  PString Host= "localhost",
713  int Port=3306,int Option=0);
714 
718  PString Pass="",PString Host= "localhost",
719  int Port=3306,int Option=0);
720 
724  PString Pass,PString Host, int Port=5432,int Option=0);
725 
728  void Disconnect();
730 
737  PStringArray TableList(PString option = "");
738 
743 
746  virtual void OnSQLError(PString RetCode, PString RetString) {};
747 
748 
755  void SetPrecision(int Digit);
756 
759  void SetTimeFormat(PTime::TimeFormat tformat);
760 
763  operator HDBC() { return m_hDBC; };
765 
767 
768  protected:
769  SQLRETURN m_nReturn; // Internal SQL Error code
770  HENV m_hEnv; // Handle to environment
771  HDBC m_hDBC; // Handle to database connection
772 };
773 
774 
781 class PDSNConnection : public PODBC
782 {
783  PCLASSINFO(PDSNConnection, PODBC);
784 
785  public:
788  PDSNConnection();
789  ~PDSNConnection();
791 
798  PBoolean Connect( PString Source ,PString Username, PString Password);
799 };
800 
801 
802  //--
809 class PODBCRecord : public PObject
810 {
811  PCLASSINFO(PODBCRecord, PObject);
812 
813  public:
818  PODBCRecord(PODBCStmt * hStmt);
819 
824 
830  void Data(PINDEX Column, PODBC::Field & field);
831 
837  USHORT Column,
838  LPVOID pBuffer,
839  ULONG pBufLen,
840  SQLINTEGER * dataLen=NULL,
841  int Type=SQL_C_DEFAULT
842  );
843 
844  /* Get Long Character Data. Long Data fields cannot be bound
845  and Data must be Got from the RecordSet.
846  */
847  PString GetLongData(PINDEX Column);
848 
851  PBoolean PostNew(PODBC::Row & rec);
852 
856 
860  PBoolean PostDelete(PINDEX row= 1);
861 
864  PBoolean InternalSaveLongData(SQLRETURN nRet,PODBC::Row & rec);
865 
869  USHORT Column,LPVOID pBuffer,
870  ULONG pBufferSize,
871  LONG * pReturnedBufferSize=NULL,
872  USHORT nType=SQL_C_TCHAR
873  );
875 
881  PINDEX ColumnByName(PString Column);
882 
885  PINDEX ColumnCount();
886 
889  PODBC::FieldTypes ColumnType(PINDEX Column );
890 
893  DWORD ColumnSize( PINDEX Column );
894 
897  DWORD ColumnScale( PINDEX Column );
898 
901  PString ColumnName( PINDEX Column);
902 
907  unsigned int ColumnPrecision( PINDEX Column );
908 
911  PBoolean IsColumnNullable( PINDEX Column );
912 
915  PBoolean IsColumnUpdatable( PINDEX Column );
916 
919  PBoolean IsColumnAutoIndex( PINDEX Column );
920 
922 
927  static unsigned int Precision;
928  static int MaxCharSize;
930 
931 
932  protected:
933  HSTMT m_hStmt;
936 
937  friend class PODBC::Field;
938  friend class PODBC::Row;
939 };
940 
941 #endif // P_ODBC
942 
943 #endif // PTLIB_PODBC_H
944 
945 
946 // End Of File ///////////////////////////////////////////////////////////////