SQLiteInterface.h
См. документацию.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00023
00024 #ifndef SQLiteInterface_H_
00025 #define SQLiteInterface_H_
00026
00027 #include <string>
00028 #include <list>
00029 #include <vector>
00030 #include <iostream>
00031 #include <sqlite3.h>
00032 #include "PassiveTimer.h"
00033
00034 class SQLiteResult;
00035
00080
00081
00082
00083
00084
00085
00086 class SQLiteInterface
00087 {
00088 public:
00089
00090 SQLiteInterface();
00091 ~SQLiteInterface();
00092
00093 bool connect( const std::string& dbfile, bool create = false );
00094 bool close();
00095 bool isConnection();
00096 bool ping();
00097
00098 void setOperationTimeout( timeout_t msec );
00099 inline timeout_t getOperationTimeout(){ return opTimeout; }
00100
00101 inline void setOperationCheckPause( timeout_t msec ){ opCheckPause = msec; }
00102 inline timeout_t getOperationCheckPause(){ return opCheckPause; }
00103
00104 SQLiteResult query( const std::string& q );
00105 const std::string lastQuery();
00106
00107 bool insert( const std::string& q );
00108 int insert_id();
00109
00110 std::string error();
00111
00112 protected:
00113
00114 bool wait( sqlite3_stmt* stmt, int result );
00115 static bool checkResult( int rc );
00116
00117 private:
00118
00119 sqlite3* db;
00120
00121
00122 std::string lastQ;
00123 std::string lastE;
00124 bool queryok;
00125 bool connected;
00126
00127 timeout_t opTimeout;
00128 timeout_t opCheckPause;
00129 };
00130
00131 class SQLiteResult
00132 {
00133 public:
00134 SQLiteResult(){}
00135 SQLiteResult( sqlite3_stmt* s, bool finalize=true );
00136 ~SQLiteResult();
00137
00138 typedef std::vector<std::string> COL;
00139 typedef std::list<COL> ROW;
00140
00141 typedef ROW::iterator iterator;
00142
00143 inline iterator begin(){ return res.begin(); }
00144 inline iterator end(){ return res.end(); }
00145
00146 inline operator bool(){ return !res.empty(); }
00147
00148 inline int size(){ return res.size(); }
00149 inline bool empty(){ return res.empty(); }
00150
00151 protected:
00152
00153 ROW res;
00154 };
00155
00156 int num_cols( SQLiteResult::iterator& );
00157
00158 int as_int( SQLiteResult::iterator&, int col );
00159 double as_double( SQLiteResult::iterator&, int col );
00160 std::string as_string( SQLiteResult::iterator&, int col );
00161
00162
00163 int as_int( SQLiteResult::COL::iterator& );
00164 double as_double( SQLiteResult::COL::iterator& );
00165 std::string as_string( SQLiteResult::COL::iterator& );
00166
00167 #endif
00168