@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.
Files changed (289) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1596 -2
  3. package/_dist/archetype.d.ts +75 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/branches.d.ts +23 -0
  6. package/_dist/branches.d.ts.map +1 -0
  7. package/_dist/budget.d.ts +52 -0
  8. package/_dist/budget.d.ts.map +1 -0
  9. package/_dist/bundle.d.ts +2 -0
  10. package/_dist/bundle.d.ts.map +1 -0
  11. package/_dist/cache.d.ts +24 -0
  12. package/_dist/cache.d.ts.map +1 -0
  13. package/_dist/catalog.d.ts +9 -0
  14. package/_dist/catalog.d.ts.map +1 -0
  15. package/_dist/check.d.ts +4 -0
  16. package/_dist/check.d.ts.map +1 -0
  17. package/_dist/db.d.ts +36 -0
  18. package/_dist/db.d.ts.map +1 -0
  19. package/_dist/ddl.d.ts +143 -0
  20. package/_dist/ddl.d.ts.map +1 -0
  21. package/_dist/doom.d.ts +22 -0
  22. package/_dist/doom.d.ts.map +1 -0
  23. package/_dist/follow.d.ts +74 -0
  24. package/_dist/follow.d.ts.map +1 -0
  25. package/_dist/history-shapes.d.ts +3 -0
  26. package/_dist/history-shapes.d.ts.map +1 -0
  27. package/_dist/history.d.ts +18 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/inspect.d.ts +43 -0
  30. package/_dist/inspect.d.ts.map +1 -0
  31. package/_dist/jsonb.d.ts +17 -0
  32. package/_dist/jsonb.d.ts.map +1 -0
  33. package/_dist/layout.d.ts +7 -0
  34. package/_dist/layout.d.ts.map +1 -0
  35. package/_dist/ledger.d.ts +41 -0
  36. package/_dist/ledger.d.ts.map +1 -0
  37. package/_dist/meta.d.ts +89 -0
  38. package/_dist/meta.d.ts.map +1 -0
  39. package/_dist/migration.d.ts +41 -0
  40. package/_dist/migration.d.ts.map +1 -0
  41. package/_dist/mod.d.ts +201 -0
  42. package/_dist/mod.d.ts.map +1 -0
  43. package/_dist/mode.d.ts +14 -0
  44. package/_dist/mode.d.ts.map +1 -0
  45. package/_dist/overlay.d.ts +30 -0
  46. package/_dist/overlay.d.ts.map +1 -0
  47. package/_dist/owe.d.ts +180 -0
  48. package/_dist/owe.d.ts.map +1 -0
  49. package/_dist/physical.d.ts +89 -0
  50. package/_dist/physical.d.ts.map +1 -0
  51. package/_dist/point.d.ts +10 -0
  52. package/_dist/point.d.ts.map +1 -0
  53. package/_dist/raise.d.ts +22 -0
  54. package/_dist/raise.d.ts.map +1 -0
  55. package/_dist/read-db.d.ts +9 -0
  56. package/_dist/read-db.d.ts.map +1 -0
  57. package/_dist/read-store.d.ts +10 -0
  58. package/_dist/read-store.d.ts.map +1 -0
  59. package/_dist/read.d.ts +59 -0
  60. package/_dist/read.d.ts.map +1 -0
  61. package/_dist/reader.d.ts +11 -0
  62. package/_dist/reader.d.ts.map +1 -0
  63. package/_dist/rules.d.ts +19 -0
  64. package/_dist/rules.d.ts.map +1 -0
  65. package/_dist/service.d.ts +12 -0
  66. package/_dist/service.d.ts.map +1 -0
  67. package/_dist/shape.d.ts +12 -0
  68. package/_dist/shape.d.ts.map +1 -0
  69. package/_dist/statements.d.ts +4 -0
  70. package/_dist/statements.d.ts.map +1 -0
  71. package/_dist/survey.d.ts +31 -0
  72. package/_dist/survey.d.ts.map +1 -0
  73. package/_dist/tools.d.ts +15 -0
  74. package/_dist/tools.d.ts.map +1 -0
  75. package/_dist/trash.d.ts +18 -0
  76. package/_dist/trash.d.ts.map +1 -0
  77. package/_dist/unit.d.ts +11 -0
  78. package/_dist/unit.d.ts.map +1 -0
  79. package/_dist/vocab.d.ts +6 -0
  80. package/_dist/vocab.d.ts.map +1 -0
  81. package/_dist/write.d.ts +169 -0
  82. package/_dist/write.d.ts.map +1 -0
  83. package/answer.js +110 -0
  84. package/answer.js.map +1 -0
  85. package/answer.ts +122 -0
  86. package/archetype.js +730 -0
  87. package/archetype.js.map +1 -0
  88. package/archetype.ts +790 -0
  89. package/binary_cases.ts +240 -0
  90. package/branches.js +195 -0
  91. package/branches.js.map +1 -0
  92. package/branches.ts +257 -0
  93. package/budget.js +89 -0
  94. package/budget.js.map +1 -0
  95. package/budget.ts +117 -0
  96. package/builtin.js +34 -0
  97. package/builtin.js.map +1 -0
  98. package/builtin.ts +31 -0
  99. package/bundle.js +21 -0
  100. package/bundle.js.map +1 -0
  101. package/bundle.ts +22 -0
  102. package/buried.js +95 -0
  103. package/buried.js.map +1 -0
  104. package/buried.ts +124 -0
  105. package/cache.js +153 -0
  106. package/cache.js.map +1 -0
  107. package/cache.ts +154 -0
  108. package/cache_worker.ts +16 -0
  109. package/catalog.js +173 -0
  110. package/catalog.js.map +1 -0
  111. package/catalog.ts +224 -0
  112. package/check.js +80 -0
  113. package/check.js.map +1 -0
  114. package/check.ts +92 -0
  115. package/checkpoint.js +98 -0
  116. package/checkpoint.js.map +1 -0
  117. package/checkpoint.ts +129 -0
  118. package/checkpoint_worker.ts +37 -0
  119. package/db.js +170 -0
  120. package/db.js.map +1 -0
  121. package/db.ts +173 -0
  122. package/ddl.js +1441 -0
  123. package/ddl.js.map +1 -0
  124. package/ddl.ts +1508 -0
  125. package/deno.json +1 -0
  126. package/doom.js +59 -0
  127. package/doom.js.map +1 -0
  128. package/doom.ts +75 -0
  129. package/driver.js +69 -0
  130. package/driver.js.map +1 -0
  131. package/driver.ts +101 -0
  132. package/error.js +30 -0
  133. package/error.js.map +1 -0
  134. package/error.ts +46 -0
  135. package/ffi/LICENSE +202 -0
  136. package/ffi/blob.js +99 -0
  137. package/ffi/blob.js.map +1 -0
  138. package/ffi/blob.ts +156 -0
  139. package/ffi/constants.js +65 -0
  140. package/ffi/constants.js.map +1 -0
  141. package/ffi/constants.ts +67 -0
  142. package/ffi/database.js +613 -0
  143. package/ffi/database.js.map +1 -0
  144. package/ffi/database.ts +931 -0
  145. package/ffi/ffi.js +535 -0
  146. package/ffi/ffi.js.map +1 -0
  147. package/ffi/ffi.ts +604 -0
  148. package/ffi/mod.js +15 -0
  149. package/ffi/mod.js.map +1 -0
  150. package/ffi/mod.ts +28 -0
  151. package/ffi/statement.js +555 -0
  152. package/ffi/statement.js.map +1 -0
  153. package/ffi/statement.ts +788 -0
  154. package/ffi/util.js +32 -0
  155. package/ffi/util.js.map +1 -0
  156. package/ffi/util.ts +48 -0
  157. package/follow.js +342 -0
  158. package/follow.js.map +1 -0
  159. package/follow.ts +401 -0
  160. package/history-branch.js +166 -0
  161. package/history-branch.js.map +1 -0
  162. package/history-branch.ts +140 -0
  163. package/history-shapes.js +254 -0
  164. package/history-shapes.js.map +1 -0
  165. package/history-shapes.ts +268 -0
  166. package/history.js +620 -0
  167. package/history.js.map +1 -0
  168. package/history.ts +674 -0
  169. package/inspect.js +120 -0
  170. package/inspect.js.map +1 -0
  171. package/inspect.ts +164 -0
  172. package/jsonb.js +73 -0
  173. package/jsonb.js.map +1 -0
  174. package/jsonb.ts +108 -0
  175. package/layout.js +22 -0
  176. package/layout.js.map +1 -0
  177. package/layout.ts +26 -0
  178. package/ledger.js +210 -0
  179. package/ledger.js.map +1 -0
  180. package/ledger.ts +210 -0
  181. package/legacy.js +4 -0
  182. package/legacy.js.map +1 -0
  183. package/legacy.ts +3 -0
  184. package/maintenance.js +148 -0
  185. package/maintenance.js.map +1 -0
  186. package/maintenance.ts +154 -0
  187. package/measure.js +133 -0
  188. package/measure.js.map +1 -0
  189. package/measure.ts +167 -0
  190. package/measure_worker.ts +18 -0
  191. package/memo.js +1250 -0
  192. package/memo.js.map +1 -0
  193. package/memo.ts +1480 -0
  194. package/meta.js +265 -0
  195. package/meta.js.map +1 -0
  196. package/meta.ts +321 -0
  197. package/migration.js +272 -0
  198. package/migration.js.map +1 -0
  199. package/migration.ts +286 -0
  200. package/mod.js +1103 -0
  201. package/mod.js.map +1 -0
  202. package/mod.ts +1762 -0
  203. package/mode.js +192 -0
  204. package/mode.js.map +1 -0
  205. package/mode.ts +207 -0
  206. package/native.js +544 -0
  207. package/native.js.map +1 -0
  208. package/native.ts +544 -0
  209. package/overlay.js +408 -0
  210. package/overlay.js.map +1 -0
  211. package/overlay.ts +485 -0
  212. package/owe.js +719 -0
  213. package/owe.js.map +1 -0
  214. package/owe.ts +873 -0
  215. package/package.json +99 -4
  216. package/physical.js +350 -0
  217. package/physical.js.map +1 -0
  218. package/physical.ts +434 -0
  219. package/plan.js +78 -0
  220. package/plan.js.map +1 -0
  221. package/plan.ts +104 -0
  222. package/point.js +81 -0
  223. package/point.js.map +1 -0
  224. package/point.ts +52 -0
  225. package/population.js +639 -0
  226. package/population.js.map +1 -0
  227. package/population.ts +630 -0
  228. package/purge.js +117 -0
  229. package/purge.js.map +1 -0
  230. package/purge.ts +127 -0
  231. package/raise.js +270 -0
  232. package/raise.js.map +1 -0
  233. package/raise.ts +316 -0
  234. package/read-db.js +381 -0
  235. package/read-db.js.map +1 -0
  236. package/read-db.ts +282 -0
  237. package/read-store.js +74 -0
  238. package/read-store.js.map +1 -0
  239. package/read-store.ts +86 -0
  240. package/read.js +1100 -0
  241. package/read.js.map +1 -0
  242. package/read.ts +1441 -0
  243. package/reader.js +141 -0
  244. package/reader.js.map +1 -0
  245. package/reader.ts +159 -0
  246. package/rules.js +88 -0
  247. package/rules.js.map +1 -0
  248. package/rules.ts +113 -0
  249. package/service.js +27 -0
  250. package/service.js.map +1 -0
  251. package/service.ts +41 -0
  252. package/shape.js +76 -0
  253. package/shape.js.map +1 -0
  254. package/shape.ts +108 -0
  255. package/sparse_presence_fixture.ts +45 -0
  256. package/sqlitepath.js +24 -0
  257. package/sqlitepath.js.map +1 -0
  258. package/sqlitepath.ts +29 -0
  259. package/statements.js +78 -0
  260. package/statements.js.map +1 -0
  261. package/statements.ts +96 -0
  262. package/survey.js +135 -0
  263. package/survey.js.map +1 -0
  264. package/survey.ts +196 -0
  265. package/tools.js +17 -0
  266. package/tools.js.map +1 -0
  267. package/tools.ts +50 -0
  268. package/trash.js +144 -0
  269. package/trash.js.map +1 -0
  270. package/trash.ts +187 -0
  271. package/unit.js +215 -0
  272. package/unit.js.map +1 -0
  273. package/unit.ts +211 -0
  274. package/versions.js +115 -0
  275. package/versions.js.map +1 -0
  276. package/versions.ts +110 -0
  277. package/vocab.js +19 -0
  278. package/vocab.js.map +1 -0
  279. package/vocab.json +27 -0
  280. package/vocab.ts +19 -0
  281. package/watch.js +33 -0
  282. package/watch.js.map +1 -0
  283. package/watch.ts +42 -0
  284. package/write.js +1164 -0
  285. package/write.js.map +1 -0
  286. package/write.ts +1356 -0
  287. package/written.js +200 -0
  288. package/written.js.map +1 -0
  289. package/written.ts +202 -0
@@ -0,0 +1,788 @@
1
+ // deno-lint-ignore-file no-explicit-any -- upstream @db/sqlite (./mod.ts)
2
+ import type { Database } from './database.ts'
3
+ import { readCstr, toCString, unwrap } from './util.js'
4
+ import ffi from './ffi.js'
5
+ import {
6
+ SQLITE3_DONE,
7
+ SQLITE3_ROW,
8
+ SQLITE_BLOB,
9
+ SQLITE_FLOAT,
10
+ SQLITE_INTEGER,
11
+ SQLITE_TEXT,
12
+ } from './constants.js'
13
+
14
+ const {
15
+ sqlite3_prepare_v2,
16
+ sqlite3_reset,
17
+ sqlite3_clear_bindings,
18
+ sqlite3_step,
19
+ sqlite3_column_count,
20
+ sqlite3_column_type,
21
+ sqlite3_column_value,
22
+ sqlite3_value_subtype,
23
+ sqlite3_column_text,
24
+ sqlite3_finalize,
25
+ sqlite3_column_int64,
26
+ sqlite3_column_double,
27
+ sqlite3_column_blob,
28
+ sqlite3_column_bytes,
29
+ sqlite3_column_name,
30
+ sqlite3_expanded_sql,
31
+ sqlite3_bind_parameter_count,
32
+ sqlite3_bind_int,
33
+ sqlite3_bind_int64,
34
+ sqlite3_bind_text,
35
+ sqlite3_bind_blob,
36
+ sqlite3_bind_double,
37
+ sqlite3_bind_parameter_index,
38
+ sqlite3_sql,
39
+ sqlite3_stmt_readonly,
40
+ sqlite3_bind_parameter_name,
41
+ sqlite3_changes,
42
+ sqlite3_column_int,
43
+ } = ffi
44
+
45
+ /** Types that can be possibly serialized as SQLite bind values */
46
+ export type BindValue =
47
+ | number
48
+ | string
49
+ | symbol
50
+ | bigint
51
+ | boolean
52
+ | null
53
+ | undefined
54
+ | Date
55
+ | Uint8Array
56
+ | BindValue[]
57
+ | { [key: string]: BindValue }
58
+
59
+ export type BindParameters = BindValue[] | Record<string, BindValue>
60
+ export type RestBindParameters = BindValue[] | [BindParameters]
61
+
62
+ // A finalizer keeps the native handles, without keeping their JS owners.
63
+ // A database closes only its own statements, even when many stores are open.
64
+ type Handles = Set<Deno.PointerValue>
65
+ const handles = new WeakMap<Database, Handles>()
66
+ export const statementsOf = (db: Database): Handles => {
67
+ let held = handles.get(db)
68
+ if (!held) handles.set(db, held = new Set())
69
+ return held
70
+ }
71
+
72
+ const emptyStringBuffer = new Uint8Array(1)
73
+
74
+ const statementFinalizer = new FinalizationRegistry(
75
+ ({ handle, held }: { handle: Deno.PointerValue; held: Handles }) => {
76
+ if (held.delete(handle)) sqlite3_finalize(handle)
77
+ },
78
+ )
79
+
80
+ // https://github.com/sqlite/sqlite/blob/195611d8e6fc0bba559a49e91e6ceb42e4bdd6ba/src/json.c#L125-L126
81
+ const JSON_SUBTYPE = 74
82
+
83
+ function getColumn(
84
+ handle: Deno.PointerValue,
85
+ i: number,
86
+ int64: boolean,
87
+ parseJson: boolean,
88
+ ): any {
89
+ const ty = sqlite3_column_type(handle, i)
90
+
91
+ if (ty === SQLITE_INTEGER && !int64) return sqlite3_column_int(handle, i)
92
+
93
+ switch (ty) {
94
+ case SQLITE_TEXT: {
95
+ const ptr = sqlite3_column_text(handle, i)
96
+ if (ptr === null) return null
97
+ const text = readCstr(ptr, 0)
98
+ if (!parseJson) return text
99
+ const value = sqlite3_column_value(handle, i)
100
+ const subtype = sqlite3_value_subtype(value)
101
+ if (subtype === JSON_SUBTYPE) {
102
+ try {
103
+ return JSON.parse(text)
104
+ } catch (_error) {
105
+ return text
106
+ }
107
+ }
108
+ return text
109
+ }
110
+
111
+ case SQLITE_INTEGER: {
112
+ // A safe integer is exact as a double. Read the 64-bit value only
113
+ // when the double cannot distinguish adjacent integers.
114
+ const val = sqlite3_column_double(handle, i)
115
+ return Number.isSafeInteger(val) ? val : sqlite3_column_int64(handle, i)
116
+ }
117
+
118
+ case SQLITE_FLOAT: {
119
+ return sqlite3_column_double(handle, i)
120
+ }
121
+
122
+ case SQLITE_BLOB: {
123
+ const ptr = sqlite3_column_blob(handle, i)
124
+
125
+ if (ptr === null) {
126
+ return new Uint8Array()
127
+ }
128
+
129
+ const bytes = sqlite3_column_bytes(handle, i)
130
+ return new Uint8Array(
131
+ Deno.UnsafePointerView.getArrayBuffer(ptr, bytes).slice(0),
132
+ )
133
+ }
134
+
135
+ default: {
136
+ return null
137
+ }
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Represents a prepared statement.
143
+ *
144
+ * See `Database#prepare` for more information.
145
+ */
146
+ export class Statement<TStatement extends object = Record<string, any>> {
147
+ #handle: Deno.PointerValue
148
+ #native: { handle: Deno.PointerValue; held: Handles }
149
+ #bound = false
150
+ #hasNoArgs = false
151
+ #unsafeConcurrency
152
+
153
+ /** Unsafe Raw (pointer) to the sqlite object */
154
+ get unsafeHandle(): Deno.PointerValue {
155
+ return this.#handle
156
+ }
157
+
158
+ /** SQL string including bindings */
159
+ get expandedSql(): string {
160
+ return readCstr(sqlite3_expanded_sql(this.#handle)!)
161
+ }
162
+
163
+ /** The SQL string that we passed when creating statement */
164
+ get sql(): string {
165
+ return readCstr(sqlite3_sql(this.#handle)!)
166
+ }
167
+
168
+ /** Whether this statement doesn't make any direct changes to the DB */
169
+ get readonly(): boolean {
170
+ return sqlite3_stmt_readonly(this.#handle) !== 0
171
+ }
172
+
173
+ /** Simply run the query without retrieving any output there may be. */
174
+ run(...args: RestBindParameters): number {
175
+ return this.#runWithArgs(...args)
176
+ }
177
+
178
+ /**
179
+ * Run the query and return the resulting rows where rows are array of columns.
180
+ */
181
+ values<T extends unknown[] = any[]>(...args: RestBindParameters): T[] {
182
+ return this.#valuesWithArgs(...args)
183
+ }
184
+
185
+ /**
186
+ * Run the query and return the resulting rows where rows are objects
187
+ * mapping column name to their corresponding values.
188
+ */
189
+ all<T extends object = TStatement>(
190
+ ...args: RestBindParameters
191
+ ): T[] {
192
+ return this.#allWithArgs(...args)
193
+ }
194
+
195
+ #bindParameterCount: number
196
+
197
+ /** Number of parameters (to be) bound */
198
+ get bindParameterCount(): number {
199
+ return this.#bindParameterCount
200
+ }
201
+
202
+ int64?: boolean
203
+ parseJson?: boolean
204
+
205
+ enableInt64(): this {
206
+ this.int64 = true
207
+ return this
208
+ }
209
+
210
+ enableParseJson(): this {
211
+ this.parseJson = true
212
+ return this
213
+ }
214
+
215
+ disableInt64(): this {
216
+ this.int64 = false
217
+ return this
218
+ }
219
+
220
+ disableParseJson(): this {
221
+ this.parseJson = false
222
+ return this
223
+ }
224
+
225
+ defaultInt64(): this {
226
+ this.int64 = undefined
227
+ return this
228
+ }
229
+
230
+ defaultParseJson(): this {
231
+ this.parseJson = undefined
232
+ return this
233
+ }
234
+
235
+ constructor(public db: Database, sql: string) {
236
+ const pHandle = new BigUint64Array(1)
237
+ const cString = toCString(sql)
238
+ unwrap(
239
+ sqlite3_prepare_v2(
240
+ db.unsafeHandle,
241
+ cString,
242
+ cString.byteLength,
243
+ pHandle,
244
+ null,
245
+ ),
246
+ db.unsafeHandle,
247
+ )
248
+ this.#handle = Deno.UnsafePointer.create(pHandle[0])
249
+ const held = statementsOf(db)
250
+ held.add(this.#handle)
251
+ this.#unsafeConcurrency = db.unsafeConcurrency
252
+ this.#native = { handle: this.#handle, held }
253
+ statementFinalizer.register(this, this.#native, this)
254
+
255
+ if (
256
+ (this.#bindParameterCount = sqlite3_bind_parameter_count(
257
+ this.#handle,
258
+ )) === 0
259
+ ) {
260
+ this.#hasNoArgs = true
261
+ this.all = this.#allNoArgs
262
+ this.values = this.#valuesNoArgs
263
+ this.run = this.#runNoArgs
264
+ this.value = this.#valueNoArgs
265
+ this.get = this.#getNoArgs
266
+ }
267
+ }
268
+
269
+ /** Get bind parameter name by index */
270
+ bindParameterName(i: number): string {
271
+ return readCstr(sqlite3_bind_parameter_name(this.#handle, i)!)
272
+ }
273
+
274
+ /** Get bind parameter index by name */
275
+ bindParameterIndex(name: string): number {
276
+ if (name[0] !== ':' && name[0] !== '@' && name[0] !== '$') {
277
+ name = ':' + name
278
+ }
279
+ return sqlite3_bind_parameter_index(this.#handle, toCString(name))
280
+ }
281
+
282
+ #begin(): void {
283
+ sqlite3_reset(this.#handle)
284
+ if (!this.#bound && !this.#hasNoArgs) {
285
+ sqlite3_clear_bindings(this.#handle)
286
+ this.#bindRefs.clear()
287
+ }
288
+ }
289
+
290
+ #bindRefs: Set<any> = new Set()
291
+
292
+ #bind(i: number, param: BindValue): void {
293
+ switch (typeof param) {
294
+ case 'number': {
295
+ if (Number.isInteger(param)) {
296
+ if (
297
+ Number.isSafeInteger(param) && param >= -(2 ** 31) &&
298
+ param < 2 ** 31
299
+ ) {
300
+ unwrap(sqlite3_bind_int(this.#handle, i + 1, param))
301
+ } else {
302
+ unwrap(sqlite3_bind_int64(this.#handle, i + 1, BigInt(param)))
303
+ }
304
+ } else {
305
+ unwrap(sqlite3_bind_double(this.#handle, i + 1, param))
306
+ }
307
+ break
308
+ }
309
+ case 'string': {
310
+ if (param === '') {
311
+ // Empty string is encoded as empty buffer in Deno. And as of
312
+ // right now (Deno 1.29.1), ffi layer converts it to NULL pointer,
313
+ // which causes sqlite3_bind_text to bind the NULL value instead
314
+ // of an empty string. As a workaround let's use a special
315
+ // non-empty buffer, but specify zero length.
316
+ unwrap(
317
+ sqlite3_bind_text(this.#handle, i + 1, emptyStringBuffer, 0, null),
318
+ )
319
+ } else {
320
+ const str = new TextEncoder().encode(param)
321
+ this.#bindRefs.add(str)
322
+ unwrap(
323
+ sqlite3_bind_text(this.#handle, i + 1, str, str.byteLength, null),
324
+ )
325
+ }
326
+ break
327
+ }
328
+ case 'object': {
329
+ if (param === null) {
330
+ // pass
331
+ } else if (param instanceof Uint8Array) {
332
+ this.#bindRefs.add(param)
333
+ unwrap(
334
+ sqlite3_bind_blob(
335
+ this.#handle,
336
+ i + 1,
337
+ param.byteLength === 0
338
+ ? emptyStringBuffer
339
+ : param as BufferSource,
340
+ param.byteLength,
341
+ null,
342
+ ),
343
+ )
344
+ } else if (param instanceof Date) {
345
+ const cstring = toCString(param.toISOString())
346
+ this.#bindRefs.add(cstring)
347
+ unwrap(
348
+ sqlite3_bind_text(
349
+ this.#handle,
350
+ i + 1,
351
+ cstring,
352
+ -1,
353
+ null,
354
+ ),
355
+ )
356
+ } else {
357
+ const cstring = toCString(JSON.stringify(param))
358
+ this.#bindRefs.add(cstring)
359
+ unwrap(
360
+ sqlite3_bind_text(
361
+ this.#handle,
362
+ i + 1,
363
+ cstring,
364
+ -1,
365
+ null,
366
+ ),
367
+ )
368
+ }
369
+ break
370
+ }
371
+
372
+ case 'bigint': {
373
+ unwrap(sqlite3_bind_int64(this.#handle, i + 1, param))
374
+ break
375
+ }
376
+
377
+ case 'boolean':
378
+ unwrap(sqlite3_bind_int(
379
+ this.#handle,
380
+ i + 1,
381
+ param ? 1 : 0,
382
+ ))
383
+ break
384
+
385
+ case 'undefined':
386
+ // pass
387
+ break
388
+
389
+ default: {
390
+ throw new Error(`Value of unsupported type: ${Deno.inspect(param)}`)
391
+ }
392
+ }
393
+ }
394
+
395
+ /**
396
+ * Bind parameters to the statement. This method can only be called once
397
+ * to set the parameters to be same throughout the statement. You cannot
398
+ * change the parameters after this method is called.
399
+ *
400
+ * This method is merely just for optimization to avoid binding parameters
401
+ * each time in prepared statement.
402
+ */
403
+ bind(...params: RestBindParameters): this {
404
+ this.#bindAll(params)
405
+ this.#bound = true
406
+ return this
407
+ }
408
+
409
+ #bindAll(params: RestBindParameters | BindParameters): void {
410
+ if (this.#bound) throw new Error('Statement already bound to values')
411
+ if (
412
+ typeof params[0] === 'object' && params[0] !== null &&
413
+ !(params[0] instanceof Uint8Array) && !(params[0] instanceof Date)
414
+ ) {
415
+ params = params[0]
416
+ }
417
+ if (Array.isArray(params)) {
418
+ for (let i = 0; i < params.length; i++) {
419
+ this.#bind(i, (params as BindValue[])[i])
420
+ }
421
+ } else {
422
+ for (const [name, param] of Object.entries(params)) {
423
+ const i = this.bindParameterIndex(name)
424
+ if (i === 0) {
425
+ throw new Error(`No such parameter "${name}"`)
426
+ }
427
+ this.#bind(i - 1, param as BindValue)
428
+ }
429
+ }
430
+ }
431
+
432
+ #runNoArgs(): number {
433
+ const handle = this.#handle
434
+ this.#begin()
435
+ const status = sqlite3_step(handle)
436
+ if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) {
437
+ unwrap(status, this.db.unsafeHandle)
438
+ }
439
+ sqlite3_reset(handle)
440
+ return sqlite3_changes(this.db.unsafeHandle)
441
+ }
442
+
443
+ #runWithArgs(...params: RestBindParameters): number {
444
+ const handle = this.#handle
445
+ this.#begin()
446
+ this.#bindAll(params)
447
+ const status = sqlite3_step(handle)
448
+ if (!this.#hasNoArgs && !this.#bound && params.length) {
449
+ this.#bindRefs.clear()
450
+ }
451
+ if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) {
452
+ unwrap(status, this.db.unsafeHandle)
453
+ }
454
+ sqlite3_reset(handle)
455
+ return sqlite3_changes(this.db.unsafeHandle)
456
+ }
457
+
458
+ #valuesNoArgs<T extends Array<unknown>>(): T[] {
459
+ const handle = this.#handle
460
+ this.#begin()
461
+ const columnCount = sqlite3_column_count(handle)
462
+ const result: T[] = []
463
+ const getRowArray = new Function(
464
+ 'getColumn',
465
+ `
466
+ return function(h, int64, parseJson) {
467
+ return [${
468
+ Array.from({ length: columnCount }).map((_, i) =>
469
+ `getColumn(h, ${i}, int64, parseJson)`
470
+ )
471
+ .join(', ')
472
+ }];
473
+ };
474
+ `,
475
+ )(getColumn)
476
+ let status = sqlite3_step(handle)
477
+ while (status === SQLITE3_ROW) {
478
+ result.push(
479
+ getRowArray(
480
+ handle,
481
+ this.int64 ?? this.db.int64,
482
+ this.parseJson ?? this.db.parseJson,
483
+ ),
484
+ )
485
+ status = sqlite3_step(handle)
486
+ }
487
+ if (status !== SQLITE3_DONE) {
488
+ unwrap(status, this.db.unsafeHandle)
489
+ }
490
+ sqlite3_reset(handle)
491
+ return result as T[]
492
+ }
493
+
494
+ #valuesWithArgs<T extends Array<unknown>>(
495
+ ...params: RestBindParameters
496
+ ): T[] {
497
+ const handle = this.#handle
498
+ this.#begin()
499
+ this.#bindAll(params)
500
+ const columnCount = sqlite3_column_count(handle)
501
+ const result: T[] = []
502
+ const getRowArray = new Function(
503
+ 'getColumn',
504
+ `
505
+ return function(h, int64, parseJson) {
506
+ return [${
507
+ Array.from({ length: columnCount }).map((_, i) =>
508
+ `getColumn(h, ${i}, int64, parseJson)`
509
+ )
510
+ .join(', ')
511
+ }];
512
+ };
513
+ `,
514
+ )(getColumn)
515
+ let status = sqlite3_step(handle)
516
+ while (status === SQLITE3_ROW) {
517
+ result.push(
518
+ getRowArray(
519
+ handle,
520
+ this.int64 ?? this.db.int64,
521
+ this.parseJson ?? this.db.parseJson,
522
+ ),
523
+ )
524
+ status = sqlite3_step(handle)
525
+ }
526
+ if (!this.#hasNoArgs && !this.#bound && params.length) {
527
+ this.#bindRefs.clear()
528
+ }
529
+ if (status !== SQLITE3_DONE) {
530
+ unwrap(status, this.db.unsafeHandle)
531
+ }
532
+ sqlite3_reset(handle)
533
+ return result as T[]
534
+ }
535
+
536
+ #rowObjectFn:
537
+ | ((h: Deno.PointerValue, int64: boolean, parseJson: boolean) => any)
538
+ | undefined
539
+
540
+ getRowObject(): (
541
+ h: Deno.PointerValue,
542
+ int64: boolean,
543
+ parseJson: boolean,
544
+ ) => any {
545
+ if (!this.#rowObjectFn || !this.#unsafeConcurrency) {
546
+ const columnNames = this.columnNames()
547
+ const getRowObject = new Function(
548
+ 'getColumn',
549
+ `
550
+ return function(h, int64, parseJson) {
551
+ return {
552
+ ${
553
+ columnNames.map((name, i) =>
554
+ `"${name}": getColumn(h, ${i}, int64, parseJson)`
555
+ ).join(',\n')
556
+ }
557
+ };
558
+ };
559
+ `,
560
+ )(getColumn)
561
+ this.#rowObjectFn = getRowObject
562
+ }
563
+ return this.#rowObjectFn!
564
+ }
565
+
566
+ #allNoArgs<T extends object>(): T[] {
567
+ const handle = this.#handle
568
+ const int64 = this.int64 ?? this.db.int64
569
+ const parseJson = this.parseJson ?? this.db.parseJson
570
+ this.#begin()
571
+ const getRowObject = this.getRowObject()
572
+ const result: T[] = []
573
+ let status = sqlite3_step(handle)
574
+ while (status === SQLITE3_ROW) {
575
+ result.push(getRowObject(handle, int64, parseJson))
576
+ status = sqlite3_step(handle)
577
+ }
578
+ if (status !== SQLITE3_DONE) {
579
+ unwrap(status, this.db.unsafeHandle)
580
+ }
581
+ sqlite3_reset(handle)
582
+ return result as T[]
583
+ }
584
+
585
+ #allWithArgs<T extends object>(
586
+ ...params: RestBindParameters
587
+ ): T[] {
588
+ const handle = this.#handle
589
+ const int64 = this.int64 ?? this.db.int64
590
+ const parseJson = this.parseJson ?? this.db.parseJson
591
+ this.#begin()
592
+ this.#bindAll(params)
593
+ const getRowObject = this.getRowObject()
594
+ const result: T[] = []
595
+ let status = sqlite3_step(handle)
596
+ while (status === SQLITE3_ROW) {
597
+ result.push(getRowObject(handle, int64, parseJson))
598
+ status = sqlite3_step(handle)
599
+ }
600
+ if (!this.#hasNoArgs && !this.#bound && params.length) {
601
+ this.#bindRefs.clear()
602
+ }
603
+ if (status !== SQLITE3_DONE) {
604
+ unwrap(status, this.db.unsafeHandle)
605
+ }
606
+ sqlite3_reset(handle)
607
+ return result as T[]
608
+ }
609
+
610
+ /** Fetch only first row as an array, if any. */
611
+ value<T extends Array<unknown>>(
612
+ ...params: RestBindParameters
613
+ ): T | undefined {
614
+ const handle = this.#handle
615
+ const int64 = this.int64 ?? this.db.int64
616
+ const parseJson = this.parseJson ?? this.db.parseJson
617
+ const arr = new Array(sqlite3_column_count(handle))
618
+ sqlite3_reset(handle)
619
+ if (!this.#hasNoArgs && !this.#bound) {
620
+ sqlite3_clear_bindings(handle)
621
+ this.#bindRefs.clear()
622
+ if (params.length) {
623
+ this.#bindAll(params)
624
+ }
625
+ }
626
+
627
+ const status = sqlite3_step(handle)
628
+
629
+ if (!this.#hasNoArgs && !this.#bound && params.length) {
630
+ this.#bindRefs.clear()
631
+ }
632
+
633
+ if (status === SQLITE3_ROW) {
634
+ for (let i = 0; i < arr.length; i++) {
635
+ arr[i] = getColumn(handle, i, int64, parseJson)
636
+ }
637
+ sqlite3_reset(this.#handle)
638
+ return arr as T
639
+ } else if (status === SQLITE3_DONE) {
640
+ return
641
+ } else {
642
+ unwrap(status, this.db.unsafeHandle)
643
+ }
644
+ }
645
+
646
+ #valueNoArgs<T extends Array<unknown>>(): T | undefined {
647
+ const handle = this.#handle
648
+ const int64 = this.int64 ?? this.db.int64
649
+ const parseJson = this.parseJson ?? this.db.parseJson
650
+ const cc = sqlite3_column_count(handle)
651
+ const arr = new Array(cc)
652
+ sqlite3_reset(handle)
653
+ const status = sqlite3_step(handle)
654
+ if (status === SQLITE3_ROW) {
655
+ for (let i = 0; i < cc; i++) {
656
+ arr[i] = getColumn(handle, i, int64, parseJson)
657
+ }
658
+ sqlite3_reset(this.#handle)
659
+ return arr as T
660
+ } else if (status === SQLITE3_DONE) {
661
+ return
662
+ } else {
663
+ unwrap(status, this.db.unsafeHandle)
664
+ }
665
+ }
666
+
667
+ #columnNames: string[] | undefined
668
+ #rowObject: Record<string, unknown> = {}
669
+
670
+ columnNames(): string[] {
671
+ if (!this.#columnNames || !this.#unsafeConcurrency) {
672
+ const columnCount = sqlite3_column_count(this.#handle)
673
+ const columnNames = new Array(columnCount)
674
+ for (let i = 0; i < columnCount; i++) {
675
+ columnNames[i] = readCstr(sqlite3_column_name(this.#handle, i)!)
676
+ }
677
+ this.#columnNames = columnNames
678
+ this.#rowObject = {}
679
+ for (const name of columnNames) {
680
+ this.#rowObject![name] = undefined
681
+ }
682
+ }
683
+ return this.#columnNames!
684
+ }
685
+
686
+ /** Fetch only first row as an object, if any. */
687
+ get<T extends object = TStatement>(
688
+ ...params: RestBindParameters
689
+ ): T | undefined {
690
+ const handle = this.#handle
691
+ const int64 = this.int64 ?? this.db.int64
692
+ const parseJson = this.parseJson ?? this.db.parseJson
693
+ const columnNames = this.columnNames()
694
+
695
+ const row: Record<string, unknown> = {}
696
+ sqlite3_reset(handle)
697
+ if (!this.#hasNoArgs && !this.#bound) {
698
+ sqlite3_clear_bindings(handle)
699
+ this.#bindRefs.clear()
700
+ if (params.length) {
701
+ this.#bindAll(params)
702
+ }
703
+ }
704
+
705
+ const status = sqlite3_step(handle)
706
+
707
+ if (!this.#hasNoArgs && !this.#bound && params.length) {
708
+ this.#bindRefs.clear()
709
+ }
710
+
711
+ if (status === SQLITE3_ROW) {
712
+ for (let i = 0; i < columnNames.length; i++) {
713
+ row[columnNames[i]] = getColumn(handle, i, int64, parseJson)
714
+ }
715
+ sqlite3_reset(this.#handle)
716
+ return row as T
717
+ } else if (status === SQLITE3_DONE) {
718
+ return
719
+ } else {
720
+ unwrap(status, this.db.unsafeHandle)
721
+ }
722
+ }
723
+
724
+ #getNoArgs<T extends object>(): T | undefined {
725
+ const handle = this.#handle
726
+ const int64 = this.int64 ?? this.db.int64
727
+ const parseJson = this.parseJson ?? this.db.parseJson
728
+ const columnNames = this.columnNames()
729
+ const row: Record<string, unknown> = this.#rowObject
730
+ sqlite3_reset(handle)
731
+ const status = sqlite3_step(handle)
732
+ if (status === SQLITE3_ROW) {
733
+ for (let i = 0; i < columnNames?.length; i++) {
734
+ row[columnNames[i]] = getColumn(handle, i, int64, parseJson)
735
+ }
736
+ sqlite3_reset(handle)
737
+ return row as T
738
+ } else if (status === SQLITE3_DONE) {
739
+ return
740
+ } else {
741
+ unwrap(status, this.db.unsafeHandle)
742
+ }
743
+ }
744
+
745
+ /** Free up the statement object. */
746
+ finalize(): void {
747
+ this.#bindRefs.clear()
748
+ statementFinalizer.unregister(this)
749
+ if (this.#native.held.delete(this.#handle)) {
750
+ unwrap(sqlite3_finalize(this.#handle))
751
+ }
752
+ }
753
+
754
+ /** Coerces the statement to a string, which in this case is expanded SQL. */
755
+ toString(): string {
756
+ return readCstr(sqlite3_expanded_sql(this.#handle)!)
757
+ }
758
+
759
+ /** Iterate over resultant rows from query. */
760
+ *iter(...params: RestBindParameters): IterableIterator<any> {
761
+ this.#begin()
762
+ this.#bindAll(params)
763
+ const getRowObject = this.getRowObject()
764
+ const int64 = this.int64 ?? this.db.int64
765
+ const parseJson = this.parseJson ?? this.db.parseJson
766
+ let status = sqlite3_step(this.#handle)
767
+ while (status === SQLITE3_ROW) {
768
+ yield getRowObject(this.#handle, int64, parseJson)
769
+ status = sqlite3_step(this.#handle)
770
+ }
771
+ if (status !== SQLITE3_DONE) {
772
+ unwrap(status, this.db.unsafeHandle)
773
+ }
774
+ sqlite3_reset(this.#handle)
775
+ }
776
+
777
+ [Symbol.iterator](): IterableIterator<any> {
778
+ return this.iter()
779
+ }
780
+
781
+ [Symbol.dispose](): void {
782
+ this.finalize()
783
+ }
784
+
785
+ [Symbol.for('Deno.customInspect')](): string {
786
+ return `Statement { ${this.expandedSql} }`
787
+ }
788
+ }