-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSQLite++.cc
424 lines (349 loc) · 8.29 KB
/
SQLite++.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
Public Domain
The author or authors of this code dedicate any and all copyright interest in this code to the public domain.We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors.We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights
to this code under copyright law.
*/
#include <string.h>
#include <string>
#include <stdexcept>
#include <stdio.h>
#include <sqlite3.h>
#include "SQLite++.h"
using namespace sqlite;
using namespace std;
namespace sqlite {
class exception : public std::runtime_error
{
public:
exception(sqlite3 *db):
runtime_error("SQLite error: " + string(sqlite3_errmsg(db)))
{
}
exception(const char * msg):
runtime_error(msg)
{
}
};
}; // namespace sqlite
struct sqlite::database_i
{
sqlite3* connection;
database_i()
{
connection = NULL;
}
};
database::database(const std::string & filename):
implementation(NULL)
{
implementation = new database_i;
if(implementation)
if(sqlite3_open(filename.c_str(), &implementation->connection) != SQLITE_OK)
throw exception(implementation->connection);
}
database::database(const database & db)
{
implementation = new database_i;
if(implementation && db.implementation)
*implementation = *(db.implementation);
}
database::~database()
{
if(implementation)
{
sqlite3_close(implementation->connection);
delete implementation;
}
}
void database::execute(const std::string & sql)
{
statement s(*this, sql);
s.execute();
}
struct sqlite::value_i
{
unsigned int ref;
type value_type;
union
{
long long integer_value;
double real_value;
string *text_value;
};
value_i()
{
value_type = null;
ref = 1;
}
~value_i()
{
if((value_type == text) && text_value)
delete text_value;
}
};
value::value(const std::string & s)
{
implementation = new value_i;
if(implementation)
{
implementation->value_type = text;
implementation->text_value = new string(s);
}
}
value::value(const char * s)
{
implementation = new value_i;
if(implementation)
{
implementation->value_type = text;
implementation->text_value = new string(s);
}
}
value::value(long long l)
{
implementation = new value_i;
if(implementation)
{
implementation->value_type = integer;
implementation->integer_value = l;
}
}
value::value(double d)
{
implementation = new value_i;
if(implementation)
{
implementation->value_type = real;
implementation->real_value = d;
}
}
value::value()
{
implementation = new value_i;
}
value::value(const value & v)
{
implementation = v.implementation;
if(implementation) implementation->ref++;
}
value::~value()
{
if(implementation)
{
implementation->ref--;
if(implementation->ref<=0) delete implementation;
}
}
value & value::operator =(const value & v)
{
if(&v == this) return *this; // self-affectation
if(implementation)
{
implementation->ref--;
if(implementation->ref<=0) delete implementation;
}
implementation = v.implementation;
if(implementation) implementation->ref++;
return *this;
}
type value::getType() const
{
if(implementation)
return implementation->value_type;
else
return null;
}
string value::asText() const
{
if(!implementation || (implementation->value_type == null))
return "";
switch(implementation->value_type)
{
case integer:
char buf[100];
snprintf(buf, sizeof(buf), "%lld", implementation->integer_value);
return string(buf);
case text:
case blob:
return *(implementation->text_value);
default:
return "";
}
throw exception("Can't convert value to text");
}
long long value::asInteger() const
{
if(!implementation || (implementation->value_type == null))
return 0;
switch(implementation->value_type)
{
case integer:
return implementation->integer_value;
case real:
return implementation->real_value;
default:
return 0;
}
throw exception("Can't convert value to integer");
}
double value::asReal() const
{
if(!implementation || (implementation->value_type == null))
return 0;
switch(implementation->value_type)
{
case integer:
return implementation->integer_value;
case real:
return implementation->real_value;
default:
return 0;
}
throw exception("Can't convert value to real");
}
ostream & value::print(std::ostream & out) const
{
if(!implementation || (implementation->value_type == null))
out << "NULL";
else
switch(implementation->value_type)
{
case integer:
out << implementation->integer_value;
break;
case real:
out << implementation->real_value;
break;
case text:
out << *(implementation->text_value);
break;
case blob:
out << "BLOB";
break;
default:
out << "??";
}
return out;
}
struct sqlite::statement_i
{
database *db;
sqlite3_stmt *stmt;
int index;
};
statement::statement(database & db, const std::string & s)
{
implementation = new statement_i;
if(!implementation)
throw exception("memory exhausted");
implementation->db = &db;
if(!db.implementation)
throw exception("invalid database");
if(sqlite3_prepare(db.implementation->connection, s.c_str(), -1, &implementation->stmt, NULL) != SQLITE_OK)
throw exception(db.implementation->connection);
implementation->index = 0;
}
statement::~statement()
{
if(implementation)
{
if(implementation->stmt)
sqlite3_finalize(implementation->stmt);
delete implementation;
}
}
void statement::bind(int index, const value & v)
{
if(!implementation)
throw exception("undefined statement");
switch(v.getType())
{
case null:
if(sqlite3_bind_null(implementation->stmt, index) != SQLITE_OK)
throw exception(implementation->db->implementation->connection);
break;
case text:
if(sqlite3_bind_text(implementation->stmt, index, v.asText().c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
throw exception(implementation->db->implementation->connection);
break;
case blob:
{
string blob = v.asText();
if(sqlite3_bind_text(implementation->stmt, index, blob.data(), blob.size(), SQLITE_TRANSIENT) != SQLITE_OK)
throw exception(implementation->db->implementation->connection);
break;
}
case integer:
if(sqlite3_bind_int64(implementation->stmt, index, v.asInteger()) != SQLITE_OK)
throw exception(implementation->db->implementation->connection);
break;
case real:
if(sqlite3_bind_double(implementation->stmt, index, v.asReal()) != SQLITE_OK)
throw exception(implementation->db->implementation->connection);
break;
default:
throw exception("unknown type");
}
implementation->index = index;
}
void statement::bind(const value & v)
{
if(!implementation)
throw exception("undefined statement");
implementation->index++;
bind(implementation->index, v);
}
bool statement::step()
{
if(!implementation)
throw exception("undefined statement");
int result = sqlite3_step(implementation->stmt);
if(result == SQLITE_DONE) return false;
if(result == SQLITE_ROW) return true;
throw exception(implementation->db->implementation->connection);
}
void statement::execute()
{
while(step());
}
int statement::columns() const
{
if(!implementation)
throw exception("undefined statement");
return sqlite3_column_count(implementation->stmt);
}
value statement::column(unsigned int i) const
{
if(!implementation)
throw exception("undefined statement");
switch(sqlite3_column_type(implementation->stmt, i))
{
case SQLITE_INTEGER:
return value(sqlite3_column_int64(implementation->stmt, i));
case SQLITE_FLOAT:
return value(sqlite3_column_double(implementation->stmt, i));
case SQLITE_TEXT:
return value((const char*)sqlite3_column_text(implementation->stmt, i));
case SQLITE_BLOB:
case SQLITE_NULL:
default:
return value();
}
}
value statement::column(const string & name) const
{
if(!implementation)
throw exception("undefined statement");
for(int i=0; i<columns(); i++)
if(strcasecmp(sqlite3_column_name(implementation->stmt, i), name.c_str())==0)
return column(i);
return value();
}
value statement::operator[](unsigned int i) const
{
return column(i);
}
value statement::operator[](const string & i) const
{
return column(i);
}