-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsqlite.js
More file actions
185 lines (148 loc) · 5.23 KB
/
sqlite.js
File metadata and controls
185 lines (148 loc) · 5.23 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
const sqlite3 = globalThis.sqlite;
const kSqlite3Handle = Symbol('kSqlite3Handle');
let controllers;
class Database {
constructor(dbName = ':memory:', options = { create: true, readOnly: false }) {
let flags = 0;
if (options.create) {
flags |= sqlite3.SQLITE_OPEN_CREATE;
}
if (options.readOnly) {
flags |= sqlite3.SQLITE_OPEN_READONLY;
} else {
flags |= sqlite3.SQLITE_OPEN_READWRITE;
}
this[kSqlite3Handle] = sqlite3.open(dbName, flags);
}
close() {
let ret = false;
if (this[kSqlite3Handle]) {
ret = sqlite3.close(this[kSqlite3Handle]);
this[kSqlite3Handle] = null;
}
return ret;
}
exec(sql) {
if (!this[kSqlite3Handle]) {
throw new Error('Invalid DB');
}
return sqlite3.exec(this[kSqlite3Handle], sql);
}
prepare(sql) {
if (!this[kSqlite3Handle]) {
throw new Error('Invalid DB');
}
return new Statement(sqlite3.prepare(this[kSqlite3Handle], sql));
}
// Code for transactions is largely copied from better-sqlite3 and Bun
// https://github.com/JoshuaWise/better-sqlite3/blob/master/lib/methods/transaction.js
// https://github.com/oven-sh/bun/blob/main/src/js/bun/sqlite.ts
get inTransaction() {
if (!this[kSqlite3Handle]) {
return false;
}
return sqlite3.in_transaction(this[kSqlite3Handle]);
}
transaction(fn) {
if (typeof fn !== 'function') {
throw new TypeError('Expected first argument to be a function');
}
const db = this;
const controller = getController(db);
// Each version of the transaction function has these same properties.
const properties = {
default: { value: wrapTransaction(fn, db, controller.default) },
deferred: { value: wrapTransaction(fn, db, controller.deferred) },
immediate: { value: wrapTransaction(fn, db, controller.immediate) },
exclusive: { value: wrapTransaction(fn, db, controller.exclusive) },
};
Object.defineProperties(properties.default.value, properties);
Object.defineProperties(properties.deferred.value, properties);
Object.defineProperties(properties.immediate.value, properties);
Object.defineProperties(properties.exclusive.value, properties);
// Return the default version of the transaction function.
return properties.default.value;
}
loadExtension(file, entrypoint=undefined) {
return sqlite3.load_extension(this[kSqlite3Handle],file,entrypoint);
}
}
// Return the database's cached transaction controller, or create a new one.
const getController = db => {
let controller = (controllers ||= new WeakMap()).get(db);
if (!controller) {
const shared = {
commit: db.prepare('COMMIT'),
rollback: db.prepare('ROLLBACK'),
savepoint: db.prepare('SAVEPOINT `\t_bs3.\t`'),
release: db.prepare('RELEASE `\t_bs3.\t`'),
rollbackTo: db.prepare('ROLLBACK TO `\t_bs3.\t`'),
};
controller = {
default: Object.assign({ begin: db.prepare('BEGIN') }, shared),
deferred: Object.assign({ begin: db.prepare('BEGIN DEFERRED') }, shared),
immediate: Object.assign({ begin: db.prepare('BEGIN IMMEDIATE') }, shared),
exclusive: Object.assign({ begin: db.prepare('BEGIN EXCLUSIVE') }, shared),
};
controllers.set(db, controller);
}
return controller;
};
// Return a new transaction function by wrapping the given function.
const wrapTransaction = (fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) =>
function transaction() {
let before, after, undo;
if (db.inTransaction) {
before = savepoint;
after = release;
undo = rollbackTo;
} else {
before = begin;
after = commit;
undo = rollback;
}
try {
before.run();
const result = Function.prototype.apply.call(fn, this, arguments);
after.run();
return result;
} catch (ex) {
if (db.inTransaction) {
undo.run();
if (undo !== rollback) {
after.run();
}
}
throw ex;
}
};
const kSqlite3Stmt = Symbol('kSqlite3Stmt');
class Statement {
constructor(stmt) {
this[kSqlite3Stmt] = stmt;
}
finalize() {
return sqlite3.stmt_finalize(this[kSqlite3Stmt]);
}
toString() {
return sqlite3.stmt_expand(this[kSqlite3Stmt]);
}
all(...args) {
if (args && args.length === 1 && typeof args[0] === 'object') {
args = args[0];
}
return sqlite3.stmt_all(this[kSqlite3Stmt], args);
}
run(...args) {
if (args && args.length === 1 && typeof args[0] === 'object') {
args = args[0];
}
return sqlite3.stmt_run(this[kSqlite3Stmt], args);
}
}
Object.defineProperty(globalThis, 'DataBase', {
enumerable: true,
configurable: true,
writable: true,
value: Database
});