/
extension-sqlite.cc
471 lines (382 loc) · 12.9 KB
/
extension-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
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
465
466
467
468
469
470
471
#include "extension-sqlite.h"
/* Open an SQLite database.
* Args: STR <path to database>, [INT options] */
static package
bf_sqlite_open(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = next_handle();
if (index == -1)
{
// We've exceeded SQLITE_MAX_HANDLES and the $server_options setting and didn't allocate anything.
free_var(arglist);
return make_raise_pack(E_QUOTA, "Too many database connections open.", var_ref(zero));
}
/* NOTE: This relies on having FileIO. If you don't, you'll need
* a function to resolve a SAFE path. */
const char *path = file_resolve_path(arglist.v.list[1].v.str);
if (path == NULL)
{
free_var(arglist);
return make_error_pack(E_INVARG);
}
int dup_check = database_already_open(path);
if (dup_check != -1)
{
free_var(arglist);
char ohno[50];
sprintf(ohno, "Database already open with handle: %i", dup_check);
return make_raise_pack(E_INVARG, ohno, var_ref(zero));
}
index = allocate_handle();
sqlite_conn *handle = &sqlite_connections[index];
if (arglist.v.list[0].v.num >= 2)
handle->options = arglist.v.list[2].v.num;
free_var(arglist);
int rc = sqlite3_open(path, &handle->id);
if (rc != SQLITE_OK)
{
const char *err = sqlite3_errmsg(handle->id);
deallocate_handle(index);
return make_raise_pack(E_NONE, err, var_ref(zero));
} else {
handle->path = str_dup(path);
Var r;
r.type = TYPE_INT;
r.v.num = index;
return make_var_pack(r);
}
}
/* Close an SQLite database.
* Args: INT <database handle> */
static package
bf_sqlite_close(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = arglist.v.list[1].v.num;
free_var(arglist);
if (!valid_handle(index))
return make_raise_pack(E_INVARG, "Invalid database handle", var_ref(zero));
deallocate_handle(index);
return no_var_pack();
}
/* Return a list of open SQLite database handles. */
static package
bf_sqlite_handles(Var arglist, Byte next, void *vdata, Objid progr)
{
free_var(arglist);
if (!is_wizard(progr))
return make_error_pack(E_PERM);
Var r = new_list(sqlite_connections.size());
int count = 0;
for (auto& it : sqlite_connections)
r.v.list[++count] = Var::new_int(it.first);
return make_var_pack(r);
}
/* Return information about the specified SQLite database handle.
* Args: <INT database handle> */
static package
bf_sqlite_info(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = arglist.v.list[1].v.num;
free_var(arglist);
if (!valid_handle(index))
return make_error_pack(E_INVARG);
sqlite_conn *handle = &sqlite_connections[index];
Var ret = new_map();
ret = mapinsert(ret, str_dup_to_var("path"), str_dup_to_var(handle->path));
ret = mapinsert(ret, str_dup_to_var("parse_types"), Var::new_int(handle->options & SQLITE_PARSE_TYPES ? 1 : 0));
ret = mapinsert(ret, str_dup_to_var("parse_objects"), Var::new_int(handle->options & SQLITE_PARSE_OBJECTS ? 1 : 0));
ret = mapinsert(ret, str_dup_to_var("sanitize_strings"), Var::new_int(handle->options & SQLITE_SANITIZE_STRINGS ? 1 : 0));
return make_var_pack(ret);
}
/* Creates and executes a prepared statement.
* Args: INT <database handle>, STR <SQL query>, LIST <values>
* e.g. sqlite_query(0, 'INSERT INTO test VALUES (?, ?);', {5, #5})
* TODO: Cache prepared statements? */
static package
bf_sqlite_execute(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = arglist.v.list[1].v.num;
if (!valid_handle(index))
{
free_var(arglist);
return make_error_pack(E_INVARG);
}
const char *query = arglist.v.list[2].v.str;
sqlite_conn *handle = &sqlite_connections[index];
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(handle->id, query, -1, &stmt, 0);
if (rc != SQLITE_OK)
{
const char *err = sqlite3_errmsg(handle->id);
free_var(arglist);
return make_raise_pack(E_NONE, err, var_ref(zero));
}
/* Take args[3] and bind it into the appropriate locations for SQLite
* (e.g. in the query values (?, ?, ?) args[3] would be {5, "oh", "hello"}) */
for (int x = 1; x <= arglist.v.list[3].v.list[0].v.num; x++)
{
switch (arglist.v.list[3].v.list[x].type)
{
case TYPE_STR:
sqlite3_bind_text(stmt, x, arglist.v.list[3].v.list[x].v.str, -1, NULL);
break;
case TYPE_INT:
sqlite3_bind_int(stmt, x, arglist.v.list[3].v.list[x].v.num);
break;
case TYPE_FLOAT:
sqlite3_bind_double(stmt, x, *arglist.v.list[3].v.list[x].v.fnum);
break;
case TYPE_OBJ:
sqlite3_bind_text(stmt, x, str_dup(reset_stream(object_to_string(&arglist.v.list[3].v.list[x]))), -1, NULL);
break;
}
}
rc = sqlite3_step(stmt);
int col = sqlite3_column_count(stmt);
Var r = new_list(0);
while (rc == SQLITE_ROW)
{
Var row = new_list(0);
for (int x = 0; x < col; x++)
{
// Ideally we would know the type and use sqlite3_column<TYPE> but we don't!
char *str = (char*)sqlite3_column_text(stmt, x);
if (handle->options & SQLITE_SANITIZE_STRINGS)
sanitize_string_for_moo(str);
Var s;
if (!(handle->options & SQLITE_PARSE_TYPES))
{
s.type = TYPE_STR;
s.v.str = str_dup(str);
} else {
s = string_to_moo_type(str, handle->options & SQLITE_PARSE_OBJECTS, handle->options & SQLITE_SANITIZE_STRINGS);
}
row = listappend(row, s);
}
r = listappend(r, row);
rc = sqlite3_step(stmt);
}
/* TODO: Reset the prepared statement bindings and cache it.
* (Remove finalize when that happens) */
sqlite3_finalize(stmt);
free_var(arglist);
return make_var_pack(r);
}
/* Execute an SQL command.
* Args: INT <database handle>, STR <query> */
static package
bf_sqlite_query(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = arglist.v.list[1].v.num;
if (!valid_handle(index))
{
free_var(arglist);
return make_error_pack(E_INVARG);
}
const char *query = arglist.v.list[2].v.str;
char *err_msg = 0;
sqlite_conn *handle = &sqlite_connections[index];
int rc = sqlite3_exec(handle->id, query, callback, handle, &err_msg);
free_var(arglist);
Var r;
if (rc != SQLITE_OK)
{
r.type = TYPE_STR;
r.v.str = str_dup(err_msg);
sqlite3_free(err_msg);
return make_var_pack(r);
} else {
r = var_dup(last_result);
free_var(last_result);
last_result = new_list(0);
return make_var_pack(r);
}
}
/* Identifies the row ID of the last insert command.
* (this was an early API test, I don't really see the usefulness) */
static package
bf_sqlite_last_insert_row_id(Var arglist, Byte next, void *vdata, Objid progr)
{
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
int index = arglist.v.list[1].v.num;
free_var(arglist);
if (!valid_handle(index))
return make_error_pack(E_INVARG);
sqlite_conn *handle = &sqlite_connections[index];
Var r;
r.type = TYPE_INT;
r.v.num = sqlite3_last_insert_rowid(handle->id);
return make_var_pack(r);
}
/* -------------------------------------------------------- */
/* Return true if a handle is valid and active. */
bool valid_handle(int handle)
{
if (handle < 0 || handle >= next_sqlite_handle || sqlite_connections.count(handle) == 0)
return false;
return true;
}
/* Return the index of the next handle.
* If we've exceeded our maximum connection limit, -1 will be returned.
* Otherwise, a valid SQLite handle integer is returned. */
int next_handle()
{
if (sqlite_connections.size() >= server_int_option("sqlite_max_handles", SQLITE_MAX_HANDLES))
return -1;
return next_sqlite_handle;
}
/* Create an empty connection and add it to the open connection map. */
int allocate_handle()
{
int handle = next_handle();
if (handle == -1)
return -1;
next_sqlite_handle++;
sqlite_conn connection;
connection.path = NULL;
connection.options = SQLITE_PARSE_TYPES | SQLITE_PARSE_OBJECTS;
sqlite_connections[handle] = connection;
return handle;
}
/* Free up memory and remove a handle from the connection map. */
void deallocate_handle(int handle)
{
sqlite_conn conn = sqlite_connections[handle];
sqlite3_close(conn.id);
if (conn.path != NULL)
free_str(conn.path);
sqlite_connections.erase(handle);
if (sqlite_connections.size() == 0)
next_sqlite_handle = 1;
}
/* Check if a database at 'path' is already open.
* If so, return its handle. Otherwise, return -1. */
int database_already_open(const char *path)
{
std::map <int, sqlite_conn>::iterator it;
for (it = sqlite_connections.begin(); it != sqlite_connections.end(); it++)
{
if (it->second.path != NULL && strcmp(it->second.path, path) == 0)
return it->first;
}
return -1;
}
/* Callback function for execute. Shoves our result into an ugly global
* for the MOO to soak into a Var. */
int callback(void *index, int argc, char **argv, char **azColName)
{
sqlite_conn *handle = (sqlite_conn*)index;
Var ret = new_list(0);
for (int i = 0; i < argc; i++)
{
Var s;
if (!(handle->options & SQLITE_PARSE_TYPES))
{
s.type = TYPE_STR;
if (handle->options & SQLITE_SANITIZE_STRINGS)
sanitize_string_for_moo(argv[i]);
s.v.str = str_dup(argv[i]);
} else {
s = string_to_moo_type(argv[i], handle->options & SQLITE_PARSE_OBJECTS, handle->options & SQLITE_SANITIZE_STRINGS);
}
ret = listappend(ret, s);
}
last_result = listappend(last_result, ret);
return 0;
}
/* The MOO database really dislikes newlines, so we'll want to strip them.
* I like what MOOSQL did here by replacing them with tabs, so we'll do that.
* TODO: Check the performance impact of this being on by default with long strings. */
void sanitize_string_for_moo(char *string)
{
char *p = string;
while (*p)
{
if (*p == '\n')
*p = '\t';
++p;
}
}
/* Take a result string and convert it into a MOO type.
* Return a Var of the appropriate MOO type for the value.
* TODO: Try to parse strings containing MOO lists? */
Var string_to_moo_type(char* str, bool parse_objects, bool sanitize_string)
{
Var s;
if (str == NULL)
{
s.type = TYPE_STR;
s.v.str = str_dup("NULL");
return s;
}
double double_test = 0.0;
int int_test = 0;
if (str[0] == '#' && parse_objects && parse_number(str + 1, &int_test, 0) == 1)
{
// Add one to the pointer to skip over the # and check the rest for numeracy
s.type = TYPE_OBJ;
s.v.obj = int_test;
} else if (parse_number(str, &int_test, 0) == 1) {
s.type = TYPE_INT;
s.v.num = int_test;
} else if (parse_float(str, &double_test) == 1)
{
s = new_float(double_test);
} else {
if (sanitize_string)
sanitize_string_for_moo(str);
s.type = TYPE_STR;
s.v.str = str_dup(str);
}
return s;
}
/* Converts a MOO object (supplied to a prepared statement) into a string similar to
* tostr(#xxx) */
Stream* object_to_string(Var *thing)
{
static Stream *s = 0;
if (!s)
s = new_stream(11);
stream_printf(s, "#%d", thing->v.num);
return s;
}
void
register_sqlite() {
oklog("REGISTER_SQLITE: v%s (SQLite Library v%s)\n", SQLITE_MOO_VERSION, sqlite3_libversion());
register_function("sqlite_open", 1, 2, bf_sqlite_open, TYPE_STR, TYPE_INT);
register_function("sqlite_close", 1, 1, bf_sqlite_close, TYPE_INT);
register_function("sqlite_handles", 0, 0, bf_sqlite_handles);
register_function("sqlite_info", 1, 1, bf_sqlite_info, TYPE_INT);
register_function("sqlite_query", 2, 2, bf_sqlite_query, TYPE_INT, TYPE_STR);
register_function("sqlite_execute", 3, 3, bf_sqlite_execute, TYPE_INT, TYPE_STR, TYPE_LIST);
register_function("sqlite_last_insert_row_id", 1, 1, bf_sqlite_last_insert_row_id, TYPE_INT);
}