PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 28310 $
31 * $Author: rjongbloed $
32 * $Date: 2012-09-06 20:19:38 +1000 (Thu, 06 Sep 2012) $
33 */
34
93 //--
94
95 #ifndef PTLIB_PODBC_H
96 #define PTLIB_PODBC_H
97
98 #if _MSC_VER > 1000
99 #pragma once
100 #endif // _MSC_VER > 1000
101
102
103 #if P_ODBC
104
105 #include <ptclib/vartype.h>
106
107
118 class PODBC : public PObject
119 {
120 PCLASSINFO(PODBC, PObject);
121 public:
126 PODBC();
127
130 ~PODBC();
132
134 typedef unsigned RowIndex;
135 enum { UndefinedRowIndex = 0 };
136
137 class Field;
138 class Row;
139 class RecordSet;
140 class Statement; // Internal use
141 struct FieldExtra; // Internal use
142
143
146 class Field : public PVarType
147 {
148 PCLASSINFO(Field, PObject);
149 public:
150 ~Field();
151
152 Field & operator=(const Field & other) { PVarType::operator=(other); return *this; }
153 Field & operator=(const PVarType & other) { PVarType::operator=(other); return *this; }
154
155 // Overrides from PVarType, make sure can't change type
156 virtual bool SetType(BasicType type, PINDEX options = 0);
157
160 void SetDefaultValues();
161
164 void SetNULL();
165
167 bool IsNULL() const;
168
170 PINDEX GetColumn() const { return m_column; }
171
173 const PString GetName() const { return m_name; }
174
175 int GetDataType() const { return m_odbcType; }
176 unsigned GetScale() const { return m_scale; }
177 bool IsNullable() const { return m_isNullable; }
178 bool IsReadOnly() const { return m_isReadOnly; }
179 bool IsUpdatable() const { return !IsReadOnly(); }
180 bool IsAutoIncrement() const { return m_isAutoIncrement; }
181 bool IsAutoIndex() { return IsAutoIncrement(); }
182 unsigned GetPrecision() const { return m_decimals; }
183
184 // For backward compatibility
185 bool Post();
186
187 protected:
188 Field(Row & row, PINDEX column);
189
190 // Call backs from PVarType
191 virtual void OnGetValue();
192 virtual void OnValueChanged();
193 virtual void InternalCopy(const PVarType & other);
194
195 Row & m_row;
196 PINDEX m_column;
197
198 PString m_name;
199 int m_odbcType;
200 unsigned m_scale;
201 bool m_isNullable;
202 bool m_isReadOnly;
203 bool m_isAutoIncrement;
204 unsigned m_decimals;
205
206 FieldExtra * m_extra; // Some types are not compatible with PVarType
207
208 private:
209 Field(const Field & other) : PVarType(), m_row(other.m_row) { }
210
211 friend class Row;
212 friend class RecordSet;
213 };
214
215
223 class Row : public PObject
224 {
225 PCLASSINFO(Row, PObject);
226 public:
230 Row(RecordSet & recordSet);
231
234 PINDEX Columns() const { return m_fields.GetSize(); }
235
238 PStringArray ColumnNames() const;
239
243 PINDEX ColumnByName(const PCaselessString & columnName) const;
244
248 Field & Column(PINDEX column) const;
249
252 Field & Column(const PString & name) const;
253
256 Field & operator[](PINDEX column) const { return Column(column); }
257
260 Field & operator[](const PString & columnName) const { return Column(columnName); }
261
264 PString ColumnName(PINDEX column) const { return Column(column).GetName(); }
265
268 unsigned ColumnType(PINDEX column) const { return Column(column).GetDataType(); }
269
272 PINDEX ColumnSize(PINDEX column) const { return Column(column).GetSize(); }
273
276 unsigned ColumnScale(PINDEX column) const { return Column(column).GetScale(); }
277
282 unsigned ColumnPrecision(PINDEX column) const { return Column(column).GetPrecision(); }
283
286 bool IsColumnNullable(PINDEX column) const { return Column(column).IsNullable(); }
287
290 bool IsColumnUpdatable(PINDEX column) const { return Column(column).IsUpdatable(); }
291
294 bool IsColumnAutoIndex(PINDEX column) const { return Column(column).IsAutoIndex(); }
296
299 void SetNewRow();
300
303 bool MoveTo(RowIndex row);
304
307 bool Move(int offset);
308
311 bool First();
312
315 bool Next();
316
319 bool Previous();
320
323 bool Last();
324
328 bool Delete(RowIndex rowIndex = 0);
329
336 bool Commit();
337
338 RowIndex GetRowIndex() const { return m_rowIndex; }
339
340 // For backward compatibility
341 PINDEX Rows();
342 PINDEX ColumnCount() { return Columns(); }
343 bool PostNew() { return Commit(); }
344 bool PostUpdate() { return Commit(); }
345 bool Post() { return Commit(); }
346 bool Navigate(RowIndex row) { return MoveTo(row); }
347
348 protected:
349 RecordSet & m_recordSet;
350 RowIndex m_rowIndex;
351 PArray<Field> m_fields;
352
353 private:
354 Row(const Row & other) : PObject(other), m_recordSet(other.m_recordSet) { }
355 void operator=(const Row &) { }
356
357 friend class Field;
358 friend class RecordSet;
359 };
361
362
366 class RecordSet : public PObject
367 {
368 PCLASSINFO(RecordSet, PObject);
369 public:
376 RecordSet(PODBC & odbc, const PString & query = PString::Empty());
377
378 // For backward compatibility
379 RecordSet(PODBC * odbc, const PString & query);
380
382 ~RecordSet();
384
390 bool Query(const PString & query);
391
394 bool Select(
395 const PString & table,
396 const PString & whereClause = PString::Empty(),
397 const PString & fields = PString::Empty(),
398 const PString & orderedBy = PString::Empty(),
399 bool descending = false
400 );
401
406 RowIndex Rows(bool forceCount = true);
407
410 PINDEX Columns() { return m_cursor.Columns(); }
411
414 PString ColumnName(PINDEX column) { return m_cursor.ColumnName(column); }
415
418 PStringArray ColumnNames() { return m_cursor.ColumnNames(); }
419
422 Row & NewRow();
423
426 bool MoveTo(RowIndex row) { return m_cursor.MoveTo(row); }
427
430 bool Move(int offset) { return m_cursor.Move(offset); }
431
434 bool First() { return m_cursor.First(); }
435
438 bool Next() { return m_cursor.Next(); }
439
442 bool Previous() { return m_cursor.Previous(); }
443
446 bool Last() { return m_cursor.Last(); }
447
450 bool DeleteRow(RowIndex row = 0);
451
455 Row & operator[](PINDEX row);
456
461 Field & operator()(RowIndex row, PINDEX col);
462
466 Field & Column(PINDEX column) { return m_cursor.Column(column); }
467
470 Field & Column(const PString & name) { return m_cursor.Column(name); }
471
474 bool Commit() { return m_cursor.Commit(); }
476
477 // For backward compatibility
478 bool Post() { return m_cursor.Commit(); }
479
480 protected:
481 Statement * m_statement; // ODBC Fetched Statement Info
482 RowIndex m_totalRows;
483 Row m_cursor;
484
485 private:
486 RecordSet(const RecordSet & other);
487 void operator=(const RecordSet &) { }
488
489 friend class Field;
490 friend class Row;
491 };
492
493 typedef RecordSet Table; // For backward compatibility
494
495
500 P_DECLARE_ENUM(DriverType,
501 DSN,
502 mySQL,
503 postgreSQL,
504 Oracle,
505 IBM_DB2,
506 MSSQL,
507 MSAccess,
508 Paradox,
509 Foxpro,
510 dBase,
511 Excel,
512 Ascii,
513 ConnectionString
514 );
515 static const char * GetDriverName(DriverType type);
516 friend std::ostream & operator<<(std::ostream & strm, DriverType type) { return strm << PODBC::GetDriverName(type); }
517
518
521 enum MSSQLProtocols
522 {
523 MSSQLNamedPipes,
524 MSSQLWinSock,
525 MSSQLIPX,
526 MSSQLBanyan,
527 MSSQLRPC
528 };
529
537 struct ConnectData
538 {
539 ConnectData() : m_driver(DSN), m_port(0), m_exclusive(false), m_trusted(false), m_options(0) { }
540
541 DriverType m_driver;
542 PString m_database;
543 PDirectory m_directory;
544 PString m_username;
545 PString m_password;
546 PString m_host;
547 unsigned m_port;
548 bool m_exclusive;
549 bool m_trusted;
550 int m_options;
551 };
552
560 bool Connect(const ConnectData & connectInfo);
561
566 bool Connect(
567 const PString & source
568 );
569
576 bool Connect(
577 const PString & source,
578 const PString & username,
579 const PString & password
580 );
581
584 bool Connect_DB2(
585 const PFilePath & dbPath
586 );
587
590 bool Connect_XLS(
591 const PFilePath & xlsPath,
592 const PString & defDir = PString::Empty()
593 );
594
597 bool Connect_TXT(
598 const PFilePath & txtPath
599 );
600
603 bool Connect_FOX(
604 const PFilePath & dbPath,
605 const PString & user = PString::Empty(),
606 const PString & pass = PString::Empty(),
607 const PString & type = "DBF",
608 bool exclusive = false
609 );
610
613 bool Connect_MDB(
614 const PFilePath & mdbPath,
615 const PString & user = PString::Empty(),
616 const PString & pass = PString::Empty(),
617 bool exclusive = false
618 );
619
622 bool Connect_PDOX(
623 const PDirectory & dbPath,
624 const PDirectory & defaultDir,
625 int version = 5
626 );
627
630 bool Connect_Oracle(
631 const PString & server,
632 const PString & user = PString::Empty(),
633 const PString & pass = PString::Empty());
634
637 bool Connect_DBASE(
638 const PDirectory & dbPath
639 );
640
643 bool Connect_MSSQL(
644 const PString & user = PString::Empty(),
645 const PString & pass = PString::Empty(),
646 const PString & host = "(local)",
647 bool trusted = true,
648 MSSQLProtocols Proto = MSSQLNamedPipes
649 );
650
653 bool Connect_mySQL(
654 const PString & user = PString::Empty(),
655 const PString & pass = PString::Empty(),
656 const PString & host = "localhost",
657 int port = 3306
658 );
659
662 bool ConnectDB_mySQL(
663 const PString & db,
664 const PString & user = PString::Empty(),
665 const PString & pass = PString::Empty(),
666 const PString & host = "localhost",
667 int port = 3306
668 );
669
672 bool Connect_postgreSQL(
673 const PString & db,
674 const PString & user,
675 const PString & pass,
676 const PString & host,
677 int port = 5432
678 );
679
682 bool IsConnected() const;
683
686 virtual void OnConnected();
687
690 void Disconnect();
691
696 PStringList GetDrivers(
697 bool withAttributes = true
698 ) const;
699
704 PStringList GetSources(
705 bool system = false,
706 bool withDescription = true
707 ) const;
709
717 PStringArray TableList(const PString & options = PString::Empty());
718
724 bool Execute(const PString & sql);
725
726 // For backward compatibility
727 __inline bool Query(const PString & sql) { return Execute(sql); }
729
730
736 static PString GetFieldType(DriverType driver, PVarType::BasicType type, unsigned size = 0);
737
744 void SetPrecision(unsigned precision);
745
746 unsigned GetPrecision() const { return m_precision; }
747
748 PTime::TimeFormat GetTimeFormat() const { return m_timeFormat; }
749 void SetTimeFormat(PTime::TimeFormat fmt) { m_timeFormat = fmt; }
750
751 PTime::TimeFormat GetDateFormat() const { return m_dateFormat; }
752 void SetDateFormat(PTime::TimeFormat fmt) { m_dateFormat = fmt; }
753
754 PTime::TimeFormat GetDateTimeFormat() const { return m_dateTimeFormat; }
755 void SetDateTimeFormat(PTime::TimeFormat fmt) { m_dateTimeFormat = fmt; }
756
763 PINDEX GetMaxChunkSize() const { return m_needChunking ? m_maxChunkSize : P_MAX_INDEX; }
764
766 int GetLastError() const { return m_lastError; }
767
769 PString GetLastErrorText() const { return m_lastErrorText; }
770
773 virtual void OnSQLError(
774 int native,
775 const PString & code,
776 const PString & message
777 );
779
780 // For backward compatibility
781 bool DataSource(DriverType driver, ConnectData Data);
782
783 protected:
784 struct Link;
785
786 Link * m_link;
787 int m_lastError;
788 PString m_lastErrorText;
789 unsigned m_precision;
790 PTime::TimeFormat m_timeFormat;
791 PTime::TimeFormat m_dateFormat;
792 PTime::TimeFormat m_dateTimeFormat;
793 bool m_needChunking;
794 PINDEX m_maxChunkSize;
795
796 P_REMOVE_VIRTUAL_VOID(OnSQLError(const PString &, const PString &));
797
798 friend class Statement;
799 };
800
801
802 // For backward compatibility
803 typedef PODBC::Statement PODBCStmt;
804 typedef PODBC::Row POBDCRecord;
805 typedef PODBC PDSNConnection;
806
807
808 #endif // P_ODBC
809
810 #endif // PTLIB_PODBC_H
811
812
813 // End Of File ///////////////////////////////////////////////////////////////