vsql-node 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +203 -2
- package/binding.gyp +61 -0
- package/index.d.ts +133 -0
- package/internal/amalgamation/LICENSE +21 -0
- package/internal/amalgamation/LICENSE-V +21 -0
- package/internal/amalgamation/afunix.h +26 -0
- package/internal/amalgamation/atomic.h +571 -0
- package/internal/amalgamation/core_windows.c +25 -0
- package/internal/amalgamation/core_windows.h +9 -0
- package/internal/amalgamation/execute_capture_nix.c +7 -0
- package/internal/amalgamation/execute_capture_nix.h +266 -0
- package/internal/amalgamation/gc/cord.h +426 -0
- package/internal/amalgamation/gc/cord_pos.h +151 -0
- package/internal/amalgamation/gc/ec.h +91 -0
- package/internal/amalgamation/gc/gc.h +2826 -0
- package/internal/amalgamation/gc/gc_allocator.h +480 -0
- package/internal/amalgamation/gc/gc_backptr.h +109 -0
- package/internal/amalgamation/gc/gc_config_macros.h +548 -0
- package/internal/amalgamation/gc/gc_disclaim.h +85 -0
- package/internal/amalgamation/gc/gc_gcj.h +123 -0
- package/internal/amalgamation/gc/gc_inline.h +261 -0
- package/internal/amalgamation/gc/gc_mark.h +516 -0
- package/internal/amalgamation/gc/gc_pthread_redirects.h +140 -0
- package/internal/amalgamation/gc/gc_tiny_fl.h +107 -0
- package/internal/amalgamation/gc/gc_typed.h +196 -0
- package/internal/amalgamation/gc/gc_version.h +55 -0
- package/internal/amalgamation/gc/javaxfc.h +76 -0
- package/internal/amalgamation/gc/leak_detector.h +120 -0
- package/internal/amalgamation/gc.c +45932 -0
- package/internal/amalgamation/gc.h +4 -0
- package/internal/amalgamation/gc_boehm_helpers.c +64 -0
- package/internal/amalgamation/gc_debugger_linux.h +61 -0
- package/internal/amalgamation/getrandom_linux.h +33 -0
- package/internal/amalgamation/ipv6_v6only.h +5 -0
- package/internal/amalgamation/libvsql.c +27 -0
- package/internal/amalgamation/libvsql.h +1772 -0
- package/internal/amalgamation/libvsql_linux_amd64.c +106591 -0
- package/internal/amalgamation/libvsql_linux_arm64.c +106571 -0
- package/internal/amalgamation/libvsql_macos_amd64.c +106728 -0
- package/internal/amalgamation/libvsql_macos_arm64.c +106708 -0
- package/internal/amalgamation/libvsql_windows_amd64.c +108235 -0
- package/internal/amalgamation/segfault_handler_nix.c +56 -0
- package/internal/amalgamation/segfault_handler_nix.h +449 -0
- package/internal/amalgamation/segfault_handler_windows.h +52 -0
- package/internal/amalgamation/thread_helper.c +12 -0
- package/internal/amalgamation/thread_helper.h +96 -0
- package/internal/amalgamation/ticks_windows.c +3 -0
- package/internal/amalgamation/ticks_windows.h +31 -0
- package/internal/amalgamation/ticks_windows_api.h +6 -0
- package/internal/amalgamation/vsql_runtime.c +14 -0
- package/internal/amalgamation/vsql_runtime_linux.c +6 -0
- package/internal/amalgamation/vsql_runtime_macos.c +6 -0
- package/internal/amalgamation/vsql_runtime_windows.c +6 -0
- package/lib/binding.js +13 -0
- package/lib/connection.js +492 -0
- package/lib/convert.js +213 -0
- package/lib/errors.js +17 -0
- package/lib/index.js +41 -0
- package/libvsql_flags.gypi +30 -0
- package/package.json +45 -4
- package/prebuilds/darwin-arm64/vsql-node.glibc.node +0 -0
- package/prebuilds/darwin-x64/vsql-node.glibc.node +0 -0
- package/prebuilds/linux-arm64/vsql-node.glibc.node +0 -0
- package/prebuilds/linux-x64/vsql-node.glibc.node +0 -0
- package/prebuilds/win32-x64/vsql-node.glibc.node +0 -0
- package/src/binding.c +773 -0
- package/src/runtime.c +20 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// vsql_runtime.c is the C that V links into vsql and libvsql from outside of
|
|
2
|
+
// the generated C: the Boehm garbage collector (gc.c) and V's own helpers.
|
|
3
|
+
//
|
|
4
|
+
// It's compiled separately from vsql.c and libvsql.c, since V declares parts
|
|
5
|
+
// of the garbage collector API itself, with different types to gc.h.
|
|
6
|
+
#if defined(_WIN32)
|
|
7
|
+
#include "vsql_runtime_windows.c"
|
|
8
|
+
#elif defined(__APPLE__)
|
|
9
|
+
#include "vsql_runtime_macos.c"
|
|
10
|
+
#elif defined(__linux__)
|
|
11
|
+
#include "vsql_runtime_linux.c"
|
|
12
|
+
#else
|
|
13
|
+
#error "There is no generated C for this OS."
|
|
14
|
+
#endif
|
package/lib/binding.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The addon (src/binding.c), from prebuilds or build/Release.
|
|
4
|
+
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
const { VsqlError } = require('./errors');
|
|
8
|
+
|
|
9
|
+
const lib = require('node-gyp-build')(path.join(__dirname, '..'));
|
|
10
|
+
|
|
11
|
+
lib.setErrorFactory((sqlstate, message) => new VsqlError(sqlstate, message));
|
|
12
|
+
|
|
13
|
+
module.exports = lib;
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const lib = require('./binding');
|
|
4
|
+
const { toParams, types } = require('./convert');
|
|
5
|
+
|
|
6
|
+
// Handles that weren't freed (with close) are freed when their connection or
|
|
7
|
+
// statement is garbage collected. Handles are numbers that libvsql reuses, so
|
|
8
|
+
// a statement's handle is only freed while its connection is open.
|
|
9
|
+
const connections = new FinalizationRegistry((state) => {
|
|
10
|
+
try {
|
|
11
|
+
closeConnection(state);
|
|
12
|
+
} catch {
|
|
13
|
+
// There's nothing to report the error to.
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const statements = new FinalizationRegistry(({ state, stmt }) => {
|
|
18
|
+
if (state.open) {
|
|
19
|
+
try {
|
|
20
|
+
lib.freeHandle(lib.SQL_HANDLE_STMT, stmt);
|
|
21
|
+
} catch {
|
|
22
|
+
// There's nothing to report the error to.
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// closeConnection rolls back the transaction (if there is one) and frees the
|
|
28
|
+
// handles of a connection, which frees the handles of its statements.
|
|
29
|
+
function closeConnection(state) {
|
|
30
|
+
if (!state.open) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
state.open = false;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
if (state.inTransaction) {
|
|
37
|
+
state.inTransaction = false;
|
|
38
|
+
lib.endTran(state.dbc, false);
|
|
39
|
+
}
|
|
40
|
+
lib.disconnect(state.dbc);
|
|
41
|
+
} finally {
|
|
42
|
+
try {
|
|
43
|
+
lib.freeHandle(lib.SQL_HANDLE_DBC, state.dbc);
|
|
44
|
+
} finally {
|
|
45
|
+
lib.freeHandle(lib.SQL_HANDLE_ENV, state.env);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Connection is a connection to a database, see connect.
|
|
51
|
+
//
|
|
52
|
+
// Each connection has its own environment, so connections in different worker
|
|
53
|
+
// threads can be used at the same time.
|
|
54
|
+
class Connection {
|
|
55
|
+
constructor(database, options = {}) {
|
|
56
|
+
if (typeof database !== 'string') {
|
|
57
|
+
throw new TypeError('the database must be a string');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.name = database;
|
|
61
|
+
this.bigint = Boolean(options.bigint);
|
|
62
|
+
|
|
63
|
+
// The handles and state that are needed to close the connection (without
|
|
64
|
+
// the Connection, when it's garbage collected).
|
|
65
|
+
this._state = { env: 0, dbc: 0, open: false, inTransaction: false };
|
|
66
|
+
|
|
67
|
+
// The statements with rows that haven't all been fetched, which are closed
|
|
68
|
+
// when the transaction ends.
|
|
69
|
+
this._cursors = new Set();
|
|
70
|
+
|
|
71
|
+
const env = lib.allocHandle(lib.SQL_HANDLE_ENV, lib.SQL_NULL_HANDLE);
|
|
72
|
+
let dbc;
|
|
73
|
+
try {
|
|
74
|
+
dbc = lib.allocHandle(lib.SQL_HANDLE_DBC, env);
|
|
75
|
+
try {
|
|
76
|
+
lib.connect(dbc, database);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
lib.freeHandle(lib.SQL_HANDLE_DBC, dbc);
|
|
79
|
+
throw e;
|
|
80
|
+
}
|
|
81
|
+
} catch (e) {
|
|
82
|
+
lib.freeHandle(lib.SQL_HANDLE_ENV, env);
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
Object.assign(this._state, { env, dbc, open: true });
|
|
86
|
+
connections.register(this, this._state, this._state);
|
|
87
|
+
|
|
88
|
+
// Each statement is committed when it finishes, unless it's in a
|
|
89
|
+
// transaction started by begin.
|
|
90
|
+
try {
|
|
91
|
+
lib.setAutocommit(dbc, true);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
this.close();
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get open() {
|
|
99
|
+
return this._state.open;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get inTransaction() {
|
|
103
|
+
return this._state.inTransaction;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_check() {
|
|
107
|
+
if (!this._state.open) {
|
|
108
|
+
throw new TypeError('the connection is closed');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// prepare prepares a statement, with a "?" for each parameter.
|
|
113
|
+
prepare(sql) {
|
|
114
|
+
this._check();
|
|
115
|
+
|
|
116
|
+
return new Statement(this, sql);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// run, get, all and iterate prepare a statement, use it once and close it.
|
|
120
|
+
|
|
121
|
+
run(sql, params) {
|
|
122
|
+
const stmt = this.prepare(sql);
|
|
123
|
+
try {
|
|
124
|
+
return stmt.run(params);
|
|
125
|
+
} finally {
|
|
126
|
+
stmt.close();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get(sql, params) {
|
|
131
|
+
const stmt = this.prepare(sql);
|
|
132
|
+
try {
|
|
133
|
+
return stmt.get(params);
|
|
134
|
+
} finally {
|
|
135
|
+
stmt.close();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
all(sql, params) {
|
|
140
|
+
const stmt = this.prepare(sql);
|
|
141
|
+
try {
|
|
142
|
+
return stmt.all(params);
|
|
143
|
+
} finally {
|
|
144
|
+
stmt.close();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
iterate(sql, params) {
|
|
149
|
+
const stmt = this.prepare(sql);
|
|
150
|
+
let rows;
|
|
151
|
+
try {
|
|
152
|
+
rows = stmt.iterate(params);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
stmt.close();
|
|
155
|
+
throw e;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return (function* closing() {
|
|
159
|
+
try {
|
|
160
|
+
yield* rows;
|
|
161
|
+
} finally {
|
|
162
|
+
stmt.close();
|
|
163
|
+
}
|
|
164
|
+
})();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// begin starts a transaction, which is ended by commit or rollback. Until
|
|
168
|
+
// then, each statement is committed when it finishes (or when all the rows of
|
|
169
|
+
// a query have been fetched).
|
|
170
|
+
begin() {
|
|
171
|
+
this._check();
|
|
172
|
+
if (this._state.inTransaction) {
|
|
173
|
+
throw new TypeError('there is already a transaction');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// vsql's transactions are READ COMMITTED.
|
|
177
|
+
lib.startTran(this._state.dbc, lib.SQL_TRANSACTION_READ_WRITE,
|
|
178
|
+
lib.SQL_TRANSACTION_READ_COMMITTED);
|
|
179
|
+
this._state.inTransaction = true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
_end(commit) {
|
|
183
|
+
this._check();
|
|
184
|
+
if (!this._state.inTransaction) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// The rows of the transaction's queries are closed by it.
|
|
189
|
+
for (const stmt of this._cursors) {
|
|
190
|
+
stmt._closeCursor();
|
|
191
|
+
}
|
|
192
|
+
this._state.inTransaction = false;
|
|
193
|
+
lib.endTran(this._state.dbc, commit);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// commit commits the transaction, if there is one.
|
|
197
|
+
commit() {
|
|
198
|
+
this._end(true);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// rollback rolls back the transaction, if there is one.
|
|
202
|
+
rollback() {
|
|
203
|
+
this._end(false);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// transaction calls fn in a transaction, which is committed when it returns
|
|
207
|
+
// (and its value is returned), or rolled back if it throws.
|
|
208
|
+
transaction(fn) {
|
|
209
|
+
this.begin();
|
|
210
|
+
let result;
|
|
211
|
+
try {
|
|
212
|
+
result = fn();
|
|
213
|
+
if (typeof result?.then === 'function') {
|
|
214
|
+
throw new TypeError("the function of a transaction can't be async");
|
|
215
|
+
}
|
|
216
|
+
} catch (e) {
|
|
217
|
+
if (this._state.open) {
|
|
218
|
+
this.rollback();
|
|
219
|
+
}
|
|
220
|
+
throw e;
|
|
221
|
+
}
|
|
222
|
+
this.commit();
|
|
223
|
+
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// close closes the connection and its statements. A transaction is rolled
|
|
228
|
+
// back. It does nothing if the connection is already closed.
|
|
229
|
+
close() {
|
|
230
|
+
if (!this._state.open) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The statements (and their rows) are freed by closing the connection.
|
|
235
|
+
for (const stmt of this._cursors) {
|
|
236
|
+
stmt._open = false;
|
|
237
|
+
}
|
|
238
|
+
this._cursors.clear();
|
|
239
|
+
connections.unregister(this._state);
|
|
240
|
+
closeConnection(this._state);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
[Symbol.dispose]() {
|
|
244
|
+
this.close();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Statement is a prepared statement, see Connection.prepare. It can be executed
|
|
249
|
+
// any number of times, with different parameters.
|
|
250
|
+
class Statement {
|
|
251
|
+
constructor(connection, sql) {
|
|
252
|
+
if (typeof sql !== 'string') {
|
|
253
|
+
throw new TypeError('the SQL must be a string');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
this.connection = connection;
|
|
257
|
+
this.sql = sql;
|
|
258
|
+
this._state = connection._state;
|
|
259
|
+
this._raw = false;
|
|
260
|
+
this._open = false;
|
|
261
|
+
this._closed = false;
|
|
262
|
+
|
|
263
|
+
// Each execution has a number, so that an iterator only fetches the rows
|
|
264
|
+
// of its own.
|
|
265
|
+
this._execution = 0;
|
|
266
|
+
this._iterating = false;
|
|
267
|
+
|
|
268
|
+
this._stmt = lib.allocHandle(lib.SQL_HANDLE_STMT, this._state.dbc);
|
|
269
|
+
try {
|
|
270
|
+
lib.prepare(this._stmt, sql);
|
|
271
|
+
this._describe();
|
|
272
|
+
} catch (e) {
|
|
273
|
+
lib.freeHandle(lib.SQL_HANDLE_STMT, this._stmt);
|
|
274
|
+
throw e;
|
|
275
|
+
}
|
|
276
|
+
statements.register(this, { state: this._state, stmt: this._stmt }, this);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// _describe reads the columns of the statement (if it's a query). A
|
|
280
|
+
// statement can have no columns until it's executed (such as a SELECT from a
|
|
281
|
+
// table that doesn't exist yet), so n is given again after each execution.
|
|
282
|
+
_describe(n = lib.numResultCols(this._stmt)) {
|
|
283
|
+
this._columns = [];
|
|
284
|
+
this._names = [];
|
|
285
|
+
this._kinds = '';
|
|
286
|
+
this._converters = [];
|
|
287
|
+
for (let i = 1; i <= n; i++) {
|
|
288
|
+
const [name, dataType, size, digits, nullable] = lib.describeCol(this._stmt, i);
|
|
289
|
+
const [type, kind, converter] = types.get(dataType) ?? ['', 's', null];
|
|
290
|
+
this._columns.push(column(name, type, size, digits, nullable));
|
|
291
|
+
this._names.push(name);
|
|
292
|
+
this._kinds += kind === 'I' && this.connection.bigint ? 'b' : kind;
|
|
293
|
+
if (converter !== null) {
|
|
294
|
+
this._converters.push([i - 1, converter]);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// reader is true if the statement returns rows (such as a SELECT). It's
|
|
300
|
+
// only certain once the statement has been executed.
|
|
301
|
+
get reader() {
|
|
302
|
+
return this._columns.length > 0;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// columns describes the columns of each row.
|
|
306
|
+
columns() {
|
|
307
|
+
return this._columns.map((c) => ({ ...c }));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// raw makes each row an array (in the order of the columns) instead of an
|
|
311
|
+
// object, which is needed when columns have the same name.
|
|
312
|
+
raw(on = true) {
|
|
313
|
+
this._raw = Boolean(on);
|
|
314
|
+
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
_check() {
|
|
319
|
+
if (this._closed) {
|
|
320
|
+
throw new TypeError('the statement is closed');
|
|
321
|
+
}
|
|
322
|
+
this.connection._check();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
_closeCursor() {
|
|
326
|
+
if (this._open) {
|
|
327
|
+
this._open = false;
|
|
328
|
+
this._iterating = false;
|
|
329
|
+
this.connection._cursors.delete(this);
|
|
330
|
+
lib.closeCursor(this._stmt);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_execute(params) {
|
|
335
|
+
this._check();
|
|
336
|
+
if (this._iterating) {
|
|
337
|
+
throw new TypeError('the statement is being iterated');
|
|
338
|
+
}
|
|
339
|
+
const values = toParams(params);
|
|
340
|
+
this._closeCursor();
|
|
341
|
+
this._execution++;
|
|
342
|
+
lib.execute(this._stmt, values);
|
|
343
|
+
const n = lib.numResultCols(this._stmt);
|
|
344
|
+
if (n !== this._columns.length) {
|
|
345
|
+
this._describe(n);
|
|
346
|
+
}
|
|
347
|
+
if (this.reader) {
|
|
348
|
+
this._open = true;
|
|
349
|
+
this.connection._cursors.add(this);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// _query executes a statement that must return rows.
|
|
354
|
+
_query(params) {
|
|
355
|
+
this._execute(params);
|
|
356
|
+
if (!this.reader) {
|
|
357
|
+
throw new TypeError("the statement doesn't return rows, use run()");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
_row(values) {
|
|
362
|
+
for (const [i, converter] of this._converters) {
|
|
363
|
+
if (values[i] !== null) {
|
|
364
|
+
values[i] = converter(values[i]);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (this._raw) {
|
|
368
|
+
return values;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const row = {};
|
|
372
|
+
for (let i = 0; i < values.length; i++) {
|
|
373
|
+
row[this._names[i]] = values[i];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return row;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
_fetch(max) {
|
|
380
|
+
const rows = lib.fetch(this._stmt, this._kinds, max);
|
|
381
|
+
if (max === 0 || rows.length < max) {
|
|
382
|
+
this._closeCursor();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return rows;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// run executes the statement. changes is the number of rows that were
|
|
389
|
+
// inserted, updated or deleted.
|
|
390
|
+
run(params) {
|
|
391
|
+
this._execute(params);
|
|
392
|
+
if (this.reader) {
|
|
393
|
+
this._closeCursor();
|
|
394
|
+
return { changes: 0 };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return { changes: lib.rowCount(this._stmt) };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// get executes a query and returns its first row, or undefined if there are
|
|
401
|
+
// no rows.
|
|
402
|
+
get(params) {
|
|
403
|
+
this._query(params);
|
|
404
|
+
try {
|
|
405
|
+
const rows = this._fetch(1);
|
|
406
|
+
return rows.length === 0 ? undefined : this._row(rows[0]);
|
|
407
|
+
} finally {
|
|
408
|
+
this._closeCursor();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// all executes a query and returns all of its rows.
|
|
413
|
+
all(params) {
|
|
414
|
+
this._query(params);
|
|
415
|
+
try {
|
|
416
|
+
return this._fetch(0).map((values) => this._row(values));
|
|
417
|
+
} finally {
|
|
418
|
+
this._closeCursor();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// iterate executes a query and returns an iterator of its rows, which are
|
|
423
|
+
// fetched one at a time. The statement can't be executed again until the
|
|
424
|
+
// iterator is finished (or exited with break or return). Ending the
|
|
425
|
+
// transaction finishes the iterator.
|
|
426
|
+
iterate(params) {
|
|
427
|
+
this._query(params);
|
|
428
|
+
this._iterating = true;
|
|
429
|
+
|
|
430
|
+
const stmt = this;
|
|
431
|
+
const execution = this._execution;
|
|
432
|
+
const current = () => stmt._open && stmt._execution === execution && stmt._state.open;
|
|
433
|
+
return (function* rows() {
|
|
434
|
+
try {
|
|
435
|
+
while (current()) {
|
|
436
|
+
const [values] = stmt._fetch(1);
|
|
437
|
+
if (values === undefined) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
yield stmt._row(values);
|
|
441
|
+
}
|
|
442
|
+
} finally {
|
|
443
|
+
if (current()) {
|
|
444
|
+
stmt._closeCursor();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
})();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// close frees the statement. It does nothing if it's already closed, or if
|
|
451
|
+
// the connection is closed (which closes its statements).
|
|
452
|
+
close() {
|
|
453
|
+
if (this._closed) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
this._closed = true;
|
|
457
|
+
statements.unregister(this);
|
|
458
|
+
if (!this._state.open) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
try {
|
|
463
|
+
this._closeCursor();
|
|
464
|
+
} finally {
|
|
465
|
+
lib.freeHandle(lib.SQL_HANDLE_STMT, this._stmt);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
[Symbol.dispose]() {
|
|
470
|
+
this.close();
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// column describes a column: its name and type, whether it can be NULL, the
|
|
475
|
+
// maximum length of a CHARACTER or CHARACTER VARYING, and the precision and
|
|
476
|
+
// scale of a DECIMAL or NUMERIC (the others are null).
|
|
477
|
+
function column(name, type, size, digits, nullable) {
|
|
478
|
+
let length = null;
|
|
479
|
+
let precision = null;
|
|
480
|
+
let scale = null;
|
|
481
|
+
if (type === 'CHARACTER' || type === 'CHARACTER VARYING') {
|
|
482
|
+
// The size is in octets, with 4 for each character.
|
|
483
|
+
length = size > 0 ? size / 4 : null;
|
|
484
|
+
} else if (type === 'DECIMAL' || type === 'NUMERIC') {
|
|
485
|
+
precision = size;
|
|
486
|
+
scale = digits;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return { name, type, nullable: nullable !== lib.SQL_NO_NULLS, length, precision, scale };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
module.exports = { Connection, Statement };
|