CppSQLite3.h

Go to the documentation of this file.
00001 
00002 
00003 // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
00004 
00005 //
00006 
00007 // Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
00008 
00009 // 
00010 
00011 // Permission to use, copy, modify, and distribute this software and its
00012 
00013 // documentation for any purpose, without fee, and without a written
00014 
00015 // agreement, is hereby granted, provided that the above copyright notice, 
00016 
00017 // this paragraph and the following two paragraphs appear in all copies, 
00018 
00019 // modifications, and distributions.
00020 
00021 //
00022 
00023 // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
00024 
00025 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
00026 
00027 // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
00028 
00029 // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 //
00032 
00033 // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
00034 
00035 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00036 
00037 // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
00038 
00039 // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
00040 
00041 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00042 
00043 //
00044 
00045 // V3.0         03/08/2004      -Initial Version for sqlite3
00046 
00047 //
00048 
00049 // V3.1         16/09/2004      -Implemented getXXXXField using sqlite3 functions
00050 
00051 //                                              -Added CppSQLiteDB3::tableExists()
00052 
00054 
00055 #ifndef _CppSQLite3_H_
00056 
00057 #define _CppSQLite3_H_
00058 
00059 
00060 
00061 #include "sqlite3.h"
00062 
00063 #include <cstdio>
00064 
00065 #include <cstring>
00066 
00067 
00068 
00069 #define CPPSQLITE_ERROR 1000
00070 
00071 
00072 
00073 class CppSQLite3Exception
00074 
00075 {
00076 
00077 public:
00078 
00079 
00080 
00081     CppSQLite3Exception(const int nErrCode,
00082 
00083                         char* szErrMess,
00084 
00085                         bool bDeleteMsg=true);
00086 
00087   CppSQLite3Exception(const int nErrCode,
00088 
00089                       const char* szErrMess,
00090 
00091                       bool bDeleteMsg=true);
00092 
00093   
00094 
00095     CppSQLite3Exception(const CppSQLite3Exception&  e);
00096 
00097 
00098 
00099     virtual ~CppSQLite3Exception();
00100 
00101 
00102 
00103     const int errorCode() { return mnErrCode; }
00104 
00105 
00106 
00107     const char* errorMessage() { return mpszErrMess; }
00108 
00109 
00110 
00111     static const char* errorCodeAsString(int nErrCode);
00112 
00113 
00114 
00115 private:
00116 
00117 
00118 
00119     int mnErrCode;
00120 
00121     char* mpszErrMess;
00122 
00123 };
00124 
00125 
00126 
00127 
00128 
00129 class CppSQLite3Buffer
00130 
00131 {
00132 
00133 public:
00134 
00135 
00136 
00137     CppSQLite3Buffer();
00138 
00139 
00140 
00141     ~CppSQLite3Buffer();
00142 
00143 
00144 
00145     const char* format(const char* szFormat, ...);
00146 
00147 
00148 
00149     operator const char*() { return mpBuf; }
00150 
00151 
00152 
00153     void clear();
00154 
00155 
00156 
00157 private:
00158 
00159 
00160 
00161     char* mpBuf;
00162 
00163 };
00164 
00165 
00166 
00167 
00168 
00169 class CppSQLite3Binary
00170 
00171 {
00172 
00173 public:
00174 
00175 
00176 
00177     CppSQLite3Binary();
00178 
00179 
00180 
00181     ~CppSQLite3Binary();
00182 
00183 
00184 
00185     void setBinary(const unsigned char* pBuf, int nLen);
00186 
00187     void setEncoded(const unsigned char* pBuf);
00188 
00189 
00190 
00191     const unsigned char* getEncoded();
00192 
00193     const unsigned char* getBinary();
00194 
00195 
00196 
00197     int getBinaryLength();
00198 
00199 
00200 
00201     unsigned char* allocBuffer(int nLen);
00202 
00203 
00204 
00205     void clear();
00206 
00207 
00208 
00209 private:
00210 
00211 
00212 
00213     unsigned char* mpBuf;
00214 
00215     int mnBinaryLen;
00216 
00217     int mnBufferLen;
00218 
00219     int mnEncodedLen;
00220 
00221     bool mbEncoded;
00222 
00223 };
00224 
00225 
00226 
00227 
00228 
00229 class CppSQLite3Query
00230 
00231 {
00232 
00233 public:
00234 
00235 
00236 
00237     CppSQLite3Query();
00238 
00239 
00240 
00241     CppSQLite3Query(const CppSQLite3Query& rQuery);
00242 
00243 
00244 
00245     CppSQLite3Query(sqlite3* pDB,
00246 
00247                                 sqlite3_stmt* pVM,
00248 
00249                 bool bEof,
00250 
00251                 bool bOwnVM=true);
00252 
00253 
00254 
00255     CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
00256 
00257 
00258 
00259     virtual ~CppSQLite3Query();
00260 
00261 
00262 
00263     int numFields();
00264 
00265 
00266 
00267     int fieldIndex(const char* szField);
00268 
00269     const char* fieldName(int nCol);
00270 
00271 
00272 
00273     const char* fieldDeclType(int nCol);
00274 
00275     int fieldDataType(int nCol);
00276 
00277 
00278 
00279     const char* fieldValue(int nField);
00280 
00281     const char* fieldValue(const char* szField);
00282 
00283 
00284 
00285     int getIntField(int nField, int nNullValue=0);
00286 
00287     int getIntField(const char* szField, int nNullValue=0);
00288 
00289 
00290 
00291     double getFloatField(int nField, double fNullValue=0.0);
00292 
00293     double getFloatField(const char* szField, double fNullValue=0.0);
00294 
00295 
00296 
00297     const char* getStringField(int nField, const char* szNullValue="");
00298 
00299     const char* getStringField(const char* szField, const char* szNullValue="");
00300 
00301 
00302 
00303     const unsigned char* getBlobField(int nField, int& nLen);
00304 
00305     const unsigned char* getBlobField(const char* szField, int& nLen);
00306 
00307 
00308 
00309     bool fieldIsNull(int nField);
00310 
00311     bool fieldIsNull(const char* szField);
00312 
00313 
00314 
00315     bool eof();
00316 
00317 
00318 
00319     void nextRow();
00320 
00321 
00322 
00323     void finalize();
00324 
00325 
00326 
00327 private:
00328 
00329 
00330 
00331     void checkVM();
00332 
00333 
00334 
00335         sqlite3* mpDB;
00336 
00337     sqlite3_stmt* mpVM;
00338 
00339     bool mbEof;
00340 
00341     int mnCols;
00342 
00343     bool mbOwnVM;
00344 
00345 };
00346 
00347 
00348 
00349 
00350 
00351 class CppSQLite3Table
00352 
00353 {
00354 
00355 public:
00356 
00357 
00358 
00359     CppSQLite3Table();
00360 
00361 
00362 
00363     CppSQLite3Table(const CppSQLite3Table& rTable);
00364 
00365 
00366 
00367     CppSQLite3Table(char** paszResults, int nRows, int nCols);
00368 
00369 
00370 
00371     virtual ~CppSQLite3Table();
00372 
00373 
00374 
00375     CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
00376 
00377 
00378 
00379     int numFields();
00380 
00381 
00382 
00383     int numRows();
00384 
00385 
00386 
00387     const char* fieldName(int nCol);
00388 
00389 
00390 
00391     const char* fieldValue(int nField);
00392 
00393     const char* fieldValue(const char* szField);
00394 
00395 
00396 
00397     int getIntField(int nField, int nNullValue=0);
00398 
00399     int getIntField(const char* szField, int nNullValue=0);
00400 
00401 
00402 
00403     double getFloatField(int nField, double fNullValue=0.0);
00404 
00405     double getFloatField(const char* szField, double fNullValue=0.0);
00406 
00407 
00408 
00409     const char* getStringField(int nField, const char* szNullValue="");
00410 
00411     const char* getStringField(const char* szField, const char* szNullValue="");
00412 
00413 
00414 
00415     bool fieldIsNull(int nField);
00416 
00417     bool fieldIsNull(const char* szField);
00418 
00419 
00420 
00421     void setRow(int nRow);
00422 
00423 
00424 
00425     void finalize();
00426 
00427 
00428 
00429 private:
00430 
00431 
00432 
00433     void checkResults();
00434 
00435 
00436 
00437     int mnCols;
00438 
00439     int mnRows;
00440 
00441     int mnCurrentRow;
00442 
00443     char** mpaszResults;
00444 
00445 };
00446 
00447 
00448 
00449 
00450 
00451 class CppSQLite3Statement
00452 
00453 {
00454 
00455 public:
00456 
00457 
00458 
00459     CppSQLite3Statement();
00460 
00461 
00462 
00463     CppSQLite3Statement(const CppSQLite3Statement& rStatement);
00464 
00465 
00466 
00467     CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
00468 
00469 
00470 
00471     virtual ~CppSQLite3Statement();
00472 
00473 
00474 
00475     CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
00476 
00477 
00478 
00479     int execDML();
00480 
00481 
00482 
00483     CppSQLite3Query execQuery();
00484 
00485 
00486 
00487     void bind(int nParam, const char* szValue);
00488 
00489     void bind(int nParam, const int nValue);
00490 
00491     void bind(int nParam, const double dwValue);
00492 
00493     void bind(int nParam, const unsigned char* blobValue, int nLen);
00494 
00495     void bindNull(int nParam);
00496 
00497 
00498 
00499     void reset();
00500 
00501 
00502 
00503     void finalize();
00504 
00505 
00506 
00507 private:
00508 
00509 
00510 
00511     void checkDB();
00512 
00513     void checkVM();
00514 
00515 
00516 
00517     sqlite3* mpDB;
00518 
00519     sqlite3_stmt* mpVM;
00520 
00521 };
00522 
00523 
00524 
00525 
00526 
00527 class CppSQLite3DB
00528 
00529 {
00530 
00531 public:
00532 
00533 
00534 
00535     CppSQLite3DB();
00536 
00537 
00538 
00539     virtual ~CppSQLite3DB();
00540 
00541 
00542 
00543     void open(const char* szFile);
00544 
00545 
00546 
00547     void close();
00548 
00549 
00550 
00551         bool tableExists(const char* szTable);
00552 
00553 
00554 
00555     int execDML(const char* szSQL);
00556 
00557 
00558 
00559     CppSQLite3Query execQuery(const char* szSQL);
00560 
00561 
00562 
00563     int execScalar(const char* szSQL);
00564 
00565 
00566 
00567     CppSQLite3Table getTable(const char* szSQL);
00568 
00569 
00570 
00571     CppSQLite3Statement compileStatement(const char* szSQL);
00572 
00573 
00574 
00575     sqlite_int64 lastRowId();
00576 
00577 
00578 
00579     void interrupt() { sqlite3_interrupt(mpDB); }
00580 
00581 
00582 
00583     void setBusyTimeout(int nMillisecs);
00584 
00585 
00586 
00587     static const char* SQLiteVersion() { return SQLITE_VERSION; }
00588 
00589 
00590 
00591 private:
00592 
00593 
00594 
00595     CppSQLite3DB(const CppSQLite3DB& db);
00596 
00597     CppSQLite3DB& operator=(const CppSQLite3DB& db);
00598 
00599 
00600 
00601     sqlite3_stmt* compile(const char* szSQL);
00602 
00603 
00604 
00605     void checkDB();
00606 
00607 
00608 
00609     sqlite3* mpDB;
00610 
00611     int mnBusyTimeoutMs;
00612 
00613 };
00614 
00615 
00616 
00617 #endif
00618