D:/Papagan/490.2006/490.2006/korsan/Papagan/490.2006/korsan/Papagan/CppSQLite3.h

Go to the documentation of this file.
00001 
00002 // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
00003 //
00004 // Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
00005 // 
00006 // Permission to use, copy, modify, and distribute this software and its
00007 // documentation for any purpose, without fee, and without a written
00008 // agreement, is hereby granted, provided that the above copyright notice, 
00009 // this paragraph and the following two paragraphs appear in all copies, 
00010 // modifications, and distributions.
00011 //
00012 // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
00013 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
00014 // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
00015 // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 //
00017 // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00019 // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
00020 // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
00021 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00022 //
00023 // V3.0         03/08/2004      -Initial Version for sqlite3
00024 //
00025 // V3.1         16/09/2004      -Implemented getXXXXField using sqlite3 functions
00026 //                                              -Added CppSQLiteDB3::tableExists()
00028 #ifndef _CppSQLite3_H_
00029 #define _CppSQLite3_H_
00030 
00031 #include "sqlite3.h"
00032 #include <cstdio>
00033 #include <cstring>
00034 
00035 #define CPPSQLITE_ERROR 1000
00036 
00037 class CppSQLite3Exception
00038 {
00039 public:
00040 
00041     CppSQLite3Exception(const int nErrCode,
00042                     char* szErrMess,
00043                     bool bDeleteMsg=true);
00044 
00045     CppSQLite3Exception(const CppSQLite3Exception&  e);
00046 
00047     virtual ~CppSQLite3Exception();
00048 
00049     const int errorCode() { return mnErrCode; }
00050 
00051     const char* errorMessage() { return mpszErrMess; }
00052 
00053     static const char* errorCodeAsString(int nErrCode);
00054 
00055 private:
00056 
00057     int mnErrCode;
00058     char* mpszErrMess;
00059 };
00060 
00061 
00062 class CppSQLite3Buffer
00063 {
00064 public:
00065 
00066     CppSQLite3Buffer();
00067 
00068     ~CppSQLite3Buffer();
00069 
00070     const char* format(const char* szFormat, ...);
00071 
00072     operator const char*() { return mpBuf; }
00073 
00074     void clear();
00075 
00076 private:
00077 
00078     char* mpBuf;
00079 };
00080 
00081 
00082 class CppSQLite3Binary
00083 {
00084 public:
00085 
00086     CppSQLite3Binary();
00087 
00088     ~CppSQLite3Binary();
00089 
00090     void setBinary(const unsigned char* pBuf, int nLen);
00091     void setEncoded(const unsigned char* pBuf);
00092 
00093     const unsigned char* getEncoded();
00094     const unsigned char* getBinary();
00095 
00096     int getBinaryLength();
00097 
00098     unsigned char* allocBuffer(int nLen);
00099 
00100     void clear();
00101 
00102 private:
00103 
00104     unsigned char* mpBuf;
00105     int mnBinaryLen;
00106     int mnBufferLen;
00107     int mnEncodedLen;
00108     bool mbEncoded;
00109 };
00110 
00111 
00112 class CppSQLite3Query
00113 {
00114 public:
00115 
00116     CppSQLite3Query();
00117 
00118     CppSQLite3Query(const CppSQLite3Query& rQuery);
00119 
00120     CppSQLite3Query(sqlite3* pDB,
00121                                 sqlite3_stmt* pVM,
00122                 bool bEof,
00123                 bool bOwnVM=true);
00124 
00125     CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
00126 
00127     virtual ~CppSQLite3Query();
00128 
00129     int numFields();
00130 
00131     int fieldIndex(const char* szField);
00132     const char* fieldName(int nCol);
00133 
00134     const char* fieldDeclType(int nCol);
00135     int fieldDataType(int nCol);
00136 
00137     const char* fieldValue(int nField);
00138     const char* fieldValue(const char* szField);
00139 
00140     int getIntField(int nField, int nNullValue=0);
00141     int getIntField(const char* szField, int nNullValue=0);
00142 
00143     double getFloatField(int nField, double fNullValue=0.0);
00144     double getFloatField(const char* szField, double fNullValue=0.0);
00145 
00146     const char* getStringField(int nField, const char* szNullValue="");
00147     const char* getStringField(const char* szField, const char* szNullValue="");
00148 
00149     const unsigned char* getBlobField(int nField, int& nLen);
00150     const unsigned char* getBlobField(const char* szField, int& nLen);
00151 
00152     bool fieldIsNull(int nField);
00153     bool fieldIsNull(const char* szField);
00154 
00155     bool eof();
00156 
00157     void nextRow();
00158 
00159     void finalize();
00160 
00161 private:
00162 
00163     void checkVM();
00164 
00165         sqlite3* mpDB;
00166     sqlite3_stmt* mpVM;
00167     bool mbEof;
00168     int mnCols;
00169     bool mbOwnVM;
00170 };
00171 
00172 
00173 class CppSQLite3Table
00174 {
00175 public:
00176 
00177     CppSQLite3Table();
00178 
00179     CppSQLite3Table(const CppSQLite3Table& rTable);
00180 
00181     CppSQLite3Table(char** paszResults, int nRows, int nCols);
00182 
00183     virtual ~CppSQLite3Table();
00184 
00185     CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
00186 
00187     int numFields();
00188 
00189     int numRows();
00190 
00191     const char* fieldName(int nCol);
00192 
00193     const char* fieldValue(int nField);
00194     const char* fieldValue(const char* szField);
00195 
00196     int getIntField(int nField, int nNullValue=0);
00197     int getIntField(const char* szField, int nNullValue=0);
00198 
00199     double getFloatField(int nField, double fNullValue=0.0);
00200     double getFloatField(const char* szField, double fNullValue=0.0);
00201 
00202     const char* getStringField(int nField, const char* szNullValue="");
00203     const char* getStringField(const char* szField, const char* szNullValue="");
00204 
00205     bool fieldIsNull(int nField);
00206     bool fieldIsNull(const char* szField);
00207 
00208     void setRow(int nRow);
00209 
00210     void finalize();
00211 
00212 private:
00213 
00214     void checkResults();
00215 
00216     int mnCols;
00217     int mnRows;
00218     int mnCurrentRow;
00219     char** mpaszResults;
00220 };
00221 
00222 
00223 class CppSQLite3Statement
00224 {
00225 public:
00226 
00227     CppSQLite3Statement();
00228 
00229     CppSQLite3Statement(const CppSQLite3Statement& rStatement);
00230 
00231     CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
00232 
00233     virtual ~CppSQLite3Statement();
00234 
00235     CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
00236 
00237     int execDML();
00238 
00239     CppSQLite3Query execQuery();
00240 
00241     void bind(int nParam, const char* szValue);
00242     void bind(int nParam, const int nValue);
00243     void bind(int nParam, const double dwValue);
00244     void bind(int nParam, const unsigned char* blobValue, int nLen);
00245     void bindNull(int nParam);
00246 
00247     void reset();
00248 
00249     void finalize();
00250 
00251 private:
00252 
00253     void checkDB();
00254     void checkVM();
00255 
00256     sqlite3* mpDB;
00257     sqlite3_stmt* mpVM;
00258 };
00259 
00260 
00261 class CppSQLite3DB
00262 {
00263 public:
00264 
00265     CppSQLite3DB();
00266 
00267     virtual ~CppSQLite3DB();
00268 
00269     void open(const char* szFile);
00270 
00271     void close();
00272 
00273         bool tableExists(const char* szTable);
00274 
00275     int execDML(const char* szSQL);
00276 
00277     CppSQLite3Query execQuery(const char* szSQL);
00278 
00279     int execScalar(const char* szSQL);
00280 
00281     CppSQLite3Table getTable(const char* szSQL);
00282 
00283     CppSQLite3Statement compileStatement(const char* szSQL);
00284 
00285     sqlite_int64 lastRowId();
00286 
00287     void interrupt() { sqlite3_interrupt(mpDB); }
00288 
00289     void setBusyTimeout(int nMillisecs);
00290 
00291     static const char* SQLiteVersion() { return SQLITE_VERSION; }
00292 
00293 private:
00294 
00295     CppSQLite3DB(const CppSQLite3DB& db);
00296     CppSQLite3DB& operator=(const CppSQLite3DB& db);
00297 
00298     sqlite3_stmt* compile(const char* szSQL);
00299 
00300     void checkDB();
00301 
00302     sqlite3* mpDB;
00303     int mnBusyTimeoutMs;
00304 };
00305 
00306 #endif

Generated on Mon May 29 01:10:33 2006 for Papagan by  doxygen 1.4.6-NO