00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00032
00033 #ifdef HAVE_SQLITE3
00034
00035 #ifndef __SQLITE3_STORAGE_H__
00036 #define __SQLITE3_STORAGE_H__
00037
00038 #include <sqlite3.h>
00039
00040 #include "storage/sql_storage.h"
00041 #include "sync.h"
00042 #include "timer.h"
00043
00044 class Sqlite3Storage;
00045 class Sqlite3Result;
00046
00048 class SLTask : public zmm::Object
00049 {
00050 public:
00052 SLTask();
00053
00056 virtual void run(sqlite3 **db, Sqlite3Storage *sl) = 0;
00057
00060 bool is_running();
00061
00063 void sendSignal();
00064
00065 void sendSignal(zmm::String error);
00066
00067 void waitForTask();
00068
00069 bool didContamination() { return contamination; }
00070 bool didDecontamination() { return decontamination; }
00071
00072 zmm::String getError() { return error; }
00073
00074 protected:
00078 bool running;
00079
00081 bool contamination;
00082
00084 bool decontamination;
00085
00086 zmm::Ref<Cond> cond;
00087 zmm::Ref<Mutex> mutex;
00088 zmm::String error;
00089 };
00090
00091 #ifdef AUTO_CREATE_DATABASE
00093 class SLInitTask : public SLTask
00094 {
00095 public:
00097 SLInitTask() : SLTask() {}
00098 virtual void run(sqlite3 **db, Sqlite3Storage *sl);
00099 };
00100 #endif
00101
00102
00104 class SLSelectTask : public SLTask
00105 {
00106 public:
00109 SLSelectTask(const char *query);
00110 virtual void run(sqlite3 **db, Sqlite3Storage *sl);
00111 inline zmm::Ref<SQLResult> getResult() { return RefCast(pres, SQLResult); };
00112 protected:
00114 const char *query;
00116 zmm::Ref<Sqlite3Result> pres;
00117 };
00118
00120 class SLExecTask : public SLTask
00121 {
00122 public:
00125 SLExecTask(const char *query, bool getLastInsertId);
00126 virtual void run(sqlite3 **db, Sqlite3Storage *sl);
00127 inline int getLastInsertId() { return lastInsertId; }
00128 protected:
00130 const char *query;
00131
00132 int lastInsertId;
00133 bool getLastInsertIdFlag;
00134 };
00135
00137 class SLBackupTask : public SLTask
00138 {
00139 public:
00141 SLBackupTask(bool restore) { this->restore = restore; };
00142 virtual void run(sqlite3 **db, Sqlite3Storage *sl);
00143 protected:
00144 bool restore;
00145 };
00146
00148 class Sqlite3Storage : private SQLStorage
00149 {
00150 private:
00151 Sqlite3Storage();
00152 friend zmm::Ref<Storage> Storage::createInstance();
00153 virtual void init();
00154 virtual void shutdownDriver();
00155
00156 virtual zmm::String quote(zmm::String str);
00157 virtual inline zmm::String quote(int val) { return zmm::String::from(val); }
00158 virtual inline zmm::String quote(unsigned int val) { return zmm::String::from(val); }
00159 virtual inline zmm::String quote(long val) { return zmm::String::from(val); }
00160 virtual inline zmm::String quote(unsigned long val) { return zmm::String::from(val); }
00161 virtual inline zmm::String quote(bool val) { return zmm::String(val ? '1' : '0'); }
00162 virtual inline zmm::String quote(char val) { return quote(zmm::String(val)); }
00163 virtual zmm::Ref<SQLResult> select(const char *query, int length);
00164 virtual int exec(const char *query, int length, bool getLastInsertId = false);
00165 virtual void storeInternalSetting(zmm::String key, zmm::String value);
00166
00167 void _exec(const char *query);
00168
00169 zmm::String startupError;
00170
00171 zmm::String getError(zmm::String query, zmm::String error, sqlite3 *db);
00172
00173 static void *staticThreadProc(void *arg);
00174 void threadProc();
00175
00176 void addTask(zmm::Ref<SLTask> task, bool onlyIfDirty = false);
00177
00178 pthread_t sqliteThread;
00179 zmm::Ref<Cond> cond;
00180 zmm::Ref<Mutex> sqliteMutex;
00181
00183 bool shutdownFlag;
00184
00186 zmm::Ref<zmm::ObjectQueue<SLTask> > taskQueue;
00187 bool taskQueueOpen;
00188
00189 virtual void threadCleanup() {}
00190 virtual bool threadCleanupRequired() { return false; }
00191
00192 inline void signal() { cond->signal(); }
00193
00194 zmm::Ref<zmm::StringBuffer> insertBuffer;
00195 virtual void _addToInsertBuffer(zmm::Ref<zmm::StringBuffer> query);
00196 virtual void _flushInsertBuffer();
00197
00198 bool dirty;
00199
00200 friend class SLSelectTask;
00201 friend class SLExecTask;
00202 friend class SLInitTask;
00203 friend class Sqlite3BackupTimerSubscriber;
00204 };
00205
00207 class Sqlite3Result : public SQLResult
00208 {
00209 private:
00210 Sqlite3Result();
00211 virtual ~Sqlite3Result();
00212 virtual zmm::Ref<SQLRow> nextRow();
00213 virtual unsigned long long getNumRows() { return nrow; }
00214
00215 char **table;
00216 char **row;
00217
00218 int cur_row;
00219
00220 int nrow;
00221 int ncolumn;
00222
00223 friend class SLSelectTask;
00224 friend class Sqlite3Row;
00225 friend class Sqlite3Storage;
00226 };
00227
00229 class Sqlite3Row : public SQLRow
00230 {
00231 private:
00232 Sqlite3Row(char **row, zmm::Ref<SQLResult> sqlResult);
00233 inline virtual char* col_c_str(int index) { return row[index]; }
00234 char **row;
00235 zmm::Ref<Sqlite3Result> res;
00236
00237 friend class Sqlite3Result;
00238 };
00239
00240 class Sqlite3BackupTimerSubscriber : public TimerSubscriberObject
00241 {
00243 virtual void timerNotify(zmm::Ref<zmm::Object> sqlite3storage);
00244 };
00245
00246 #endif // __SQLITE3_STORAGE_H__
00247
00248 #endif // HAVE_SQLITE3