Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals | Related Pages

/var/home/joerg/c/CDB/include/CDB.h

Go to the documentation of this file.
00001 
00037 #ifndef _CDB_H
00038 #define _CDB_H
00039 
00040 #ifdef HAVE_CONFIG_H
00041 #include "config.h"
00042 #else
00043 #include "sysdep.h"
00044 #endif
00045 
00046 #include <cdbstring.h>
00047 
00048 #include <iostream>
00049 #include <string>
00050 #include <list>
00051 
00053 #define CDB_VERSION_MAJOR 0
00054 
00055 #define CDB_VERSION_MINOR 2
00056 
00057 #define CDB_VERSION_RELEASE 0
00058 
00060 #define V_TO_NUM(maj, min, rel) (maj * 10000 + min * 100 + rel)
00061 
00063 #define CDB_VERSION_NUMBER V_TO_NUM(CDB_VERSION_MAJOR, CDB_VERSION_MINOR, CDB_VERSION_RELEASE)
00064 
00066 #define CDB_DEFAULT_RUNTIME_VERSION_CHECK \
00067         { \
00068                 int ma, mi, re; \
00069                 get_cdb_runtime_version(&ma, &mi, &re); \
00070                 if ( CDB_VERSION_NUMBER != V_TO_NUM(ma, mi, re) ) { \
00071                         std::cerr << "WARNING: CDB-Api version difference, compiled: "; \
00072                         std::cerr << CDB_VERSION_MAJOR << "." << CDB_VERSION_MINOR; \
00073                         std::cerr << "." << CDB_VERSION_RELEASE << std::endl; \
00074                         std::cerr << "                                      runtime: "; \
00075                         std::cerr << ma << "." << mi << "." << re << std::endl; \
00076                 } \
00077         }
00078 
00079 #if defined(HAVE_UINT64_T) && defined(HAVE_INT64_T)
00080 #ifdef HAVE_STDINT_H
00081 #include<stdint.h>
00082 #endif
00083 
00084 #ifdef HAVE_INTTYPES_H
00085 #include<inttypes.h>
00086 #endif
00087 #else
00088 typedef unsigned long long int uint64_t;
00089 typedef long long int int64_t;
00090 #endif
00091 
00093 #define CDB_OPT_DONT_CLOSE_HANDLE 1
00094 
00095 #include <time.h>
00096 #include "cdb_driver_symbol.h"
00097 
00101 typedef enum { E_NULL_DUMMY } CDB_NULL_T;
00102 #define CDB_NULL (CDB_NULL_T(E_NULL_DUMMY))
00103 
00114 extern "C" void get_cdb_runtime_version(int *ma, int *mi, int *re);
00115 
00122 typedef class CDBdatetime {
00123 public:
00124         CDBdatetime() {
00125                 year = month = day = hour = minute = second = msecond = 0;
00126         }
00127         CDBdatetime(const CDBdatetime &c) {
00128                 operator=(c);
00129         }
00130         CDBdatetime(const struct tm &t) {
00131                 operator=(t);
00132         }
00133         CDBdatetime(time_t t) {
00134                 operator=(t);
00135         }
00136         inline CDBdatetime &operator=(const CDBdatetime &c) {
00137                 year = c.year;
00138                 month = c.month;
00139                 day = c.day;
00140                 hour = c.hour;
00141                 minute = c.minute;
00142                 second = c.second;
00143                 msecond = c.msecond;
00144                 return *this;
00145         }
00146         inline CDBdatetime &operator=(const struct tm &t) {
00147                 year = t.tm_year+1900;
00148                 month = t.tm_mon+1;
00149                 day = t.tm_mday;
00150                 hour = t.tm_hour;
00151                 minute = t.tm_min;
00152                 second = t.tm_sec;
00153                 msecond = 0;
00154                 return *this;
00155         }
00156         inline CDBdatetime &operator=(time_t t) {
00157                 struct tm *bdt = localtime(&t);
00158                 return operator=(*bdt);
00159         }
00160         int operator==(const CDBdatetime &c) {
00161                 return (year == c.year && month == c.month && day == c.day && hour == c.hour && minute == c.minute && second == c.second &&msecond == c.msecond);
00162         }
00163         
00169         static CDBdatetime now() {
00170                 time_t _time = time(NULL);
00171                 struct tm *bdt = localtime(&_time);
00172                 return CDBdatetime(*bdt);
00173         }
00174 
00175         inline time_t mktime(struct tm *pb = NULL) {
00176                 struct tm _pb;
00177                 if ( pb == NULL ) {
00178                         pb = &_pb;
00179                 }
00180                 pb->tm_year = year-1900;
00181                 pb->tm_mon = month-1;
00182                 pb->tm_mday = day;
00183                 pb->tm_hour = hour;
00184                 pb->tm_min = minute;
00185                 pb->tm_sec = second;
00186                 return ::mktime( pb );
00187         }
00188 
00189         int year, month, day, hour, minute, second, msecond;
00190 } Tdatetime;
00191 
00198 typedef class CDBdate{
00199 public:
00200         CDBdate() {
00201                 year = month = day = 0;
00202         }
00203         CDBdate(const CDBdate &c) {
00204                 operator=(c);
00205         }
00206         CDBdate(const struct tm &t) {
00207                 operator=(t);
00208         }
00209         CDBdate &operator=(const CDBdate &c) {
00210                 year = c.year;
00211                 month = c.month;
00212                 day = c.day;
00213                 return *this;
00214         }
00215         CDBdate&operator=(const struct tm &t) {
00216                 year = t.tm_year;
00217                 month = t.tm_mon;
00218                 day = t.tm_mday;
00219                 return *this;
00220         }
00221         int operator==(const CDBdate &c) {
00222                 return (year == c.year && month == c.month && day == c.day);
00223         }
00224         
00230         static CDBdate now() {
00231                 time_t _time;
00232                 struct tm *bdt = localtime(&_time);
00233                 return CDBdate(*bdt);
00234         }
00235         int year, month, day;
00236 } Tdate;
00237 
00244 typedef class CDBtime {
00245 public:
00246         CDBtime() {
00247                 hour = minute = second = msecond = 0;
00248         }
00249         CDBtime(const CDBtime &c) {
00250                 operator=(c);
00251         }
00252         CDBtime(const struct tm &t) {
00253                 operator=(t);
00254         }
00255         CDBtime &operator=(const CDBtime &c) {
00256                 hour = c.hour;
00257                 minute = c.minute;
00258                 second = c.second;
00259                 msecond = c.msecond;
00260                 return *this;
00261         }
00262         CDBtime &operator=(const struct tm &t) {
00263                 hour = t.tm_hour;
00264                 minute = t.tm_min;
00265                 second = t.tm_sec;
00266                 msecond = 0;
00267                 return *this;
00268         }
00269         int operator==(const CDBtime &c) {
00270                 return (hour == c.hour && minute == c.minute && second == c.second &&msecond == c.msecond);
00271         }
00272         
00278         static CDBtime now() {
00279                 time_t _time;
00280                 struct tm *bdt = localtime(&_time);
00281                 return CDBtime(*bdt);
00282         }
00283         int hour, minute, second, msecond;
00284 } Ttime;
00285 
00293 class CDB {
00294 public:
00295         CDB();
00296         CDB(CDB& c);
00297         CDB(const CDBstring &driver_name);
00298         virtual ~CDB();
00299 
00309         int load(const CDBstring &driver_name);
00310 
00320         int load_static(driver_func_t func);
00321         
00330         CDBstring get_driver_name();
00331         
00347         int last_error();
00348         
00352         CDBstring error_msg();
00353 
00354         inline Tptr connect(const CDBstring& conn_str) {
00355                 if ( !handle ) return NULL;
00356                 return (*syms->connect)(conn_str.c_str());
00357         }
00358         inline int getcdbopts() {
00359                 if ( !handle ) return -1;
00360                 return (*syms->getcdbopts)();
00361         }
00362         inline void setopt(Tptr conn, int opt) {
00363                 if ( !handle ) return;
00364                 (*syms->setopt)(conn, opt);
00365         }
00366         inline int status(Tptr conn) {
00367                 if ( !handle ) return -1;
00368                 return (*syms->status)(conn);
00369         }
00370         inline void disconnect(Tptr conn) {
00371                 if ( !handle ) return;
00372                 (*syms->disconnect)(conn);
00373         }
00374         inline CDBstring error(Tptr conn) {
00375                 if ( !handle ) return "";
00376                 return (*syms->error)(conn);
00377         }
00378         inline int read_integer(Tptr res, int row, int col, int *p) {
00379                 if ( !handle ) return 0;
00380                 return (*syms->read_integer)(res, row, col, p);
00381         }
00382         inline int read_long_integer(Tptr res, int row, int col, int64_t *p) {
00383                 if ( !handle ) return 0;
00384                 return (*syms->read_long_integer)(res, row, col, p);
00385         }
00386         inline int read_double(Tptr res, int row, int col, double *d_ptr) {
00387                 if ( !handle ) return 0;
00388                 return (*syms->read_double)(res, row, col, d_ptr);
00389         }
00390         inline CDBstring read_string(Tptr res, int row, int col) {
00391                 if ( !handle ) return "";
00392                 return (*syms->read_string)(res, row, col);
00393         }
00394         inline int read_isnull(Tptr res, int row, int col) {
00395                 if ( !handle ) return 0;
00396                 return (*syms->read_isnull)(res, row, col);
00397         }
00398         inline int read_date(Tptr res, int row, int col, int *year, int *mon, int *day) {
00399                 if ( !handle ) return 0;
00400                 return (*syms->read_date)(res, row, col, year, mon, day);
00401         }
00402         inline int read_time(Tptr res, int row, int col, int *hour, int *min, int *sec) {
00403                 if ( !handle ) return 0;
00404                 return (*syms->read_time)(res, row, col, hour, min, sec);
00405         }
00406         inline int read_datetime(Tptr res, int row, int col, int *year, int *mon, int *day, int *hour, int *min, int *sec) {
00407                 if ( !handle ) return 0;
00408                 return (*syms->read_datetime)(res, row, col, year, mon, day, hour, min, sec);
00409         }
00410         inline int count_rows(Tptr res) {
00411                 if ( !handle ) return -1;
00412                 return (*syms->count_rows)(res);
00413         }
00414         inline int count_cols(Tptr res) {
00415                 if ( !handle ) return -1;
00416                 return (*syms->count_cols)(res);
00417         }
00418         inline int open_query(Tptr db, Tptr* res, const CDBstring& str_sql) {
00419                 if ( !handle ) return -1;
00420                 return (*syms->open_query)(db,  res, str_sql.c_str());
00421         }
00422         inline int exec_query(Tptr db, Tptr* res, const CDBstring& str_sql) {
00423                 if ( !handle ) return -1;
00424                 return (*syms->exec_query)(db,  res, str_sql.c_str());
00425         }
00426         inline void close_query(Tptr res) {
00427                 if ( !handle ) return;
00428                 (*syms->close_query)(res);
00429                 return;
00430         }
00431         inline CDBstring sqlerr(Tptr res) {
00432                 if ( !handle ) return "";
00433                 return (*syms->sqlerr)(res);
00434         }
00435         inline int search_col(Tptr res, const CDBstring& fn) {
00436                 if ( !handle ) return -1;
00437                 return (*syms->search_col)(res, fn.c_str());
00438         }
00439         inline CDBstring version()
00440         {
00441                 if ( !handle ) return "no version";
00442                 return syms->version;
00443         }
00444 
00445 /*      inline int get_next_bind_var(Tptr res, char *name, size_t name_size, tvartype *type, size_t *len) {
00446                 if ( !handle ) return -1;
00447                 return (*syms->get_next_bind_var)(res, name, name_size, type, len);
00448         }*/
00449 
00450         inline int bv_set_unfilled(Tptr res) {
00451                 if ( !handle ) return -1;
00452                 return (*syms->bv_set_unfilled)(res);
00453         }
00454 
00455         inline int bv_get_count(Tptr res) {
00456                 if ( !handle ) return -1;
00457                 return (*syms->bv_get_count)(res);
00458         }
00459 
00460         inline char bv_get_filled(Tptr res, int pos) {
00461                 if ( !handle ) return (unsigned char)-1;
00462                 return (*syms->bv_get_filled)(res, pos);
00463         }
00464 
00465         inline char bv_finished(Tptr res) {
00466                 if ( !handle ) return (unsigned char)-1;
00467                 return (*syms->bv_finished)(res);
00468         }
00469         
00470         inline int bv_get_pos_by_name(Tptr res, const CDBstring& pos, int *res_vec, int *res_vec_size) {
00471                 if ( !handle ) return -1;
00472                 return (*syms->bv_get_pos_by_name)(res, pos.c_str(), res_vec, res_vec_size);
00473         }
00474 
00475         inline int bv_bind_by_pos(Tptr res, int pos, tvartype type, Tptr data) {
00476                 if ( !handle ) return -1;
00477                 return (*syms->bv_bind_by_pos)(res, pos, type, data);
00478         }
00479 
00480         inline int process(Tptr res) {
00481                 if ( !handle ) return -1;
00482                 return (*syms->process)(res);
00483         }
00484 
00485         inline driver_func_t get_static_func() {
00486                 return mstatic_func;
00487         }
00488 private:
00489         CDBstring mdriver_name, merror_msg;
00490         void *handle;
00491         int mlast_error;
00492         driver_func_t mstatic_func;
00493 
00494         driver_symbols_t *syms;
00495 };
00496 
00504 class CDBconnection {
00505 public:
00507         CDBconnection(CDBconnection& c);
00508         
00518         CDBconnection(CDB* tDB);
00519 
00532         CDBconnection(CDB* tDB, const CDBstring& conn_str);
00533 
00535         virtual ~CDBconnection();
00536 
00538         Tptr getConn();
00539 
00541         CDB *getDB();
00542 
00551         int Connect(const CDBstring& conn_str);
00552 
00558         void Disconnect();
00559 
00567         int Status();
00568 
00576         CDBstring Error();
00577 
00583         void setopt(int opt);
00584 
00585 private:
00586         Tptr mConn;
00587         CDB *mDB;
00588 };
00589 
00590 /* Forward declaration */
00591 class CDBquery;
00592 
00600 class CDBfield {
00601 public:
00602         CDBquery *mQ;
00603         int row,col;
00604 
00606         CDBfield();
00608         CDBfield(const CDBfield &c);
00609 
00611         const char* string();
00612 
00613         CDBstring ustr();
00614 
00616         Tdate date();
00618         Ttime time();
00620         Tdatetime datetime();
00622         int isnull();
00624         int integer();
00626         int64_t long_integer();
00628         double floating();
00629 
00630         CDBfield& operator=(const CDBfield& c);
00631 };
00632 
00642 class CDBrow {
00643 public:
00644         CDBquery *mQ;
00645         int row;
00646 
00647         CDBrow();
00649         CDBrow(const CDBrow& c);
00651         CDBrow(CDBquery *Q, int trow = 0);
00652 
00653         CDBrow& operator=(const CDBrow& c);
00655         CDBfield operator[](int i);
00657         CDBfield operator[](const CDBstring& fn);
00658 };
00659 
00670 class CDBquery {
00671 public:
00672         CDBconnection *mDB;
00673         Tptr mres;
00674 
00675         CDBquery();
00676         CDBquery(CDBconnection* c);
00677         ~CDBquery();
00678 
00690         int open(const CDBstring& str_sql);
00691 
00703         int exec(const CDBstring& str_sql);
00704 
00705         int process();
00706 
00707         int bind(int pos, int data);
00708         int bind(int pos, int64_t data);
00709         int bind(int pos, float data);
00710         int bind(int pos, double data);
00711         int bind(int pos, const CDBstring& data);
00712         int bind(int pos, const Tdatetime &data);
00713         int bind(int pos, const Tdate &data);
00714         int bind(int pos, const Ttime &data);
00715 
00716         int bind(const CDBstring &name, int data);
00717         int bind(const CDBstring &name, int16_t data);
00718         int bind(const CDBstring &name, float data);
00719         int bind(const CDBstring &name, double data);
00720         int bind(const CDBstring &name, const CDBstring& data);
00721         int bind(const CDBstring &name, const Tdatetime &data);
00722         int bind(const CDBstring &name, const Tdate &data);
00723         int bind(const CDBstring &name, const Ttime &data);
00724 
00725         int bind_count();
00726         int bind_reset();
00727         bool bind_filled(int pos);
00728         bool bind_finished();
00729         int bind_get_pos_by_name(const CDBstring& name, int *res, int *res_size);
00730         int bind_to_next();
00731 
00736         void close();
00737 
00741         CDBstring sqlerr();
00742 
00744         int rows();
00746         int cols();
00747 
00754         CDBrow operator[](int i);
00755 
00759         CDBquery &operator<<(int i);
00760 
00764         CDBquery &operator<<(int64_t i);
00765 
00769         CDBquery &operator<<(float f);
00770 
00774         CDBquery &operator<<(double f);
00775 
00779         CDBquery &operator<<(const CDBstring& s);
00780 
00784         CDBquery &operator<<(const Tdate &d);
00785 
00789         CDBquery &operator<<(const Ttime &t);
00790 
00794         CDBquery &operator<<(const Tdatetime &dt);
00795 
00799         CDBquery &operator<<(const CDB_NULL_T &n);
00800 
00810         CDBquery &operator<<(CDBrow r);
00811 
00812         char *getBuffer(int size);
00813 private:
00814         char opened;
00815         std::list<char*> mBuffVec;
00816 };
00817 
00833 class CEXquery {
00834 public:
00835         CEXquery() {
00836                 mcaller = NULL;
00837         }
00838         CEXquery(CDBquery *c) {
00839                 mcaller  = c;
00840         }
00842         CDBquery *caller() {
00843                 return mcaller;
00844         }
00845 private:
00846         CDBquery *mcaller;
00847 };
00848 
00849 #endif

Generated on Mon Feb 6 00:30:32 2006 for CDB by  doxygen 1.4.4