00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef _SQLITEDB_H_
00015 #define _SQLITEDB_H_
00016
00017 #include "sqlite3.h"
00018 #include "CCTChar.h"
00019 #include <map>
00020 #include <vector>
00021 #include <set>
00022 #include <debug.h>
00023
00025 typedef std::vector<std::tstring> STRARRAY;
00027 typedef std::map<std::tstring, int> STR2INT;
00028
00029 namespace SQLite
00030 {
00031
00035 class RefObj
00036 {
00037 public:
00041 RefObj() : m_nRef(0) {};
00045 virtual ~RefObj() {ASSERT(m_nRef == 0);};
00046
00047 protected:
00049 int m_nRef;
00050
00051 public:
00055 void AddRef() {m_nRef++;};
00059 void DecRef() {m_nRef--; ASSERT(m_nRef >= 0); if (m_nRef == 0) delete this;};
00060 };
00061
00062 class RealRecord;
00063
00067 class Record
00068 {
00069 public:
00070
00074 Record() : m_pRecord(NULL) {};
00079 Record(const Record& record) : m_pRecord(record.m_pRecord) {AddRef();};
00083 virtual ~Record() {DecRef();};
00084 protected:
00089 Record(RealRecord* pRecord) : m_pRecord(pRecord) {AddRef();};
00090
00091
00092 friend class Recordset;
00093
00094 protected:
00096 RealRecord* m_pRecord;
00097
00098 public:
00099
00104 bool IsValid() const {return (m_pRecord != NULL);};
00105
00107 std::tstring operator[](int nColumn) const;
00109 std::tstring operator[](const std::tstring& sName) const;
00111 std::tstring GetField(int nColumn) const;
00113 std::tstring GetField(const std::tstring& sName) const;
00119 inline int GetNumField(int nColumn) const {return _ttoi(GetField(nColumn).c_str());};
00125 inline int GetNumField(const std::tstring& sName) const {return _ttoi(GetField(sName).c_str());};
00126
00132 const Record& operator=(const Record& other) {if (m_pRecord == other.m_pRecord) return *this; DecRef(); m_pRecord = other.m_pRecord; AddRef(); return *this;};
00133
00134 protected:
00135
00137 void AddRef() const;
00139 void DecRef() const;
00140 };
00141
00142 class Realset;
00143 class RealTransaction;
00144
00148 class Recordset
00149 {
00150 public:
00154 Recordset() : m_pSet(NULL) {};
00159 Recordset(const Recordset& set) : m_pSet(set.m_pSet) {AddRef();};
00163 virtual ~Recordset() {DecRef();};
00164 protected:
00169 Recordset(Realset* pSet) : m_pSet(pSet) {AddRef();};
00170
00171 protected:
00172
00173 friend class RealTransaction;
00174 friend class RealStatement;
00175 friend class DB;
00176
00177 protected:
00178
00180 Realset* m_pSet;
00181
00182 public:
00183
00185 bool IsValid() const;
00186
00188 Record GetRecord(int nNum) const;
00190 int GetRecordCount() const;
00192 int GetColumnCount() const;
00194 std::tstring GetColumnName(int nNum) const;
00195
00199 void clear() {DecRef(); m_pSet = NULL;};
00200
00201 public:
00202
00208 virtual const Recordset& operator=(const Recordset& other) {SetRealset(other.m_pSet); return *this;};
00209
00210 protected:
00211
00213 void AddRef() const;
00215 void DecRef() const;
00216
00221 void SetRealset(Realset* pSet) {if (m_pSet == pSet) return; DecRef(); m_pSet = pSet; AddRef();};
00222 };
00223
00224 class RealStatement;
00225
00229 class Statement
00230 {
00231 public:
00232
00236 Statement() : m_pReal(NULL) {};
00241 Statement(const Statement& statement) : m_pReal(statement.m_pReal) {AddRef();};
00245 virtual ~Statement() {DecRef();};
00246 protected:
00251 Statement(RealStatement* pReal) : m_pReal(pReal) {AddRef();};
00252
00253 protected:
00254
00255 friend class DB;
00256
00257 protected:
00259 RealStatement* m_pReal;
00260
00261 public:
00263 DB GetDB() const;
00265 bool IsValid() const;
00266
00268 bool Execute();
00270 Recordset Query();
00272 bool HasParameter(const TCHAR* pParam) const;
00278 inline bool HasParameter(const std::tstring& sParam) const {return HasParameter(sParam.c_str());};
00279
00283 void clear() {DecRef(); m_pReal = NULL;};
00284
00285
00287 bool BindParameter(const TCHAR* pParam, const TCHAR* pValue);
00294 inline bool BindParameter(const std::tstring& sParam, const std::tstring& sValue) {return BindParameter(sParam.c_str(), sValue.c_str());};
00301 inline bool BindParameter(const std::tstring& sParam, const TCHAR* pValue) {return BindParameter(sParam.c_str(), pValue);};
00308 inline bool BindParameter(const TCHAR* pParam, const std::tstring& sValue) {return BindParameter(pParam, sValue.c_str());};
00309
00310
00312 bool BindParameter(const TCHAR* pParam, int nValue);
00319 inline bool BindParameter(const std::tstring& sParam, int nValue) {return BindParameter(sParam.c_str(), nValue);};
00320
00321
00323 bool BindParameter(int nParam, const TCHAR* pValue);
00330 inline bool BindParameter(int nParam, const std::tstring& sValue) {return BindParameter(nParam, sValue.c_str());};
00331
00333 bool BindParameter(int nParam, int nValue);
00334 #ifdef _DEBUG
00335
00340 bool IsEmptyStatement() const {return (m_pReal == NULL);};
00341 #endif
00342
00344 sqlite_int64 GetLastInsertRowid() const;
00345
00347 std::tstring GetLastError();
00348
00349 public:
00350
00356 virtual const Statement& operator=(const Statement& other) {if (m_pReal == other.m_pReal) return *this; DecRef(); m_pReal = other.m_pReal; AddRef(); return *this;};
00357
00358 protected:
00359
00361 void AddRef() const;
00363 void DecRef() const;
00364 };
00365
00369 class Transaction
00370 {
00371 public:
00372
00377 Transaction(const Transaction& other) : m_pReal(other.m_pReal) {AddRef();};
00381 virtual ~Transaction() {DecRef();};
00382 protected:
00387 Transaction(RealTransaction* pReal) : m_pReal(pReal) {AddRef();};
00388
00389 public:
00390
00391 friend class DB;
00393 typedef enum {TTDeferred, TTImmediate, TTExclusive} TransactionType;
00394
00395 protected:
00397 RealTransaction* m_pReal;
00398
00399 public:
00400
00402 bool Close();
00404 bool Commit();
00406 bool Rollback();
00407
00409 bool Execute(LPCTSTR pSQL);
00415 inline bool Execute(const std::string& sSQL) {return Execute(sSQL.c_str());};
00417 bool Execute(Statement& state);
00419 Recordset Query(LPCTSTR pSQL);
00425 inline Recordset Query(const std::string& sSQL) {return Query(sSQL.c_str());};
00427 Recordset Query(Statement& state);
00428
00430 const std::tstring& GetLastError();
00431
00432 public:
00433
00439 virtual const Transaction& operator=(const Transaction& other) {if (m_pReal == other.m_pReal) return *this; DecRef(); m_pReal = other.m_pReal; AddRef(); return *this;};
00440
00441 protected:
00442
00444 void AddRef() const;
00446 void DecRef() const;
00447 };
00448
00449 class RealDB;
00450
00454 class DB
00455 {
00456 public:
00457
00459 DB(LPCTSTR pFilename = NULL);
00464 DB(const DB& db) : m_pDB(db.m_pDB) {AddRef();};
00468 virtual ~DB() {DecRef();};
00469 protected:
00474 DB(RealDB* pDB) : m_pDB(pDB) {AddRef();};
00475
00476 protected:
00477
00478 friend class RealStatement;
00479
00480 protected:
00481
00483 RealDB* m_pDB;
00484
00485 public:
00486
00488 bool IsOpen() const;
00489
00490 public:
00491
00493 bool Open(LPCTSTR pFilename);
00495 bool Close();
00497 bool Execute(LPCTSTR pSQL);
00503 inline bool Execute(const std::tstring& sSQL) {return Execute(sSQL.c_str());};
00505 Recordset Query(LPCTSTR pSQL);
00511 inline Recordset Query(const std::tstring& sSQL) {return Query(sSQL.c_str());};
00513 Transaction BeginTransaction(Transaction::TransactionType eType, const std::tstring& sName = _T(""));
00515 Statement CreateStatement(LPCTSTR pStatement);
00521 inline Statement CreateStatement(const std::tstring& sStatement) {return CreateStatement(sStatement.c_str());};
00522
00524 std::tstring GetLastError();
00525
00527 sqlite_int64 GetLastInsertRowid() const;
00528
00529 protected:
00530
00532 void AddRef() const;
00534 void DecRef() const;
00535
00536 public:
00537
00543 bool operator!=(const DB& db) const {return (m_pDB != db.m_pDB);};
00544
00545
00547 static std::string printf(const char* pString, ...);
00548 };
00549
00550
00551 };
00552
00553 #endif //#define _SQLITEDB_H_