@@ -35,8 +35,8 @@ export function waSqlitePool(path: string): SqliteDriverConnectionPool {
35
35
const m = new mutex . Mutex ( ) ;
36
36
37
37
class StatementImpl implements SqliteDriverStatement {
38
- private preparePromise : Promise < void > ;
39
- private bindPromise ?: Promise < void > ;
38
+ private preparePromise : Promise < { error : SqliteError | null } > ;
39
+ private bindPromise ?: Promise < { error : SqliteError | null } > ;
40
40
private columns : string [ ] = [ ] ;
41
41
42
42
private stringRef ?: number ;
@@ -52,7 +52,7 @@ class StatementImpl implements SqliteDriverStatement {
52
52
}
53
53
54
54
async prepare ( ) {
55
- await m . runExclusive ( ( ) => this . _prepare ( ) ) ;
55
+ return await m . runExclusive ( ( ) => this . _prepare ( ) ) ;
56
56
}
57
57
58
58
async _prepare ( ) {
@@ -66,22 +66,36 @@ class StatementImpl implements SqliteDriverStatement {
66
66
67
67
this . statementRef = r ?. stmt ;
68
68
this . columns = sqlite3 . column_names ( this . statementRef ! ) ;
69
+ return { error : null } ;
69
70
} catch ( e : any ) {
70
- throw new SqliteError ( {
71
- code : 'SQLITE_ERROR' ,
72
- message : e . message
73
- } ) ;
71
+ return {
72
+ error : new SqliteError ( {
73
+ code : 'SQLITE_ERROR' ,
74
+ message : e . message
75
+ } )
76
+ } ;
77
+ }
78
+ }
79
+
80
+ private async _waitForPrepare ( ) {
81
+ const { error } = await ( this . bindPromise ?? this . preparePromise ) ;
82
+ if ( error ) {
83
+ throw error ;
74
84
}
75
85
}
76
86
77
87
async getColumns ( ) : Promise < string [ ] > {
78
- await this . preparePromise ;
88
+ await this . _waitForPrepare ( ) ;
79
89
return sqlite3 . column_names ( this . statementRef ! ) ;
80
90
}
81
91
82
92
bind ( parameters : SqliteParameterBinding ) : void {
83
- this . bindPromise = this . preparePromise . then ( async ( ) => {
93
+ this . bindPromise = this . preparePromise . then ( async ( result ) => {
94
+ if ( result . error ) {
95
+ return result ;
96
+ }
84
97
await m . runExclusive ( ( ) => this . bindImpl ( parameters ) ) ;
98
+ return { error : null } ;
85
99
} ) ;
86
100
}
87
101
@@ -125,13 +139,12 @@ class StatementImpl implements SqliteDriverStatement {
125
139
}
126
140
127
141
async step ( n ?: number , options ?: StepOptions ) : Promise < SqliteStepResult > {
128
- await this . preparePromise ;
142
+ await this . _waitForPrepare ( ) ;
143
+
129
144
return await m . runExclusive ( ( ) => this . _step ( n , options ) ) ;
130
145
}
131
146
132
147
async _step ( n ?: number , options ?: StepOptions ) : Promise < SqliteStepResult > {
133
- await this . preparePromise ;
134
-
135
148
try {
136
149
if ( this . done ) {
137
150
return { done : true } ;
@@ -185,7 +198,10 @@ class StatementImpl implements SqliteDriverStatement {
185
198
}
186
199
187
200
async _finalize ( ) {
201
+ // Wait for these to complete, but ignore any errors.
202
+ // TODO: also wait for run/step to complete
188
203
await this . preparePromise ;
204
+ await this . bindPromise ;
189
205
190
206
if ( this . statementRef ) {
191
207
sqlite3 . finalize ( this . statementRef ) ;
0 commit comments