/
storage.h
197 lines (176 loc) · 7.66 KB
/
storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (C) 2007 Butterfat, LLC (http://butterfat.net)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Created by bmuller <bmuller@butterfat.net>
*/
namespace stactiverecord {
class KVT {
public:
std::string key, svalue;
int ivalue;
coltype type;
KVT(std::string _key, std::string _value) : key(_key), svalue(_value), type(STRING) {};
KVT(std::string _key, int _value) : key(_key), ivalue(_value), type(INTEGER) {};
KVT(std::string _key, coltype ct) : key(_key), type(ct) {};
};
class Row {
public:
SarVector<int> ints;
SarVector<std::string> strings;
SarVector<DateTime> dts;
void operator<<(int i) { ints << i; };
void operator<<(std::string s) { strings << s; };
bool operator!=(Row& other) {
return !(*this == other);
};
bool operator==(Row& other) {
return ints == other.ints && strings == other.strings && dts == other.dts;
};
void get_string(int position, std::string& s) {
if(position > strings.size() - 1)
throw Sar_ColumnNotFoundException("String column not found.");
s = strings[position];
};
int get_int(int position) {
if(position > ints.size() - 1)
throw Sar_ColumnNotFoundException("Int column not found.");
return ints[position];
};
void dump() {
std::cout << "Ints:\n";
ints.dump();
std::cout << "Strings:\n";
strings.dump();
};
};
class Sar_Dbi {
public:
// config is in form scheme://[user[:password]@]host[:port]/database
static Sar_Dbi* makeStorage(std::string config, std::string prefix="");
static Sar_Dbi* dbi;
static SarMap<std::string> parseconfig(std::string config);
std::string table_prefix;
SarVector<std::string> initialized_tables;
Sar_Dbi(std::string prefix = "") : initialized_tables(), table_prefix(prefix) {};
virtual void initialize_tables(std::string classname) = 0;
int next_id(std::string classname);
bool exists(std::string classname, int id);
void make_existing(std::string classname, int id);
int current_id(std::string classname);
void delete_record(int id, std::string classname);
void delete_records(std::string classname);
// get std::string values
void get(int id, std::string classname, SarMap<std::string>& values);
// get int values
void get(int id, std::string classname, SarMap<int>& values);
// get datetime values
void get(int id, std::string classname, SarMap<DateTime>& values);
// get record relations (of a specific class)
void get(int id, std::string classname, std::string related_classname, SarVector<int>& related);
// get all related classes
void get(int id, std::string classname, SarMap< SarVector<int> >& sm);
// insert/modify std::string values
void set(int id, std::string classname, SarMap<std::string> values, bool isinsert);
// insert/modify int values
void set(int id, std::string classname, SarMap<int> values, bool isinsert);
// insert/modify datetime values
void set(int id, std::string classname, SarMap<DateTime> values, bool isinsert);
// insert record relations
void set(int id, std::string classname, SarVector<int> related, std::string related_classname);
// delete std::string / int / datetime value
void del(int id, std::string classname, SarVector<std::string> keys, coltype ct);
// delete record relations
void del(int id, std::string classname, SarVector<int> related, std::string related_classname);
// Some searching/static stuff
// get all objects of a type
void get(std::string classname, SarVector<int>& results);
// using a query with a conditional "where"
void get_where(std::string classname, std::string key, Where * where, SarVector<int>& results);
// Convert a where object to a string value for a query
virtual void where_to_string(Where * where, std::string& swhere);
SarVector<Row> select(std::string table, SarVector<KVT> cols, Q qwhere, bool distinct=false);
virtual SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false) = 0;
void update(std::string table, SarVector<KVT> cols, Q qwhere);
virtual void update(std::string table, SarVector<KVT> cols, std::string where="") = 0;
void remove(std::string table, Q qwhere);
virtual void remove(std::string table, std::string where="") = 0;
virtual void insert(std::string table, SarVector<KVT> cols) = 0;
virtual void execute(std::string query) = 0;
bool table_is_initialized(std::string tablename);
};
#ifdef HAVE_SQLITE3
#include <sqlite3.h>
class SQLiteStorage : public Sar_Dbi {
private:
void close();
sqlite3 *db;
bool is_closed;
void test_result(int result, const std::string& context);
public:
SQLiteStorage(std::string location, std::string prefix="");
~SQLiteStorage() { close(); };
void execute(std::string query);
void initialize_tables(std::string classname);
SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
void update(std::string table, SarVector<KVT> cols, std::string where="");
void remove(std::string table, std::string where="");
void insert(std::string table, SarVector<KVT> cols);
};
#endif
#ifdef HAVE_POSTGRESQL
#include <postgresql/libpq-fe.h>
class PostgreSQLStorage : public Sar_Dbi {
private:
void close();
PGconn *db;
bool is_closed;
void test_result(int result, const std::string& context);
public:
PostgreSQLStorage(std::string location, std::string prefix="");
~PostgreSQLStorage() { close(); };
void execute(std::string query);
void initialize_tables(std::string classname);
SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
void update(std::string table, SarVector<KVT> cols, std::string where="");
void remove(std::string table, std::string where="");
void insert(std::string table, SarVector<KVT> cols);
void where_to_string(Where * where, std::string& swhere);
};
#endif
#ifdef HAVE_MYSQL
#include <mysql/mysql.h>
class MySQLStorage : public Sar_Dbi {
private:
void close();
MYSQL *db;
bool is_closed;
void test_result(int result, const std::string& context);
public:
MySQLStorage(std::string config, std::string prefix);
~MySQLStorage() { close(); };
void execute(std::string query);
void initialize_tables(std::string classname);
SarVector<Row> select(std::string table, SarVector<KVT> cols, std::string where="", bool distinct=false);
void update(std::string table, SarVector<KVT> cols, std::string where="");
void remove(std::string table, std::string where="");
void insert(std::string table, SarVector<KVT> cols);
};
#endif
};