-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteDatabase.js
100 lines (82 loc) · 2.45 KB
/
SQLiteDatabase.js
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
'use strict';
const Database = require("@farjs/better-sqlite3-wrapper");
const SQLiteResult = require('@expo/websql/lib/sqlite/SQLiteResult');
const READ_ONLY_ERROR = new Error('could not prepare statement (23 not authorized)');
function SQLiteDatabase(name) {
const db = new Database(name);
db._lastInsertRowIdQuery = db.prepare("SELECT last_insert_rowid() AS id;");
db._changesQuery = db.prepare("SELECT changes() AS changes;");
this._db = db;
}
function runSelect(db, sql, args, cb) {
let err;
let resultSet;
try {
const rows = db.prepare(sql).all(args);
const insertId = undefined;
const rowsAffected = 0;
resultSet = new SQLiteResult(null, insertId, rowsAffected, rows);
}
catch (e) {
err = e;
}
if (err) {
return cb(new SQLiteResult(err));
}
cb(resultSet);
}
function runNonSelect(db, sql, args, cb) {
let err;
let resultSet;
try {
db.prepare(sql).run(args);
const insertId = db._lastInsertRowIdQuery.get().id;
const rowsAffected = db._changesQuery.get().changes;
const rows = [];
resultSet = new SQLiteResult(null, insertId, rowsAffected, rows);
}
catch (e) {
err = e;
}
if (err) {
return cb(new SQLiteResult(err));
}
cb(resultSet);
}
SQLiteDatabase.prototype.exec = function exec(queries, readOnly, callback) {
const db = this._db;
const len = queries.length;
const results = new Array(len);
var i = 0;
function checkDone() {
if (++i === len) {
callback(null, results);
} else {
doNext();
}
}
function onQueryComplete(i) {
return function (res) {
results[i] = res;
checkDone();
};
}
function doNext() {
const query = queries[i];
const sql = query.sql;
const args = query.args;
// We try to sniff whether it's a SELECT query or not.
// This is inherently error-prone, although it will probably work in the 99%
// case.
const isSelect = /^\s*SELECT\b/i.test(sql);
if (readOnly && !isSelect) {
onQueryComplete(i)(new SQLiteResult(READ_ONLY_ERROR));
} else if (isSelect) {
runSelect(db, sql, args, onQueryComplete(i));
} else {
runNonSelect(db, sql, args, onQueryComplete(i));
}
}
doNext();
};
module.exports = SQLiteDatabase;