@otwdesign/db-core 1.3.1 → 1.4.0
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/dist/backup/api.js +67 -9
- package/dist/backup/api.js.map +1 -1
- package/dist/backup/catalog.d.ts +9 -2
- package/dist/backup/catalog.js +42 -18
- package/dist/backup/catalog.js.map +1 -1
- package/dist/backup/dump.d.ts +6 -2
- package/dist/backup/dump.js +188 -20
- package/dist/backup/dump.js.map +1 -1
- package/dist/backup/restore.d.ts +9 -1
- package/dist/backup/restore.js +131 -11
- package/dist/backup/restore.js.map +1 -1
- package/dist/backup/split.d.ts +7 -2
- package/dist/backup/split.js +38 -6
- package/dist/backup/split.js.map +1 -1
- package/dist/backup/staging.d.ts +6 -1
- package/dist/backup/staging.js +38 -10
- package/dist/backup/staging.js.map +1 -1
- package/dist/drivers/mysql.d.ts +6 -0
- package/dist/drivers/mysql.js +142 -18
- package/dist/drivers/mysql.js.map +1 -1
- package/dist/drivers/pglite.d.ts +18 -3
- package/dist/drivers/pglite.js +357 -55
- package/dist/drivers/pglite.js.map +1 -1
- package/dist/drivers/postgres-catalog.d.ts +16 -1
- package/dist/drivers/postgres-catalog.js +34 -0
- package/dist/drivers/postgres-catalog.js.map +1 -1
- package/dist/drivers/postgres.d.ts +10 -0
- package/dist/drivers/postgres.js +203 -29
- package/dist/drivers/postgres.js.map +1 -1
- package/dist/drivers/read-only.d.ts +4 -0
- package/dist/drivers/read-only.js +156 -0
- package/dist/drivers/read-only.js.map +1 -0
- package/dist/drivers/sql.d.ts +51 -4
- package/dist/drivers/sql.js +112 -35
- package/dist/drivers/sql.js.map +1 -1
- package/dist/drivers/sqlite.d.ts +33 -1
- package/dist/drivers/sqlite.js +645 -69
- package/dist/drivers/sqlite.js.map +1 -1
- package/dist/export.d.ts +4 -5
- package/dist/export.js +21 -3
- package/dist/export.js.map +1 -1
- package/dist/format.js +7 -1
- package/dist/format.js.map +1 -1
- package/dist/handler.js +37 -15
- package/dist/handler.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +37 -3
- package/dist/words.d.ts +19 -0
- package/dist/words.js +76 -0
- package/dist/words.js.map +1 -0
- package/package.json +8 -7
- package/src/backup/api.ts +82 -14
- package/src/backup/catalog.ts +43 -18
- package/src/backup/dump.ts +208 -21
- package/src/backup/restore.ts +152 -12
- package/src/backup/split.ts +38 -6
- package/src/backup/staging.ts +51 -10
- package/src/drivers/mysql.ts +171 -21
- package/src/drivers/pglite.ts +419 -56
- package/src/drivers/postgres-catalog.ts +51 -1
- package/src/drivers/postgres.ts +241 -33
- package/src/drivers/read-only.ts +174 -0
- package/src/drivers/sql.ts +170 -37
- package/src/drivers/sqlite.ts +739 -98
- package/src/export.ts +26 -6
- package/src/format.ts +9 -1
- package/src/handler.ts +55 -19
- package/src/index.ts +7 -2
- package/src/types.ts +38 -1
- package/src/words.ts +67 -0
package/src/export.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { csvHeader, csvLine, jsonObject } from './format.ts'
|
|
2
|
-
import type { ExportFormat, SqlDialect } from './types.ts'
|
|
2
|
+
import type { ExportColumn, ExportFormat, SqlDialect } from './types.ts'
|
|
3
3
|
|
|
4
4
|
/** Rows as a driver hands them over for an export: a batch at a time, names first. */
|
|
5
5
|
export interface ExportBatch {
|
|
6
|
-
columns: readonly
|
|
6
|
+
columns: readonly ExportColumn[]
|
|
7
7
|
rows: readonly (readonly (string | null)[])[]
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -21,7 +21,8 @@ export const EXPORT_TYPES: Record<ExportFormat, { type: string; extension: strin
|
|
|
21
21
|
* - CSV: a header line, then RFC 4180 rows. NULL is an empty field and an empty string a quoted
|
|
22
22
|
* one (""), as PostgreSQL's COPY reads them.
|
|
23
23
|
* - JSON: an array of objects, each value a string or null, as the database wrote it.
|
|
24
|
-
* - SQL: an INSERT a row, in the driver's own quoting
|
|
24
|
+
* - SQL: an INSERT a row, in the driver's own quoting, that puts the row back as it was: bytes
|
|
25
|
+
* as bytes, the keys the rows had, and without the columns the database computes itself.
|
|
25
26
|
*/
|
|
26
27
|
export function exportStream(
|
|
27
28
|
format: ExportFormat,
|
|
@@ -33,15 +34,31 @@ export function exportStream(
|
|
|
33
34
|
const encoder = new TextEncoder()
|
|
34
35
|
let rows = 0
|
|
35
36
|
let first = true
|
|
36
|
-
let columns: readonly
|
|
37
|
+
let columns: readonly ExportColumn[] = []
|
|
38
|
+
/** An SQL export's INSERT up to its values, and the columns (by place) it gives values for. */
|
|
39
|
+
let insert = ''
|
|
40
|
+
let filled: number[] = []
|
|
37
41
|
const table = `${dialect.identifier(target.schema)}.${dialect.identifier(target.table)}`
|
|
38
42
|
|
|
43
|
+
/** A value as SQL: bytes in the database's own hex form are a literal as they are. */
|
|
44
|
+
const sqlValue = (value: string | null, column: ExportColumn): string =>
|
|
45
|
+
(column.bytes && value !== null ? dialect.bytes?.(value) : null) ?? dialect.literal(value)
|
|
46
|
+
|
|
39
47
|
function chunk(batch: ExportBatch): string {
|
|
40
48
|
let out = ''
|
|
41
49
|
if (first) {
|
|
42
50
|
columns = batch.columns
|
|
43
51
|
if (format === 'csv') out += csvHeader(columns.map((c) => c.name))
|
|
44
52
|
if (format === 'json') out += '['
|
|
53
|
+
if (format === 'sql') {
|
|
54
|
+
out += dialect.exportHeader ?? ''
|
|
55
|
+
filled = columns.flatMap((c, i) => (c.generated ? [] : [i]))
|
|
56
|
+
const names = filled.map((i) => dialect.identifier(columns[i]!.name)).join(', ')
|
|
57
|
+
const overriding = columns.some((c) => c.identity) ? (dialect.overriding ?? '') : ''
|
|
58
|
+
insert = filled.length
|
|
59
|
+
? `INSERT INTO ${table} (${names})${overriding} VALUES `
|
|
60
|
+
: `INSERT INTO ${table} ${dialect.insertDefaults ?? 'DEFAULT VALUES'}`
|
|
61
|
+
}
|
|
45
62
|
}
|
|
46
63
|
for (const row of batch.rows) {
|
|
47
64
|
if (format === 'csv') out += csvLine(row)
|
|
@@ -52,8 +69,8 @@ export function exportStream(
|
|
|
52
69
|
)
|
|
53
70
|
out += `${first && rows === 0 ? '\n' : ',\n'}${JSON.stringify(object)}`
|
|
54
71
|
} else {
|
|
55
|
-
const
|
|
56
|
-
out +=
|
|
72
|
+
const values = filled.map((i) => sqlValue(row[i] ?? null, columns[i]!)).join(', ')
|
|
73
|
+
out += `${insert}${filled.length ? `(${values})` : ''};\n`
|
|
57
74
|
}
|
|
58
75
|
rows++
|
|
59
76
|
}
|
|
@@ -67,6 +84,9 @@ export function exportStream(
|
|
|
67
84
|
const next = await batches.next()
|
|
68
85
|
if (next.done) {
|
|
69
86
|
if (format === 'json') controller.enqueue(encoder.encode(first ? '[]\n' : '\n]\n'))
|
|
87
|
+
if (format === 'sql' && !first && dialect.exportFooter) {
|
|
88
|
+
controller.enqueue(encoder.encode(dialect.exportFooter))
|
|
89
|
+
}
|
|
70
90
|
controller.close()
|
|
71
91
|
done(rows)
|
|
72
92
|
return
|
package/src/format.ts
CHANGED
|
@@ -28,7 +28,15 @@ export function jsonObject(
|
|
|
28
28
|
values: readonly (string | null)[],
|
|
29
29
|
): Record<string, string | null> {
|
|
30
30
|
const object: Record<string, string | null> = {}
|
|
31
|
-
|
|
31
|
+
// Defined, not assigned: a column named __proto__ is a column, and assigning it sets none.
|
|
32
|
+
names.forEach((name, i) =>
|
|
33
|
+
Object.defineProperty(object, name, {
|
|
34
|
+
value: values[i] ?? null,
|
|
35
|
+
enumerable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
}),
|
|
39
|
+
)
|
|
32
40
|
return object
|
|
33
41
|
}
|
|
34
42
|
|
package/src/handler.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { driverKind } from './connections.ts'
|
|
2
2
|
import { openDriver } from './drivers/index.ts'
|
|
3
3
|
import { DbAdminError, QueryError } from './errors.ts'
|
|
4
|
-
import { EXPORT_TYPES, exportStream } from './export.ts'
|
|
4
|
+
import { EXPORT_TYPES, exportStream, type ExportBatch } from './export.ts'
|
|
5
5
|
import type {
|
|
6
6
|
Access,
|
|
7
7
|
AuditEvent,
|
|
@@ -148,7 +148,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
148
148
|
const backups = () => (backupApi ??= import('./backup/api.ts'))
|
|
149
149
|
const maxRestoreBytes = options.backups?.maxRestoreBytes ?? 1_073_741_824
|
|
150
150
|
/** What a backup endpoint needs to know, from the connection it reached. */
|
|
151
|
-
const backupContext = (target: ReturnType<typeof reach
|
|
151
|
+
const backupContext = (target: Awaited<ReturnType<typeof reach>>, viewer: Viewer) => ({
|
|
152
152
|
...target,
|
|
153
153
|
viewer,
|
|
154
154
|
record,
|
|
@@ -185,16 +185,31 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
185
185
|
return found
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
/** Each connection's databases, as its server last listed them. */
|
|
189
|
+
const listed = new Map<string, Set<string>>()
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether the connection's server has the database, for its login to open. A name not in the
|
|
193
|
+
* list as last read has the list read again: a database may have been made since.
|
|
194
|
+
*/
|
|
195
|
+
async function hasDatabase(connection: ConnectionConfig, database: string): Promise<boolean> {
|
|
196
|
+
if (listed.get(connection.name)?.has(database)) return true
|
|
197
|
+
const { databases } = await driver(connection).databases!()
|
|
198
|
+
listed.set(connection.name, new Set(databases))
|
|
199
|
+
return databases.includes(database)
|
|
200
|
+
}
|
|
201
|
+
|
|
188
202
|
/**
|
|
189
203
|
* The connection by name, with this viewer's access to it and the driver for `database`: one of
|
|
190
204
|
* the server's other databases, or the connection's own when none is named. Unknown and hidden
|
|
191
|
-
* connections look the same.
|
|
205
|
+
* connections look the same. A database is opened only if the server lists it: each one opened
|
|
206
|
+
* takes a place among the eight kept open, and a made-up name would push a real one out.
|
|
192
207
|
*/
|
|
193
|
-
function reach(
|
|
208
|
+
async function reach(
|
|
194
209
|
viewer: Viewer,
|
|
195
210
|
name: unknown,
|
|
196
211
|
database?: unknown,
|
|
197
|
-
): { connection: ConnectionConfig; access: Access; db: Driver; database?: string } {
|
|
212
|
+
): Promise<{ connection: ConnectionConfig; access: Access; db: Driver; database?: string }> {
|
|
198
213
|
const connection = typeof name === 'string' ? connections.get(name) : undefined
|
|
199
214
|
const access = connection ? accessOf(viewer, connection) : null
|
|
200
215
|
if (!connection || !access) {
|
|
@@ -205,6 +220,13 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
205
220
|
}
|
|
206
221
|
const named = databaseName(database)
|
|
207
222
|
if (!opensOthers(connection)) throw oneDatabase(connection)
|
|
223
|
+
if (!(await hasDatabase(connection, named))) {
|
|
224
|
+
throw new DbAdminError(
|
|
225
|
+
`There’s no database "${named}" on the server of connection "${connection.name}", or its login may not open it.`,
|
|
226
|
+
404,
|
|
227
|
+
'unknown-database',
|
|
228
|
+
)
|
|
229
|
+
}
|
|
208
230
|
return { connection, access, db: driverAt(connection, named), database: named }
|
|
209
231
|
}
|
|
210
232
|
|
|
@@ -261,7 +283,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
261
283
|
method: 'GET',
|
|
262
284
|
path: '/api/databases',
|
|
263
285
|
async run(request, viewer) {
|
|
264
|
-
const { connection, db } = reach(
|
|
286
|
+
const { connection, db } = await reach(
|
|
265
287
|
viewer,
|
|
266
288
|
new URL(request.url).searchParams.get('connection'),
|
|
267
289
|
)
|
|
@@ -274,7 +296,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
274
296
|
path: '/api/tables',
|
|
275
297
|
async run(request, viewer) {
|
|
276
298
|
const params = new URL(request.url).searchParams
|
|
277
|
-
const { db } = reach(viewer, params.get('connection'), params.get('database'))
|
|
299
|
+
const { db } = await reach(viewer, params.get('connection'), params.get('database'))
|
|
278
300
|
return { tables: await db.tables() }
|
|
279
301
|
},
|
|
280
302
|
},
|
|
@@ -283,7 +305,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
283
305
|
path: '/api/table',
|
|
284
306
|
async run(request, viewer) {
|
|
285
307
|
const params = new URL(request.url).searchParams
|
|
286
|
-
const { db } = reach(viewer, params.get('connection'), params.get('database'))
|
|
308
|
+
const { db } = await reach(viewer, params.get('connection'), params.get('database'))
|
|
287
309
|
const table = await db.table(
|
|
288
310
|
text(params.get('schema'), 'schema'),
|
|
289
311
|
text(params.get('name'), 'name'),
|
|
@@ -297,7 +319,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
297
319
|
path: '/api/info',
|
|
298
320
|
async run(request, viewer) {
|
|
299
321
|
const params = new URL(request.url).searchParams
|
|
300
|
-
const { db } = reach(viewer, params.get('connection'), params.get('database'))
|
|
322
|
+
const { db } = await reach(viewer, params.get('connection'), params.get('database'))
|
|
301
323
|
return { info: await db.info() }
|
|
302
324
|
},
|
|
303
325
|
},
|
|
@@ -306,7 +328,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
306
328
|
path: '/api/count',
|
|
307
329
|
async run(request, viewer) {
|
|
308
330
|
const body = await json(request)
|
|
309
|
-
const { connection, db, database } = reach(viewer, body.connection, body.database)
|
|
331
|
+
const { connection, db, database } = await reach(viewer, body.connection, body.database)
|
|
310
332
|
const target = {
|
|
311
333
|
schema: text(body.schema, 'schema'),
|
|
312
334
|
table: text(body.table, 'table'),
|
|
@@ -348,7 +370,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
348
370
|
path: '/api/rows',
|
|
349
371
|
async run(request, viewer) {
|
|
350
372
|
const body = await json(request)
|
|
351
|
-
const { connection, db, database } = reach(viewer, body.connection, body.database)
|
|
373
|
+
const { connection, db, database } = await reach(viewer, body.connection, body.database)
|
|
352
374
|
const page = rowsRequest(body)
|
|
353
375
|
const started = performance.now()
|
|
354
376
|
const statement = `${page.schema}.${page.table}`
|
|
@@ -386,7 +408,11 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
386
408
|
path: '/api/query',
|
|
387
409
|
async run(request, viewer) {
|
|
388
410
|
const body = await json(request)
|
|
389
|
-
const { connection, access, db, database } = reach(
|
|
411
|
+
const { connection, access, db, database } = await reach(
|
|
412
|
+
viewer,
|
|
413
|
+
body.connection,
|
|
414
|
+
body.database,
|
|
415
|
+
)
|
|
390
416
|
const sql = text(body.sql, 'sql')
|
|
391
417
|
const maxRows =
|
|
392
418
|
body.maxRows === undefined
|
|
@@ -443,7 +469,11 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
443
469
|
path: '/api/apply',
|
|
444
470
|
async run(request, viewer) {
|
|
445
471
|
const body = await json(request)
|
|
446
|
-
const { connection, access, db, database } = reach(
|
|
472
|
+
const { connection, access, db, database } = await reach(
|
|
473
|
+
viewer,
|
|
474
|
+
body.connection,
|
|
475
|
+
body.database,
|
|
476
|
+
)
|
|
447
477
|
const target = { schema: text(body.schema, 'schema'), table: text(body.table, 'table') }
|
|
448
478
|
const changes = changesOf(body.changes)
|
|
449
479
|
const dryRun = body.dryRun === true
|
|
@@ -489,7 +519,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
489
519
|
path: '/api/export',
|
|
490
520
|
async run(request, viewer) {
|
|
491
521
|
const body = await json(request)
|
|
492
|
-
const { connection, db, database } = reach(viewer, body.connection, body.database)
|
|
522
|
+
const { connection, db, database } = await reach(viewer, body.connection, body.database)
|
|
493
523
|
const format = formatOf(body.format)
|
|
494
524
|
const page = rowsRequest({ ...body, limit: undefined, offset: undefined })
|
|
495
525
|
const started = performance.now()
|
|
@@ -497,7 +527,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
497
527
|
// Read the first batch before answering, so a bad table or filter is a proper error.
|
|
498
528
|
const first = await batches.next()
|
|
499
529
|
let replayed = false
|
|
500
|
-
const replay: AsyncIterator<
|
|
530
|
+
const replay: AsyncIterator<ExportBatch> = {
|
|
501
531
|
next: async () => {
|
|
502
532
|
if (replayed) return batches.next()
|
|
503
533
|
replayed = true
|
|
@@ -539,7 +569,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
539
569
|
path: '/api/backup-options',
|
|
540
570
|
async run(request, viewer) {
|
|
541
571
|
const params = new URL(request.url).searchParams
|
|
542
|
-
const target = reach(viewer, params.get('connection'), params.get('database'))
|
|
572
|
+
const target = await reach(viewer, params.get('connection'), params.get('database'))
|
|
543
573
|
return (await backups()).options(backupContext(target, viewer))
|
|
544
574
|
},
|
|
545
575
|
},
|
|
@@ -556,7 +586,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
556
586
|
path,
|
|
557
587
|
async run(request: Request, viewer: Viewer) {
|
|
558
588
|
const body = await json(request)
|
|
559
|
-
const target = reach(viewer, body.connection, body.database)
|
|
589
|
+
const target = await reach(viewer, body.connection, body.database)
|
|
560
590
|
return (await backups())[action](backupContext(target, viewer), body)
|
|
561
591
|
},
|
|
562
592
|
})),
|
|
@@ -566,7 +596,7 @@ export function createDbAdmin(options: DbAdminOptions): DbAdmin {
|
|
|
566
596
|
async run(request, viewer) {
|
|
567
597
|
// The body is the piece itself; the rest comes in the address.
|
|
568
598
|
const params = new URL(request.url).searchParams
|
|
569
|
-
const target = reach(viewer, params.get('connection'), params.get('database'))
|
|
599
|
+
const target = await reach(viewer, params.get('connection'), params.get('database'))
|
|
570
600
|
return (await backups()).chunk(backupContext(target, viewer), request)
|
|
571
601
|
},
|
|
572
602
|
},
|
|
@@ -883,10 +913,16 @@ function changesOf(value: unknown): Change[] {
|
|
|
883
913
|
if (change.type === 'insert')
|
|
884
914
|
return { type: 'insert', values: values(change.values ?? {}, `${at}.values`, true, false) }
|
|
885
915
|
if (change.type === 'update') {
|
|
916
|
+
const set = values(change.values, `${at}.values`, true, true)
|
|
917
|
+
const original =
|
|
918
|
+
change.original === undefined
|
|
919
|
+
? undefined
|
|
920
|
+
: values(change.original, `${at}.original`, true, false)
|
|
886
921
|
return {
|
|
887
922
|
type: 'update',
|
|
888
923
|
key: values(change.key, `${at}.key`, false, true) as Record<string, string>,
|
|
889
|
-
values:
|
|
924
|
+
values: set,
|
|
925
|
+
...(original ? { original } : {}),
|
|
890
926
|
}
|
|
891
927
|
}
|
|
892
928
|
if (change.type === 'delete') {
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
export { connectionsFromEnv, driverKind } from './connections.ts'
|
|
2
2
|
export { openDriver, type DriverOptions } from './drivers/index.ts'
|
|
3
3
|
export { backtick, mysqlDriver, mysqlLiteral } from './drivers/mysql.ts'
|
|
4
|
-
export { sqliteDriver, sqliteLiteral, sqlitePath } from './drivers/sqlite.ts'
|
|
5
|
-
export {
|
|
4
|
+
export { sqliteDriver, sqliteLiteral, sqlitePath, type SqliteOptions } from './drivers/sqlite.ts'
|
|
5
|
+
export {
|
|
6
|
+
pgliteDriver,
|
|
7
|
+
type PGliteLike,
|
|
8
|
+
type PGliteOptions,
|
|
9
|
+
type PGliteQueryable,
|
|
10
|
+
} from './drivers/pglite.ts'
|
|
6
11
|
export { postgresDriver, type PostgresModules, type PostgresOptions } from './drivers/postgres.ts'
|
|
7
12
|
export { literal as literalPostgres, quote as quotePostgres } from './drivers/postgres-catalog.ts'
|
|
8
13
|
export { ConnectionError, DbAdminError, QueryError } from './errors.ts'
|
package/src/types.ts
CHANGED
|
@@ -181,6 +181,12 @@ export type Change =
|
|
|
181
181
|
type: 'update'
|
|
182
182
|
key: Readonly<Record<string, string>>
|
|
183
183
|
values: Readonly<Record<string, string | null>>
|
|
184
|
+
/**
|
|
185
|
+
* What the edited cells held when the person edited them, as the grid showed them. With
|
|
186
|
+
* it, nothing is saved if one holds something else by now: someone changed it meanwhile.
|
|
187
|
+
* Without it, the update goes by the key alone, and the last to save wins.
|
|
188
|
+
*/
|
|
189
|
+
original?: Readonly<Record<string, string | null>>
|
|
184
190
|
}
|
|
185
191
|
| { type: 'delete'; key: Readonly<Record<string, string>> }
|
|
186
192
|
|
|
@@ -199,6 +205,31 @@ export type ExportFormat = 'csv' | 'json' | 'sql'
|
|
|
199
205
|
export interface SqlDialect {
|
|
200
206
|
identifier(name: string): string
|
|
201
207
|
literal(value: string | null): string
|
|
208
|
+
/**
|
|
209
|
+
* Bytes as the grid shows them (MySQL's `0x1F`, SQLite's `X'1F'`), as a literal that is those
|
|
210
|
+
* bytes; null when `shown` isn't in that form. Absent where the shown form is text the
|
|
211
|
+
* database reads as bytes itself (PostgreSQL's `\x1f`).
|
|
212
|
+
*/
|
|
213
|
+
bytes?(shown: string): string | null
|
|
214
|
+
/** What an INSERT says before VALUES to bring its own values for a column numbered always. */
|
|
215
|
+
overriding?: string
|
|
216
|
+
/** An INSERT of nothing but defaults, where it isn't `DEFAULT VALUES`. */
|
|
217
|
+
insertDefaults?: string
|
|
218
|
+
/** What an SQL export starts with, so its literals are read as written whatever the session. */
|
|
219
|
+
exportHeader?: string
|
|
220
|
+
/** What it ends with, putting back what the header changed. */
|
|
221
|
+
exportFooter?: string
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A column of an export, with what an SQL export must know to put its values back. */
|
|
225
|
+
export interface ExportColumn {
|
|
226
|
+
name: string
|
|
227
|
+
/** Its values are bytes, in the database's own hex form. */
|
|
228
|
+
bytes?: boolean
|
|
229
|
+
/** The database computes it from other columns: an INSERT leaves it out. */
|
|
230
|
+
generated?: boolean
|
|
231
|
+
/** The database numbers it always: an INSERT says it brings its own values. */
|
|
232
|
+
identity?: boolean
|
|
202
233
|
}
|
|
203
234
|
|
|
204
235
|
/** The server a connection reaches, for the toolbar. */
|
|
@@ -262,7 +293,7 @@ export interface Driver {
|
|
|
262
293
|
export(
|
|
263
294
|
request: Pick<RowsRequest, 'schema' | 'table' | 'filters' | 'order'>,
|
|
264
295
|
options: { timeoutMs: number },
|
|
265
|
-
): AsyncGenerator<{ columns:
|
|
296
|
+
): AsyncGenerator<{ columns: ExportColumn[]; rows: (string | null)[][] }>
|
|
266
297
|
readonly sql: SqlDialect
|
|
267
298
|
/** One statement. */
|
|
268
299
|
query(sql: string, options: QueryOptions): Promise<QueryResult>
|
|
@@ -300,6 +331,12 @@ export interface Session {
|
|
|
300
331
|
* Finish quickly, without waiting on a slow download.
|
|
301
332
|
*/
|
|
302
333
|
readonly exclusive: boolean
|
|
334
|
+
/**
|
|
335
|
+
* Whether the transaction it opened is still open: false once a statement has ended it, by a
|
|
336
|
+
* COMMIT or a ROLLBACK of its own. A restore asks after each statement of its file, so one
|
|
337
|
+
* that ends the transaction is the last to run. A driver that can't tell leaves it out.
|
|
338
|
+
*/
|
|
339
|
+
inTransaction?(): boolean
|
|
303
340
|
}
|
|
304
341
|
|
|
305
342
|
/** Who is asking, as the host's `authorize()` says. */
|
package/src/words.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A PostgreSQL statement's first words, read the way the server reads them. What refuses a
|
|
3
|
+
* statement by how it starts (a restore's COMMIT, a read-only connection's COPY) must see the
|
|
4
|
+
* word the server will, whatever comments and line ends are put before it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Where the SQL goes on from `at`, past spaces and comments, as PostgreSQL reads them: a `--`
|
|
9
|
+
* comment ends at a line feed or a carriage return, whichever comes first, and block comments
|
|
10
|
+
* nest. Read otherwise, a word after a carriage return would be taken for part of the comment
|
|
11
|
+
* here and for SQL by the server.
|
|
12
|
+
*/
|
|
13
|
+
export function pastComments(statement: string, from: number): number {
|
|
14
|
+
let at = from
|
|
15
|
+
for (;;) {
|
|
16
|
+
while (/\s/.test(statement[at] ?? '')) at += 1
|
|
17
|
+
if (statement.startsWith('--', at)) {
|
|
18
|
+
const end = statement.slice(at).search(/[\n\r]/)
|
|
19
|
+
if (end === -1) return statement.length
|
|
20
|
+
at += end + 1
|
|
21
|
+
} else if (statement.startsWith('/*', at)) {
|
|
22
|
+
let depth = 0
|
|
23
|
+
do {
|
|
24
|
+
if (statement.startsWith('/*', at)) {
|
|
25
|
+
depth += 1
|
|
26
|
+
at += 2
|
|
27
|
+
} else if (statement.startsWith('*/', at)) {
|
|
28
|
+
depth -= 1
|
|
29
|
+
at += 2
|
|
30
|
+
} else at += 1
|
|
31
|
+
} while (depth > 0 && at < statement.length)
|
|
32
|
+
} else return at
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The statement's first two words with one space between, past the comments and spaces before
|
|
38
|
+
* and between them: `PREPARE TRANSACTION`, however it's written.
|
|
39
|
+
*/
|
|
40
|
+
export function firstWords(statement: string): string {
|
|
41
|
+
const words: string[] = []
|
|
42
|
+
let at = 0
|
|
43
|
+
while (words.length < 2) {
|
|
44
|
+
at = pastComments(statement, at)
|
|
45
|
+
const word = /^[A-Za-z_]+/.exec(statement.slice(at, at + 64))?.[0]
|
|
46
|
+
if (!word) break
|
|
47
|
+
words.push(word)
|
|
48
|
+
at += word.length
|
|
49
|
+
}
|
|
50
|
+
return words.join(' ')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The statement's first word in capitals, past spaces, comments, brackets and empty statements
|
|
55
|
+
* (`;COPY …` is COPY to the server). Read a character at a time: a pattern for "any number of
|
|
56
|
+
* comments" tries every way to split them when no word follows, and a statement of a hundred
|
|
57
|
+
* characters can hold the thread for hours.
|
|
58
|
+
*/
|
|
59
|
+
export function leadingWord(sql: string): string {
|
|
60
|
+
let at = 0
|
|
61
|
+
for (;;) {
|
|
62
|
+
at = pastComments(sql, at)
|
|
63
|
+
if (sql[at] === ';' || sql[at] === '(') at += 1
|
|
64
|
+
else break
|
|
65
|
+
}
|
|
66
|
+
return (/^[A-Za-z_]+/.exec(sql.slice(at, at + 64))?.[0] ?? '').toUpperCase()
|
|
67
|
+
}
|