-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.cpp
More file actions
239 lines (205 loc) · 6.25 KB
/
statement.cpp
File metadata and controls
239 lines (205 loc) · 6.25 KB
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
#include "statement.h"
#include "database.h"
void Statement::_bind_methods()
{
ObjectTypeDB::bind_method(_MD("prepare", "database", "statement"), &Statement::prepare);
ObjectTypeDB::bind_method(_MD("finalize"), &Statement::finalize);
ObjectTypeDB::bind_method(_MD("getSql"), &Statement::getSql);
ObjectTypeDB::bind_method(_MD("getParamCount"), &Statement::getParamCount);
ObjectTypeDB::bind_method(_MD("getParamName", "index"), &Statement::getParamName);
ObjectTypeDB::bind_method(_MD("getParamIndex", "parameter_name"), &Statement::getParamIndex);
ObjectTypeDB::bind_method(_MD("bind", "index", "value"), &Statement::bind);
ObjectTypeDB::bind_method(_MD("step"), &Statement::step);
ObjectTypeDB::bind_method(_MD("getRow", "method"), &Statement::getRow, DEFVAL(GET_BY_NAME));
ObjectTypeDB::bind_method(_MD("getResults", "method"), &Statement::getResults, DEFVAL(GET_BY_NAME));
ObjectTypeDB::bind_method(_MD("getColumnCount"), &Statement::getColumnCount);
ObjectTypeDB::bind_method(_MD("getColumn", "column_index"), &Statement::getColumn);
ObjectTypeDB::bind_method(_MD("getColumnAsBool", "column_index"), &Statement::getColumnAsBool);
BIND_CONSTANT(STEP_ROW);
BIND_CONSTANT(STEP_DONE);
BIND_CONSTANT(STEP_BUSY);
BIND_CONSTANT(STEP_MISUSE);
BIND_CONSTANT(GET_BY_NAME);
BIND_CONSTANT(GET_BY_INDEX);
BIND_CONSTANT(GET_BY_BOTH);
}
Error Statement::prepare(Ref<Database>& db, const String& statement)
{
int err = sqlite3_prepare16_v2(
db->mpDatabase,
statement.c_str(),
statement.length()*2,
&mpStatement,
NULL);
if (err == SQLITE_OK) {
return OK;
}
else {
// TODO: Error handling
ERR_PRINT(("Prepare code: " + itos(err)).utf8().get_data());
ERR_PRINT("Failed to prepare statement");
return FAILED;
}
}
void Statement::finalize()
{
if (mpStatement) {
sqlite3_finalize(mpStatement);
mpStatement = nullptr;
}
}
String Statement::getSql() const
{
if (mpStatement) {
return String::utf8(sqlite3_sql(mpStatement));
}
else {
ERR_PRINT("Tried to get SQL of uninitialized Statement");
return String();
}
}
int Statement::getParamCount() const
{
return sqlite3_bind_parameter_count(mpStatement);
}
String Statement::getParamName(int index) const
{
const char* name = sqlite3_bind_parameter_name(mpStatement, index);
if (name) {
return String::utf8(name);
}
else {
return String();
}
}
int Statement::getParamIndex(const String& parameter_name) const
{
return sqlite3_bind_parameter_index(mpStatement, parameter_name.utf8().get_data());
}
Error Statement::bind(const Variant& index, const Variant& value)
{
int parameter_index;
if (index.is_num()) {
parameter_index = index;
}
else if (index.get_type() == Variant::STRING) {
parameter_index = getParamIndex(index);
}
else {
ERR_PRINT("Invalid type for parameter index, must be int or string");
return ERR_INVALID_PARAMETER;
}
// We now have the correct index
int err;
if (value.get_type() == Variant::RAW_ARRAY) {
typedef DVector<uint8_t> RawArray;
const RawArray& array = static_cast<RawArray>(value);
RawArray::Read read = array.read();
err = sqlite3_bind_blob(mpStatement, parameter_index, read.ptr(), array.size(), SQLITE_TRANSIENT);
}
else if (value.get_type() == Variant::REAL) {
err = sqlite3_bind_double(mpStatement, parameter_index, value);
}
else if (value.get_type() == Variant::INT || value.get_type() == Variant::BOOL) {
err = sqlite3_bind_int(mpStatement, parameter_index, value);
}
else if (value.get_type() == Variant::STRING) {
err = sqlite3_bind_text16(mpStatement, parameter_index, ((String)value).c_str(), ((String)value).length(), SQLITE_TRANSIENT);
}
else if (value.get_type() == Variant::NIL) {
err = sqlite3_bind_null(mpStatement, parameter_index);
}
else {
ERR_PRINT("Type supplied for parameter value is not supported");
return ERR_INVALID_PARAMETER;
}
if (err == SQLITE_OK) {
return OK;
}
else if (err == SQLITE_RANGE) {
return ERR_PARAMETER_RANGE_ERROR;
}
else {
// If this is coming up then we might need to add some more specific error handling.
return FAILED;
}
}
Statement::ReturnCode Statement::step()
{
return static_cast<ReturnCode>(sqlite3_step(mpStatement));
}
Dictionary Statement::getRow(GetMethod method) const
{
Dictionary res;
int num_columns = getColumnCount();
for (int i = 0; i < num_columns; ++i) {
// Get the value
Variant val = getColumn(i);
// Store it in the result list
if (method == GET_BY_NAME || method == GET_BY_BOTH) {
res[String(static_cast<const CharType*>(sqlite3_column_name16(mpStatement, i)))] = val;
}
if (method == GET_BY_INDEX || method == GET_BY_BOTH) {
res[i] = val;
}
}
return res;
}
Array Statement::getResults(GetMethod method)
{
Array res;
ReturnCode code;
do {
code = step();
if (code == STEP_ROW) {
// Store the data
res.push_back(getRow(method));
}
// TODO: Error handling
} while (code == STEP_ROW);
return res;
}
int Statement::getColumnCount() const
{
return sqlite3_column_count(mpStatement);
}
Variant Statement::getColumn(int column_index) const
{
int type = sqlite3_column_type(mpStatement, column_index);
if (type == SQLITE_INTEGER) {
return Variant(sqlite3_column_int(mpStatement, column_index));
}
else if (type == SQLITE_FLOAT) {
return Variant(sqlite3_column_double(mpStatement, column_index));
}
else if (type == SQLITE_TEXT) {
return Variant(String(static_cast<const CharType*>(sqlite3_column_text16(mpStatement, column_index))));
}
else if (type == SQLITE_BLOB) {
typedef DVector<uint8_t> RawArray;
RawArray array;
// Convert value to blob
const void* blob = sqlite3_column_blob(mpStatement, column_index);
int bytes = sqlite3_column_bytes(mpStatement, column_index);
// Copy data
array.resize(bytes);
RawArray::Write write = array.write();
copymem(write.ptr(), blob, bytes);
return Variant(array);
}
else {// else if (type == SQLITE_NULL) {
return Variant();
}
}
bool Statement::getColumnAsBool(int column_index)
{
return sqlite3_column_int(mpStatement, column_index) != 0 ? true : false;
}
Statement::Statement()
: mpStatement(nullptr)
{
}
Statement::~Statement()
{
finalize();
}