-
Notifications
You must be signed in to change notification settings - Fork 3
/
libsqlite.hpp
245 lines (229 loc) · 6.02 KB
/
libsqlite.hpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Copyrights:
* Netlogic Belgium:
* Ruben De Smet
*
* this file is licensed under the terms stated in the LICENCE file
*
*/
#include <sqlite3.h>
#include <string>
#include <vector>
#include <exception>
#include <memory>
#ifndef SQLITEPP
#define SQLITEPP
namespace sqlite
{
class sqlite;
class statement;
typedef std::shared_ptr<statement> statement_ptr;
typedef std::shared_ptr<sqlite> sqlite_ptr;
class exception : public std::exception
{
friend statement;
friend sqlite;
public:
exception(std::string msg)
{
this->_msg = msg;
}
std::string what()
{
return "Sqlite error: " + this->_msg;
}
private:
void _set_errorno(int err)
{
}
std::string _msg;
};
class statement
{
friend sqlite;
friend exception;
public:
void set_sql(std::string sql)
{
if(this->_prepared)
{
exception e("Can not set sql on prepared query.");
throw e;
}
else
{
this->_sql = sql;
}
}
void prepare()
{
this->_prepared = true;
const char* tail;
int rc = sqlite3_prepare_v2(this->_db,
this->_sql.c_str(),
this->_sql.length(),
&this->_s,
&tail);
if(rc != SQLITE_OK)
{
exception e("Could not prepare sql.");
e._set_errorno(rc);
throw e;
}
this->_tail = std::string(tail);
}
bool step()
{
if(!this->_valid)
{
exception e("Trying to step an invalid statement.");
}
int rc = sqlite3_step(this->_s);
if(rc == SQLITE_DONE)
{
this->_valid = false;
return false;
}
if(rc == SQLITE_ROW)
{
this->_has_row = true;
return true;
}
// Ok, this means error if we get here
exception e("Sqlite had an error: " + std::string(sqlite3_errmsg(this->_db)));
return false;
}
void reset()
{
int rc = sqlite3_reset(this->_s);
if(rc != SQLITE_OK)
{
exception e("Could not reset the virtual machine.");
throw e;
}
this->_valid = true;
this->_has_row = false;
this->_prepared = false;
}
void exec()
{
this->prepare();
this->step();
}
double get_double(int fieldnumber)
{
return sqlite3_column_double(this->_s, fieldnumber);
}
int get_int(int fieldnumber)
{
return sqlite3_column_int(this->_s, fieldnumber);
}
std::string get_text(int fieldnumber)
{
return std::string((const char*)sqlite3_column_text(this->_s, fieldnumber));
}
std::string get_blob(int fieldnumber)
{
return std::string((const char*)sqlite3_column_blob(this->_s, fieldnumber),
sqlite3_column_bytes(this->_s, fieldnumber));
}
void bind(int where, std::string text)
{
int rc = sqlite3_bind_text(this->_s, where, text.c_str(), text.length(), SQLITE_STATIC);
if(rc != SQLITE_OK)
{
exception e("Could not bind text.");
throw e;
}
}
void bind(int where, double d)
{
int rc = sqlite3_bind_double(this->_s, where, d);
if(rc != SQLITE_OK)
{
exception e("Could not bind double.");
throw e;
}
}
void bind(int where, int i)
{
int rc = sqlite3_bind_int(this->_s, where, i);
if(rc != SQLITE_OK)
{
exception e("Could not bind int.");
throw e;
}
}
void bind_null(int where)
{
int rc = sqlite3_bind_null(this->_s, where);
if(rc != SQLITE_OK)
{
exception e("Could not bind to NULL.");
throw e;
}
}
virtual ~statement()
{
sqlite3_finalize(this->_s);
}
private:
statement(sqlite3* db)
{
this->_db = db;
this->_prepared = false;
this->_valid = true;
this->_has_row = false;
}
statement(sqlite3* db, std::string sql)
{
this->_db = db;
this->_prepared = false;
this->_valid = true;
this->_has_row = false;
this->set_sql(sql);
}
sqlite3* _db;
bool _prepared, _valid, _has_row;
std::string _sql;
std::string _tail;
sqlite3_stmt* _s;
};
class sqlite
{
friend statement;
public:
sqlite(std::string filename) throw (exception)
{
this->_filename = filename;
int rc = sqlite3_open(filename.c_str(), &this->_db);
if(rc != SQLITE_OK)
{
exception e("Could not open '" + filename + "'");
throw e;
}
}
std::shared_ptr<statement> get_statement()
{
statement_ptr st(new statement(this->_db));
return st;
}
statement_ptr get_statement(std::string sql)
{
statement_ptr st(new statement(this->_db, sql));
return st;
}
int64_t last_insert_id()
{
return sqlite3_last_insert_rowid(this->_db);
}
virtual ~sqlite()
{
sqlite3_close(this->_db);
}
private:
std::string _filename;
sqlite3* _db;
};
}
#endif