-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdsqlite.h
464 lines (392 loc) · 14.5 KB
/
sdsqlite.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
///
/// Author: Matt Giger
/// http://www.superdeadly.com
/// Copyright (c) 2009 Super Deadly Software, LLC.
///
/// License:
/// 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, dis-
/// tribute, sublicense, and/or sell copies of the Software, and to permit
/// persons to whom the Software is furnished to do so, subject to the fol-
/// lowing 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 MERCHANTABIL-
/// ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
/// SHALL THE AUTHOR 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.
///
#ifndef _sdsqlite_
#define _sdsqlite_
#include <string>
#include <istream>
#include <ostream>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <cstring>
#include <cstdlib>
#include "sqlite3.h"
namespace sd
{
//////////////////////////////////////////////////////////////////////
///
/// @class sqlite
///
/// Sqlite database instance.
///
//////////////////////////////////////////////////////////////////////
struct sqlite
{
///
/// Empty constructor.
sqlite(void);
///
/// @param path Path to sqlite database file
///
/// Construct a database instance to the specified file.
sqlite(const std::string& path);
///
/// Destructor closes current database connection.
~sqlite(void);
///
/// @return Sqlite handle to database
///
/// Return the internal sqlite handle to the database.
sqlite3* handle(void) { return db_; }
///
/// @return True if the database is open on a valid instance
///
/// Is this database available for querying.
bool is_open(void) { return db_ != 0; }
///
/// @return Integer value of the last "ROWID" for the previous insert statement
///
/// Sometimes it is convenient to have the internal sqlite ROWID for the previous insert
/// statement so you can perform other operations on the just inserted row.
int last_rowid(void) { return static_cast<int>(sqlite3_last_insert_rowid(db_)); }
///
/// @param path Path to sqlite database file
///
/// Close any previous connection and open a new connection to the
/// sqlite database in the specified file.
void open(const std::string& path);
///
/// Close the connection to the current database.
void close(void);
///
/// Begin a database transaction.
void begin(void);
///
/// Commit the current database transaction.
void commit(void);
///
/// Rollback the current transction on the database.
void rollback(void);
///
/// @param name Table name to check for
/// @return True if table name exists
///
/// Return whether a table with the specified name exists in the current database.
bool table_exists(const std::string& name);
///
/// @param stmt Sql statement
/// @return Reference to the current database
///
/// Operator shift function to simplify the execution of a sql statement.
sqlite& operator<<(const std::string& stmt);
std::string path_; ///< Filesystem path to database
private:
sqlite3* db_; ///< Sqlite database handle
int trans_count_; ///< Reference count to handle nested transactions
};
//////////////////////////////////////////////////////////////////////
///
/// @class sql
///
/// Sqlite prepared statement.
///
//////////////////////////////////////////////////////////////////////
struct sql
{
struct end {}; ///< Utility class to mark the completion of a statement.
struct null {}; ///< Utility class to mark a null value.
///
/// @param db Sqlite database
///
/// Construct a statement to be executed on the specified database.
sql(sqlite& db);
///
/// Destructor. Completes any pending uncompleted statement.
~sql(void);
///
/// @return Number of input parameters required by statement.
inline int in_params(void) const { return icol_; }
///
/// @return Number of output parameters supplied by statement.
inline int out_params(void) const { return ocol_; }
///
/// @param idx Zero based index into select statement results.
/// @return Type of result expected.
///
/// Return the type of result expected for the specified column.
inline int col_type(int idx) { return sqlite3_column_type(stmt_, idx); }
///
/// Reset the statement to be empty.
void reset(void);
///
/// @param stmt An sql statement
///
/// Prepare the specified sql statement to be executed on the database.
void prepare(const std::string& stmt);
///
/// @return True if there are more results pending from a select.
///
/// Step the prepared statement. This will make the values of a select available
/// or execute an insert or create or other general database statement.
bool step(void);
///
/// @return Reference to the statement
///
/// This terminates the statement by calling step(), which is most useful for
/// insert statements as a useful shorthand.
inline sql& operator<<(const end&) { step(); return *this; }
///
/// @return Reference to the statement
///
/// Bind a null value to the current input.
inline sql& operator<<(const null&) { sqlite3_bind_null(stmt_, ++ipos_); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(bool val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(char val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(unsigned char val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(short val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(unsigned short val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(int val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(unsigned int val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(long val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(unsigned long val) { sqlite3_bind_int(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(long long val) { sqlite3_bind_int64(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(unsigned long long val) { sqlite3_bind_int64(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(float val) { sqlite3_bind_double(stmt_, ++ipos_, val); return *this; }
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind a value to the current input.
inline sql& operator<<(double val) { sqlite3_bind_double(stmt_, ++ipos_, val); return *this; }
///
/// @param val Statement to prepare or value to bind
/// @return Reference to the statement
///
/// If the statement is not prepared, prepare val as the sql statement.
/// Otherwise bind the string value to the current input.
sql& operator<<(const char* val);
///
/// @param val Statement to prepare or value to bind
/// @return Reference to the statement
///
/// If the statement is not prepared, prepare val as the sql statement.
/// Otherwise bind the string value to the current input.
sql& operator<<(const std::string& val);
///
/// @param val Value to bind
/// @return Reference to the statement
///
/// Bind an input stream value to the current input.
sql& operator<<(std::istream& val);
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(bool& val) { val = sqlite3_column_int(stmt_, opos_++) != 0; return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(char& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(unsigned char& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(short& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(unsigned short& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(int& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(unsigned int& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(long& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(unsigned long& val) { val = sqlite3_column_int(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(long long& val) { val = sqlite3_column_int64(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(unsigned long long& val) { val = sqlite3_column_int64(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(float& val) { val = sqlite3_column_double(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
inline sql& operator>>(double& val) { val = sqlite3_column_double(stmt_, opos_++); return *this; }
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a value from the current result row.
sql& operator>>(std::string& val);
///
/// @param val Reference to variable to have value returned in
/// @return Reference to the statement
///
/// Extract a stream from the current result row.
sql& operator>>(std::ostream& val);
private:
///
/// Private copy constructor to protect from statement duplication.
sql(const sql&);
///
/// Private copy operator to protect from statement duplication.
sql& operator=(const sql&);
sqlite& db_; ///< Reference to the database connection
sqlite3_stmt* stmt_; ///< Sqlite statement handle
int ipos_; ///< Current insert input position
int opos_; ///< Current select output position
int icol_; ///< Number of insert columns expected
int ocol_; ///< Number of select columns expected
};
//////////////////////////////////////////////////////////////////////
///
/// @class db_error
///
/// Database exception class.
///
//////////////////////////////////////////////////////////////////////
struct db_error : std::exception
{
///
/// @param what Error string
///
/// Database exception constructor.
db_error(const std::string& what) :
what_(what) {}
///
/// Virtual destructor.
~db_error(void) throw() {}
///
/// @return Exception description string.
const char* what() const throw() { return what_.c_str(); }
std::string what_; ///< Exception description string
};
}
#endif