@@ -2,16 +2,15 @@ import type * as sqlite from './node-sqlite.js';
2
2
3
3
import {
4
4
PrepareOptions ,
5
- ResetOptions ,
5
+ QueryOptions ,
6
+ SqliteArrayRow ,
6
7
SqliteChanges ,
7
8
SqliteDriverConnection ,
8
9
SqliteDriverConnectionPool ,
9
10
SqliteDriverStatement ,
11
+ SqliteObjectRow ,
10
12
SqliteParameterBinding ,
11
- SqliteRow ,
12
- SqliteStepResult ,
13
- SqliteValue ,
14
- StepOptions ,
13
+ StreamQueryOptions ,
15
14
UpdateListener
16
15
} from '../driver-api.js' ;
17
16
@@ -43,20 +42,14 @@ interface InternalStatement extends SqliteDriverStatement {
43
42
44
43
class NodeSqliteSyncStatement implements InternalStatement {
45
44
public statement : sqlite . StatementSync ;
46
- private options : PrepareOptions ;
47
- private bindPositional : SqliteValue [ ] = [ ] ;
48
- private bindNamed : Record < string , SqliteValue > = { } ;
49
- private statementDone = false ;
50
- private iterator : Iterator < unknown > | undefined = undefined ;
51
45
52
46
readonly persisted : boolean ;
53
47
54
48
[ Symbol . dispose ] : ( ) => void = undefined as any ;
55
49
56
50
constructor ( statement : sqlite . StatementSync , options : PrepareOptions ) {
57
51
this . statement = statement ;
58
- this . options = options ;
59
- this . persisted = options . persist ?? false ;
52
+ this . persisted = options . autoFinalize ?? false ;
60
53
61
54
if ( typeof Symbol . dispose != 'undefined' ) {
62
55
this [ Symbol . dispose ] = ( ) => this . finalize ( ) ;
@@ -72,120 +65,69 @@ class NodeSqliteSyncStatement implements InternalStatement {
72
65
return [ ] ;
73
66
}
74
67
75
- bind ( parameters : SqliteParameterBinding ) : void {
76
- if ( parameters == null ) {
77
- return ;
78
- }
79
- if ( Array . isArray ( parameters ) ) {
80
- let bindArray = this . bindPositional ;
81
-
82
- for ( let i = 0 ; i < parameters . length ; i ++ ) {
83
- if ( typeof parameters [ i ] != 'undefined' ) {
84
- bindArray [ i ] = parameters [ i ] ! ;
85
- }
86
- }
87
- } else {
88
- let previous = this . bindNamed ;
89
- this . bindNamed = { ...previous , ...parameters } ;
90
- }
91
- }
92
-
93
- async run ( options ?: StepOptions ) : Promise < SqliteChanges > {
68
+ async run (
69
+ parameters : SqliteParameterBinding ,
70
+ options ?: QueryOptions
71
+ ) : Promise < SqliteChanges > {
94
72
try {
95
73
if ( options ?. requireTransaction ) {
96
74
// TODO: Implement
97
75
}
98
76
99
77
const statement = this . statement ;
100
- this . reset ( ) ;
101
-
102
- try {
103
- const bindNamed = this . bindNamed ;
104
- const bindPositional = this . bindPositional ;
105
-
106
- statement . setReadBigInts ( true ) ;
107
- const r = statement . run ( bindNamed , ...bindPositional ) ;
108
- return {
109
- changes : Number ( r . changes ) ,
110
- lastInsertRowId : r . lastInsertRowid as bigint
111
- } ;
112
- } finally {
113
- this . reset ( ) ;
114
- }
78
+ statement . setReadBigInts ( true ) ;
79
+ const r = statement . run ( ...convertParameters ( parameters ) ) ;
80
+ return {
81
+ changes : Number ( r . changes ) ,
82
+ lastInsertRowId : r . lastInsertRowid as bigint
83
+ } ;
115
84
} catch ( e ) {
116
85
throw mapError ( e ) ;
117
86
}
118
87
}
119
88
120
- async step ( n ?: number , options ?: StepOptions ) : Promise < SqliteStepResult > {
89
+ async all (
90
+ parameters : SqliteParameterBinding ,
91
+ options ?: QueryOptions
92
+ ) : Promise < SqliteObjectRow [ ] > {
121
93
try {
122
- const all = n == null ;
123
-
124
- const statement = this . statement ;
125
- if ( this . statementDone ) {
126
- return { done : true } ;
127
- }
128
-
129
94
if ( options ?. requireTransaction ) {
130
- // TODO: implement
95
+ // TODO: Implement
131
96
}
132
97
133
- const bindNamed = this . bindNamed ;
134
- const bindPositional = this . bindPositional ;
135
-
136
- let iterator = this . iterator ;
137
- const num_rows = n ?? 1 ;
138
- if ( iterator == null ) {
139
- if ( this . options . rawResults ) {
140
- // Not supported
141
- }
142
- if ( this . options . bigint ) {
143
- statement . setReadBigInts ( true ) ;
144
- }
145
- iterator = statement
146
- . all ( bindNamed , ...bindPositional )
147
- [ Symbol . iterator ] ( ) ;
148
- this . iterator = iterator ;
149
- }
150
- let rows : SqliteRow [ ] = [ ] ;
151
- let isDone = false ;
152
- for ( let i = 0 ; i < num_rows || all ; i ++ ) {
153
- const { value, done } = iterator . next ( ) ;
154
- if ( done ) {
155
- isDone = true ;
156
- break ;
157
- }
158
- rows . push ( value as SqliteRow ) ;
159
- }
160
- if ( isDone ) {
161
- this . statementDone = true ;
162
- }
163
- return { rows, done : isDone } ;
98
+ const statement = this . statement ;
99
+ statement . setReadBigInts ( options ?. bigint ?? false ) ;
100
+ const rows = statement . all ( ...convertParameters ( parameters ) ) ;
101
+ return rows ;
164
102
} catch ( e ) {
165
103
throw mapError ( e ) ;
166
104
}
167
105
}
168
106
169
- finalize ( ) : void {
170
- const existingIter = this . iterator ;
171
- if ( existingIter != null ) {
172
- existingIter . return ?.( ) ;
173
- }
174
- this . iterator = undefined ;
175
- this . statementDone = false ;
107
+ allArray (
108
+ parameters : SqliteParameterBinding ,
109
+ options : QueryOptions
110
+ ) : Promise < SqliteArrayRow [ ] > {
111
+ throw new Error ( 'array rows are not supported' ) ;
176
112
}
177
113
178
- reset ( options ?: ResetOptions ) : void {
179
- if ( this . iterator ) {
180
- const iter = this . iterator ;
181
- iter . return ?.( ) ;
182
- this . iterator = undefined ;
183
- }
184
- if ( options ?. clearBindings ) {
185
- this . bindNamed = { } ;
186
- this . bindPositional = [ ] ;
187
- }
188
- this . statementDone = false ;
114
+ async * stream (
115
+ parameters : SqliteParameterBinding ,
116
+ options : StreamQueryOptions
117
+ ) : AsyncIterator < SqliteObjectRow [ ] > {
118
+ const rows = await this . all ( parameters , options ) ;
119
+ yield rows ;
120
+ }
121
+
122
+ streamArray (
123
+ parameters : SqliteParameterBinding ,
124
+ options : StreamQueryOptions
125
+ ) : AsyncIterator < SqliteArrayRow [ ] > {
126
+ throw new Error ( 'array rows are not supported' ) ;
127
+ }
128
+
129
+ finalize ( ) : void {
130
+ // We don't use any iterators internally - nothing to cancel here
189
131
}
190
132
}
191
133
@@ -243,3 +185,11 @@ export class NodeSqliteConnection implements SqliteDriverConnection {
243
185
throw new Error ( 'not supported yet' ) ;
244
186
}
245
187
}
188
+
189
+ function convertParameters ( parameters : SqliteParameterBinding ) : any [ ] {
190
+ if ( Array . isArray ( parameters ) ) {
191
+ return parameters ;
192
+ } else {
193
+ return [ parameters ] ;
194
+ }
195
+ }
0 commit comments