podbc.h
Go to the documentation of this file.
/*
* podbc.h
*
* Virteos ODBC Implementation for PWLib Library.
*
* Virteos is a Trade Mark of ISVO (Asia) Pte Ltd.
*
* Copyright (c) 2005 ISVO (Asia) Pte Ltd. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
*
* The Original Code is derived from and used in conjunction with the
* pwlib Libaray of the OpenH323 Project (www.openh323.org/)
*
* The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
*
* Portions: Simple ODBC Wrapper Article www.codeproject.com
*
* Contributor(s): ______________________________________.
*
* $Revision: 24177 $
* $Author: rjongbloed $
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
*/
//--
#ifndef PTLIB_PODBC_H
#define PTLIB_PODBC_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#if defined(P_ODBC) && !defined(_WIN32_WCE)
#include <odbcinst.h>
#include <sql.h>
#include <sqlext.h>
#ifdef _MSC_VER
#include <tchar.h>
#pragma comment(lib,"odbc32.lib")
#pragma comment(lib,"odbcCP32.lib")
#else
#ifdef UNICODE
typedef WCHAR TCHAR;
typedef LPWSTR LPTSTR;
typedef LPCWSTR LPCTSTR;
// Needs a definition one day ... #define _T(x)
#else
typedef CHAR TCHAR;
typedef LPSTR LPTSTR;
typedef LPCSTR LPCTSTR;
#define _T(x) x
#endif
#endif // _MSC_VER
// Max SQL String Data Length
#define MAX_DATA_LEN 1024
class PODBC;
class PODBCRecord;
class PODBCStmt : public PObject
{
PCLASSINFO(PODBCStmt, PObject);
public:
PODBCStmt(PODBC * odbc);
~PODBCStmt();
operator HSTMT() { return m_hStmt; };
PBoolean IsValid();
DWORD GetChangedRowCount(void);
PBoolean Query(PString strSQL);
PBoolean Fetch();
PBoolean FetchRow(PINDEX nRow,PBoolean Absolute=1);
PBoolean FetchPrevious();
PBoolean FetchNext();
PBoolean FetchFirst();
PBoolean FetchLast();
PBoolean Cancel();
PStringArray TableList(PString option = "");
PBoolean SQL_OK(SQLRETURN res);
void GetLastError();
PODBC * GetLink() const { return odbclink; }
int GetDBase() const { return dbase; }
protected:
HSTMT m_hStmt;
PODBC * odbclink;
int dbase;
};
class PODBC : public PObject
{
PCLASSINFO(PODBC, PObject);
public:
PODBC();
~PODBC();
enum FieldTypes
{
LongVarChar =-1,
Binary =-2,
VarBinary =-3,
LongVarBinary =-4,
BigInt =-5,
TinyInt =-6,
Bit =-7,
Guid =-11,
Unknown = 0,
Char = 1,
Numeric = 2,
Decimal = 3,
Integer = 4,
SmallInt = 5,
Float = 6,
Real = 7,
Double = 8,
DateTime = 9,
VarChar =12,
Date =91,
Time =92,
TimeStamp =93
};
enum PwType
{
oPString, // String Value
oBOOL, // Boolean
ochar, // Character
oshort, // Short
oint, // Integer use .AsInteger()
olong, // long
odouble, // Double use .AsReal()
oPBYTEArray,// Binary Data
oPInt64, // BigInt use .AsInt64()
oPTime, // Time use PTime( "Value" )
oPGUID // GUID use PGUID( "Value" ) To Be Implemented...?
};
enum DataSources
{
mySQL,
MSSQL,
Oracle,
IBM_DB2,
DBASE,
Paradox,
Excel,
Ascii,
Foxpro,
MSAccess,
postgreSQL
};
enum MSSQLProtocols
{
MSSQLNamedPipes,
MSSQLWinSock,
MSSQLIPX,
MSSQLBanyan,
MSSQLRPC
};
class ConnectData
{
public:
PFilePath DBPath;
PString DefDir;
PString User;
PString Pass;
PBoolean Excl_Trust;
PString Host;
int Port;
int opt;
};
class Row;
class Field : public PObject
{
PCLASSINFO(Field, PObject);
public:
class Bind
{
public:
PString sbin;
PString sbinlong;
short int ssint;
long int slint;
double sdoub;
unsigned char sbit;
unsigned char * suchar;
PInt64 sbint;
DATE_STRUCT date;
TIME_STRUCT time;
TIMESTAMP_STRUCT timestamp;
SQLGUID guid;
SQLLEN dataLen;
};
PBoolean Post();
PString operator=(const PString & str);
PString AsString();
void SetValue(PString value);
void SetDefaultValues();
PBoolean DataFragment(PString & Buffer ,PINDEX & fragment, SQLINTEGER & size);
Bind Data;
PwType Type;
FieldTypes ODBCType;
PString Name;
PINDEX col;
PBoolean isReadOnly;
PBoolean isNullable;
PBoolean isAutoInc;
int Decimals;
PBoolean LongData;
Row * row;
};
class Row : public PObject
{
public:
Row(PODBCStmt * stmt);
Field & Column(PINDEX col);
Field & Column(PString name);
PStringArray ColumnNames();
PINDEX Columns();
PINDEX Rows();
Field & operator[] (PINDEX col);
Field & operator[] (PString col);
PBoolean Navigate(PINDEX row);
void SetNewRow();
PBoolean Post();
PBoolean Delete(PINDEX row =0);
PODBCRecord * rec;
PINDEX CurRow;
PBoolean NewRow;
PINDEX RowCount;
protected:
PArray<Field> Fields;
};
class Table : public PObject
{
public:
Table(PODBC * odbc, PString Query);
~Table();
Row NewRow();
PBoolean DeleteRow(PINDEX row = 0);
PBoolean Post();
PINDEX Rows();
PINDEX Columns();
PStringArray ColumnNames();
Row & RecordHandler();
Row & operator[] (PINDEX row);
Field & operator() (PINDEX row, PINDEX col);
Field & Column(PINDEX col);
Field & Column(PString Name);
protected:
PODBCStmt stmt;
PString tableName;
Row * RowHandler;
};
Table LoadTable(PString table);
PBoolean Query(PString Query);
PBoolean DataSource(DataSources Source, ConnectData Data);
virtual PBoolean Connect(LPCTSTR svSource);
PBoolean Connect_DB2(PFilePath DBPath);
PBoolean Connect_XLS(PFilePath XLSPath,PString DefDir = "");
PBoolean Connect_TXT(PFilePath TXTPath);
PBoolean Connect_FOX(PFilePath DBPath,PString User = "",
PString Pass = "",PString Type= "DBF",
PBoolean Exclusive=false);
PBoolean Connect_MDB(PFilePath MDBPath,PString User ="",
PString Pass = "",PBoolean Exclusive=false);
PBoolean Connect_PDOX(PDirectory DBPath,PDirectory DefaultDir,
int version =5);
PBoolean Connect_Oracle(PString Server,PString User="", PString Pass="");
PBoolean Connect_DBASE(PDirectory DBPath);
PBoolean Connect_MSSQL(PString User="",PString Pass="",
PString Host ="(local)",PBoolean Trusted = true,
MSSQLProtocols Proto=MSSQLNamedPipes);
PBoolean Connect_mySQL(PString User="",PString Pass="",
PString Host= "localhost",
int Port=3306,int Option=0);
PBoolean ConnectDB_mySQL(PString DB,PString User="",
PString Pass="",PString Host= "localhost",
int Port=3306,int Option=0);
PBoolean Connect_postgreSQL(PString DB,PString User,
PString Pass,PString Host, int Port=5432,int Option=0);
void Disconnect();
PStringArray TableList(PString option = "");
PBoolean NeedLongDataLen();
virtual void OnSQLError(PString RetCode, PString RetString) {};
void SetPrecision(int Digit);
void SetTimeFormat(PTime::TimeFormat tformat);
operator HDBC() { return m_hDBC; };
PODBC::DataSources dbase;
protected:
SQLRETURN m_nReturn; // Internal SQL Error code
HENV m_hEnv; // Handle to environment
HDBC m_hDBC; // Handle to database connection
};
class PDSNConnection : public PODBC
{
PCLASSINFO(PDSNConnection, PODBC);
public:
PDSNConnection();
~PDSNConnection();
PBoolean Connect( PString Source ,PString Username, PString Password);
};
//--
class PODBCRecord : public PObject
{
PCLASSINFO(PODBCRecord, PObject);
public:
PODBCRecord(PODBCStmt * hStmt);
~PODBCRecord(){};
void Data(PINDEX Column, PODBC::Field & field);
PBoolean InternalGetData(
USHORT Column,
LPVOID pBuffer,
ULONG pBufLen,
SQLINTEGER * dataLen=NULL,
int Type=SQL_C_DEFAULT
);
/* Get Long Character Data. Long Data fields cannot be bound
and Data must be Got from the RecordSet.
*/
PString GetLongData(PINDEX Column);
PBoolean PostNew(PODBC::Row & rec);
PBoolean PostUpdate(PODBC::Row & rec);
PBoolean PostDelete(PINDEX row= 1);
PBoolean InternalSaveLongData(SQLRETURN nRet,PODBC::Row & rec);
PBoolean InternalBindColumn(
USHORT Column,LPVOID pBuffer,
ULONG pBufferSize,
LONG * pReturnedBufferSize=NULL,
USHORT nType=SQL_C_TCHAR
);
PINDEX ColumnByName(PString Column);
PINDEX ColumnCount();
PODBC::FieldTypes ColumnType(PINDEX Column );
DWORD ColumnSize( PINDEX Column );
DWORD ColumnScale( PINDEX Column );
PString ColumnName( PINDEX Column);
unsigned int ColumnPrecision( PINDEX Column );
PBoolean IsColumnNullable( PINDEX Column );
PBoolean IsColumnUpdatable( PINDEX Column );
PBoolean IsColumnAutoIndex( PINDEX Column );
static unsigned int Precision;
static int MaxCharSize;
static PTime::TimeFormat TimeFormat;
protected:
HSTMT m_hStmt;
PODBCStmt * Stmt;
PODBC::DataSources dbase;
friend class PODBC::Field;
friend class PODBC::Row;
};
#endif // P_ODBC
#endif // PTLIB_PODBC_H
// End Of File ///////////////////////////////////////////////////////////////
PODBC::oshort
Definition: podbc.h:319
TCHAR
CHAR TCHAR
ODBC Support for PWLIB.
Definition: podbc.h:116
PODBCStmt::m_hStmt
HSTMT m_hStmt
Definition: podbc.h:242
PODBC::Field::Type
PwType Type
Data Field to ODBC Bind to.
Definition: podbc.h:448
PODBC::Ascii
Definition: podbc.h:342
PODBC::OnSQLError
virtual void OnSQLError(PString RetCode, PString RetString)
OnSQL Error.
Definition: podbc.h:746
PODBC::Field::Bind::sbint
PInt64 sbint
Unsigned char SQLCHAR *.
Definition: podbc.h:410
PODBC::Field::Bind::dataLen
SQLLEN dataLen
GUID Structure (not Fully Supported)
Definition: podbc.h:415
PODBCStmt::GetDBase
int GetDBase() const
Definition: podbc.h:238
PODBC::Table::stmt
PODBCStmt stmt
Definition: podbc.h:626
PODBC::MSSQL
Definition: podbc.h:336
PODBC::Foxpro
Definition: podbc.h:343
PODBCRecord::dbase
PODBC::DataSources dbase
Statement Class.
Definition: podbc.h:935
PODBC::oint
Definition: podbc.h:320
PODBC::Field::isNullable
PBoolean isNullable
Is Field Readonly.
Definition: podbc.h:457
PODBC::Table::RowHandler
Row * RowHandler
Name of the Fetched Table (if used in Constructor)
Definition: podbc.h:628
PODBC::Table::tableName
PString tableName
ODBC Fetched Statement Info.
Definition: podbc.h:627
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PODBCStmt::FetchFirst
PBoolean FetchFirst()
FetchFirst Fetch the First row in the RecordSet.
PODBC::Row::Fields
PArray< Field > Fields
Number of Rows.
Definition: podbc.h:544
PODBCStmt::GetChangedRowCount
DWORD GetChangedRowCount(void)
GetChangedRowCount retreives the number of rows updated/altered by UPDATE/INSERT statements.
PODBCStmt::GetLink
PODBC * GetLink() const
Definition: podbc.h:237
PODBC::PwType
PwType
Converted Pwlib Field Types.
Definition: podbc.h:314
PODBCRecord::m_hStmt
HSTMT m_hStmt
Time Format.
Definition: podbc.h:933
PODBC::Field::Bind::sbinlong
PString sbinlong
Strings & Binary Data.
Definition: podbc.h:404
PODBC::mySQL
Definition: podbc.h:335
PODBC
PODBC Class The Main ODBC class.
Definition: podbc.h:259
PODBCStmt::IsValid
PBoolean IsValid()
IsValid Checks to ensure a Handle has been allocated and is effective.
PODBC::m_hDBC
HDBC m_hDBC
Definition: podbc.h:771
PODBC::ConnectData::Excl_Trust
PBoolean Excl_Trust
Password.
Definition: podbc.h:377
PODBC::oPTime
Definition: podbc.h:325
PODBCStmt::dbase
int dbase
Reference to the PODBC Class.
Definition: podbc.h:244
PODBC::ConnectData
This class is a multipurpose use class for storing parameters when initiating connection to DataSourc...
Definition: podbc.h:370
PDSNConnection
DSN (Data Source Name) Connection.
Definition: podbc.h:781
PODBC::Field::isAutoInc
PBoolean isAutoInc
Allows Nulls.
Definition: podbc.h:458
PFilePath
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PODBC::Field::Bind::timestamp
TIMESTAMP_STRUCT timestamp
Time Structure.
Definition: podbc.h:413
PODBC::m_hEnv
HENV m_hEnv
Definition: podbc.h:770
PODBC::ConnectData::opt
int opt
Port to connect to mySQL.
Definition: podbc.h:380
PODBC::Oracle
Definition: podbc.h:337
PODBC::Field::Bind::guid
SQLGUID guid
TimeStamp Structure.
Definition: podbc.h:414
PODBC::MSSQLIPX
Definition: podbc.h:354
PODBC::oBOOL
Definition: podbc.h:317
PODBCStmt::GetLastError
void GetLastError()
Get the Last Error This returns the Error ID & String to PODBC::OnSQLError.
PODBC::MSSQLNamedPipes
Definition: podbc.h:352
PStringArray
This is an array collection class of PString objects.
Definition: pstring.h:2024
PODBC::ConnectData::DefDir
PString DefDir
Database file Path (not Oracle,xxSQL)
Definition: podbc.h:374
PODBCStmt::FetchPrevious
PBoolean FetchPrevious()
FetchPrevious Fetch the previous Row from current row.
PODBC::ConnectData::Pass
PString Pass
UserName.
Definition: podbc.h:376
PODBCStmt::~PODBCStmt
~PODBCStmt()
Deconstructor.
PODBC::oPString
Definition: podbc.h:316
PODBCStmt::odbclink
PODBC * odbclink
Definition: podbc.h:243
PODBC::Field::Decimals
int Decimals
Field AutoIncrements.
Definition: podbc.h:459
PTime::TimeFormat
TimeFormat
Standard time formats for string representations of a time and date.
Definition: ptime.h:399
PODBC::DataSources
DataSources
Datasources that are supported by this implementation used in the PODBC::DataSource Function...
Definition: podbc.h:333
PBoolean
BOOL PBoolean
Definition: object.h:102
PDirectory
Class to represent a directory in the operating system file system.
Definition: pdirect.h:182
PODBC::Row::CurRow
PINDEX CurRow
Record Structure.
Definition: podbc.h:539
PODBC::Field::Bind::sbit
unsigned char sbit
Double SQLDOUBLE.
Definition: podbc.h:408
PODBC::Field::Bind::suchar
unsigned char * suchar
Bit SQLCHAR.
Definition: podbc.h:409
PODBC::FieldTypes
FieldTypes
Raw SQL data type codes Refer <sql.h> SQL_* This list is not inclusive.
Definition: podbc.h:283
PODBC::ConnectData::DBPath
PFilePath DBPath
Definition: podbc.h:373
PODBC::Field::Bind::slint
long int slint
Short Integer SQLSMALLINT.
Definition: podbc.h:406
PODBCStmt::Fetch
PBoolean Fetch()
Fetch General call to retreive the next row of data.
PODBC::oPInt64
Definition: podbc.h:324
PODBCStmt::Query
PBoolean Query(PString strSQL)
Query function is the Main function to pass SQL statements to retreive/ add/Modify database data...
PODBC::Field
Definition: podbc.h:390
PODBCStmt::Cancel
PBoolean Cancel()
Cancel the Current Statement.
LPCTSTR
LPCSTR LPCTSTR
Definition: podbc.h:118
PODBC::Row::NewRow
PBoolean NewRow
Current Row.
Definition: podbc.h:540
PODBC::Field::LongData
PBoolean LongData
Number of decimal places to Round.
Definition: podbc.h:460
PODBC::IBM_DB2
Definition: podbc.h:338
PODBC::Table
PODBC::Table This is the main Class to access Data returned by a Select Query.
Definition: podbc.h:553
PODBC::m_nReturn
SQLRETURN m_nReturn
Database Type connected to.
Definition: podbc.h:769
PODBC::MSSQLBanyan
Definition: podbc.h:355
PString
The character string class.
Definition: pstring.h:108
LPTSTR
LPSTR LPTSTR
Definition: podbc.h:117
PODBCRecord
PODBCRecord This Class is used to analyse the fetched data and handles Data Conversion/Read Write ope...
Definition: podbc.h:809
PODBC::Field::Bind
SQL compliant Bound DataTypes.
Definition: podbc.h:400
PODBC::ConnectData::User
PString User
Used with Paradox/DBase/Excel (& mySQL db)
Definition: podbc.h:375
PODBCRecord::TimeFormat
static PTime::TimeFormat TimeFormat
Long Data Limit KBytes def = 56; (56 Kbytes)
Definition: podbc.h:929
PODBCRecord::~PODBCRecord
~PODBCRecord()
Deconstructor.
Definition: podbc.h:822
PODBC::DBASE
Definition: podbc.h:339
PODBC::Field::row
Row * row
LongData Length is Required.
Definition: podbc.h:463
PODBC::MSSQLProtocols
MSSQLProtocols
MSSQL protocols.If your interested?
Definition: podbc.h:350
PODBC::Row
This class functions as a simple wrapper of the PODBCStmt class to fetch/Save data to the Datasource...
Definition: podbc.h:476
PODBCStmt::FetchLast
PBoolean FetchLast()
FetchLast Fetch the Last row in the RecordSet.
PODBC::Row::rec
PODBCRecord * rec
Definition: podbc.h:537
PODBC::Field::Name
PString Name
ODBC Type (For saving/Conversion)
Definition: podbc.h:452
PODBC::Row::RowCount
PINDEX RowCount
Flag to Indicate New Row (requires either Post or Delete)
Definition: podbc.h:541
PODBCRecord::MaxCharSize
static int MaxCharSize
Double Real Float Decimal digit rounding def= 4;.
Definition: podbc.h:928
PODBC::Paradox
Definition: podbc.h:340
PODBC::olong
Definition: podbc.h:321
PArray
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:1024
PODBC::ConnectData::Host
PString Host
Whether Datasource is locked or Trusted.
Definition: podbc.h:378
PODBCStmt::FetchNext
PBoolean FetchNext()
FetchNext: Fetch the Next row.
PODBC::Field::ODBCType
FieldTypes ODBCType
pwlib Type for conversion
Definition: podbc.h:449
PODBCStmt::FetchRow
PBoolean FetchRow(PINDEX nRow, PBoolean Absolute=1)
FetchRow More detailed fetching of Rows.
PODBC::Excel
Definition: podbc.h:341
PODBC::Field::Bind::ssint
short int ssint
Long Data.
Definition: podbc.h:405
PODBC::Field::Bind::time
TIME_STRUCT time
Date Structure.
Definition: podbc.h:412
PODBCStmt::SQL_OK
PBoolean SQL_OK(SQLRETURN res)
Is the SQL Instruction OK If an Error is detected then GetLastError is called to Retrieve the SQL Err...
PODBCStmt
Definition: podbc.h:138
PODBCStmt::PODBCStmt
PODBCStmt(PODBC *odbc)
Constructor PODBC (Datasources call) or thro&#39; DSNConnection (Connection call).
PODBC::ConnectData::Port
int Port
URL for Host Datasouce xxSQL.
Definition: podbc.h:379
PODBC::Field::Bind::sbin
PString sbin
Definition: podbc.h:403
PODBCRecord::Stmt
PODBCStmt * Stmt
Definition: podbc.h:934
PODBC::MSAccess
Definition: podbc.h:344
PODBC::Field::Bind::sdoub
double sdoub
Integer SQLINTEGER.
Definition: podbc.h:407
PODBC::Field::col
PINDEX col
Column Name.
Definition: podbc.h:453
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PODBCStmt::TableList
PStringArray TableList(PString option="")
Retreive the List of Tables from the current Datasource The option field can be used to specify the T...
PODBC::Field::isReadOnly
PBoolean isReadOnly
Column Number (For Saving/Conversion)
Definition: podbc.h:456
PODBC::odouble
Definition: podbc.h:322
PODBC::Field::Bind::date
DATE_STRUCT date
Bit Integer SQLBIGINT.
Definition: podbc.h:411
PODBC::MSSQLWinSock
Definition: podbc.h:353
PODBCRecord::Precision
static unsigned int Precision
Conversion Settings.
Definition: podbc.h:927
PODBC::ochar
Definition: podbc.h:318
PODBC::oPBYTEArray
Definition: podbc.h:323