@yaks/sqlite 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +1596 -2
- package/_dist/archetype.d.ts +75 -0
- package/_dist/archetype.d.ts.map +1 -0
- package/_dist/branches.d.ts +23 -0
- package/_dist/branches.d.ts.map +1 -0
- package/_dist/budget.d.ts +52 -0
- package/_dist/budget.d.ts.map +1 -0
- package/_dist/bundle.d.ts +2 -0
- package/_dist/bundle.d.ts.map +1 -0
- package/_dist/cache.d.ts +24 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/catalog.d.ts +9 -0
- package/_dist/catalog.d.ts.map +1 -0
- package/_dist/check.d.ts +4 -0
- package/_dist/check.d.ts.map +1 -0
- package/_dist/db.d.ts +36 -0
- package/_dist/db.d.ts.map +1 -0
- package/_dist/ddl.d.ts +143 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/doom.d.ts +22 -0
- package/_dist/doom.d.ts.map +1 -0
- package/_dist/follow.d.ts +74 -0
- package/_dist/follow.d.ts.map +1 -0
- package/_dist/history-shapes.d.ts +3 -0
- package/_dist/history-shapes.d.ts.map +1 -0
- package/_dist/history.d.ts +18 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/inspect.d.ts +43 -0
- package/_dist/inspect.d.ts.map +1 -0
- package/_dist/jsonb.d.ts +17 -0
- package/_dist/jsonb.d.ts.map +1 -0
- package/_dist/layout.d.ts +7 -0
- package/_dist/layout.d.ts.map +1 -0
- package/_dist/ledger.d.ts +41 -0
- package/_dist/ledger.d.ts.map +1 -0
- package/_dist/meta.d.ts +89 -0
- package/_dist/meta.d.ts.map +1 -0
- package/_dist/migration.d.ts +41 -0
- package/_dist/migration.d.ts.map +1 -0
- package/_dist/mod.d.ts +201 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mode.d.ts +14 -0
- package/_dist/mode.d.ts.map +1 -0
- package/_dist/overlay.d.ts +30 -0
- package/_dist/overlay.d.ts.map +1 -0
- package/_dist/owe.d.ts +180 -0
- package/_dist/owe.d.ts.map +1 -0
- package/_dist/physical.d.ts +89 -0
- package/_dist/physical.d.ts.map +1 -0
- package/_dist/point.d.ts +10 -0
- package/_dist/point.d.ts.map +1 -0
- package/_dist/raise.d.ts +22 -0
- package/_dist/raise.d.ts.map +1 -0
- package/_dist/read-db.d.ts +9 -0
- package/_dist/read-db.d.ts.map +1 -0
- package/_dist/read-store.d.ts +10 -0
- package/_dist/read-store.d.ts.map +1 -0
- package/_dist/read.d.ts +59 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/reader.d.ts +11 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/rules.d.ts +19 -0
- package/_dist/rules.d.ts.map +1 -0
- package/_dist/service.d.ts +12 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/shape.d.ts +12 -0
- package/_dist/shape.d.ts.map +1 -0
- package/_dist/statements.d.ts +4 -0
- package/_dist/statements.d.ts.map +1 -0
- package/_dist/survey.d.ts +31 -0
- package/_dist/survey.d.ts.map +1 -0
- package/_dist/tools.d.ts +15 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/trash.d.ts +18 -0
- package/_dist/trash.d.ts.map +1 -0
- package/_dist/unit.d.ts +11 -0
- package/_dist/unit.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/write.d.ts +169 -0
- package/_dist/write.d.ts.map +1 -0
- package/answer.js +110 -0
- package/answer.js.map +1 -0
- package/answer.ts +122 -0
- package/archetype.js +730 -0
- package/archetype.js.map +1 -0
- package/archetype.ts +790 -0
- package/binary_cases.ts +240 -0
- package/branches.js +195 -0
- package/branches.js.map +1 -0
- package/branches.ts +257 -0
- package/budget.js +89 -0
- package/budget.js.map +1 -0
- package/budget.ts +117 -0
- package/builtin.js +34 -0
- package/builtin.js.map +1 -0
- package/builtin.ts +31 -0
- package/bundle.js +21 -0
- package/bundle.js.map +1 -0
- package/bundle.ts +22 -0
- package/buried.js +95 -0
- package/buried.js.map +1 -0
- package/buried.ts +124 -0
- package/cache.js +153 -0
- package/cache.js.map +1 -0
- package/cache.ts +154 -0
- package/cache_worker.ts +16 -0
- package/catalog.js +173 -0
- package/catalog.js.map +1 -0
- package/catalog.ts +224 -0
- package/check.js +80 -0
- package/check.js.map +1 -0
- package/check.ts +92 -0
- package/checkpoint.js +98 -0
- package/checkpoint.js.map +1 -0
- package/checkpoint.ts +129 -0
- package/checkpoint_worker.ts +37 -0
- package/db.js +170 -0
- package/db.js.map +1 -0
- package/db.ts +173 -0
- package/ddl.js +1441 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +1508 -0
- package/deno.json +1 -0
- package/doom.js +59 -0
- package/doom.js.map +1 -0
- package/doom.ts +75 -0
- package/driver.js +69 -0
- package/driver.js.map +1 -0
- package/driver.ts +101 -0
- package/error.js +30 -0
- package/error.js.map +1 -0
- package/error.ts +46 -0
- package/ffi/LICENSE +202 -0
- package/ffi/blob.js +99 -0
- package/ffi/blob.js.map +1 -0
- package/ffi/blob.ts +156 -0
- package/ffi/constants.js +65 -0
- package/ffi/constants.js.map +1 -0
- package/ffi/constants.ts +67 -0
- package/ffi/database.js +613 -0
- package/ffi/database.js.map +1 -0
- package/ffi/database.ts +931 -0
- package/ffi/ffi.js +535 -0
- package/ffi/ffi.js.map +1 -0
- package/ffi/ffi.ts +604 -0
- package/ffi/mod.js +15 -0
- package/ffi/mod.js.map +1 -0
- package/ffi/mod.ts +28 -0
- package/ffi/statement.js +555 -0
- package/ffi/statement.js.map +1 -0
- package/ffi/statement.ts +788 -0
- package/ffi/util.js +32 -0
- package/ffi/util.js.map +1 -0
- package/ffi/util.ts +48 -0
- package/follow.js +342 -0
- package/follow.js.map +1 -0
- package/follow.ts +401 -0
- package/history-branch.js +166 -0
- package/history-branch.js.map +1 -0
- package/history-branch.ts +140 -0
- package/history-shapes.js +254 -0
- package/history-shapes.js.map +1 -0
- package/history-shapes.ts +268 -0
- package/history.js +620 -0
- package/history.js.map +1 -0
- package/history.ts +674 -0
- package/inspect.js +120 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +164 -0
- package/jsonb.js +73 -0
- package/jsonb.js.map +1 -0
- package/jsonb.ts +108 -0
- package/layout.js +22 -0
- package/layout.js.map +1 -0
- package/layout.ts +26 -0
- package/ledger.js +210 -0
- package/ledger.js.map +1 -0
- package/ledger.ts +210 -0
- package/legacy.js +4 -0
- package/legacy.js.map +1 -0
- package/legacy.ts +3 -0
- package/maintenance.js +148 -0
- package/maintenance.js.map +1 -0
- package/maintenance.ts +154 -0
- package/measure.js +133 -0
- package/measure.js.map +1 -0
- package/measure.ts +167 -0
- package/measure_worker.ts +18 -0
- package/memo.js +1250 -0
- package/memo.js.map +1 -0
- package/memo.ts +1480 -0
- package/meta.js +265 -0
- package/meta.js.map +1 -0
- package/meta.ts +321 -0
- package/migration.js +272 -0
- package/migration.js.map +1 -0
- package/migration.ts +286 -0
- package/mod.js +1103 -0
- package/mod.js.map +1 -0
- package/mod.ts +1762 -0
- package/mode.js +192 -0
- package/mode.js.map +1 -0
- package/mode.ts +207 -0
- package/native.js +544 -0
- package/native.js.map +1 -0
- package/native.ts +544 -0
- package/overlay.js +408 -0
- package/overlay.js.map +1 -0
- package/overlay.ts +485 -0
- package/owe.js +719 -0
- package/owe.js.map +1 -0
- package/owe.ts +873 -0
- package/package.json +99 -4
- package/physical.js +350 -0
- package/physical.js.map +1 -0
- package/physical.ts +434 -0
- package/plan.js +78 -0
- package/plan.js.map +1 -0
- package/plan.ts +104 -0
- package/point.js +81 -0
- package/point.js.map +1 -0
- package/point.ts +52 -0
- package/population.js +639 -0
- package/population.js.map +1 -0
- package/population.ts +630 -0
- package/purge.js +117 -0
- package/purge.js.map +1 -0
- package/purge.ts +127 -0
- package/raise.js +270 -0
- package/raise.js.map +1 -0
- package/raise.ts +316 -0
- package/read-db.js +381 -0
- package/read-db.js.map +1 -0
- package/read-db.ts +282 -0
- package/read-store.js +74 -0
- package/read-store.js.map +1 -0
- package/read-store.ts +86 -0
- package/read.js +1100 -0
- package/read.js.map +1 -0
- package/read.ts +1441 -0
- package/reader.js +141 -0
- package/reader.js.map +1 -0
- package/reader.ts +159 -0
- package/rules.js +88 -0
- package/rules.js.map +1 -0
- package/rules.ts +113 -0
- package/service.js +27 -0
- package/service.js.map +1 -0
- package/service.ts +41 -0
- package/shape.js +76 -0
- package/shape.js.map +1 -0
- package/shape.ts +108 -0
- package/sparse_presence_fixture.ts +45 -0
- package/sqlitepath.js +24 -0
- package/sqlitepath.js.map +1 -0
- package/sqlitepath.ts +29 -0
- package/statements.js +78 -0
- package/statements.js.map +1 -0
- package/statements.ts +96 -0
- package/survey.js +135 -0
- package/survey.js.map +1 -0
- package/survey.ts +196 -0
- package/tools.js +17 -0
- package/tools.js.map +1 -0
- package/tools.ts +50 -0
- package/trash.js +144 -0
- package/trash.js.map +1 -0
- package/trash.ts +187 -0
- package/unit.js +215 -0
- package/unit.js.map +1 -0
- package/unit.ts +211 -0
- package/versions.js +115 -0
- package/versions.js.map +1 -0
- package/versions.ts +110 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +27 -0
- package/vocab.ts +19 -0
- package/watch.js +33 -0
- package/watch.js.map +1 -0
- package/watch.ts +42 -0
- package/write.js +1164 -0
- package/write.js.map +1 -0
- package/write.ts +1356 -0
- package/written.js +200 -0
- package/written.js.map +1 -0
- package/written.ts +202 -0
package/ffi/database.ts
ADDED
|
@@ -0,0 +1,931 @@
|
|
|
1
|
+
// deno-lint-ignore-file no-explicit-any -- upstream @db/sqlite (./mod.ts)
|
|
2
|
+
import ffi from './ffi.js'
|
|
3
|
+
import { fileURLToPath as fromFileUrl } from 'node:url'
|
|
4
|
+
import {
|
|
5
|
+
SQLITE3_OPEN_CREATE,
|
|
6
|
+
SQLITE3_OPEN_MEMORY,
|
|
7
|
+
SQLITE3_OPEN_READONLY,
|
|
8
|
+
SQLITE3_OPEN_READWRITE,
|
|
9
|
+
SQLITE_BLOB,
|
|
10
|
+
SQLITE_FLOAT,
|
|
11
|
+
SQLITE_INTEGER,
|
|
12
|
+
SQLITE_NULL,
|
|
13
|
+
SQLITE_TEXT,
|
|
14
|
+
} from './constants.js'
|
|
15
|
+
import { readCstr, toCString, unwrap } from './util.js'
|
|
16
|
+
import {
|
|
17
|
+
type RestBindParameters,
|
|
18
|
+
Statement,
|
|
19
|
+
statementsOf,
|
|
20
|
+
} from './statement.js'
|
|
21
|
+
import { type BlobOpenOptions, SQLBlob } from './blob.js'
|
|
22
|
+
|
|
23
|
+
/** Various options that can be configured when opening Database connection. */
|
|
24
|
+
export interface DatabaseOpenOptions {
|
|
25
|
+
/** Whether to open database only in read-only mode. By default, this is false. */
|
|
26
|
+
readonly?: boolean
|
|
27
|
+
/** Whether to create a new database file at specified path if one does not exist already. By default this is true. */
|
|
28
|
+
create?: boolean
|
|
29
|
+
/** Raw SQLite C API flags. Specifying this ignores all other options. */
|
|
30
|
+
flags?: number
|
|
31
|
+
/** Opens an in-memory database. */
|
|
32
|
+
memory?: boolean
|
|
33
|
+
/** Whether to support BigInt columns. False by default, integers larger than 32 bit will be inaccurate. */
|
|
34
|
+
int64?: boolean
|
|
35
|
+
/** Apply agressive optimizations that are not possible with concurrent clients. */
|
|
36
|
+
unsafeConcurrency?: boolean
|
|
37
|
+
/** Enable or disable extension loading */
|
|
38
|
+
enableLoadExtension?: boolean
|
|
39
|
+
/** Whether to parse JSON columns as JS objects. True by default. */
|
|
40
|
+
parseJson?: boolean
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Transaction function created using `Database#transaction`. */
|
|
44
|
+
export type Transaction<T extends (...args: any[]) => void> =
|
|
45
|
+
& ((...args: Parameters<T>) => ReturnType<T>)
|
|
46
|
+
& {
|
|
47
|
+
/** BEGIN */
|
|
48
|
+
default: Transaction<T>
|
|
49
|
+
/** BEGIN DEFERRED */
|
|
50
|
+
deferred: Transaction<T>
|
|
51
|
+
/** BEGIN IMMEDIATE */
|
|
52
|
+
immediate: Transaction<T>
|
|
53
|
+
/** BEGIN EXCLUSIVE */
|
|
54
|
+
exclusive: Transaction<T>
|
|
55
|
+
database: Database
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Options for user-defined functions.
|
|
60
|
+
*
|
|
61
|
+
* @link https://www.sqlite.org/c3ref/c_deterministic.html
|
|
62
|
+
*/
|
|
63
|
+
export interface FunctionOptions {
|
|
64
|
+
varargs?: boolean
|
|
65
|
+
deterministic?: boolean
|
|
66
|
+
directOnly?: boolean
|
|
67
|
+
innocuous?: boolean
|
|
68
|
+
subtype?: boolean
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Options for user-defined aggregate functions.
|
|
73
|
+
*/
|
|
74
|
+
export interface AggregateFunctionOptions extends FunctionOptions {
|
|
75
|
+
start: any | (() => any)
|
|
76
|
+
step: (aggregate: any, ...args: any[]) => void
|
|
77
|
+
final?: (aggregate: any) => any
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const {
|
|
81
|
+
sqlite3_open_v2,
|
|
82
|
+
sqlite3_close_v2,
|
|
83
|
+
sqlite3_changes,
|
|
84
|
+
sqlite3_total_changes,
|
|
85
|
+
sqlite3_last_insert_rowid,
|
|
86
|
+
sqlite3_get_autocommit,
|
|
87
|
+
sqlite3_exec,
|
|
88
|
+
sqlite3_free,
|
|
89
|
+
sqlite3_libversion,
|
|
90
|
+
sqlite3_sourceid,
|
|
91
|
+
sqlite3_complete,
|
|
92
|
+
sqlite3_finalize,
|
|
93
|
+
sqlite3_result_blob,
|
|
94
|
+
sqlite3_result_double,
|
|
95
|
+
sqlite3_result_error,
|
|
96
|
+
sqlite3_result_int64,
|
|
97
|
+
sqlite3_result_null,
|
|
98
|
+
sqlite3_result_text,
|
|
99
|
+
sqlite3_value_blob,
|
|
100
|
+
sqlite3_value_bytes,
|
|
101
|
+
sqlite3_value_double,
|
|
102
|
+
sqlite3_value_int64,
|
|
103
|
+
sqlite3_value_text,
|
|
104
|
+
sqlite3_value_type,
|
|
105
|
+
sqlite3_create_function,
|
|
106
|
+
sqlite3_result_int,
|
|
107
|
+
sqlite3_aggregate_context,
|
|
108
|
+
sqlite3_enable_load_extension,
|
|
109
|
+
sqlite3_load_extension,
|
|
110
|
+
sqlite3_backup_init,
|
|
111
|
+
sqlite3_backup_step,
|
|
112
|
+
sqlite3_backup_finish,
|
|
113
|
+
sqlite3_errcode,
|
|
114
|
+
} = ffi
|
|
115
|
+
|
|
116
|
+
/** SQLite version string */
|
|
117
|
+
export const SQLITE_VERSION: string = readCstr(sqlite3_libversion()!)
|
|
118
|
+
/** SQLite source ID string */
|
|
119
|
+
export const SQLITE_SOURCEID: string = readCstr(sqlite3_sourceid()!)
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether the given SQL statement is complete.
|
|
123
|
+
*
|
|
124
|
+
* @param statement SQL statement string
|
|
125
|
+
*/
|
|
126
|
+
export function isComplete(statement: string): boolean {
|
|
127
|
+
return Boolean(sqlite3_complete(toCString(statement)))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const BIG_MAX = BigInt(Number.MAX_SAFE_INTEGER)
|
|
131
|
+
|
|
132
|
+
/** What a connection holds natively: its handle and the callbacks it calls. */
|
|
133
|
+
type Native = {
|
|
134
|
+
handle: Deno.PointerValue
|
|
135
|
+
callbacks: Set<Deno.UnsafeCallback>
|
|
136
|
+
statements: Set<Deno.PointerValue>
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Finalizes the connection's statements, frees its callbacks and closes
|
|
140
|
+
* it. */
|
|
141
|
+
const shut = ({ handle, callbacks, statements }: Native): number => {
|
|
142
|
+
for (const stmt of statements) sqlite3_finalize(stmt)
|
|
143
|
+
statements.clear()
|
|
144
|
+
for (const cb of callbacks) cb.close()
|
|
145
|
+
callbacks.clear()
|
|
146
|
+
return sqlite3_close_v2(handle)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A connection nothing reaches any longer closes, as its statements finalize
|
|
150
|
+
// (./statement.ts). Its pages and caches are SQLite's, outside the JS heap,
|
|
151
|
+
// so one left open (a test's fresh in-memory database, say) held them until
|
|
152
|
+
// the process exited: a test runtime hundreds of files in held hundreds of
|
|
153
|
+
// megabytes of them. A statement holds its database, so none is in use.
|
|
154
|
+
const unreached = new FinalizationRegistry(shut)
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Represents a SQLite3 database connection.
|
|
158
|
+
*
|
|
159
|
+
* Example:
|
|
160
|
+
* ```ts ignore
|
|
161
|
+
* // Open a database from file, creates if doesn't exist.
|
|
162
|
+
* const db = new Database("myfile.db");
|
|
163
|
+
*
|
|
164
|
+
* // Open an in-memory database.
|
|
165
|
+
* const db = new Database(":memory:");
|
|
166
|
+
*
|
|
167
|
+
* // Open a read-only database.
|
|
168
|
+
* const db = new Database("myfile.db", { readonly: true });
|
|
169
|
+
*
|
|
170
|
+
* // Or open using File URL
|
|
171
|
+
* const db = new Database(new URL("./myfile.db", import.meta.url));
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export class Database {
|
|
175
|
+
#path: string
|
|
176
|
+
#handle: Deno.PointerValue
|
|
177
|
+
#open = true
|
|
178
|
+
#enableLoadExtension = false
|
|
179
|
+
|
|
180
|
+
/** Whether to support BigInt columns. False by default, integers larger than 32 bit will be inaccurate. */
|
|
181
|
+
int64: boolean
|
|
182
|
+
|
|
183
|
+
/** Whether to parse JSON columns as JS objects. True by default. */
|
|
184
|
+
parseJson: boolean
|
|
185
|
+
|
|
186
|
+
unsafeConcurrency: boolean
|
|
187
|
+
|
|
188
|
+
/** Whether DB connection is open */
|
|
189
|
+
get open(): boolean {
|
|
190
|
+
return this.#open
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Unsafe Raw (pointer) to the sqlite object */
|
|
194
|
+
get unsafeHandle(): Deno.PointerValue {
|
|
195
|
+
return this.#handle
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Retain a native callback installed on this connection until it closes. */
|
|
199
|
+
keepCallback(callback: Deno.UnsafeCallback): void {
|
|
200
|
+
this.#callbacks.add(callback)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Path of the database file. */
|
|
204
|
+
get path(): string {
|
|
205
|
+
return this.#path
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Number of rows changed by the last executed statement. */
|
|
209
|
+
get changes(): number {
|
|
210
|
+
return sqlite3_changes(this.#handle)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Number of rows changed since the database connection was opened. */
|
|
214
|
+
get totalChanges(): number {
|
|
215
|
+
return sqlite3_total_changes(this.#handle)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Gets last inserted Row ID */
|
|
219
|
+
get lastInsertRowId(): number {
|
|
220
|
+
return Number(sqlite3_last_insert_rowid(this.#handle))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Whether autocommit is enabled. Enabled by default, can be disabled using BEGIN statement. */
|
|
224
|
+
get autocommit(): boolean {
|
|
225
|
+
return sqlite3_get_autocommit(this.#handle) === 1
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Whether DB is in mid of a transaction */
|
|
229
|
+
get inTransaction(): boolean {
|
|
230
|
+
return this.#open && !this.autocommit
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
get enableLoadExtension(): boolean {
|
|
234
|
+
return this.#enableLoadExtension
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
set enableLoadExtension(enabled: boolean) {
|
|
238
|
+
if (sqlite3_enable_load_extension === null) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
'Extension loading is not supported by the shared library that was used.',
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
const result = sqlite3_enable_load_extension(this.#handle, Number(enabled))
|
|
244
|
+
unwrap(result, this.#handle)
|
|
245
|
+
this.#enableLoadExtension = enabled
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
constructor(path: string | URL, options: DatabaseOpenOptions = {}) {
|
|
249
|
+
this.#path = path instanceof URL ? fromFileUrl(path) : path
|
|
250
|
+
let flags = 0
|
|
251
|
+
this.int64 = options.int64 ?? false
|
|
252
|
+
this.parseJson = options.parseJson ?? true
|
|
253
|
+
this.unsafeConcurrency = options.unsafeConcurrency ?? false
|
|
254
|
+
if (options.flags !== undefined) {
|
|
255
|
+
flags = options.flags
|
|
256
|
+
} else {
|
|
257
|
+
if (options.memory) {
|
|
258
|
+
flags |= SQLITE3_OPEN_MEMORY
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (options.readonly ?? false) {
|
|
262
|
+
flags |= SQLITE3_OPEN_READONLY
|
|
263
|
+
} else {
|
|
264
|
+
flags |= SQLITE3_OPEN_READWRITE
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if ((options.create ?? true) && !options.readonly) {
|
|
268
|
+
flags |= SQLITE3_OPEN_CREATE
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const pHandle = new BigUint64Array(1)
|
|
273
|
+
const result = sqlite3_open_v2(toCString(this.#path), pHandle, flags, null)
|
|
274
|
+
this.#handle = Deno.UnsafePointer.create(pHandle[0])
|
|
275
|
+
if (result !== 0) sqlite3_close_v2(this.#handle)
|
|
276
|
+
unwrap(result)
|
|
277
|
+
unreached.register(
|
|
278
|
+
this,
|
|
279
|
+
{
|
|
280
|
+
handle: this.#handle,
|
|
281
|
+
callbacks: this.#callbacks,
|
|
282
|
+
statements: statementsOf(this),
|
|
283
|
+
},
|
|
284
|
+
this,
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
if (options.enableLoadExtension) {
|
|
288
|
+
this.enableLoadExtension = options.enableLoadExtension
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Creates a new Prepared Statement from the given SQL statement.
|
|
294
|
+
*
|
|
295
|
+
* Example:
|
|
296
|
+
* ```ts ignore
|
|
297
|
+
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");
|
|
298
|
+
*
|
|
299
|
+
* for (const row of stmt.all(1)) {
|
|
300
|
+
* console.log(row);
|
|
301
|
+
* }
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* Bind parameters can be either provided as an array of values, or as an object
|
|
305
|
+
* mapping the parameter name to the value.
|
|
306
|
+
*
|
|
307
|
+
* Example:
|
|
308
|
+
* ```ts ignore
|
|
309
|
+
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = ?");
|
|
310
|
+
* const row = stmt.get(1);
|
|
311
|
+
*
|
|
312
|
+
* // or
|
|
313
|
+
*
|
|
314
|
+
* const stmt = db.prepare("SELECT * FROM mytable WHERE id = :id");
|
|
315
|
+
* const row = stmt.get({ id: 1 });
|
|
316
|
+
* ```
|
|
317
|
+
*
|
|
318
|
+
* Statements are automatically freed once GC catches them, however
|
|
319
|
+
* you can also manually free using `finalize` method.
|
|
320
|
+
*
|
|
321
|
+
* @param sql SQL statement string
|
|
322
|
+
* @returns Statement object
|
|
323
|
+
*/
|
|
324
|
+
prepare<T extends object = Record<string, any>>(sql: string): Statement<T> {
|
|
325
|
+
return new Statement<T>(this, sql)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Open a Blob for incremental I/O.
|
|
330
|
+
*
|
|
331
|
+
* Make sure to close the blob after you are done with it,
|
|
332
|
+
* otherwise you will have memory leaks.
|
|
333
|
+
*/
|
|
334
|
+
openBlob(options: BlobOpenOptions): SQLBlob {
|
|
335
|
+
return new SQLBlob(this, options)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Simply executes the SQL statement (supports multiple statements separated by semicolon).
|
|
340
|
+
* Returns the number of changes made by last statement.
|
|
341
|
+
*
|
|
342
|
+
* Example:
|
|
343
|
+
* ```ts ignore
|
|
344
|
+
* // Create table
|
|
345
|
+
* db.exec("create table users (id integer not null, username varchar(20) not null)");
|
|
346
|
+
*
|
|
347
|
+
* // Inserts
|
|
348
|
+
* db.exec("insert into users (id, username) values(?, ?)", id, username);
|
|
349
|
+
*
|
|
350
|
+
* // Insert with named parameters
|
|
351
|
+
* db.exec("insert into users (id, username) values(:id, :username)", { id, username });
|
|
352
|
+
*
|
|
353
|
+
* // Pragma statements
|
|
354
|
+
* db.exec("pragma journal_mode = WAL");
|
|
355
|
+
* db.exec("pragma synchronous = normal");
|
|
356
|
+
* db.exec("pragma temp_store = memory");
|
|
357
|
+
* ```
|
|
358
|
+
*
|
|
359
|
+
* Under the hood, it uses `sqlite3_exec` if no parameters are given to bind
|
|
360
|
+
* with the SQL statement, a prepared statement otherwise.
|
|
361
|
+
*/
|
|
362
|
+
exec(sql: string, ...params: RestBindParameters): number {
|
|
363
|
+
if (params.length === 0) {
|
|
364
|
+
const pErr = new BigUint64Array(1)
|
|
365
|
+
sqlite3_exec(
|
|
366
|
+
this.#handle,
|
|
367
|
+
toCString(sql),
|
|
368
|
+
null,
|
|
369
|
+
null,
|
|
370
|
+
new Uint8Array(pErr.buffer),
|
|
371
|
+
)
|
|
372
|
+
const errPtr = Deno.UnsafePointer.create(pErr[0])
|
|
373
|
+
if (errPtr !== null) {
|
|
374
|
+
const err = readCstr(errPtr)
|
|
375
|
+
sqlite3_free(errPtr)
|
|
376
|
+
throw new Error(err)
|
|
377
|
+
}
|
|
378
|
+
return sqlite3_changes(this.#handle)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const stmt = this.prepare(sql)
|
|
382
|
+
stmt.run(...params)
|
|
383
|
+
return sqlite3_changes(this.#handle)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Alias for `exec`. */
|
|
387
|
+
run(sql: string, ...params: RestBindParameters): number {
|
|
388
|
+
return this.exec(sql, ...params)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Safely execute SQL with parameters using a tagged template */
|
|
392
|
+
sql<T extends Record<string, any> = Record<string, any>>(
|
|
393
|
+
strings: TemplateStringsArray,
|
|
394
|
+
...parameters: RestBindParameters
|
|
395
|
+
): T[] {
|
|
396
|
+
const sql = strings.join('?')
|
|
397
|
+
const stmt = this.prepare(sql)
|
|
398
|
+
return stmt.all(...parameters)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Wraps a callback function in a transaction.
|
|
403
|
+
*
|
|
404
|
+
* - When function is called, the transaction is started.
|
|
405
|
+
* - When function returns, the transaction is committed.
|
|
406
|
+
* - When function throws an error, the transaction is rolled back.
|
|
407
|
+
*
|
|
408
|
+
* Example:
|
|
409
|
+
* ```ts ignore
|
|
410
|
+
* const stmt = db.prepare("insert into users (id, username) values(?, ?)");
|
|
411
|
+
*
|
|
412
|
+
* interface User {
|
|
413
|
+
* id: number;
|
|
414
|
+
* username: string;
|
|
415
|
+
* }
|
|
416
|
+
*
|
|
417
|
+
* const insertUsers = db.transaction((data: User[]) => {
|
|
418
|
+
* for (const user of data) {
|
|
419
|
+
* stmt.run(user);
|
|
420
|
+
* }
|
|
421
|
+
* });
|
|
422
|
+
*
|
|
423
|
+
* insertUsers([
|
|
424
|
+
* { id: 1, username: "alice" },
|
|
425
|
+
* { id: 2, username: "bob" },
|
|
426
|
+
* ]);
|
|
427
|
+
*
|
|
428
|
+
* // May also use `insertUsers.deferred`, `immediate`, or `exclusive`.
|
|
429
|
+
* // They corresspond to using `BEGIN DEFERRED`, `BEGIN IMMEDIATE`, and `BEGIN EXCLUSIVE`.
|
|
430
|
+
* // For eg.
|
|
431
|
+
*
|
|
432
|
+
* insertUsers.deferred([
|
|
433
|
+
* { id: 1, username: "alice" },
|
|
434
|
+
* { id: 2, username: "bob" },
|
|
435
|
+
* ]);
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
transaction<T extends (this: Transaction<T>, ...args: any[]) => void>(
|
|
439
|
+
fn: T,
|
|
440
|
+
): Transaction<T> {
|
|
441
|
+
// Based on https://github.com/WiseLibs/better-sqlite3/blob/master/lib/methods/transaction.js
|
|
442
|
+
const controller = getController(this)
|
|
443
|
+
|
|
444
|
+
// Each version of the transaction function has these same properties
|
|
445
|
+
const properties = {
|
|
446
|
+
default: { value: wrapTransaction(fn, this, controller.default) },
|
|
447
|
+
deferred: { value: wrapTransaction(fn, this, controller.deferred) },
|
|
448
|
+
immediate: { value: wrapTransaction(fn, this, controller.immediate) },
|
|
449
|
+
exclusive: { value: wrapTransaction(fn, this, controller.exclusive) },
|
|
450
|
+
database: { value: this, enumerable: true },
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
Object.defineProperties(properties.default.value, properties)
|
|
454
|
+
Object.defineProperties(properties.deferred.value, properties)
|
|
455
|
+
Object.defineProperties(properties.immediate.value, properties)
|
|
456
|
+
Object.defineProperties(properties.exclusive.value, properties)
|
|
457
|
+
|
|
458
|
+
// Return the default version of the transaction function
|
|
459
|
+
return properties.default.value as Transaction<T>
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#callbacks = new Set<Deno.UnsafeCallback>()
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Creates a new user-defined function.
|
|
466
|
+
*
|
|
467
|
+
* Example:
|
|
468
|
+
* ```ts ignore
|
|
469
|
+
* db.function("add", (a: number, b: number) => a + b);
|
|
470
|
+
* db.prepare("select add(1, 2)").value<[number]>()!; // [3]
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
473
|
+
function(
|
|
474
|
+
name: string,
|
|
475
|
+
fn: CallableFunction,
|
|
476
|
+
options?: FunctionOptions,
|
|
477
|
+
): void {
|
|
478
|
+
if (sqlite3_create_function === null) {
|
|
479
|
+
throw new Error(
|
|
480
|
+
'User-defined functions are not supported by the shared library that was used.',
|
|
481
|
+
)
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const cb = new Deno.UnsafeCallback(
|
|
485
|
+
{
|
|
486
|
+
parameters: ['pointer', 'i32', 'pointer'],
|
|
487
|
+
result: 'void',
|
|
488
|
+
} as const,
|
|
489
|
+
(ctx, nArgs, pArgs) => {
|
|
490
|
+
const argptr = new Deno.UnsafePointerView(pArgs!)
|
|
491
|
+
const args: any[] = []
|
|
492
|
+
for (let i = 0; i < nArgs; i++) {
|
|
493
|
+
const arg = Deno.UnsafePointer.create(
|
|
494
|
+
argptr.getBigUint64(i * 8),
|
|
495
|
+
)
|
|
496
|
+
const type = sqlite3_value_type(arg)
|
|
497
|
+
switch (type) {
|
|
498
|
+
case SQLITE_INTEGER: {
|
|
499
|
+
const value = sqlite3_value_int64(arg)
|
|
500
|
+
if (value < -BIG_MAX || value > BIG_MAX) {
|
|
501
|
+
args.push(value)
|
|
502
|
+
} else {
|
|
503
|
+
args.push(Number(value))
|
|
504
|
+
}
|
|
505
|
+
break
|
|
506
|
+
}
|
|
507
|
+
case SQLITE_FLOAT:
|
|
508
|
+
args.push(sqlite3_value_double(arg))
|
|
509
|
+
break
|
|
510
|
+
case SQLITE_TEXT:
|
|
511
|
+
args.push(
|
|
512
|
+
new TextDecoder().decode(
|
|
513
|
+
new Uint8Array(
|
|
514
|
+
Deno.UnsafePointerView.getArrayBuffer(
|
|
515
|
+
sqlite3_value_text(arg)!,
|
|
516
|
+
sqlite3_value_bytes(arg),
|
|
517
|
+
),
|
|
518
|
+
),
|
|
519
|
+
),
|
|
520
|
+
)
|
|
521
|
+
break
|
|
522
|
+
case SQLITE_BLOB:
|
|
523
|
+
args.push(
|
|
524
|
+
new Uint8Array(
|
|
525
|
+
Deno.UnsafePointerView.getArrayBuffer(
|
|
526
|
+
sqlite3_value_blob(arg)!,
|
|
527
|
+
sqlite3_value_bytes(arg),
|
|
528
|
+
),
|
|
529
|
+
),
|
|
530
|
+
)
|
|
531
|
+
break
|
|
532
|
+
case SQLITE_NULL:
|
|
533
|
+
args.push(null)
|
|
534
|
+
break
|
|
535
|
+
default:
|
|
536
|
+
throw new Error(`Unknown type: ${type}`)
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
let result: any
|
|
541
|
+
try {
|
|
542
|
+
result = fn(...args)
|
|
543
|
+
} catch (err) {
|
|
544
|
+
const buf = new TextEncoder().encode(
|
|
545
|
+
err instanceof Error ? err.message : String(err),
|
|
546
|
+
)
|
|
547
|
+
sqlite3_result_error(ctx, buf, buf.byteLength)
|
|
548
|
+
return
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (result === undefined || result === null) {
|
|
552
|
+
sqlite3_result_null(ctx)
|
|
553
|
+
} else if (typeof result === 'boolean') {
|
|
554
|
+
sqlite3_result_int(ctx, result ? 1 : 0)
|
|
555
|
+
} else if (typeof result === 'number') {
|
|
556
|
+
if (Number.isSafeInteger(result)) {
|
|
557
|
+
sqlite3_result_int64(ctx, BigInt(result))
|
|
558
|
+
} else sqlite3_result_double(ctx, result)
|
|
559
|
+
} else if (typeof result === 'bigint') {
|
|
560
|
+
sqlite3_result_int64(ctx, result)
|
|
561
|
+
} else if (typeof result === 'string') {
|
|
562
|
+
const buffer = new TextEncoder().encode(result)
|
|
563
|
+
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0n)
|
|
564
|
+
} else if (result instanceof Uint8Array) {
|
|
565
|
+
sqlite3_result_blob(ctx, result as BufferSource, result.length, -1n)
|
|
566
|
+
} else {
|
|
567
|
+
const buffer = new TextEncoder().encode(
|
|
568
|
+
`Invalid return value: ${Deno.inspect(result)}`,
|
|
569
|
+
)
|
|
570
|
+
sqlite3_result_error(ctx, buffer, buffer.byteLength)
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
let flags = 1
|
|
576
|
+
|
|
577
|
+
if (options?.deterministic) {
|
|
578
|
+
flags |= 0x000000800
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (options?.directOnly) {
|
|
582
|
+
flags |= 0x000080000
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if (options?.subtype) {
|
|
586
|
+
flags |= 0x000100000
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (options?.directOnly) {
|
|
590
|
+
flags |= 0x000200000
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const err = sqlite3_create_function(
|
|
594
|
+
this.#handle,
|
|
595
|
+
toCString(name),
|
|
596
|
+
options?.varargs ? -1 : fn.length,
|
|
597
|
+
flags,
|
|
598
|
+
null,
|
|
599
|
+
cb.pointer,
|
|
600
|
+
null,
|
|
601
|
+
null,
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
unwrap(err, this.#handle)
|
|
605
|
+
|
|
606
|
+
this.keepCallback(cb as Deno.UnsafeCallback)
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Creates a new user-defined aggregate function.
|
|
611
|
+
*/
|
|
612
|
+
aggregate(name: string, options: AggregateFunctionOptions): void {
|
|
613
|
+
if (
|
|
614
|
+
sqlite3_aggregate_context === null || sqlite3_create_function === null
|
|
615
|
+
) {
|
|
616
|
+
throw new Error(
|
|
617
|
+
'User-defined functions are not supported by the shared library that was used.',
|
|
618
|
+
)
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const contexts = new Map<number | bigint, any>()
|
|
622
|
+
|
|
623
|
+
const cb = new Deno.UnsafeCallback(
|
|
624
|
+
{
|
|
625
|
+
parameters: ['pointer', 'i32', 'pointer'],
|
|
626
|
+
result: 'void',
|
|
627
|
+
} as const,
|
|
628
|
+
(ctx, nArgs, pArgs) => {
|
|
629
|
+
const aggrCtx = sqlite3_aggregate_context(ctx, 8)
|
|
630
|
+
const aggrPtr = Deno.UnsafePointer.value(aggrCtx)
|
|
631
|
+
let aggregate
|
|
632
|
+
if (contexts.has(aggrPtr)) {
|
|
633
|
+
aggregate = contexts.get(aggrPtr)
|
|
634
|
+
} else {
|
|
635
|
+
aggregate = typeof options.start === 'function'
|
|
636
|
+
? options.start()
|
|
637
|
+
: options.start
|
|
638
|
+
contexts.set(aggrPtr, aggregate)
|
|
639
|
+
}
|
|
640
|
+
const argptr = new Deno.UnsafePointerView(pArgs!)
|
|
641
|
+
const args: any[] = []
|
|
642
|
+
for (let i = 0; i < nArgs; i++) {
|
|
643
|
+
const arg = Deno.UnsafePointer.create(
|
|
644
|
+
argptr.getBigUint64(i * 8),
|
|
645
|
+
)
|
|
646
|
+
const type = sqlite3_value_type(arg)
|
|
647
|
+
switch (type) {
|
|
648
|
+
case SQLITE_INTEGER: {
|
|
649
|
+
const value = sqlite3_value_int64(arg)
|
|
650
|
+
if (value < -BIG_MAX || value > BIG_MAX) {
|
|
651
|
+
args.push(value)
|
|
652
|
+
} else {
|
|
653
|
+
args.push(Number(value))
|
|
654
|
+
}
|
|
655
|
+
break
|
|
656
|
+
}
|
|
657
|
+
case SQLITE_FLOAT:
|
|
658
|
+
args.push(sqlite3_value_double(arg))
|
|
659
|
+
break
|
|
660
|
+
case SQLITE_TEXT:
|
|
661
|
+
args.push(
|
|
662
|
+
new TextDecoder().decode(
|
|
663
|
+
new Uint8Array(
|
|
664
|
+
Deno.UnsafePointerView.getArrayBuffer(
|
|
665
|
+
sqlite3_value_text(arg)!,
|
|
666
|
+
sqlite3_value_bytes(arg),
|
|
667
|
+
),
|
|
668
|
+
),
|
|
669
|
+
),
|
|
670
|
+
)
|
|
671
|
+
break
|
|
672
|
+
case SQLITE_BLOB:
|
|
673
|
+
args.push(
|
|
674
|
+
new Uint8Array(
|
|
675
|
+
Deno.UnsafePointerView.getArrayBuffer(
|
|
676
|
+
sqlite3_value_blob(arg)!,
|
|
677
|
+
sqlite3_value_bytes(arg),
|
|
678
|
+
),
|
|
679
|
+
),
|
|
680
|
+
)
|
|
681
|
+
break
|
|
682
|
+
case SQLITE_NULL:
|
|
683
|
+
args.push(null)
|
|
684
|
+
break
|
|
685
|
+
default:
|
|
686
|
+
throw new Error(`Unknown type: ${type}`)
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
let result: any
|
|
691
|
+
try {
|
|
692
|
+
result = options.step(aggregate, ...args)
|
|
693
|
+
} catch (err) {
|
|
694
|
+
const buf = new TextEncoder().encode(
|
|
695
|
+
err instanceof Error ? err.message : String(err),
|
|
696
|
+
)
|
|
697
|
+
sqlite3_result_error(ctx, buf, buf.byteLength)
|
|
698
|
+
return
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
contexts.set(aggrPtr, result)
|
|
702
|
+
},
|
|
703
|
+
)
|
|
704
|
+
|
|
705
|
+
const cbFinal = new Deno.UnsafeCallback(
|
|
706
|
+
{
|
|
707
|
+
parameters: ['pointer'],
|
|
708
|
+
result: 'void',
|
|
709
|
+
} as const,
|
|
710
|
+
(ctx) => {
|
|
711
|
+
const aggrCtx = sqlite3_aggregate_context(ctx, 0)
|
|
712
|
+
const aggrPtr = Deno.UnsafePointer.value(aggrCtx)
|
|
713
|
+
const aggregate = contexts.get(aggrPtr)
|
|
714
|
+
contexts.delete(aggrPtr)
|
|
715
|
+
let result: any
|
|
716
|
+
try {
|
|
717
|
+
result = options.final ? options.final(aggregate) : aggregate
|
|
718
|
+
} catch (err) {
|
|
719
|
+
const buf = new TextEncoder().encode(
|
|
720
|
+
err instanceof Error ? err.message : String(err),
|
|
721
|
+
)
|
|
722
|
+
sqlite3_result_error(ctx, buf, buf.byteLength)
|
|
723
|
+
return
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (result === undefined || result === null) {
|
|
727
|
+
sqlite3_result_null(ctx)
|
|
728
|
+
} else if (typeof result === 'boolean') {
|
|
729
|
+
sqlite3_result_int(ctx, result ? 1 : 0)
|
|
730
|
+
} else if (typeof result === 'number') {
|
|
731
|
+
if (Number.isSafeInteger(result)) {
|
|
732
|
+
sqlite3_result_int64(ctx, BigInt(result))
|
|
733
|
+
} else sqlite3_result_double(ctx, result)
|
|
734
|
+
} else if (typeof result === 'bigint') {
|
|
735
|
+
sqlite3_result_int64(ctx, result)
|
|
736
|
+
} else if (typeof result === 'string') {
|
|
737
|
+
const buffer = new TextEncoder().encode(result)
|
|
738
|
+
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0n)
|
|
739
|
+
} else if (result instanceof Uint8Array) {
|
|
740
|
+
sqlite3_result_blob(ctx, result as BufferSource, result.length, -1n)
|
|
741
|
+
} else {
|
|
742
|
+
const buffer = new TextEncoder().encode(
|
|
743
|
+
`Invalid return value: ${Deno.inspect(result)}`,
|
|
744
|
+
)
|
|
745
|
+
sqlite3_result_error(ctx, buffer, buffer.byteLength)
|
|
746
|
+
}
|
|
747
|
+
},
|
|
748
|
+
)
|
|
749
|
+
|
|
750
|
+
let flags = 1
|
|
751
|
+
|
|
752
|
+
if (options?.deterministic) {
|
|
753
|
+
flags |= 0x000000800
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
if (options?.directOnly) {
|
|
757
|
+
flags |= 0x000080000
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
if (options?.subtype) {
|
|
761
|
+
flags |= 0x000100000
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
if (options?.directOnly) {
|
|
765
|
+
flags |= 0x000200000
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
const err = sqlite3_create_function(
|
|
769
|
+
this.#handle,
|
|
770
|
+
toCString(name),
|
|
771
|
+
options?.varargs ? -1 : options.step.length - 1,
|
|
772
|
+
flags,
|
|
773
|
+
null,
|
|
774
|
+
null,
|
|
775
|
+
cb.pointer,
|
|
776
|
+
cbFinal.pointer,
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
unwrap(err, this.#handle)
|
|
780
|
+
|
|
781
|
+
this.keepCallback(cb as Deno.UnsafeCallback)
|
|
782
|
+
this.keepCallback(cbFinal as Deno.UnsafeCallback)
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Loads an SQLite extension library from the named file.
|
|
787
|
+
*/
|
|
788
|
+
loadExtension(file: string, entryPoint?: string): void {
|
|
789
|
+
if (sqlite3_load_extension === null) {
|
|
790
|
+
throw new Error(
|
|
791
|
+
'Extension loading is not supported by the shared library that was used.',
|
|
792
|
+
)
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
if (!this.enableLoadExtension) {
|
|
796
|
+
throw new Error('Extension loading is not enabled')
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
const pzErrMsg = new BigUint64Array(1)
|
|
800
|
+
|
|
801
|
+
const result = sqlite3_load_extension(
|
|
802
|
+
this.#handle,
|
|
803
|
+
toCString(file),
|
|
804
|
+
entryPoint ? toCString(entryPoint) : null,
|
|
805
|
+
pzErrMsg,
|
|
806
|
+
)
|
|
807
|
+
|
|
808
|
+
const pzErrPtr = Deno.UnsafePointer.create(
|
|
809
|
+
pzErrMsg[0],
|
|
810
|
+
)
|
|
811
|
+
if (pzErrPtr !== null) {
|
|
812
|
+
const pzErr = readCstr(pzErrPtr)
|
|
813
|
+
sqlite3_free(pzErrPtr)
|
|
814
|
+
throw new Error(pzErr)
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
unwrap(result, this.#handle)
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Closes the database connection.
|
|
822
|
+
*
|
|
823
|
+
* Calling this method more than once is no-op.
|
|
824
|
+
*/
|
|
825
|
+
close(): void {
|
|
826
|
+
if (!this.#open) return
|
|
827
|
+
unreached.unregister(this)
|
|
828
|
+
unwrap(shut({
|
|
829
|
+
handle: this.#handle,
|
|
830
|
+
callbacks: this.#callbacks,
|
|
831
|
+
statements: statementsOf(this),
|
|
832
|
+
}))
|
|
833
|
+
this.#open = false
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* @param dest The destination database connection.
|
|
838
|
+
* @param name Destination database name. "main" for main database, "temp" for temporary database, or the name specified after the AS keyword in an ATTACH statement for an attached database.
|
|
839
|
+
* @param pages The number of pages to copy. If it is negative, all remaining pages are copied (default).
|
|
840
|
+
*/
|
|
841
|
+
backup(dest: Database, name = 'main', pages = -1): void {
|
|
842
|
+
const backup = sqlite3_backup_init(
|
|
843
|
+
dest.#handle,
|
|
844
|
+
toCString(name),
|
|
845
|
+
this.#handle,
|
|
846
|
+
toCString('main'),
|
|
847
|
+
)
|
|
848
|
+
if (backup) {
|
|
849
|
+
unwrap(sqlite3_backup_step(backup, pages))
|
|
850
|
+
unwrap(sqlite3_backup_finish(backup))
|
|
851
|
+
} else {
|
|
852
|
+
unwrap(sqlite3_errcode(dest.#handle), dest.#handle)
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
[Symbol.for('Deno.customInspect')](): string {
|
|
857
|
+
return `SQLite3.Database { path: ${this.path} }`
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
const controllers = new WeakMap()
|
|
862
|
+
|
|
863
|
+
// Return the database's cached transaction controller, or create a new one
|
|
864
|
+
const getController = (db: Database) => {
|
|
865
|
+
let controller = controllers.get(db)
|
|
866
|
+
if (!controller) {
|
|
867
|
+
const shared = {
|
|
868
|
+
commit: db.prepare('COMMIT'),
|
|
869
|
+
rollback: db.prepare('ROLLBACK'),
|
|
870
|
+
savepoint: db.prepare('SAVEPOINT `\t_bs3.\t`'),
|
|
871
|
+
release: db.prepare('RELEASE `\t_bs3.\t`'),
|
|
872
|
+
rollbackTo: db.prepare('ROLLBACK TO `\t_bs3.\t`'),
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
controllers.set(
|
|
876
|
+
db,
|
|
877
|
+
controller = {
|
|
878
|
+
default: Object.assign(
|
|
879
|
+
{ begin: db.prepare('BEGIN') },
|
|
880
|
+
shared,
|
|
881
|
+
),
|
|
882
|
+
deferred: Object.assign(
|
|
883
|
+
{ begin: db.prepare('BEGIN DEFERRED') },
|
|
884
|
+
shared,
|
|
885
|
+
),
|
|
886
|
+
immediate: Object.assign(
|
|
887
|
+
{ begin: db.prepare('BEGIN IMMEDIATE') },
|
|
888
|
+
shared,
|
|
889
|
+
),
|
|
890
|
+
exclusive: Object.assign(
|
|
891
|
+
{ begin: db.prepare('BEGIN EXCLUSIVE') },
|
|
892
|
+
shared,
|
|
893
|
+
),
|
|
894
|
+
},
|
|
895
|
+
)
|
|
896
|
+
}
|
|
897
|
+
return controller
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// Return a new transaction function by wrapping the given function
|
|
901
|
+
const wrapTransaction = <T extends (...args: any[]) => void>(
|
|
902
|
+
fn: T,
|
|
903
|
+
db: Database,
|
|
904
|
+
{ begin, commit, rollback, savepoint, release, rollbackTo }: any,
|
|
905
|
+
) =>
|
|
906
|
+
function sqliteTransaction(...args: Parameters<T>): ReturnType<T> {
|
|
907
|
+
const { apply } = Function.prototype
|
|
908
|
+
let before, after, undo
|
|
909
|
+
if (db.inTransaction) {
|
|
910
|
+
before = savepoint
|
|
911
|
+
after = release
|
|
912
|
+
undo = rollbackTo
|
|
913
|
+
} else {
|
|
914
|
+
before = begin
|
|
915
|
+
after = commit
|
|
916
|
+
undo = rollback
|
|
917
|
+
}
|
|
918
|
+
before.run()
|
|
919
|
+
try {
|
|
920
|
+
// @ts-ignore An outer value of 'this' is shadowed by this container.
|
|
921
|
+
const result = apply.call(fn, this, args)
|
|
922
|
+
after.run()
|
|
923
|
+
return result
|
|
924
|
+
} catch (ex) {
|
|
925
|
+
if (!db.autocommit) {
|
|
926
|
+
undo.run()
|
|
927
|
+
if (undo !== rollback) after.run()
|
|
928
|
+
}
|
|
929
|
+
throw ex
|
|
930
|
+
}
|
|
931
|
+
}
|