forked from dustin/kvtest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite-base.cc
208 lines (176 loc) · 5.54 KB
/
sqlite-base.cc
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
198
199
200
201
202
203
204
205
206
207
208
#include <string.h>
#include "sqlite-base.hh"
#define MAX_STEPS 10000
namespace kvtest {
PreparedStatement::PreparedStatement(sqlite3 *d, const char *query) {
db = d;
if(sqlite3_prepare_v2(db, query, strlen(query), &st, NULL)
!= SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(db));
}
}
PreparedStatement::~PreparedStatement() {
sqlite3_finalize(st);
}
void PreparedStatement::bind(int pos, const char *s) {
sqlite3_bind_text(st, pos, s, strlen(s), SQLITE_TRANSIENT);
}
int PreparedStatement::execute() {
int steps_run = 0, rc = 0;
while ((rc = sqlite3_step(st)) != SQLITE_DONE) {
steps_run++;
assert(steps_run < MAX_STEPS);
}
return sqlite3_changes(db);
}
bool PreparedStatement::fetch() {
bool rv = true;
assert(st);
switch(sqlite3_step(st)) {
case SQLITE_BUSY:
throw std::runtime_error("DB was busy.");
break;
case SQLITE_ROW:
break;
case SQLITE_DONE:
rv = false;
break;
default:
throw std::runtime_error("Unhandled case.");
}
return rv;
}
const char *PreparedStatement::column(int x) {
return (char*)sqlite3_column_text(st, x);
}
void PreparedStatement::reset() {
if(sqlite3_reset(st) != SQLITE_OK) {
throw std::runtime_error("Error resetting statement.");
}
}
BaseSqlite3::BaseSqlite3(const char *fn) {
filename = fn;
db = NULL;
open();
}
BaseSqlite3::~BaseSqlite3() {
close();
}
void BaseSqlite3::open() {
if(!db) {
if(sqlite3_open(filename, &db) != SQLITE_OK) {
throw std::runtime_error("Error initializing sqlite3");
}
intransaction = false;
initTables();
initStatements();
}
}
void BaseSqlite3::close() {
if(db) {
intransaction = false;
destroyStatements();
sqlite3_close(db);
db = NULL;
}
}
void BaseSqlite3::reset() {
if(db) {
rollback();
close();
open();
destroyTables();
initTables();
execute("vacuum");
}
}
void BaseSqlite3::begin() {
if(!intransaction) {
execute("begin");
intransaction = true;
}
}
void BaseSqlite3::commit() {
if(intransaction) {
intransaction = false;
execute("commit");
}
}
void BaseSqlite3::rollback() {
if(intransaction) {
intransaction = false;
execute("rollback");
}
}
void BaseSqlite3::execute(const char *query) {
PreparedStatement st(db, query);
st.execute();
}
// Sqlite3 naive class.
void Sqlite3::initStatements() {
ins_stmt = new PreparedStatement(db, "insert into kv(k,v) values(?, ?)");
sel_stmt = new PreparedStatement(db, "select v from kv where k = ?");
del_stmt = new PreparedStatement(db, "delete from kv where k = ?");
}
void Sqlite3::destroyStatements() {
delete ins_stmt;
delete sel_stmt;
delete del_stmt;
ins_stmt = sel_stmt = del_stmt = NULL;
}
void Sqlite3::initTables() {
execute("create table if not exists kv"
" (k varchar(250) primary key on conflict replace,"
" v text)");
if(auditable) {
execute("create table if not exists history ("
" id integer primary key autoincrement,"
" op char(1) not null,"
" key varchar(250) not null,"
" value text null)");
execute("create trigger if not exists on_audit_insert"
" before insert on kv for each row begin"
" insert into history (op,key,value)"
" values ('s', new.k, new.v);"
" end");
execute("create trigger if not exists on_audit_delete"
" before delete on kv for each row begin"
" insert into history (op,key)"
" values ('d', old.k);"
" end");
}
}
void Sqlite3::destroyTables() {
execute("drop table if exists kv");
execute("drop table if exists history");
execute("drop trigger if exists on_audit_insert");
execute("drop trigger if exists on_audit_delete");
}
void Sqlite3::set(std::string &key, std::string &val,
kvtest::Callback<bool> &cb) {
ins_stmt->bind(1, key.c_str());
ins_stmt->bind(2, val.c_str());
bool rv = ins_stmt->execute() == 1;
cb.callback(rv);
ins_stmt->reset();
}
void Sqlite3::get(std::string &key, kvtest::Callback<kvtest::GetValue> &cb) {
sel_stmt->bind(1, key.c_str());
if(sel_stmt->fetch()) {
std::string str(sel_stmt->column(0));
kvtest::GetValue rv(str, true);
cb.callback(rv);
} else {
std::string str(":(");
kvtest::GetValue rv(str, false);
cb.callback(rv);
}
sel_stmt->reset();
}
void Sqlite3::del(std::string &key, kvtest::Callback<bool> &cb) {
del_stmt->bind(1, key.c_str());
bool rv = del_stmt->execute() == 1;
cb.callback(rv);
del_stmt->reset();
}
}