| @@ -0,0 +1,460 @@ |
| 1 | +/* Binding of SQLite for Louise (packages/sqlite/src/lib.lse). Functions |
| 2 | + * follow the extern convention of louise_rt.h: borrowed arguments, owned |
| 3 | + * results. |
| 4 | + * |
| 5 | + * A binding may build no list or record (louise_rt.h), so values cross as |
| 6 | + * MessagePack Bytes, which the Louise side reads and writes with |
| 7 | + * std/msgpack: |
| 8 | + * parameters an array of nil, bool, int, float, str or bin; |
| 9 | + * a result [columns, rows, changes, last_rowid], where columns is an |
| 10 | + * array of str and each row an array of values. |
| 11 | + * An error is the string "<extended result code> <message>". */ |
| 12 | +#include <pthread.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include "louise_rt.h" |
| 19 | +#include "sqlite3.h" |
| 20 | + |
| 21 | +/* A connection: SQLite's handle, NULL once closed, and a lock so that one |
| 22 | + * statement at a time runs on it, whichever pool thread calls. */ |
| 23 | +typedef struct { |
| 24 | + pthread_mutex_t lock; |
| 25 | + sqlite3 *db; |
| 26 | +} conn; |
| 27 | + |
| 28 | +static void conn_finalize(void *p) { |
| 29 | + conn *c = p; |
| 30 | + if (c->db != NULL) sqlite3_close_v2(c->db); |
| 31 | + pthread_mutex_destroy(&c->lock); |
| 32 | + free(c); |
| 33 | +} |
| 34 | + |
| 35 | +static tl_value fail(tl_ctx *ctx, int code, const char *message) { |
| 36 | + char text[1024]; |
| 37 | + int n = snprintf(text, sizeof text, "%d %s", code, message != NULL ? message : ""); |
| 38 | + if (n < 0) n = 0; |
| 39 | + if ((size_t)n >= sizeof text) n = sizeof text - 1; |
| 40 | + return tl_error(ctx, tl_string_from(ctx, text, (size_t)n)); |
| 41 | +} |
| 42 | + |
| 43 | +static tl_value fail_db(tl_ctx *ctx, sqlite3 *db) { |
| 44 | + return fail(ctx, sqlite3_extended_errcode(db), sqlite3_errmsg(db)); |
| 45 | +} |
| 46 | + |
| 47 | +/* open(path, flags, busy_ms) -> Result<Resource<Db>, String>. Extended |
| 48 | + * result codes on; waits up to busy_ms for a lock held by another |
| 49 | + * connection before SQLITE_BUSY. */ |
| 50 | +tl_value louise_sqlite_open(tl_ctx *ctx, tl_value path, tl_value flags, tl_value busy_ms) { |
| 51 | + int64_t f, busy; |
| 52 | + if (!tl_int_to_i64(flags, &f) || !tl_int_to_i64(busy_ms, &busy) || busy < 0 || busy > INT32_MAX) |
| 53 | + return fail(ctx, SQLITE_MISUSE, "invalid open flags or busy timeout"); |
| 54 | + sqlite3 *db = NULL; |
| 55 | + int rc = sqlite3_open_v2(tl_string_ptr(path), &db, (int)f | SQLITE_OPEN_FULLMUTEX, NULL); |
| 56 | + if (rc != SQLITE_OK) { |
| 57 | + tl_value e = db != NULL ? fail_db(ctx, db) : fail(ctx, rc, sqlite3_errstr(rc)); |
| 58 | + sqlite3_close_v2(db); |
| 59 | + return e; |
| 60 | + } |
| 61 | + sqlite3_extended_result_codes(db, 1); |
| 62 | + sqlite3_busy_timeout(db, (int)busy); |
| 63 | + conn *c = malloc(sizeof *c); |
| 64 | + if (c == NULL) { |
| 65 | + sqlite3_close_v2(db); |
| 66 | + return fail(ctx, SQLITE_NOMEM, "out of memory"); |
| 67 | + } |
| 68 | + pthread_mutex_init(&c->lock, NULL); |
| 69 | + c->db = db; |
| 70 | + return tl_ok(ctx, tl_resource_new(ctx, c, conn_finalize, true)); |
| 71 | +} |
| 72 | + |
| 73 | +/* close(db) -> (): later calls fail with SQLITE_MISUSE. Closing twice is |
| 74 | + * harmless. */ |
| 75 | +tl_value louise_sqlite_close(tl_ctx *ctx, tl_value resource) { |
| 76 | + (void)ctx; |
| 77 | + conn *c = tl_resource_get(resource); |
| 78 | + pthread_mutex_lock(&c->lock); |
| 79 | + if (c->db != NULL) sqlite3_close_v2(c->db); |
| 80 | + c->db = NULL; |
| 81 | + pthread_mutex_unlock(&c->lock); |
| 82 | + return TL_UNIT; |
| 83 | +} |
| 84 | + |
| 85 | +/* version() -> String: the linked SQLite's version, "3.54.1". */ |
| 86 | +tl_value louise_sqlite_version(tl_ctx *ctx) { |
| 87 | + return tl_string_cstr(ctx, sqlite3_libversion()); |
| 88 | +} |
| 89 | + |
| 90 | +/* ---- MessagePack out ----------------------------------------------------- */ |
| 91 | + |
| 92 | +typedef struct { |
| 93 | + uint8_t *data; |
| 94 | + size_t len, cap; |
| 95 | + int failed; |
| 96 | +} buf; |
| 97 | + |
| 98 | +static void put(buf *b, const void *bytes, size_t n) { |
| 99 | + if (b->failed) return; |
| 100 | + if (b->len + n > b->cap) { |
| 101 | + size_t cap = b->cap ? b->cap : 256; |
| 102 | + while (cap < b->len + n) cap *= 2; |
| 103 | + uint8_t *data = realloc(b->data, cap); |
| 104 | + if (data == NULL) { |
| 105 | + b->failed = 1; |
| 106 | + return; |
| 107 | + } |
| 108 | + b->data = data; |
| 109 | + b->cap = cap; |
| 110 | + } |
| 111 | + memcpy(b->data + b->len, bytes, n); |
| 112 | + b->len += n; |
| 113 | +} |
| 114 | + |
| 115 | +static void put_byte(buf *b, uint8_t x) { put(b, &x, 1); } |
| 116 | + |
| 117 | +static void put_be(buf *b, uint64_t x, int width) { |
| 118 | + uint8_t out[8]; |
| 119 | + for (int i = 0; i < width; i++) out[i] = (uint8_t)(x >> (8 * (width - 1 - i))); |
| 120 | + put(b, out, (size_t)width); |
| 121 | +} |
| 122 | + |
| 123 | +/* Fixed widths throughout (int 64, str 32, array 32): any MessagePack |
| 124 | + * reader accepts them, and an array's header can be written before its |
| 125 | + * length is known, then patched. */ |
| 126 | +static void put_int(buf *b, int64_t x) { |
| 127 | + put_byte(b, 0xd3); |
| 128 | + put_be(b, (uint64_t)x, 8); |
| 129 | +} |
| 130 | + |
| 131 | +static void put_float(buf *b, double x) { |
| 132 | + uint64_t bits; |
| 133 | + memcpy(&bits, &x, sizeof bits); |
| 134 | + put_byte(b, 0xcb); |
| 135 | + put_be(b, bits, 8); |
| 136 | +} |
| 137 | + |
| 138 | +static void put_str(buf *b, const void *s, size_t n) { |
| 139 | + put_byte(b, 0xdb); |
| 140 | + put_be(b, n, 4); |
| 141 | + put(b, s, n); |
| 142 | +} |
| 143 | + |
| 144 | +static void put_bin(buf *b, const void *s, size_t n) { |
| 145 | + put_byte(b, 0xc6); |
| 146 | + put_be(b, n, 4); |
| 147 | + put(b, s, n); |
| 148 | +} |
| 149 | + |
| 150 | +/* An array header whose count is patched later; its offset. */ |
| 151 | +static size_t open_array(buf *b) { |
| 152 | + size_t at = b->len; |
| 153 | + put_byte(b, 0xdd); |
| 154 | + put_be(b, 0, 4); |
| 155 | + return at; |
| 156 | +} |
| 157 | + |
| 158 | +static void close_array(buf *b, size_t at, uint32_t count) { |
| 159 | + if (b->failed) return; |
| 160 | + for (int i = 0; i < 4; i++) b->data[at + 1 + i] = (uint8_t)(count >> (8 * (3 - i))); |
| 161 | +} |
| 162 | + |
| 163 | +/* ---- MessagePack in: the parameters ---------------------------------------- */ |
| 164 | + |
| 165 | +typedef struct { |
| 166 | + const uint8_t *p, *end; |
| 167 | +} reader; |
| 168 | + |
| 169 | +static int take(reader *r, size_t n, const uint8_t **out) { |
| 170 | + if ((size_t)(r->end - r->p) < n) return 0; |
| 171 | + *out = r->p; |
| 172 | + r->p += n; |
| 173 | + return 1; |
| 174 | +} |
| 175 | + |
| 176 | +static int take_be(reader *r, int width, uint64_t *out) { |
| 177 | + const uint8_t *s; |
| 178 | + if (!take(r, (size_t)width, &s)) return 0; |
| 179 | + uint64_t x = 0; |
| 180 | + for (int i = 0; i < width; i++) x = (x << 8) | s[i]; |
| 181 | + *out = x; |
| 182 | + return 1; |
| 183 | +} |
| 184 | + |
| 185 | +/* The parameters' count, from the top-level array's header. */ |
| 186 | +static int array_header(reader *r, uint64_t *count) { |
| 187 | + const uint8_t *t; |
| 188 | + if (!take(r, 1, &t)) return 0; |
| 189 | + if ((*t & 0xf0) == 0x90) { |
| 190 | + *count = *t & 0x0f; |
| 191 | + return 1; |
| 192 | + } |
| 193 | + if (*t == 0xdc) return take_be(r, 2, count); |
| 194 | + if (*t == 0xdd) return take_be(r, 4, count); |
| 195 | + return 0; |
| 196 | +} |
| 197 | + |
| 198 | +/* Bind the next value of `r` to parameter `index`; SQLITE_OK, an SQLite |
| 199 | + * code from binding, or SQLITE_MISMATCH for a value this format lacks. */ |
| 200 | +static int bind_next(sqlite3_stmt *stmt, int index, reader *r) { |
| 201 | + const uint8_t *t, *s; |
| 202 | + uint64_t n; |
| 203 | + if (!take(r, 1, &t)) return SQLITE_MISMATCH; |
| 204 | + uint8_t tag = *t; |
| 205 | + if (tag <= 0x7f) return sqlite3_bind_int64(stmt, index, tag); |
| 206 | + if (tag >= 0xe0) return sqlite3_bind_int64(stmt, index, (int8_t)tag); |
| 207 | + if ((tag & 0xe0) == 0xa0) { |
| 208 | + n = tag & 0x1f; |
| 209 | + goto text; |
| 210 | + } |
| 211 | + switch (tag) { |
| 212 | + case 0xc0: return sqlite3_bind_null(stmt, index); |
| 213 | + case 0xc2: return sqlite3_bind_int64(stmt, index, 0); |
| 214 | + case 0xc3: return sqlite3_bind_int64(stmt, index, 1); |
| 215 | + case 0xcc: case 0xcd: case 0xce: case 0xcf: { |
| 216 | + int width = 1 << (tag - 0xcc); |
| 217 | + if (!take_be(r, width, &n) || n > INT64_MAX) return SQLITE_MISMATCH; |
| 218 | + return sqlite3_bind_int64(stmt, index, (int64_t)n); |
| 219 | + } |
| 220 | + case 0xd0: case 0xd1: case 0xd2: case 0xd3: { |
| 221 | + int width = 1 << (tag - 0xd0); |
| 222 | + if (!take_be(r, width, &n)) return SQLITE_MISMATCH; |
| 223 | + int shift = 64 - 8 * width; |
| 224 | + int64_t x = shift ? ((int64_t)(n << shift)) >> shift : (int64_t)n; |
| 225 | + return sqlite3_bind_int64(stmt, index, x); |
| 226 | + } |
| 227 | + case 0xca: { |
| 228 | + if (!take_be(r, 4, &n)) return SQLITE_MISMATCH; |
| 229 | + uint32_t bits = (uint32_t)n; |
| 230 | + float x; |
| 231 | + memcpy(&x, &bits, sizeof x); |
| 232 | + return sqlite3_bind_double(stmt, index, x); |
| 233 | + } |
| 234 | + case 0xcb: { |
| 235 | + if (!take_be(r, 8, &n)) return SQLITE_MISMATCH; |
| 236 | + double x; |
| 237 | + memcpy(&x, &n, sizeof x); |
| 238 | + return sqlite3_bind_double(stmt, index, x); |
| 239 | + } |
| 240 | + case 0xd9: case 0xda: case 0xdb: |
| 241 | + if (!take_be(r, 1 << (tag - 0xd9), &n)) return SQLITE_MISMATCH; |
| 242 | + goto text; |
| 243 | + case 0xc4: case 0xc5: case 0xc6: |
| 244 | + if (!take_be(r, 1 << (tag - 0xc4), &n) || !take(r, n, &s)) return SQLITE_MISMATCH; |
| 245 | + return sqlite3_bind_blob64(stmt, index, s, n, SQLITE_TRANSIENT); |
| 246 | + default: |
| 247 | + return SQLITE_MISMATCH; |
| 248 | + } |
| 249 | +text: |
| 250 | + if (!take(r, n, &s)) return SQLITE_MISMATCH; |
| 251 | + return sqlite3_bind_text64(stmt, index, (const char *)s, n, SQLITE_TRANSIENT, SQLITE_UTF8); |
| 252 | +} |
| 253 | + |
| 254 | +/* Step over one value of `r` (any this format has); 0 if malformed. */ |
| 255 | +static int skip_value(reader *r) { |
| 256 | + const uint8_t *t; |
| 257 | + uint64_t n; |
| 258 | + if (!take(r, 1, &t)) return 0; |
| 259 | + uint8_t tag = *t; |
| 260 | + if (tag <= 0x7f || tag >= 0xe0 || tag == 0xc0 || tag == 0xc2 || tag == 0xc3) return 1; |
| 261 | + if ((tag & 0xe0) == 0xa0) return take(r, tag & 0x1f, &t); |
| 262 | + switch (tag) { |
| 263 | + case 0xcc: case 0xcd: case 0xce: case 0xcf: return take(r, (size_t)1 << (tag - 0xcc), &t); |
| 264 | + case 0xd0: case 0xd1: case 0xd2: case 0xd3: return take(r, (size_t)1 << (tag - 0xd0), &t); |
| 265 | + case 0xca: return take(r, 4, &t); |
| 266 | + case 0xcb: return take(r, 8, &t); |
| 267 | + case 0xd9: case 0xda: case 0xdb: return take_be(r, 1 << (tag - 0xd9), &n) && take(r, n, &t); |
| 268 | + case 0xc4: case 0xc5: case 0xc6: return take_be(r, 1 << (tag - 0xc4), &n) && take(r, n, &t); |
| 269 | + default: return 0; |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +/* Which value a parameter takes: `?` the next by position, `?N` and `$N` |
| 274 | + * the Nth (1-based); 0 for a name this binding does not support. */ |
| 275 | +static int64_t value_number(const char *name, int position) { |
| 276 | + if (name == NULL) return position; |
| 277 | + if (name[0] != '?' && name[0] != '$') return 0; |
| 278 | + if (name[1] < '1' || name[1] > '9') return 0; |
| 279 | + int64_t n = 0; |
| 280 | + for (const char *d = name + 1; *d; d++) { |
| 281 | + if (*d < '0' || *d > '9' || n > 100000) return 0; |
| 282 | + n = n * 10 + (*d - '0'); |
| 283 | + } |
| 284 | + return n; |
| 285 | +} |
| 286 | + |
| 287 | +/* Bind every parameter of `stmt` from the encoded values. Placeholders are |
| 288 | + * read by name: SQLite numbers `$3` by where it first appears, not by 3. */ |
| 289 | +static int bind_all(sqlite3_stmt *stmt, tl_value params, const char **why) { |
| 290 | + reader r = {tl_bytes_ptr(params), tl_bytes_ptr(params) + tl_bytes_len(params)}; |
| 291 | + uint64_t count; |
| 292 | + if (!array_header(&r, &count)) { |
| 293 | + *why = "the parameters are not an encoded array"; |
| 294 | + return SQLITE_MISUSE; |
| 295 | + } |
| 296 | + const uint8_t *values = r.p; |
| 297 | + int slots = sqlite3_bind_parameter_count(stmt); |
| 298 | + for (int slot = 1; slot <= slots; slot++) { |
| 299 | + int64_t k = value_number(sqlite3_bind_parameter_name(stmt, slot), slot); |
| 300 | + if (k < 1) { |
| 301 | + *why = "only ?, ?N and $N placeholders are supported"; |
| 302 | + return SQLITE_RANGE; |
| 303 | + } |
| 304 | + if ((uint64_t)k > count) { |
| 305 | + *why = "a placeholder has no value"; |
| 306 | + return SQLITE_RANGE; |
| 307 | + } |
| 308 | + /* Skip to the kth value: parameters are few, so a scan is fine. */ |
| 309 | + reader at = {values, r.end}; |
| 310 | + for (int64_t i = 1; i < k; i++) |
| 311 | + if (!skip_value(&at)) goto malformed; |
| 312 | + int rc = bind_next(stmt, slot, &at); |
| 313 | + if (rc == SQLITE_MISMATCH) goto malformed; |
| 314 | + if (rc != SQLITE_OK) { |
| 315 | + *why = NULL; |
| 316 | + return rc; |
| 317 | + } |
| 318 | + } |
| 319 | + return SQLITE_OK; |
| 320 | +malformed: |
| 321 | + *why = "a parameter is not a value SQLite can take"; |
| 322 | + return SQLITE_MISMATCH; |
| 323 | +} |
| 324 | + |
| 325 | +/* Whether only whitespace, semicolons and comments are left of `tail`. */ |
| 326 | +static int blank(const char *tail) { |
| 327 | + while (*tail) { |
| 328 | + if (*tail == ' ' || *tail == '\t' || *tail == '\n' || *tail == '\r' || *tail == ';') { |
| 329 | + tail++; |
| 330 | + } else if (tail[0] == '-' && tail[1] == '-') { |
| 331 | + while (*tail && *tail != '\n') tail++; |
| 332 | + } else if (tail[0] == '/' && tail[1] == '*') { |
| 333 | + const char *end = strstr(tail + 2, "*/"); |
| 334 | + if (end == NULL) return 1; |
| 335 | + tail = end + 2; |
| 336 | + } else { |
| 337 | + return 0; |
| 338 | + } |
| 339 | + } |
| 340 | + return 1; |
| 341 | +} |
| 342 | + |
| 343 | +static void put_column(buf *b, sqlite3_stmt *stmt, int i) { |
| 344 | + switch (sqlite3_column_type(stmt, i)) { |
| 345 | + case SQLITE_INTEGER: put_int(b, sqlite3_column_int64(stmt, i)); break; |
| 346 | + case SQLITE_FLOAT: put_float(b, sqlite3_column_double(stmt, i)); break; |
| 347 | + case SQLITE_TEXT: { |
| 348 | + const unsigned char *s = sqlite3_column_text(stmt, i); |
| 349 | + put_str(b, s, (size_t)sqlite3_column_bytes(stmt, i)); |
| 350 | + break; |
| 351 | + } |
| 352 | + case SQLITE_BLOB: { |
| 353 | + const void *s = sqlite3_column_blob(stmt, i); |
| 354 | + put_bin(b, s, (size_t)sqlite3_column_bytes(stmt, i)); |
| 355 | + break; |
| 356 | + } |
| 357 | + default: put_byte(b, 0xc0); break; |
| 358 | + } |
| 359 | +} |
| 360 | + |
| 361 | +/* Prepare, bind and step one statement to its end, under the connection's |
| 362 | + * lock (the caller holds it). */ |
| 363 | +static tl_value run_locked(tl_ctx *ctx, sqlite3 *db, tl_value sql, tl_value params) { |
| 364 | + sqlite3_stmt *stmt = NULL; |
| 365 | + const char *tail = NULL; |
| 366 | + int rc = sqlite3_prepare_v2(db, tl_string_ptr(sql), (int)tl_string_len(sql) + 1, &stmt, &tail); |
| 367 | + if (rc != SQLITE_OK) return fail_db(ctx, db); |
| 368 | + if (stmt == NULL) return fail(ctx, SQLITE_MISUSE, "no statement"); |
| 369 | + if (tail != NULL && !blank(tail)) { |
| 370 | + sqlite3_finalize(stmt); |
| 371 | + return fail(ctx, SQLITE_MISUSE, "one statement at a time (use exec for a script)"); |
| 372 | + } |
| 373 | + const char *why = NULL; |
| 374 | + rc = bind_all(stmt, params, &why); |
| 375 | + if (rc != SQLITE_OK) { |
| 376 | + tl_value e = why != NULL ? fail(ctx, rc, why) : fail_db(ctx, db); |
| 377 | + sqlite3_finalize(stmt); |
| 378 | + return e; |
| 379 | + } |
| 380 | + buf b = {0}; |
| 381 | + put_byte(&b, 0x94); |
| 382 | + int columns = sqlite3_column_count(stmt); |
| 383 | + put_byte(&b, 0xdd); |
| 384 | + put_be(&b, (uint64_t)columns, 4); |
| 385 | + for (int i = 0; i < columns; i++) { |
| 386 | + const char *name = sqlite3_column_name(stmt, i); |
| 387 | + put_str(&b, name, name != NULL ? strlen(name) : 0); |
| 388 | + } |
| 389 | + size_t rows_at = open_array(&b); |
| 390 | + uint32_t rows = 0; |
| 391 | + while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 392 | + put_byte(&b, 0xdd); |
| 393 | + put_be(&b, (uint64_t)columns, 4); |
| 394 | + for (int i = 0; i < columns; i++) put_column(&b, stmt, i); |
| 395 | + rows++; |
| 396 | + } |
| 397 | + close_array(&b, rows_at, rows); |
| 398 | + if (rc != SQLITE_DONE) { |
| 399 | + tl_value e = fail_db(ctx, db); |
| 400 | + sqlite3_finalize(stmt); |
| 401 | + free(b.data); |
| 402 | + return e; |
| 403 | + } |
| 404 | + int64_t changes = sqlite3_stmt_readonly(stmt) ? 0 : sqlite3_changes64(db); |
| 405 | + sqlite3_finalize(stmt); |
| 406 | + put_int(&b, changes); |
| 407 | + put_int(&b, sqlite3_last_insert_rowid(db)); |
| 408 | + if (b.failed) { |
| 409 | + free(b.data); |
| 410 | + return fail(ctx, SQLITE_NOMEM, "out of memory"); |
| 411 | + } |
| 412 | + tl_value out = tl_bytes_from(ctx, b.data, b.len); |
| 413 | + free(b.data); |
| 414 | + return tl_ok(ctx, out); |
| 415 | +} |
| 416 | + |
| 417 | +/* run(db, sql, params) -> Result<Bytes, String>: one statement, every row. */ |
| 418 | +tl_value louise_sqlite_run(tl_ctx *ctx, tl_value resource, tl_value sql, tl_value params) { |
| 419 | + conn *c = tl_resource_get(resource); |
| 420 | + pthread_mutex_lock(&c->lock); |
| 421 | + tl_value out = c->db == NULL ? fail(ctx, SQLITE_MISUSE, "the connection is closed") |
| 422 | + : run_locked(ctx, c->db, sql, params); |
| 423 | + pthread_mutex_unlock(&c->lock); |
| 424 | + return out; |
| 425 | +} |
| 426 | + |
| 427 | +/* exec(db, script) -> Result<(), String>: statements without parameters |
| 428 | + * (a schema), in order; stops at the first error. */ |
| 429 | +tl_value louise_sqlite_exec(tl_ctx *ctx, tl_value resource, tl_value script) { |
| 430 | + conn *c = tl_resource_get(resource); |
| 431 | + pthread_mutex_lock(&c->lock); |
| 432 | + tl_value out; |
| 433 | + if (c->db == NULL) { |
| 434 | + out = fail(ctx, SQLITE_MISUSE, "the connection is closed"); |
| 435 | + } else { |
| 436 | + char *message = NULL; |
| 437 | + int rc = sqlite3_exec(c->db, tl_string_ptr(script), NULL, NULL, &message); |
| 438 | + if (rc == SQLITE_OK) { |
| 439 | + out = tl_ok(ctx, TL_UNIT); |
| 440 | + } else { |
| 441 | + out = fail(ctx, sqlite3_extended_errcode(c->db), |
| 442 | + message != NULL ? message : sqlite3_errmsg(c->db)); |
| 443 | + } |
| 444 | + sqlite3_free(message); |
| 445 | + } |
| 446 | + pthread_mutex_unlock(&c->lock); |
| 447 | + return out; |
| 448 | +} |
| 449 | + |
| 450 | +/* in_transaction(db) -> Bool: whether a transaction is open on it |
| 451 | + * (autocommit off), so that a connection left mid-transaction by a crashed |
| 452 | + * holder is rolled back before reuse. */ |
| 453 | +tl_value louise_sqlite_in_transaction(tl_ctx *ctx, tl_value resource) { |
| 454 | + (void)ctx; |
| 455 | + conn *c = tl_resource_get(resource); |
| 456 | + pthread_mutex_lock(&c->lock); |
| 457 | + int open = c->db != NULL && !sqlite3_get_autocommit(c->db); |
| 458 | + pthread_mutex_unlock(&c->lock); |
| 459 | + return tl_bool(open); |
| 460 | +} |