19 |
|
CheckError(::sqlite3_close(connection), connection); |
20 |
|
} |
21 |
|
|
22 |
< |
Statement::Statement(Connection &connection, const std::string &statement) : connection(connection) |
22 |
> |
Statement Connection::Prepare(const std::string &statement) const |
23 |
|
{ |
24 |
< |
CheckError(::sqlite3_prepare_v2(connection.GetHandle(), statement.data(), statement.size(), &this->statement, NULL), connection.GetHandle()); |
24 |
> |
return Statement(connection, statement); |
25 |
> |
} |
26 |
> |
|
27 |
> |
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 |
|
Statement::~Statement() |
34 |
|
::sqlite3_finalize(statement); |
35 |
|
} |
36 |
|
|
37 |
< |
void Statement::Set(unsigned index, double value) |
37 |
> |
void Statement::Bind(int index, double value) const |
38 |
> |
{ |
39 |
> |
CheckError(::sqlite3_bind_double(statement, ++index, value), connection); |
40 |
> |
} |
41 |
> |
|
42 |
> |
void Statement::Bind(int index, int value) const |
43 |
> |
{ |
44 |
> |
CheckError(::sqlite3_bind_int(statement, ++index, value), connection); |
45 |
> |
} |
46 |
> |
|
47 |
> |
void Statement::Bind(int index, int64_t value) const |
48 |
|
{ |
49 |
< |
CheckError(::sqlite3_bind_double(statement, index, value), connection.GetHandle()); |
49 |
> |
CheckError(::sqlite3_bind_int64(statement, ++index, value), connection); |
50 |
> |
} |
51 |
> |
|
52 |
> |
void Statement::Bind(int index, const Null &value) const |
53 |
> |
{ |
54 |
> |
CheckError(::sqlite3_bind_null(statement, ++index), connection); |
55 |
> |
} |
56 |
> |
|
57 |
> |
void Statement::Bind(int index, const std::string &value) const |
58 |
> |
{ |
59 |
> |
CheckError(::sqlite3_bind_text(statement, ++index, value.data(), value.size(), SQLITE_TRANSIENT), connection); |
60 |
> |
} |
61 |
> |
|
62 |
> |
Results Statement::Execute() const |
63 |
> |
{ |
64 |
> |
return Results(connection, statement); |
65 |
> |
} |
66 |
> |
|
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 |
< |
void Statement::Set(unsigned index, int value) |
109 |
> |
template <> |
110 |
> |
double Results::Get(int index) |
111 |
|
{ |
112 |
< |
CheckError(::sqlite3_bind_int(statement, index, value), connection.GetHandle()); |
112 |
> |
return ::sqlite3_column_double(statement, index); |
113 |
|
} |
114 |
|
|
115 |
< |
void Statement::Set(unsigned index, int64_t value) |
115 |
> |
template <> |
116 |
> |
int Results::Get(int index) |
117 |
|
{ |
118 |
< |
CheckError(::sqlite3_bind_int64(statement, index, value), connection.GetHandle()); |
118 |
> |
return ::sqlite3_column_int(statement, index); |
119 |
|
} |
120 |
|
|
121 |
< |
void Statement::Set(unsigned index, const Null &value) |
121 |
> |
template <> |
122 |
> |
int64_t Results::Get(int index) |
123 |
|
{ |
124 |
< |
CheckError(::sqlite3_bind_null(statement, index), connection.GetHandle()); |
124 |
> |
return ::sqlite3_column_int64(statement, index); |
125 |
|
} |
126 |
|
|
127 |
< |
void Statement::Set(unsigned index, const std::string &value) |
127 |
> |
template <> |
128 |
> |
std::string Results::Get(int index) |
129 |
|
{ |
130 |
< |
CheckError(::sqlite3_bind_text(statement, index, value.data(), value.size(), SQLITE_TRANSIENT), connection.GetHandle()); |
130 |
> |
return std::string(reinterpret_cast<const char *>(::sqlite3_column_text(statement, index)), ::sqlite3_column_bytes(statement, index)); |
131 |
|
} |
132 |
|
|
133 |
|
} |