|
| 1 | +import { |
| 2 | + SqliteChanges, |
| 3 | + SqliteDriverConnection, |
| 4 | + SqliteDriverStatement, |
| 5 | + SqliteStepResult, |
| 6 | + UpdateListener |
| 7 | +} from '@sqlite-js/driver'; |
| 8 | +import { mapError } from '@sqlite-js/driver/util'; |
| 9 | +import { |
| 10 | + InferBatchResult, |
| 11 | + SqliteBind, |
| 12 | + SqliteCommand, |
| 13 | + SqliteCommandResponse, |
| 14 | + SqliteCommandType, |
| 15 | + SqliteFinalize, |
| 16 | + SqliteParse, |
| 17 | + SqliteParseResult, |
| 18 | + SqlitePrepare, |
| 19 | + SqliteReset, |
| 20 | + SqliteRun, |
| 21 | + SqliteStep, |
| 22 | + WorkerDriver |
| 23 | +} from './async-commands.js'; |
| 24 | + |
| 25 | +export class WorkerConnectionAdapter implements WorkerDriver { |
| 26 | + constructor(public connnection: SqliteDriverConnection) {} |
| 27 | + |
| 28 | + statements = new Map<number, SqliteDriverStatement>(); |
| 29 | + |
| 30 | + async close() { |
| 31 | + await this.connnection.close(); |
| 32 | + } |
| 33 | + |
| 34 | + private requireStatement(id: number) { |
| 35 | + const statement = this.statements.get(id); |
| 36 | + if (statement == null) { |
| 37 | + throw new Error(`statement not found: ${id}`); |
| 38 | + } |
| 39 | + return statement; |
| 40 | + } |
| 41 | + |
| 42 | + private _prepare(command: SqlitePrepare): void { |
| 43 | + const { id, sql } = command; |
| 44 | + |
| 45 | + const existing = this.statements.get(id); |
| 46 | + if (existing != null) { |
| 47 | + throw new Error( |
| 48 | + `Replacing statement ${id} without finalizing the previous one` |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + const statement = this.connnection.prepare(sql, { |
| 53 | + bigint: command.bigint, |
| 54 | + persist: command.persist, |
| 55 | + rawResults: command.rawResults |
| 56 | + }); |
| 57 | + this.statements.set(id, statement); |
| 58 | + } |
| 59 | + |
| 60 | + private async _parse(command: SqliteParse): Promise<SqliteParseResult> { |
| 61 | + const { id } = command; |
| 62 | + const statement = this.requireStatement(id); |
| 63 | + return { columns: await statement.getColumns() }; |
| 64 | + } |
| 65 | + |
| 66 | + private _bind(command: SqliteBind): void { |
| 67 | + const { id, parameters } = command; |
| 68 | + const statement = this.requireStatement(id); |
| 69 | + statement.bind(parameters); |
| 70 | + } |
| 71 | + |
| 72 | + private _step(command: SqliteStep): Promise<SqliteStepResult> { |
| 73 | + const { id, n, requireTransaction } = command; |
| 74 | + const statement = this.requireStatement(id); |
| 75 | + return statement.step(n, { requireTransaction }); |
| 76 | + } |
| 77 | + |
| 78 | + private _run(command: SqliteRun): Promise<SqliteChanges> { |
| 79 | + const { id } = command; |
| 80 | + const statement = this.requireStatement(id); |
| 81 | + return statement.run(command); |
| 82 | + } |
| 83 | + |
| 84 | + private _reset(command: SqliteReset): void { |
| 85 | + const { id } = command; |
| 86 | + const statement = this.requireStatement(id); |
| 87 | + statement.reset(command); |
| 88 | + } |
| 89 | + |
| 90 | + private _finalize(command: SqliteFinalize): void { |
| 91 | + const { id } = command; |
| 92 | + const statement = this.requireStatement(id); |
| 93 | + statement.finalize(); |
| 94 | + this.statements.delete(id); |
| 95 | + } |
| 96 | + |
| 97 | + private async _executeCommand(command: SqliteCommand): Promise<any> { |
| 98 | + switch (command.type) { |
| 99 | + case SqliteCommandType.prepare: |
| 100 | + return this._prepare(command); |
| 101 | + case SqliteCommandType.bind: |
| 102 | + return this._bind(command); |
| 103 | + case SqliteCommandType.step: |
| 104 | + return this._step(command); |
| 105 | + case SqliteCommandType.run: |
| 106 | + return this._run(command); |
| 107 | + case SqliteCommandType.reset: |
| 108 | + return this._reset(command); |
| 109 | + case SqliteCommandType.finalize: |
| 110 | + return this._finalize(command); |
| 111 | + case SqliteCommandType.parse: |
| 112 | + return this._parse(command); |
| 113 | + case SqliteCommandType.changes: |
| 114 | + return this.connnection.getLastChanges(); |
| 115 | + default: |
| 116 | + throw new Error(`Unknown command: ${command.type}`); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + async execute<const T extends SqliteCommand[]>( |
| 121 | + commands: T |
| 122 | + ): Promise<InferBatchResult<T>> { |
| 123 | + let results: SqliteCommandResponse[] = []; |
| 124 | + |
| 125 | + for (let command of commands) { |
| 126 | + try { |
| 127 | + const result = await this._executeCommand(command); |
| 128 | + results.push({ value: result }); |
| 129 | + } catch (e: any) { |
| 130 | + const err = mapError(e); |
| 131 | + results.push({ |
| 132 | + error: { message: err.message, stack: err.stack, code: err.code } |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + return results as InferBatchResult<T>; |
| 137 | + } |
| 138 | + |
| 139 | + onUpdate( |
| 140 | + listener: UpdateListener, |
| 141 | + options?: |
| 142 | + | { tables?: string[] | undefined; batchLimit?: number | undefined } |
| 143 | + | undefined |
| 144 | + ): () => void { |
| 145 | + throw new Error('Not implemented yet'); |
| 146 | + } |
| 147 | +} |
0 commit comments