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
|
var symbolFor = Symbol.for, lazy = globalThis[symbolFor("Bun.lazy")];
if (!lazy || typeof lazy !== "function")
throw new Error("Something went wrong while loading Bun. Expected 'Bun.lazy' to be defined.");
var defineProperties = Object.defineProperties, toStringTag = Symbol.toStringTag, apply = Function.prototype.apply, isArray = Array.isArray, isTypedArray = ArrayBuffer.isView, constants = {
SQLITE_OPEN_READONLY: 1,
SQLITE_OPEN_READWRITE: 2,
SQLITE_OPEN_CREATE: 4,
SQLITE_OPEN_DELETEONCLOSE: 8,
SQLITE_OPEN_EXCLUSIVE: 16,
SQLITE_OPEN_AUTOPROXY: 32,
SQLITE_OPEN_URI: 64,
SQLITE_OPEN_MEMORY: 128,
SQLITE_OPEN_MAIN_DB: 256,
SQLITE_OPEN_TEMP_DB: 512,
SQLITE_OPEN_TRANSIENT_DB: 1024,
SQLITE_OPEN_MAIN_JOURNAL: 2048,
SQLITE_OPEN_TEMP_JOURNAL: 4096,
SQLITE_OPEN_SUBJOURNAL: 8192,
SQLITE_OPEN_SUPER_JOURNAL: 16384,
SQLITE_OPEN_NOMUTEX: 32768,
SQLITE_OPEN_FULLMUTEX: 65536,
SQLITE_OPEN_SHAREDCACHE: 131072,
SQLITE_OPEN_PRIVATECACHE: 262144,
SQLITE_OPEN_WAL: 524288,
SQLITE_OPEN_NOFOLLOW: 16777216,
SQLITE_OPEN_EXRESCODE: 33554432,
SQLITE_PREPARE_PERSISTENT: 1,
SQLITE_PREPARE_NORMALIZE: 2,
SQLITE_PREPARE_NO_VTAB: 4
}, SQL, _SQL, controllers;
class Statement {
constructor(raw) {
switch (this.#raw = raw, raw.paramsCount) {
case 0: {
this.get = this.#getNoArgs, this.all = this.#allNoArgs, this.values = this.#valuesNoArgs, this.run = this.#runNoArgs;
break;
}
default: {
this.get = this.#get, this.all = this.#all, this.values = this.#values, this.run = this.#run;
break;
}
}
}
#raw;
get;
all;
values;
run;
isFinalized = !1;
toJSON() {
return {
sql: this.native.toString(),
isFinalized: this.isFinalized,
paramsCount: this.paramsCount,
columnNames: this.columnNames
};
}
get [toStringTag]() {
return `"${this.native.toString()}"`;
}
toString() {
return this.native.toString();
}
get native() {
return this.#raw;
}
#getNoArgs() {
return this.#raw.get();
}
#allNoArgs() {
return this.#raw.all();
}
#valuesNoArgs() {
return this.#raw.values();
}
#runNoArgs() {
this.#raw.run();
}
#get(...args) {
if (args.length === 0)
return this.#getNoArgs();
var arg0 = args[0];
return !isArray(arg0) && (!arg0 || typeof arg0 !== "object" || isTypedArray(arg0)) ? this.#raw.get(args) : this.#raw.get(...args);
}
#all(...args) {
if (args.length === 0)
return this.#allNoArgs();
var arg0 = args[0];
return !isArray(arg0) && (!arg0 || typeof arg0 !== "object" || isTypedArray(arg0)) ? this.#raw.all(args) : this.#raw.all(...args);
}
#values(...args) {
if (args.length === 0)
return this.#valuesNoArgs();
var arg0 = args[0];
return !isArray(arg0) && (!arg0 || typeof arg0 !== "object" || isTypedArray(arg0)) ? this.#raw.values(args) : this.#raw.values(...args);
}
#run(...args) {
if (args.length === 0)
return this.#runNoArgs();
var arg0 = args[0];
!isArray(arg0) && (!arg0 || typeof arg0 !== "object" || isTypedArray(arg0)) ? this.#raw.run(args) : this.#raw.run(...args);
}
get columnNames() {
return this.#raw.columns;
}
get paramsCount() {
return this.#raw.paramsCount;
}
finalize(...args) {
return this.isFinalized = !0, this.#raw.finalize(...args);
}
}
var cachedCount = symbolFor("Bun.Database.cache.count");
class Database {
constructor(filenameGiven, options) {
if (typeof filenameGiven === "undefined")
;
else if (typeof filenameGiven !== "string") {
if (isTypedArray(filenameGiven)) {
this.#handle = Database.deserialize(filenameGiven, typeof options === "object" && options ? !!options.readonly : ((options | 0) & constants.SQLITE_OPEN_READONLY) != 0), this.filename = ":memory:";
return;
}
throw new TypeError(`Expected 'filename' to be a string, got '${typeof filenameGiven}'`);
}
var filename = typeof filenameGiven === "string" ? filenameGiven.trim() : ":memory:", flags = constants.SQLITE_OPEN_READWRITE | constants.SQLITE_OPEN_CREATE;
if (typeof options === "object" && options) {
if (flags = 0, options.readonly)
flags = constants.SQLITE_OPEN_READONLY;
if ("readOnly" in options)
throw new TypeError('Misspelled option "readOnly" should be "readonly"');
if (options.create)
flags = constants.SQLITE_OPEN_READWRITE | constants.SQLITE_OPEN_CREATE;
if (options.readwrite)
flags |= constants.SQLITE_OPEN_READWRITE;
} else if (typeof options === "number")
flags = options;
const anonymous = filename === "" || filename === ":memory:";
if (anonymous && (flags & constants.SQLITE_OPEN_READONLY) !== 0)
throw new Error("Cannot open an anonymous database in read-only mode.");
if (!SQL)
_SQL = SQL = lazy("sqlite");
this.#handle = SQL.open(anonymous ? ":memory:" : filename, flags), this.filename = filename;
}
#handle;
#cachedQueriesKeys = [];
#cachedQueriesLengths = [];
#cachedQueriesValues = [];
filename;
get handle() {
return this.#handle;
}
get inTransaction() {
return SQL.isInTransaction(this.#handle);
}
static open(filename, options) {
return new Database(filename, options);
}
loadExtension(name, entryPoint) {
return SQL.loadExtension(this.#handle, name, entryPoint);
}
serialize(optionalName) {
return SQL.serialize(this.#handle, optionalName || "main");
}
static deserialize(serialized, isReadOnly = !1) {
if (!SQL)
_SQL = SQL = lazy("sqlite");
return SQL.deserialize(serialized, isReadOnly);
}
static setCustomSQLite(path) {
if (!SQL)
_SQL = SQL = lazy("sqlite");
return SQL.setCustomSQLite(path);
}
close() {
return this.clearQueryCache(), SQL.close(this.#handle);
}
clearQueryCache() {
for (let item of this.#cachedQueriesValues)
item.finalize();
this.#cachedQueriesKeys.length = 0, this.#cachedQueriesValues.length = 0, this.#cachedQueriesLengths.length = 0;
}
run(query, ...params) {
if (params.length === 0) {
SQL.run(this.#handle, query);
return;
}
var arg0 = params[0];
return !isArray(arg0) && (!arg0 || typeof arg0 !== "object" || isTypedArray(arg0)) ? SQL.run(this.#handle, query, params) : SQL.run(this.#handle, query, ...params);
}
prepare(query, params, flags) {
return new Statement(SQL.prepare(this.#handle, query, params, flags || 0));
}
static MAX_QUERY_CACHE_SIZE = 20;
get [cachedCount]() {
return this.#cachedQueriesKeys.length;
}
query(query) {
if (typeof query !== "string")
throw new TypeError(`Expected 'query' to be a string, got '${typeof query}'`);
if (query.length === 0)
throw new Error("SQL query cannot be empty.");
var index = this.#cachedQueriesLengths.indexOf(query.length);
while (index !== -1) {
if (this.#cachedQueriesKeys[index] !== query) {
index = this.#cachedQueriesLengths.indexOf(query.length, index + 1);
continue;
}
var stmt = this.#cachedQueriesValues[index];
if (stmt.isFinalized)
return this.#cachedQueriesValues[index] = this.prepare(query, void 0, willCache ? constants.SQLITE_PREPARE_PERSISTENT : 0);
return stmt;
}
const willCache = this.#cachedQueriesKeys.length < Database.MAX_QUERY_CACHE_SIZE;
var stmt = this.prepare(query, void 0, willCache ? constants.SQLITE_PREPARE_PERSISTENT : 0);
if (willCache)
this.#cachedQueriesKeys.push(query), this.#cachedQueriesLengths.push(query.length), this.#cachedQueriesValues.push(stmt);
return stmt;
}
transaction(fn, self) {
if (typeof fn !== "function")
throw new TypeError("Expected first argument to be a function");
const db = this, controller = getController(db, self), 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)
},
database: { value: this, enumerable: !0 }
};
return defineProperties(properties.default.value, properties), defineProperties(properties.deferred.value, properties), defineProperties(properties.immediate.value, properties), defineProperties(properties.exclusive.value, properties), properties.default.value;
}
}
Database.prototype.exec = Database.prototype.run;
var getController = (db, self) => {
let controller = (controllers ||= new WeakMap).get(db);
if (!controller) {
const shared = {
commit: db.prepare("COMMIT", void 0, 0),
rollback: db.prepare("ROLLBACK", void 0, 0),
savepoint: db.prepare("SAVEPOINT `\t_bs3.\t`", void 0, 0),
release: db.prepare("RELEASE `\t_bs3.\t`", void 0, 0),
rollbackTo: db.prepare("ROLLBACK TO `\t_bs3.\t`", void 0, 0)
};
controllers.set(db, controller = {
default: Object.assign({ begin: db.prepare("BEGIN", void 0, 0) }, shared),
deferred: Object.assign({ begin: db.prepare("BEGIN DEFERRED", void 0, 0) }, shared),
immediate: Object.assign({ begin: db.prepare("BEGIN IMMEDIATE", void 0, 0) }, shared),
exclusive: Object.assign({ begin: db.prepare("BEGIN EXCLUSIVE", void 0, 0) }, shared)
});
}
return controller;
}, wrapTransaction = (fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function transaction(...args) {
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 = fn.apply(this, args);
return after.run(), result;
} catch (ex) {
if (db.inTransaction) {
if (undo.run(), undo !== rollback)
after.run();
}
throw ex;
}
};
export {
_SQL as native,
Database as default,
constants,
Statement,
Database
};
//# debugId=75DCD993FF1B670964756e2164756e21
|