-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.ts
More file actions
167 lines (147 loc) · 3.98 KB
/
connection.ts
File metadata and controls
167 lines (147 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// deno-lint-ignore-file require-await
import {
ReservedConnection,
SqliteDriverConnection,
SqliteDriverConnectionPool
} from '@sqlite-js/driver';
import type {
ArrayRow,
Row,
SqlConnectable,
SqlConnection,
SqlConnectionOptions
} from '@stdext/sql';
import type { SqliteParameterType, SqliteQueryOptions } from './core.js';
import { type DatabaseOpenOptions } from './database.js';
/** Various options that can be configured when opening Database connection. */
export interface SqliteConnectionOptions
extends SqlConnectionOptions,
DatabaseOpenOptions {}
export class SqliteConnection implements SqlConnection {
public driver: SqliteDriverConnection | undefined;
public readonly connectionUrl: string;
get connected(): boolean {
return this.driver != null;
}
public readonly options: SqliteConnectionOptions;
constructor(
connectionUrl: string,
driver: SqliteDriverConnection | undefined,
options?: SqliteConnectionOptions
) {
this.connectionUrl = connectionUrl;
this.driver = driver;
this.options = options ?? {};
}
async connect(): Promise<void> {
// No-op
}
async close(): Promise<void> {
await this.driver?.close();
}
async execute(
sql: string,
params?: SqliteParameterType[],
_options?: SqliteQueryOptions
): Promise<number | undefined> {
using statement = this.driver!.prepare(sql);
if (params != null) {
statement.bind(params);
}
const results = await statement.run();
return results.changes;
}
async *queryMany<T extends Row<any> = Row<any>>(
sql: string,
params?: SqliteParameterType[],
options?: SqliteQueryOptions
): AsyncGenerator<T, any, unknown> {
using statement = this.driver!.prepare(sql, {
bigint: this.options.int64 ?? false
});
if (params != null) {
statement.bind(params);
}
const chunkSize = 100;
while (true) {
const { rows, done } = await statement.step(chunkSize);
if (rows != null) {
const castRows = rows as T[];
for (let row of castRows) {
yield row;
}
}
if (done) {
break;
}
}
}
async *queryManyArray<T extends ArrayRow<any> = ArrayRow<any>>(
sql: string,
params?: SqliteParameterType[],
options?: SqliteQueryOptions
): AsyncGenerator<T, any, unknown> {
using statement = this.driver!.prepare(sql, {
bigint: this.options.int64 ?? false,
rawResults: true
});
if (params != null) {
statement.bind(params);
}
const chunkSize = 100;
while (true) {
const { rows, done } = await statement.step(chunkSize);
if (rows != null) {
const castRows = rows as T[];
for (let row of castRows) {
yield row;
}
}
if (done) {
break;
}
}
}
async [Symbol.asyncDispose](): Promise<void> {
await this.close();
}
}
export class SqliteReservedConnection extends SqliteConnection {
#connect: () => Promise<ReservedConnection>;
#reserved: ReservedConnection | undefined;
constructor(
connectionUrl: string,
connect: () => Promise<ReservedConnection>,
options?: SqliteConnectionOptions
) {
super(connectionUrl, undefined, options);
this.#connect = connect;
}
async connect(): Promise<void> {
this.#reserved = await this.#connect();
this.driver = this.#reserved.connection;
}
async close(): Promise<void> {
this.driver = undefined;
await this.#reserved?.release();
}
}
export class SqliteConnectable
implements SqlConnectable<SqliteConnectionOptions, SqliteConnection>
{
readonly connection: SqliteConnection;
readonly options: SqliteConnectionOptions;
get connected(): boolean {
return this.connection.connected;
}
constructor(
connection: SqliteConnectable['connection'],
options: SqliteConnectable['options'] = {}
) {
this.connection = connection;
this.options = options;
}
[Symbol.asyncDispose](): Promise<void> {
return this.connection.close();
}
}