|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Database = require("@farjs/better-sqlite3-wrapper"); |
| 4 | +const SQLiteResult = require('@expo/websql/lib/sqlite/SQLiteResult'); |
| 5 | + |
| 6 | +const READ_ONLY_ERROR = new Error('could not prepare statement (23 not authorized)'); |
| 7 | + |
| 8 | +function SQLiteDatabase(name) { |
| 9 | + const db = new Database(name); |
| 10 | + db.pragma('journal_mode = WAL'); |
| 11 | + db._lastInsertRowIdQuery = db.prepare("SELECT last_insert_rowid() AS id;"); |
| 12 | + db._changesQuery = db.prepare("SELECT changes() AS changes;"); |
| 13 | + this._db = db; |
| 14 | +} |
| 15 | + |
| 16 | +function runSelect(db, sql, args, cb) { |
| 17 | + let err; |
| 18 | + let resultSet; |
| 19 | + try { |
| 20 | + const rows = db.prepare(sql).all(args); |
| 21 | + |
| 22 | + const insertId = undefined; |
| 23 | + const rowsAffected = 0; |
| 24 | + resultSet = new SQLiteResult(null, insertId, rowsAffected, rows); |
| 25 | + } |
| 26 | + catch (e) { |
| 27 | + err = e; |
| 28 | + } |
| 29 | + |
| 30 | + if (err) { |
| 31 | + return cb(new SQLiteResult(err)); |
| 32 | + } |
| 33 | + cb(resultSet); |
| 34 | +} |
| 35 | + |
| 36 | +function runNonSelect(db, sql, args, cb) { |
| 37 | + let err; |
| 38 | + let resultSet; |
| 39 | + try { |
| 40 | + db.prepare(sql).run(args); |
| 41 | + |
| 42 | + const insertId = db._lastInsertRowIdQuery.get().id; |
| 43 | + const rowsAffected = db._changesQuery.get().changes; |
| 44 | + const rows = []; |
| 45 | + resultSet = new SQLiteResult(null, insertId, rowsAffected, rows); |
| 46 | + } |
| 47 | + catch (e) { |
| 48 | + err = e; |
| 49 | + } |
| 50 | + |
| 51 | + if (err) { |
| 52 | + return cb(new SQLiteResult(err)); |
| 53 | + } |
| 54 | + cb(resultSet); |
| 55 | +} |
| 56 | + |
| 57 | +SQLiteDatabase.prototype.exec = function exec(queries, readOnly, callback) { |
| 58 | + const db = this._db; |
| 59 | + const len = queries.length; |
| 60 | + const results = new Array(len); |
| 61 | + |
| 62 | + var i = 0; |
| 63 | + |
| 64 | + function checkDone() { |
| 65 | + if (++i === len) { |
| 66 | + callback(null, results); |
| 67 | + } else { |
| 68 | + doNext(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + function onQueryComplete(i) { |
| 73 | + return function (res) { |
| 74 | + results[i] = res; |
| 75 | + checkDone(); |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + function doNext() { |
| 80 | + const query = queries[i]; |
| 81 | + const sql = query.sql; |
| 82 | + const args = query.args; |
| 83 | + |
| 84 | + // We try to sniff whether it's a SELECT query or not. |
| 85 | + // This is inherently error-prone, although it will probably work in the 99% |
| 86 | + // case. |
| 87 | + const isSelect = /^\s*SELECT\b/i.test(sql); |
| 88 | + |
| 89 | + if (readOnly && !isSelect) { |
| 90 | + onQueryComplete(i)(new SQLiteResult(READ_ONLY_ERROR)); |
| 91 | + } else if (isSelect) { |
| 92 | + runSelect(db, sql, args, onQueryComplete(i)); |
| 93 | + } else { |
| 94 | + runNonSelect(db, sql, args, onQueryComplete(i)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + doNext(); |
| 99 | +}; |
| 100 | + |
| 101 | +module.exports = SQLiteDatabase; |
0 commit comments