1
1
import { CompiledQuery , DatabaseConnection , Driver , QueryResult , SelectQueryNode } from 'kysely'
2
2
import type { Database } from 'node-sqlite3-wasm'
3
+ import { SQLite3Error } from 'node-sqlite3-wasm'
3
4
import { SqliteWASMDialectDatabaseConfig , SqliteWASMDialectWorkerConfig } from './SqliteWASMDialectConfig'
5
+ import { Exception } from '@xmcl/runtime-api'
4
6
5
7
declare module 'node-sqlite3-wasm' {
6
8
interface Statement {
@@ -56,7 +58,7 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
56
58
readonly #config: SqliteWASMDialectDatabaseConfig
57
59
58
60
#db?: Database
59
- #connection?: DatabaseConnection
61
+ #connection?: SqliteConnection
60
62
61
63
constructor ( config : SqliteWASMDialectDatabaseConfig ) {
62
64
super ( )
@@ -76,17 +78,23 @@ export class SqliteWASMDriver extends AbstractSqliteDriver {
76
78
}
77
79
78
80
async destroy ( ) : Promise < void > {
81
+ this . #connection?. dispose ( )
79
82
this . #db?. close ( )
80
83
}
81
84
}
82
85
83
86
class SqliteConnection implements DatabaseConnection {
84
87
readonly #db: Database
88
+ #disposed = false
85
89
86
90
constructor ( db : Database ) {
87
91
this . #db = db
88
92
}
89
93
94
+ dispose ( ) {
95
+ this . #disposed = true
96
+ }
97
+
90
98
executeQuery < O > ( compiledQuery : CompiledQuery ) : Promise < QueryResult < O > > {
91
99
const { sql, parameters } = compiledQuery
92
100
const stmt = this . #db. prepare ( sql )
@@ -110,6 +118,11 @@ class SqliteConnection implements DatabaseConnection {
110
118
: undefined ,
111
119
rows : [ ] ,
112
120
} )
121
+ } catch ( e ) {
122
+ if ( this . #disposed && e instanceof SQLite3Error ) {
123
+ return Promise . reject ( new Exception ( { type : 'sqlite3Exception' } , e . message , { cause : e } ) )
124
+ }
125
+ return Promise . reject ( e )
113
126
} finally {
114
127
stmt . finalize ( )
115
128
}
0 commit comments