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 namespace stactiverecord {
00029
00030 class KVT {
00031 public:
00032 std::string key, svalue;
00033 int ivalue;
00034 coltype type;
00035 KVT(std::string _key, std::string _value) : key(_key), svalue(_value), type(STRING) {};
00036 KVT(std::string _key, int _value) : key(_key), ivalue(_value), type(INTEGER) {};
00037 KVT(std::string _key, coltype ct) : key(_key), type(ct) {};
00038 };
00039
00040 class Row {
00041 public:
00042 SarVector<int> ints;
00043 SarVector<std::string> strings;
00044 SarVector<DateTime> dts;
00045 void operator<<(int i) { ints << i; };
00046 void operator<<(std::string s) { strings << s; };
00047 bool operator!=(Row& other) {
00048 return !(*this == other);
00049 };
00050 bool operator==(Row& other) {
00051 return ints == other.ints && strings == other.strings && dts == other.dts;
00052 };
00053 void get_string(int position, std::string& s) {
00054 if(position > strings.size() - 1)
00055 throw Sar_ColumnNotFoundException("String column not found.");
00056 s = strings[position];
00057 };
00058 int get_int(int position) {
00059 if(position > ints.size() - 1)
00060 throw Sar_ColumnNotFoundException("Int column not found.");
00061 return ints[position];
00062 };
00063 void dump() {
00064 std::cout << "Ints:\n";
00065 ints.dump();
00066 std::cout << "Strings:\n";
00067 strings.dump();
00068 };
00069 };
00070
00071
00072 class Sar_Dbi {
00073 public:
00074
00075 static Sar_Dbi* makeStorage(std::string config, std::string prefix="");
00076 static Sar_Dbi* dbi;
00077 static SarMap<std::string> parseconfig(std::string config);
00078 std::string table_prefix;
00079 SarVector<std::string> initialized_tables;
00080 Sar_Dbi(std::string prefix = "") : initialized_tables(), table_prefix(prefix) {};
00081
00082 virtual void initialize_tables(std::string classname) = 0;
00083 int next_id(std::string classname);
00084 bool exists(std::string classname, int id);
00085 void make_existing(std::string classname, int id);
00086 int current_id(std::string classname);
00087 void delete_record(int id, std::string classname);
00088 void delete_records(std::string classname);
00089
00090
00091 void get(int id, std::string classname, SarMap<std::string>& values);
00092
00093 void get(int id, std::string classname, SarMap<int>& values);
00094
00095 void get(int id, std::string classname, SarMap<DateTime>& values);
00096
00097 void get(int id, std::string classname, std::string related_classname, SarVector<int>& related);
00098
00099 void get(int id, std::string classname, SarMap< SarVector<int> >& sm);
00100
00101
00102 void set(int id, std::string classname, SarMap<std::string> values, bool isinsert);
00103
00104 void set(int id, std::string classname, SarMap<int> values, bool isinsert);
00105
00106 void set(int id, std::string classname, SarMap<DateTime> values, bool isinsert);
00107
00108 void set(int id, std::string classname, SarVector<int> related, std::string related_classname);
00109
00110
00111 void del(int id, std::string classname, SarVector<std::string> keys, coltype ct);
00112
00113 void del(int id, std::string classname, SarVector<int> related, std::string related_classname);
00114
00115
00116
00117 void get(std::string classname, SarVector<int>& results);
00118
00119 void get_where(std::string classname, std::string key, Where * where, SarVector<int>& results);
00120
00121
00122 virtual void where_to_string(Where * where, std::string& swhere);
00123
00124 SarVector<Row> select(std::string table, SarVector<KVT> cols, Q qwhere, bool distinct=false);
00125 virtual SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false) = 0;
00126 void update(std::string table, SarVector<KVT> cols, Q qwhere);
00127 virtual void update(std::string table, SarVector<KVT> cols, std::string where="") = 0;
00128 void remove(std::string table, Q qwhere);
00129 virtual void remove(std::string table, std::string where="") = 0;
00130 virtual void insert(std::string table, SarVector<KVT> cols) = 0;
00131 virtual void execute(std::string query) = 0;
00132
00133 bool table_is_initialized(std::string tablename);
00134 };
00135
00136 #ifdef HAVE_SQLITE3
00137 #include <sqlite3.h>
00138 class SQLiteStorage : public Sar_Dbi {
00139 private:
00140 void close();
00141 sqlite3 *db;
00142 bool is_closed;
00143 void test_result(int result, const std::string& context);
00144 public:
00145 SQLiteStorage(std::string location, std::string prefix="");
00146 ~SQLiteStorage() { close(); };
00147 void execute(std::string query);
00148 void initialize_tables(std::string classname);
00149 SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
00150 void update(std::string table, SarVector<KVT> cols, std::string where="");
00151 void remove(std::string table, std::string where="");
00152 void insert(std::string table, SarVector<KVT> cols);
00153 };
00154 #endif
00155
00156 #ifdef HAVE_POSTGRESQL
00157 #include <postgresql/libpq-fe.h>
00158 class PostgreSQLStorage : public Sar_Dbi {
00159 private:
00160 void close();
00161 PGconn *db;
00162 bool is_closed;
00163 void test_result(int result, const std::string& context);
00164 public:
00165 PostgreSQLStorage(std::string location, std::string prefix="");
00166 ~PostgreSQLStorage() { close(); };
00167 void execute(std::string query);
00168 void initialize_tables(std::string classname);
00169 SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
00170 void update(std::string table, SarVector<KVT> cols, std::string where="");
00171 void remove(std::string table, std::string where="");
00172 void insert(std::string table, SarVector<KVT> cols);
00173 void where_to_string(Where * where, std::string& swhere);
00174 };
00175 #endif
00176
00177 #ifdef HAVE_MYSQL
00178 #include <mysql/mysql.h>
00179 class MySQLStorage : public Sar_Dbi {
00180 private:
00181 void close();
00182 MYSQL *db;
00183 bool is_closed;
00184 void test_result(int result, const std::string& context);
00185 public:
00186 MySQLStorage(std::string config, std::string prefix);
00187 ~MySQLStorage() { close(); };
00188 void execute(std::string query);
00189 void initialize_tables(std::string classname);
00190 SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
00191 void update(std::string table, SarVector<KVT> cols, std::string where="");
00192 void remove(std::string table, std::string where="");
00193 void insert(std::string table, SarVector<KVT> cols);
00194 };
00195 #endif
00196
00197 };