Skip to content

Commit 8c1ea50

Browse files
committed
Update high-level API implementation.
1 parent 6994452 commit 8c1ea50

File tree

7 files changed

+65
-122
lines changed

7 files changed

+65
-122
lines changed

packages/api/src/api.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ export interface ReservedSqliteConnection extends SqliteConnection {
6161
}
6262

6363
export interface QueryInterface {
64-
prepare<T extends SqliteObjectRow>(
65-
query: string,
66-
args?: SqliteArguments,
67-
options?: QueryOptions
68-
): PreparedQuery<T>;
64+
prepare<T extends SqliteObjectRow>(query: string): PreparedQuery<T>;
6965

7066
run(
7167
query: string,

packages/api/src/impl.ts

Lines changed: 35 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ export class ConnectionPoolImpl
5454
throw new Error('Method not implemented.');
5555
}
5656

57-
prepare<T extends SqliteObjectRow>(
58-
sql: string,
59-
args?: SqliteArguments
60-
): PreparedQuery<T> {
61-
return new ConnectionPoolPreparedQueryImpl<T>(this, sql, args);
57+
prepare<T extends SqliteObjectRow>(sql: string): PreparedQuery<T> {
58+
return new ConnectionPoolPreparedQueryImpl<T>(this, sql);
6259
}
6360

6461
pipeline(options?: ReserveConnectionOptions | undefined): QueryPipeline {
@@ -202,12 +199,8 @@ export class ReservedConnectionImpl implements ReservedSqliteConnection {
202199
}
203200
}
204201

205-
prepare<T extends SqliteObjectRow>(
206-
sql: string,
207-
args?: SqliteArguments,
208-
options?: QueryOptions
209-
): PreparedQuery<T> {
210-
return this.connection.prepare(sql, args, options);
202+
prepare<T extends SqliteObjectRow>(sql: string): PreparedQuery<T> {
203+
return this.connection.prepare(sql);
211204
}
212205

213206
pipeline(): QueryPipeline {
@@ -294,12 +287,12 @@ export class ConnectionImpl implements SqliteConnection {
294287
constructor(private driver: SqliteDriverConnection) {}
295288

296289
private init() {
297-
this._beginExclusive ??= this.prepare('BEGIN EXCLUSIVE', undefined, {
290+
this._beginExclusive ??= this.prepare('BEGIN EXCLUSIVE', {
298291
autoFinalize: true
299292
});
300-
this._begin ??= this.prepare('BEGIN', undefined, { autoFinalize: true });
301-
this.commit ??= this.prepare('COMMIT', undefined, { autoFinalize: true });
302-
this.rollback ??= this.prepare('ROLLBACK', undefined, {
293+
this._begin ??= this.prepare('BEGIN', { autoFinalize: true });
294+
this.commit ??= this.prepare('COMMIT', { autoFinalize: true });
295+
this.rollback ??= this.prepare('ROLLBACK', {
303296
autoFinalize: true
304297
});
305298
}
@@ -366,20 +359,10 @@ export class ConnectionImpl implements SqliteConnection {
366359

367360
prepare<T extends SqliteObjectRow>(
368361
sql: string,
369-
args?: SqliteArguments,
370362
options?: PrepareOptions
371363
): PreparedQuery<T> {
372364
const statement = this.driver.prepare(sql, options);
373-
if (args) {
374-
statement.bind(args);
375-
}
376-
return new ConnectionPreparedQueryImpl(
377-
this,
378-
this.driver,
379-
statement,
380-
sql,
381-
args
382-
);
365+
return new ConnectionPreparedQueryImpl(this, this.driver, statement, sql);
383366
}
384367

385368
pipeline(): QueryPipeline {
@@ -388,49 +371,31 @@ export class ConnectionImpl implements SqliteConnection {
388371

389372
async run(query: string, args: SqliteArguments): Promise<RunResult> {
390373
using statement = this.driver.prepare(query);
391-
if (args != null) {
392-
statement.bind(args);
393-
}
394-
return await statement.run();
374+
return await statement.run(args);
395375
}
396376

397377
async *stream<T extends SqliteObjectRow>(
398-
query: string | PreparedQuery<T>,
378+
query: string,
399379
args: SqliteArguments | undefined,
400380
options?: StreamOptions | undefined
401381
): AsyncGenerator<T[], void, unknown> {
402-
using statement = this.driver.prepare(query as string, {
382+
using statement = this.driver.prepare(query);
383+
const chunkSize = options?.chunkSize;
384+
385+
const iter = statement.stream(args, {
386+
chunkMaxRows: chunkSize,
403387
bigint: options?.bigint
404388
});
405-
if (args != null) {
406-
statement.bind(args);
407-
}
408-
const chunkSize = options?.chunkSize ?? 100;
409-
410-
while (true) {
411-
const { rows, done } = await statement.step(chunkSize);
412-
if (rows != null) {
413-
yield rows as T[];
414-
}
415-
if (done) {
416-
break;
417-
}
418-
}
389+
yield* iter as AsyncGenerator<T[], void, unknown>;
419390
}
420391

421392
async select<T extends SqliteObjectRow>(
422393
query: string,
423394
args?: SqliteArguments,
424395
options?: (QueryOptions & ReserveConnectionOptions) | undefined
425396
): Promise<T[]> {
426-
using statement = this.driver.prepare(query, {
427-
bigint: options?.bigint,
428-
rawResults: false
429-
});
430-
if (args != null) {
431-
statement.bind(args);
432-
}
433-
const { rows } = await statement.step();
397+
using statement = this.driver.prepare(query);
398+
const rows = await statement.all(args, { bigint: options?.bigint });
434399
return rows as T[];
435400
}
436401

@@ -469,12 +434,8 @@ export class TransactionImpl implements SqliteTransaction {
469434
await this.con.rollback!.select();
470435
}
471436

472-
prepare<T extends SqliteObjectRow>(
473-
sql: string,
474-
args?: SqliteArguments,
475-
options?: QueryOptions
476-
): PreparedQuery<T> {
477-
const q = this.con.prepare<T>(sql, args, options);
437+
prepare<T extends SqliteObjectRow>(sql: string): PreparedQuery<T> {
438+
const q = this.con.prepare<T>(sql);
478439
// FIXME: auto-dispose these after transaction commit / rollback
479440
this.preparedQueries.push(q);
480441
return q;
@@ -601,8 +562,7 @@ class ConnectionPoolPreparedQueryImpl<T extends SqliteObjectRow>
601562

602563
constructor(
603564
private context: ConnectionPoolImpl,
604-
public sql: string,
605-
public args: SqliteArguments
565+
public sql: string
606566
) {
607567
if (typeof Symbol.dispose != 'undefined') {
608568
this[Symbol.dispose] = () => this.dispose();
@@ -663,7 +623,7 @@ class ConnectionPoolPreparedQueryImpl<T extends SqliteObjectRow>
663623
const cimpl = connection as ConnectionImpl;
664624
let sub = this.byConnection.get(cimpl);
665625
if (sub == null) {
666-
sub = cimpl.prepare(this.sql, this.args);
626+
sub = cimpl.prepare(this.sql);
667627
this.byConnection.set(cimpl, sub);
668628
}
669629
return sub;
@@ -681,8 +641,7 @@ class ConnectionPreparedQueryImpl<T extends SqliteObjectRow>
681641
private context: ConnectionImpl,
682642
private driver: SqliteDriverConnection,
683643
public statement: SqliteDriverStatement,
684-
public sql: string,
685-
public args: SqliteArguments
644+
public sql: string
686645
) {
687646
if (typeof Symbol.dispose != 'undefined') {
688647
this[Symbol.dispose] = () => this.dispose();
@@ -700,42 +659,22 @@ class ConnectionPreparedQueryImpl<T extends SqliteObjectRow>
700659
args?: SqliteArguments,
701660
options?: StreamOptions | undefined
702661
): AsyncGenerator<T[], any, unknown> {
703-
const chunkSize = options?.chunkSize ?? 10;
704-
if (args != null) {
705-
this.statement.bind(args);
706-
}
707-
try {
708-
while (true) {
709-
const { rows, done } = await this.statement.step(chunkSize);
710-
if (rows != null) {
711-
yield rows as T[];
712-
}
713-
if (done) {
714-
break;
715-
}
716-
}
717-
} finally {
718-
this.statement.reset();
662+
const chunkSize = options?.chunkSize;
663+
const iter = this.statement.stream(args, {
664+
chunkMaxRows: chunkSize
665+
});
666+
for await (let chunk of iter) {
667+
yield chunk as T[];
719668
}
720669
}
721670

722671
async run(args?: SqliteArguments): Promise<RunResult> {
723-
if (args != null) {
724-
this.statement.bind(args);
725-
}
726-
return await this.statement.run();
672+
return await this.statement.run(args);
727673
}
728674

729675
async select(args?: SqliteArguments): Promise<T[]> {
730-
try {
731-
if (args != null) {
732-
this.statement.bind(args);
733-
}
734-
const { rows } = await this.statement.step();
735-
return rows as T[];
736-
} finally {
737-
this.statement.reset();
738-
}
676+
const rows = await this.statement.all(args);
677+
return rows as T[];
739678
}
740679

741680
dispose(): void {
@@ -753,19 +692,14 @@ class QueryPipelineImpl implements QueryPipeline {
753692
this.count += 1;
754693
if (typeof query == 'string') {
755694
using statement = this.driver.prepare(query);
756-
if (args) {
757-
statement.bind(args);
758-
}
759-
this.lastPromise = statement.step(undefined, {
695+
this.lastPromise = statement.run(args, {
760696
requireTransaction: true
761697
});
762698
} else if (query instanceof ConnectionPreparedQueryImpl) {
763699
const statement = query.statement;
764-
statement.bind(args ?? []);
765-
this.lastPromise = statement.step(undefined, {
700+
this.lastPromise = statement.run(args, {
766701
requireTransaction: true
767702
});
768-
statement.reset();
769703
} else {
770704
throw new Error('not implemented yet');
771705
}

packages/better-sqlite3-driver/src/sync-driver.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ class BetterSqlitePreparedStatement implements InternalStatement {
6969

7070
const statement = this.statement;
7171

72-
statement.safeIntegers(options?.bigint ?? false);
73-
statement.raw(array);
74-
const r = statement.all(sanitizeParameters(parameters));
75-
return r;
72+
if (statement.reader) {
73+
statement.safeIntegers(options?.bigint ?? false);
74+
statement.raw(array);
75+
const rows = statement.all(sanitizeParameters(parameters));
76+
return rows;
77+
} else {
78+
statement.run(sanitizeParameters(parameters));
79+
return [];
80+
}
7681
}
7782

7883
async all(
@@ -106,8 +111,13 @@ class BetterSqlitePreparedStatement implements InternalStatement {
106111

107112
const statement = this.statement;
108113

109-
statement.safeIntegers(options?.bigint ?? false);
110-
statement.raw(array);
114+
if (statement.reader) {
115+
statement.safeIntegers(options?.bigint ?? false);
116+
statement.raw(array);
117+
} else {
118+
statement.run(sanitizeParameters(parameters));
119+
return;
120+
}
111121
const iter = statement.iterate(sanitizeParameters(parameters));
112122
const maxBuffer = options?.chunkMaxRows ?? 100;
113123
let buffer: any[] = [];
@@ -118,12 +128,15 @@ class BetterSqlitePreparedStatement implements InternalStatement {
118128
buffer = [];
119129
}
120130
}
131+
if (buffer.length > 0) {
132+
yield buffer;
133+
}
121134
}
122135

123136
async *stream(
124137
parameters?: SqliteParameterBinding,
125138
options?: StreamQueryOptions
126-
): AsyncIterator<SqliteObjectRow[]> {
139+
): AsyncIterableIterator<SqliteObjectRow[]> {
127140
try {
128141
yield* this._stream(parameters, options, false);
129142
} catch (e) {
@@ -134,7 +147,7 @@ class BetterSqlitePreparedStatement implements InternalStatement {
134147
async *streamArray(
135148
parameters?: SqliteParameterBinding,
136149
options?: StreamQueryOptions
137-
): AsyncIterator<SqliteArrayRow[]> {
150+
): AsyncIterableIterator<SqliteArrayRow[]> {
138151
try {
139152
yield* this._stream(parameters, options, true);
140153
} catch (e) {

packages/driver/src/driver-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export interface SqliteDriverStatement {
6161
stream(
6262
parameters?: SqliteParameterBinding,
6363
options?: StreamQueryOptions
64-
): AsyncIterator<SqliteObjectRow[]>;
64+
): AsyncIterableIterator<SqliteObjectRow[]>;
6565
streamArray(
6666
parameters?: SqliteParameterBinding,
6767
options?: StreamQueryOptions
68-
): AsyncIterator<SqliteArrayRow[]>;
68+
): AsyncIterableIterator<SqliteArrayRow[]>;
6969

7070
/**
7171
* Run a query, and return the number of changed rows, and last insert id.

packages/driver/src/node/impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ class NodeSqliteSyncStatement implements InternalStatement {
114114
async *stream(
115115
parameters: SqliteParameterBinding,
116116
options: StreamQueryOptions
117-
): AsyncIterator<SqliteObjectRow[]> {
117+
): AsyncIterableIterator<SqliteObjectRow[]> {
118118
const rows = await this.all(parameters, options);
119119
yield rows;
120120
}
121121

122122
streamArray(
123123
parameters: SqliteParameterBinding,
124124
options: StreamQueryOptions
125-
): AsyncIterator<SqliteArrayRow[]> {
125+
): AsyncIterableIterator<SqliteArrayRow[]> {
126126
throw new Error('array rows are not supported');
127127
}
128128

packages/driver/src/util/ErrorStatement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export class ErrorStatement implements SqliteDriverStatement {
4646
async *stream(
4747
parameters: SqliteParameterBinding,
4848
options: StreamQueryOptions
49-
): AsyncIterator<SqliteObjectRow[]> {
49+
): AsyncIterableIterator<SqliteObjectRow[]> {
5050
throw this.error;
5151
}
5252
async *streamArray(
5353
parameters: SqliteParameterBinding,
5454
options: StreamQueryOptions
55-
): AsyncIterator<SqliteArrayRow[]> {
55+
): AsyncIterableIterator<SqliteArrayRow[]> {
5656
throw this.error;
5757
}
5858

packages/driver/src/worker_threads/worker-driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ class WorkerDriverStatement implements SqliteDriverStatement {
226226
stream(
227227
parameters: SqliteParameterBinding,
228228
options?: StreamQueryOptions
229-
): AsyncIterator<SqliteObjectRow[]> {
229+
): AsyncIterableIterator<SqliteObjectRow[]> {
230230
throw new Error('Method not implemented.');
231231
}
232232

233233
streamArray(
234234
parameters: SqliteParameterBinding,
235235
options?: StreamQueryOptions
236-
): AsyncIterator<SqliteArrayRow[]> {
236+
): AsyncIterableIterator<SqliteArrayRow[]> {
237237
throw new Error('Method not implemented.');
238238
}
239239

0 commit comments

Comments
 (0)