1 |
douglas |
3 |
// Truck Computer Dooom! |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
45 |
#include "sqlite3.hpp" |
8 |
|
|
|
9 |
|
|
namespace SQLite3 |
10 |
|
|
{ |
11 |
|
|
|
12 |
|
|
Connection::Connection(const std::string &db) |
13 |
|
|
{ |
14 |
|
|
CheckError(::sqlite3_open(db.c_str(), &connection), connection); |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
Connection::~Connection() |
18 |
|
|
{ |
19 |
|
|
CheckError(::sqlite3_close(connection), connection); |
20 |
|
|
} |
21 |
|
|
|
22 |
douglas |
47 |
Statement Connection::Prepare(const std::string &statement) const |
23 |
douglas |
45 |
{ |
24 |
douglas |
47 |
return Statement(connection, statement); |
25 |
douglas |
45 |
} |
26 |
|
|
|
27 |
douglas |
47 |
Statement::Statement(::sqlite3 *connection, const std::string &statement) : connection(connection) |
28 |
|
|
{ |
29 |
|
|
CheckError(::sqlite3_prepare_v2(connection, statement.data(), statement.size(), &this->statement, NULL), connection); |
30 |
|
|
} |
31 |
|
|
|
32 |
douglas |
45 |
Statement::~Statement() |
33 |
|
|
{ |
34 |
|
|
::sqlite3_finalize(statement); |
35 |
|
|
} |
36 |
|
|
|
37 |
douglas |
47 |
void Statement::Bind(int index, double value) const |
38 |
douglas |
45 |
{ |
39 |
douglas |
47 |
CheckError(::sqlite3_bind_double(statement, ++index, value), connection); |
40 |
douglas |
45 |
} |
41 |
|
|
|
42 |
douglas |
47 |
void Statement::Bind(int index, int value) const |
43 |
douglas |
45 |
{ |
44 |
douglas |
47 |
CheckError(::sqlite3_bind_int(statement, ++index, value), connection); |
45 |
douglas |
45 |
} |
46 |
|
|
|
47 |
douglas |
47 |
void Statement::Bind(int index, int64_t value) const |
48 |
douglas |
45 |
{ |
49 |
douglas |
47 |
CheckError(::sqlite3_bind_int64(statement, ++index, value), connection); |
50 |
douglas |
45 |
} |
51 |
|
|
|
52 |
douglas |
47 |
void Statement::Bind(int index, const Null &value) const |
53 |
douglas |
45 |
{ |
54 |
douglas |
47 |
CheckError(::sqlite3_bind_null(statement, ++index), connection); |
55 |
douglas |
45 |
} |
56 |
|
|
|
57 |
douglas |
47 |
void Statement::Bind(int index, const std::string &value) const |
58 |
douglas |
45 |
{ |
59 |
douglas |
47 |
CheckError(::sqlite3_bind_text(statement, ++index, value.data(), value.size(), SQLITE_TRANSIENT), connection); |
60 |
douglas |
45 |
} |
61 |
|
|
|
62 |
douglas |
47 |
Results Statement::Execute() const |
63 |
|
|
{ |
64 |
|
|
return Results(connection, statement); |
65 |
douglas |
45 |
} |
66 |
douglas |
47 |
|
67 |
|
|
void Statement::Clear() const |
68 |
|
|
{ |
69 |
|
|
CheckError(::sqlite3_clear_bindings(statement), connection); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
Results::Results(::sqlite3 *connection, ::sqlite3_stmt *statement) : connection(connection), statement(statement), next(Begin) |
73 |
|
|
{ |
74 |
|
|
if (MoveNext()) |
75 |
|
|
next = True; |
76 |
|
|
else |
77 |
|
|
next = False; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
bool Results::MoveNext() |
81 |
|
|
{ |
82 |
|
|
if (next == Begin || next == Ignore) |
83 |
|
|
{ |
84 |
|
|
int code(::sqlite3_step(statement)); |
85 |
|
|
|
86 |
|
|
if (code == SQLITE_ROW) |
87 |
|
|
return true; |
88 |
|
|
else if (code == SQLITE_DONE) |
89 |
|
|
{ |
90 |
|
|
CheckError(::sqlite3_reset(statement), connection); |
91 |
|
|
|
92 |
|
|
return false; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
CheckError(code, connection); |
96 |
|
|
|
97 |
|
|
return false; |
98 |
|
|
} |
99 |
|
|
else |
100 |
|
|
{ |
101 |
|
|
bool next_(next); |
102 |
|
|
|
103 |
|
|
next = Ignore; |
104 |
|
|
|
105 |
|
|
return next_; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
template <> |
110 |
|
|
double Results::Get(int index) |
111 |
|
|
{ |
112 |
|
|
return ::sqlite3_column_double(statement, index); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
template <> |
116 |
|
|
int Results::Get(int index) |
117 |
|
|
{ |
118 |
|
|
return ::sqlite3_column_int(statement, index); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
template <> |
122 |
|
|
int64_t Results::Get(int index) |
123 |
|
|
{ |
124 |
|
|
return ::sqlite3_column_int64(statement, index); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
template <> |
128 |
|
|
std::string Results::Get(int index) |
129 |
|
|
{ |
130 |
|
|
return std::string(reinterpret_cast<const char *>(::sqlite3_column_text(statement, index)), ::sqlite3_column_bytes(statement, index)); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
} |