-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
database.ts
740 lines (677 loc) · 20.7 KB
/
database.ts
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import ffi from "./ffi.ts";
import { fromFileUrl } from "../deps.ts";
import {
SQLITE3_OPEN_CREATE,
SQLITE3_OPEN_MEMORY,
SQLITE3_OPEN_READONLY,
SQLITE3_OPEN_READWRITE,
SQLITE_BLOB,
SQLITE_FLOAT,
SQLITE_INTEGER,
SQLITE_NULL,
SQLITE_TEXT,
} from "./constants.ts";
import { readCstr, toCString, unwrap } from "./util.ts";
import { RestBindParameters, Statement, STATEMENTS } from "./statement.ts";
import { BlobOpenOptions, SQLBlob } from "./blob.ts";
/** Various options that can be configured when opening Database connection. */
export interface DatabaseOpenOptions {
/** Whether to open database only in read-only mode. By default, this is false. */
readonly?: boolean;
/** Whether to create a new database file at specified path if one does not exist already. By default this is true. */
create?: boolean;
/** Raw SQLite C API flags. Specifying this ignores all other options. */
flags?: number;
/** Opens an in-memory database. */
memory?: boolean;
/** Whether to support BigInt columns. False by default, integers larger than 32 bit will be inaccurate. */
int64?: boolean;
/** Apply agressive optimizations that are not possible with concurrent clients. */
unsafeConcurrency?: boolean;
}
/** Transaction function created using `Database#transaction`. */
export type Transaction<T> = ((v: T) => void) & {
/** BEGIN */
default: Transaction<T>;
/** BEGIN DEFERRED */
deferred: Transaction<T>;
/** BEGIN IMMEDIATE */
immediate: Transaction<T>;
/** BEGIN EXCLUSIVE */
exclusive: Transaction<T>;
database: Database;
};
/**
* Options for user-defined functions.
*
* @link https://www.sqlite.org/c3ref/c_deterministic.html
*/
export interface FunctionOptions {
varargs?: boolean;
deterministic?: boolean;
directOnly?: boolean;
innocuous?: boolean;
subtype?: boolean;
}
export interface AggregateFunctionOptions extends FunctionOptions {
start: any | (() => any);
step: (aggregate: any, ...args: any[]) => void;
final?: (aggregate: any) => any;
}
const {
sqlite3_open_v2,
sqlite3_close_v2,
sqlite3_changes,
sqlite3_total_changes,
sqlite3_last_insert_rowid,
sqlite3_get_autocommit,
sqlite3_exec,
sqlite3_free,
sqlite3_libversion,
sqlite3_sourceid,
sqlite3_complete,
sqlite3_finalize,
sqlite3_result_blob,
sqlite3_result_double,
sqlite3_result_error,
sqlite3_result_int64,
sqlite3_result_null,
sqlite3_result_text,
sqlite3_value_blob,
sqlite3_value_bytes,
sqlite3_value_double,
sqlite3_value_int64,
sqlite3_value_text,
sqlite3_value_type,
sqlite3_create_function,
sqlite3_result_int,
sqlite3_aggregate_context,
} = ffi;
/** SQLite version string */
export const SQLITE_VERSION = readCstr(sqlite3_libversion());
/** SQLite source ID string */
export const SQLITE_SOURCEID = readCstr(sqlite3_sourceid());
/**
* Whether the given SQL statement is complete.
*
* @param statement SQL statement string
*/
export function isComplete(statement: string): boolean {
return Boolean(sqlite3_complete(toCString(statement)));
}
/**
* Represents a SQLite3 database connection.
*
* Example:
* ```ts
* // Open a database from file, creates if doesn't exist.
* const db = new Database("myfile.db");
*
* // Open an in-memory database.
* const db = new Database(":memory:");
*
* // Open a read-only database.
* const db = new Database("myfile.db", { readonly: true });
*
* // Or open using File URL
* const db = new Database(new URL("./myfile.db", import.meta.url));
* ```
*/
export class Database {
#path: string;
#handle: Deno.PointerValue;
#open = true;
/** Whether to support BigInt columns. False by default, integers larger than 32 bit will be inaccurate. */
int64: boolean;
unsafeConcurrency: boolean;
/** Whether DB connection is open */
get open(): boolean {
return this.#open;
}
/** Unsafe Raw (pointer) to the sqlite object */
get unsafeHandle(): Deno.PointerValue {
return this.#handle;
}
/** Path of the database file. */
get path(): string {
return this.#path;
}
/** Number of rows changed by the last executed statement. */
get changes(): number {
return sqlite3_changes(this.#handle);
}
/** Number of rows changed since the database connection was opened. */
get totalChanges(): number {
return sqlite3_total_changes(this.#handle);
}
/** Gets last inserted Row ID */
get lastInsertRowId(): number {
return Number(sqlite3_last_insert_rowid(this.#handle));
}
/** Whether autocommit is enabled. Enabled by default, cab be disabled using BEGIN statement. */
get autocommit(): boolean {
return sqlite3_get_autocommit(this.#handle) === 1;
}
/** Whether DB is in mid of a transaction */
get inTransaction(): boolean {
return this.#open && !this.autocommit;
}
constructor(path: string | URL, options: DatabaseOpenOptions = {}) {
this.#path = path instanceof URL ? fromFileUrl(path) : path;
let flags = 0;
this.int64 = options.int64 ?? false;
this.unsafeConcurrency = options.unsafeConcurrency ?? false;
if (options.flags !== undefined) {
flags = options.flags;
} else {
if (options.memory) {
flags |= SQLITE3_OPEN_MEMORY;
}
if (options.readonly ?? false) {
flags |= SQLITE3_OPEN_READONLY;
} else {
flags |= SQLITE3_OPEN_READWRITE;
}
if (options.create ?? true) {
flags |= SQLITE3_OPEN_CREATE;
}
}
const pHandle = new Uint32Array(2);
const result = sqlite3_open_v2(toCString(this.#path), pHandle, flags, 0);
this.#handle = pHandle[0] + 2 ** 32 * pHandle[1];
if (result !== 0) sqlite3_close_v2(this.#handle);
unwrap(result);
}
/**
* Creates a new Prepared Statement from the given SQL statement.
*
* Example:
* ```ts
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");
*
* for (const row of stmt.all(1)) {
* console.log(row);
* }
* ```
*
* Bind parameters can be either provided as an array of values, or as an object
* mapping the parameter name to the value.
*
* Example:
* ```ts
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");
* const row = stmt.get(1);
*
* // or
*
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = :id");
* const row = stmt.get({ id: 1 });
* ```
*
* Statements are automatically freed once GC catches them, however
* you can also manually free using `finalize` method.
*
* @param sql SQL statement string
* @returns Statement object
*/
prepare(sql: string): Statement {
return new Statement(this, sql);
}
/**
* Open a Blob for incremental I/O.
*
* Make sure to close the blob after you are done with it,
* otherwise you will have memory leaks.
*/
openBlob(options: BlobOpenOptions): SQLBlob {
return new SQLBlob(this, options);
}
/**
* Simply executes the SQL statement (supports multiple statements separated by semicolon).
* Returns the number of changes made by last statement.
*
* Example:
* ```ts
* // Create table
* db.exec("create table users (id integer not null, username varchar(20) not null)");
*
* // Inserts
* db.exec("insert into users (id, username) values(?, ?)", id, username);
*
* // Insert with named parameters
* db.exec("insert into users (id, username) values(:id, :username)", { id, username });
*
* // Pragma statements
* db.exec("pragma journal_mode = WAL");
* db.exec("pragma synchronous = normal");
* db.exec("pragma temp_store = memory");
* ```
*
* Under the hood, it uses `sqlite3_exec` if no parameters are given to bind
* with the SQL statement, a prepared statement otherwise.
*/
exec(sql: string, ...params: RestBindParameters): number {
if (params.length === 0) {
const pErr = new Uint32Array(2);
sqlite3_exec(this.#handle, toCString(sql), 0, 0, pErr);
const errPtr = pErr[0] + 2 ** 32 * pErr[1];
if (errPtr !== 0) {
const err = readCstr(errPtr);
sqlite3_free(errPtr);
throw new Error(err);
}
return sqlite3_changes(this.#handle);
}
const stmt = this.prepare(sql);
stmt.run(...params);
return sqlite3_changes(this.#handle);
}
/** Alias for `exec`. */
run(sql: string, ...params: RestBindParameters): number {
return this.exec(sql, ...params);
}
/**
* Wraps a callback function in a transaction.
*
* - When function is called, the transaction is started.
* - When function returns, the transaction is committed.
* - When function throws an error, the transaction is rolled back.
*
* Example:
* ```ts
* const stmt = db.prepare("insert into users (id, username) values(?, ?)");
*
* interface User {
* id: number;
* username: string;
* }
*
* const insertUsers = db.transaction((data: User[]) => {
* for (const user of data) {
* stmt.run(user);
* }
* });
*
* insertUsers([
* { id: 1, username: "alice" },
* { id: 2, username: "bob" },
* ]);
*
* // May also use `insertUsers.deferred`, `immediate`, or `exclusive`.
* // They corresspond to using `BEGIN DEFERRED`, `BEGIN IMMEDIATE`, and `BEGIN EXCLUSIVE`.
* // For eg.
*
* insertUsers.deferred([
* { id: 1, username: "alice" },
* { id: 2, username: "bob" },
* ]);
* ```
*/
transaction<T = any>(
fn: (this: Transaction<T>, _: T) => unknown,
): Transaction<T> {
// Based on https://github.com/WiseLibs/better-sqlite3/blob/master/lib/methods/transaction.js
const controller = getController(this);
// Each version of the transaction function has these same properties
const properties = {
default: { value: wrapTransaction(fn, this, controller.default) },
deferred: { value: wrapTransaction(fn, this, controller.deferred) },
immediate: { value: wrapTransaction(fn, this, controller.immediate) },
exclusive: { value: wrapTransaction(fn, this, controller.exclusive) },
database: { value: this, enumerable: true },
};
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 as any as Transaction<T>;
}
#callbacks = new Set<Deno.UnsafeCallback>();
/**
* Creates a new user-defined function.
*
* Example:
* ```ts
* db.function("add", (a: number, b: number) => a + b);
* db.prepare("select add(1, 2)").value<[number]>()!; // [3]
* ```
*/
function(
name: string,
fn: CallableFunction,
options?: FunctionOptions,
): void {
const cb = new Deno.UnsafeCallback(
{
parameters: ["pointer", "i32", "pointer"],
result: "void",
} as const,
(ctx, nArgs, pArgs) => {
const argptr = new Deno.UnsafePointerView(pArgs);
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Number(argptr.getBigUint64(i * 8));
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
args.push(sqlite3_value_int64(arg));
break;
case SQLITE_FLOAT:
args.push(sqlite3_value_double(arg));
break;
case SQLITE_TEXT:
args.push(
new TextDecoder().decode(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_text(arg),
sqlite3_value_bytes(arg),
),
),
),
);
break;
case SQLITE_BLOB:
args.push(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_blob(arg),
sqlite3_value_bytes(arg),
),
),
);
break;
case SQLITE_NULL:
args.push(null);
break;
default:
throw new Error(`Unknown type: ${type}`);
}
}
let result: any;
try {
result = fn(...args);
} catch (err) {
const buf = new TextEncoder().encode(err.message);
sqlite3_result_error(ctx, buf, buf.byteLength);
return;
}
if (result === undefined || result === null) {
sqlite3_result_null(ctx);
} else if (typeof result === "boolean") {
sqlite3_result_int(ctx, result ? 1 : 0);
} else if (typeof result === "number") {
if (Number.isSafeInteger(result)) sqlite3_result_int64(ctx, result);
else sqlite3_result_double(ctx, result);
} else if (typeof result === "bigint") {
sqlite3_result_int64(ctx, result);
} else if (typeof result === "string") {
const buffer = new TextEncoder().encode(result);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0);
} else if (result instanceof Uint8Array) {
sqlite3_result_blob(ctx, result, result.length, -1);
} else {
const buffer = new TextEncoder().encode(
`Invalid return value: ${Deno.inspect(result)}`,
);
sqlite3_result_error(ctx, buffer, buffer.byteLength);
}
},
);
let flags = 1;
if (options?.deterministic) {
flags |= 0x000000800;
}
if (options?.directOnly) {
flags |= 0x000080000;
}
if (options?.subtype) {
flags |= 0x000100000;
}
if (options?.directOnly) {
flags |= 0x000200000;
}
const err = sqlite3_create_function(
this.#handle,
toCString(name),
options?.varargs ? -1 : fn.length,
flags,
0,
cb.pointer,
0,
0,
);
unwrap(err, this.#handle);
this.#callbacks.add(cb as Deno.UnsafeCallback);
}
/**
* Creates a new user-defined aggregate function.
*/
aggregate(name: string, options: AggregateFunctionOptions): void {
const contexts = new Map<Deno.PointerValue, any>();
const cb = new Deno.UnsafeCallback(
{
parameters: ["pointer", "i32", "pointer"],
result: "void",
} as const,
(ctx, nArgs, pArgs) => {
const aggrCtx = sqlite3_aggregate_context(ctx, 8);
let aggregate;
if (contexts.has(aggrCtx)) {
aggregate = contexts.get(aggrCtx);
} else {
aggregate = typeof options.start === "function"
? options.start()
: options.start;
contexts.set(aggrCtx, aggregate);
}
const argptr = new Deno.UnsafePointerView(pArgs);
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Number(argptr.getBigUint64(i * 8));
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
args.push(sqlite3_value_int64(arg));
break;
case SQLITE_FLOAT:
args.push(sqlite3_value_double(arg));
break;
case SQLITE_TEXT:
args.push(
new TextDecoder().decode(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_text(arg),
sqlite3_value_bytes(arg),
),
),
),
);
break;
case SQLITE_BLOB:
args.push(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_blob(arg),
sqlite3_value_bytes(arg),
),
),
);
break;
case SQLITE_NULL:
args.push(null);
break;
default:
throw new Error(`Unknown type: ${type}`);
}
}
let result: any;
try {
result = options.step(aggregate, ...args);
} catch (err) {
const buf = new TextEncoder().encode(err.message);
sqlite3_result_error(ctx, buf, buf.byteLength);
return;
}
contexts.set(aggrCtx, result);
},
);
const cbFinal = new Deno.UnsafeCallback(
{
parameters: ["pointer"],
result: "void",
} as const,
(ctx) => {
const aggrCtx = sqlite3_aggregate_context(ctx, 0);
const aggregate = contexts.get(aggrCtx);
contexts.delete(aggrCtx);
let result: any;
try {
result = options.final ? options.final(aggregate) : aggregate;
} catch (err) {
const buf = new TextEncoder().encode(err.message);
sqlite3_result_error(ctx, buf, buf.byteLength);
return;
}
if (result === undefined || result === null) {
sqlite3_result_null(ctx);
} else if (typeof result === "boolean") {
sqlite3_result_int(ctx, result ? 1 : 0);
} else if (typeof result === "number") {
if (Number.isSafeInteger(result)) sqlite3_result_int64(ctx, result);
else sqlite3_result_double(ctx, result);
} else if (typeof result === "bigint") {
sqlite3_result_int64(ctx, result);
} else if (typeof result === "string") {
const buffer = new TextEncoder().encode(result);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0);
} else if (result instanceof Uint8Array) {
sqlite3_result_blob(ctx, result, result.length, -1);
} else {
const buffer = new TextEncoder().encode(
`Invalid return value: ${Deno.inspect(result)}`,
);
sqlite3_result_error(ctx, buffer, buffer.byteLength);
}
},
);
let flags = 1;
if (options?.deterministic) {
flags |= 0x000000800;
}
if (options?.directOnly) {
flags |= 0x000080000;
}
if (options?.subtype) {
flags |= 0x000100000;
}
if (options?.directOnly) {
flags |= 0x000200000;
}
const err = sqlite3_create_function(
this.#handle,
toCString(name),
options?.varargs ? -1 : options.step.length - 1,
flags,
0,
0,
cb.pointer,
cbFinal.pointer,
);
unwrap(err, this.#handle);
this.#callbacks.add(cb as Deno.UnsafeCallback);
this.#callbacks.add(cbFinal as Deno.UnsafeCallback);
}
/**
* Closes the database connection.
*
* Calling this method more than once is no-op.
*/
close(): void {
if (!this.#open) return;
for (const [stmt, db] of STATEMENTS) {
if (db === this.#handle) {
sqlite3_finalize(stmt);
STATEMENTS.delete(stmt);
}
}
for (const cb of this.#callbacks) {
cb.close();
}
unwrap(sqlite3_close_v2(this.#handle));
this.#open = false;
}
[Symbol.for("Deno.customInspect")](): string {
return `SQLite3.Database { path: ${this.path} }`;
}
}
const controllers = new WeakMap();
// Return the database's cached transaction controller, or create a new one
const getController = (db: Database) => {
let controller = controllers.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`"),
};
controllers.set(
db,
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,
),
},
);
}
return controller;
};
// Return a new transaction function by wrapping the given function
const wrapTransaction = (
fn: any,
db: Database,
{ begin, commit, rollback, savepoint, release, rollbackTo }: any,
) =>
function sqliteTransaction(): any {
const { apply } = Function.prototype;
let before, after, undo;
if (db.inTransaction) {
before = savepoint;
after = release;
undo = rollbackTo;
} else {
before = begin;
after = commit;
undo = rollback;
}
before.run();
try {
// @ts-ignore An outer value of 'this' is shadowed by this container.
const result = apply.call(fn, this, arguments);
after.run();
return result;
} catch (ex) {
if (!db.autocommit) {
undo.run();
if (undo !== rollback) after.run();
}
throw ex;
}
};