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 protected:
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 virtual Enumeration & query(const StringBuffer & sql) const;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 virtual int execute(const StringBuffer & sql);
00087
00088 virtual bool enumeration_hasMoreElement()
00089 {
00090 return (statement && enumeration_lastReturn == SQLITE_ROW && enumeration_nextRow < enumeration_totalRows);
00091 }
00092
00093 virtual ArrayElement* enumeration_getNextElement()
00094 {
00095 StringBuffer sb;
00096 sb = (const char *) sqlite3_column_text(statement, 0);
00097 enumeration_kvp.setKey (sb);
00098 sb = (const char *) sqlite3_column_text(statement, 1);
00099 enumeration_kvp.setValue (sb);
00100
00101 enumeration_lastReturn = sqlite3_step(statement);
00102 enumeration_nextRow++;
00103
00104 return &enumeration_kvp;
00105 }
00106
00107 public:
00108
00109 mutable
00110 class SQLiteKeyValueStoreEnumeration : public Enumeration
00111 {
00112 protected:
00113 friend class SQLiteKeyValueStore;
00114
00115 SQLiteKeyValueStore & skvs;
00116
00117 void reinit(int lastRet)
00118 {
00119 skvs.enumeration_lastReturn = lastRet;
00120 skvs.enumeration_totalRows = 1;
00121 skvs.enumeration_nextRow = 0;
00122 }
00123
00124 void setTotalRows(int tr)
00125 {
00126 skvs.enumeration_totalRows = tr;
00127 }
00128
00129
00130 public:
00131
00132 SQLiteKeyValueStoreEnumeration(SQLiteKeyValueStore & instance)
00133 : skvs(instance) {}
00134
00138 virtual bool hasMoreElement() const
00139 {
00140 return skvs.enumeration_hasMoreElement();
00141 }
00142
00146 virtual ArrayElement* getNextElement()
00147 {
00148 return skvs.enumeration_getNextElement();
00149 }
00150
00151 } enumeration;
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 SQLiteKeyValueStore(const StringBuffer & table, const StringBuffer & colKey, const StringBuffer & colValue,
00164 const StringBuffer & path);
00165
00166
00167
00168
00169
00170
00171 virtual ~SQLiteKeyValueStore();
00172
00176 virtual Enumeration& getProperties() const;
00177
00178
00179
00180
00181
00182
00183
00184
00185 int connect();
00186
00187
00188
00189
00190
00191
00192
00193 int disconnect();
00194
00202 virtual int save();
00203 };
00204
00205
00206 END_NAMESPACE
00207
00210 #endif