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
00030
00031
00032
00033
00034
00035
00036 #ifndef INCL_SQLLITE_KEY_VALUE_STORE
00037 #define INCL_SQLLITE_KEY_VALUE_STORE
00038
00042 #include <sqlite3.h>
00043
00044 #include "base/util/KeyValueStore.h"
00045 #include "base/util/KeyValuePair.h"
00046 #include "client/SQLKeyValueStore.h"
00047
00048 BEGIN_NAMESPACE
00049
00050 class SQLiteKeyValueStore : public SQLKeyValueStore {
00051 private:
00052
00053 StringBuffer path;
00054
00055 sqlite3 * db;
00056 mutable sqlite3_stmt * statement;
00057
00058
00059
00060 int enumeration_lastReturn;
00061 int enumeration_totalRows;
00062 int enumeration_nextRow;
00063 KeyValuePair enumeration_kvp;
00064
00065 bool isTransactional;
00066
00067 protected:
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 virtual Enumeration & query(const StringBuffer & sql) const;
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 virtual int execute(const StringBuffer & sql);
00089
00090
00091
00092 virtual bool enumeration_hasMoreElement()
00093 {
00094 return (statement && enumeration_lastReturn == SQLITE_ROW && enumeration_nextRow < enumeration_totalRows);
00095 }
00096
00097 virtual ArrayElement* enumeration_getNextElement()
00098 {
00099 StringBuffer sb;
00100 sb = (const char *) sqlite3_column_text(statement, 0);
00101 enumeration_kvp.setKey (sb);
00102 sb = (const char *) sqlite3_column_text(statement, 1);
00103 enumeration_kvp.setValue (sb);
00104
00105 enumeration_lastReturn = sqlite3_step(statement);
00106 enumeration_nextRow++;
00107
00108 return &enumeration_kvp;
00109 }
00110
00111 public:
00112
00113 mutable
00114 class SQLiteKeyValueStoreEnumeration : public Enumeration
00115 {
00116 protected:
00117 friend class SQLiteKeyValueStore;
00118
00119 SQLiteKeyValueStore & skvs;
00120
00121 void reinit(int lastRet)
00122 {
00123 skvs.enumeration_lastReturn = lastRet;
00124 skvs.enumeration_totalRows = 1;
00125 skvs.enumeration_nextRow = 0;
00126 }
00127
00128 void setTotalRows(int tr)
00129 {
00130 skvs.enumeration_totalRows = tr;
00131 }
00132
00133
00134 public:
00135
00136 SQLiteKeyValueStoreEnumeration(SQLiteKeyValueStore & instance)
00137 : skvs(instance) {}
00138
00142 virtual bool hasMoreElement() const
00143 {
00144 return skvs.enumeration_hasMoreElement();
00145 }
00146
00150 virtual ArrayElement* getNextElement()
00151 {
00152 return skvs.enumeration_getNextElement();
00153 }
00154
00155 virtual ArrayElement* getFirstElement()
00156 {
00157 return skvs.enumeration_getNextElement();
00158 }
00159
00160 } enumeration;
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 SQLiteKeyValueStore(const StringBuffer & table, const StringBuffer & colKey, const StringBuffer & colValue,
00174 const StringBuffer & path, bool isTransactional = false);
00175
00176
00177
00178
00179
00180
00181 virtual ~SQLiteKeyValueStore();
00182
00186 virtual Enumeration& getProperties();
00187
00188
00189
00190
00191
00192
00193
00194
00195 int connect();
00196
00197
00198
00199
00200
00201
00202
00203 int disconnect();
00204
00212 virtual int close();
00213
00219 int init(const char* table, const char* colKey, const char* colValue, const char* path);
00220
00228 bool checkIfTableExists(const char* tableName);
00229 };
00230
00231
00232 END_NAMESPACE
00233
00236 #endif