vsql-node 1.2.0 → 1.2.7
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/README.md +34 -7
- package/index.d.ts +20 -4
- package/lib/connection.js +95 -18
- package/lib/convert.js +30 -7
- package/package.json +1 -1
- 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 +101 -31
package/README.md
CHANGED
|
@@ -55,11 +55,17 @@ The `length` of a column is the maximum length of a `CHARACTER` or
|
|
|
55
55
|
`CHARACTER VARYING` (in characters) or a `BINARY` or `BINARY VARYING` (in
|
|
56
56
|
octets). The `precision` and `scale` are those of a `DECIMAL` or `NUMERIC` (or
|
|
57
57
|
the `precision` of a `DECFLOAT`), or the leading field precision and fractional
|
|
58
|
-
seconds precision of an interval. The
|
|
58
|
+
seconds precision of an interval. The `precision` of a `TIME` or `TIMESTAMP`
|
|
59
|
+
(with or without a time zone) is its fractional seconds precision (6 for a
|
|
60
|
+
`TIMESTAMP` and 0 for a `TIME`, unless it's given). The others are `null`. When vsql knows the key
|
|
59
61
|
of the rows (such as when they're all the columns of a `PRIMARY KEY`), each of
|
|
60
62
|
its columns has a `key` of `"PRIMARY"` (or `"PREFERRED"` for a key that isn't
|
|
61
63
|
the primary key, such as a `UNIQUE NOT NULL` column).
|
|
62
64
|
|
|
65
|
+
The columns are checked each time a statement is executed, so a prepared
|
|
66
|
+
statement follows a table that was replaced or altered after it was prepared
|
|
67
|
+
(the rows and `stmt.columns()` have the new names and types).
|
|
68
|
+
|
|
63
69
|
`stmt.parameters()` describes each `?` with the type that vsql works out from
|
|
64
70
|
where it's used (such as the type of `x` in `x = ?`):
|
|
65
71
|
|
|
@@ -118,11 +124,29 @@ db.transaction(() => {
|
|
|
118
124
|
}); // commits 1 and 3
|
|
119
125
|
```
|
|
120
126
|
|
|
127
|
+
`fn` can't call `db.commit()` or `db.rollback()` (they throw a `TypeError`, like
|
|
128
|
+
in better-sqlite3), since `db.transaction(fn)` ends the transaction (or the
|
|
129
|
+
savepoint) itself.
|
|
130
|
+
|
|
121
131
|
Ending a transaction closes the rows of its queries, which finishes their
|
|
122
132
|
iterators. `START TRANSACTION`, `COMMIT` and `ROLLBACK` can't be executed as
|
|
123
|
-
statements.
|
|
124
|
-
|
|
125
|
-
`
|
|
133
|
+
statements.
|
|
134
|
+
|
|
135
|
+
In a transaction, `db.savepoint(name)` makes a savepoint, `db.rollback(name)`
|
|
136
|
+
rolls back what came after it (and the transaction carries on), and
|
|
137
|
+
`db.release(name)` releases it (and the savepoints after it), keeping what came
|
|
138
|
+
after it. The name is an identifier, like in SQL, so `'a'` is `A` and `'"a"'` is
|
|
139
|
+
`a`. They throw a `TypeError` if there's no transaction:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
db.begin();
|
|
143
|
+
insert.run([1]);
|
|
144
|
+
db.savepoint('a');
|
|
145
|
+
insert.run([2]);
|
|
146
|
+
db.rollback('a');
|
|
147
|
+
db.release('a');
|
|
148
|
+
db.commit(); // commits 1
|
|
149
|
+
```
|
|
126
150
|
|
|
127
151
|
`db.begin(options)` and `db.transaction(fn, options)` take `readOnly` (`false`
|
|
128
152
|
by default) and `isolationLevel` (`"READ COMMITTED"` by default, or
|
|
@@ -146,7 +170,7 @@ type, which is cast to the type it's used as:
|
|
|
146
170
|
|--------------------------|--------------------------------------------------------|
|
|
147
171
|
| `null`, `undefined` | `NULL` |
|
|
148
172
|
| `boolean` | `BOOLEAN` |
|
|
149
|
-
| `number` | `BIGINT` if it's a safe integer, otherwise `DOUBLE PRECISION` |
|
|
173
|
+
| `number` | `BIGINT` if it's a safe integer, otherwise `DOUBLE PRECISION` (`NaN` and `±Infinity` are a `RangeError`) |
|
|
150
174
|
| `bigint` | `BIGINT` (`NUMERIC` if it's out of range) |
|
|
151
175
|
| `string` | `CHARACTER VARYING` |
|
|
152
176
|
| `Buffer`, `Uint8Array` | `BINARY VARYING` (also any other view, or an `ArrayBuffer`) |
|
|
@@ -162,6 +186,9 @@ be a parameter for any type that isn't binary, in vsql's format (such as
|
|
|
162
186
|
`INTERVAL YEAR TO MONTH`). A binary string can only be a `Buffer` (or another
|
|
163
187
|
view), and a `Buffer` can only be a binary string.
|
|
164
188
|
|
|
189
|
+
A string with a lone surrogate (such as `"\uD800"`) is a `RangeError`, because
|
|
190
|
+
it can't be converted to UTF-8 (see `String.prototype.isWellFormed`).
|
|
191
|
+
|
|
165
192
|
`vsql.numeric` (from a string such as `"-12.50"`, a number or a bigint),
|
|
166
193
|
`vsql.date` (`"YYYY-MM-DD"`), `vsql.time` (`"HH:MM:SS"`) and `vsql.timestamp`
|
|
167
194
|
(`"YYYY-MM-DD HH:MM:SS"`) send a value as that type instead, and take the
|
|
@@ -288,5 +315,5 @@ which has all of libvsql's C in one `libvsql.c`.
|
|
|
288
315
|
### Releasing
|
|
289
316
|
|
|
290
317
|
Publishing a GitHub release (with a tag such as `v1.2.3`) publishes the
|
|
291
|
-
package to npm, with the
|
|
292
|
-
[
|
|
318
|
+
package to npm ([release.yml](.github/workflows/release.yml)), with the
|
|
319
|
+
prebuilds from [test.yml](.github/workflows/test.yml).
|
package/index.d.ts
CHANGED
|
@@ -43,8 +43,9 @@ export interface TypeInfo {
|
|
|
43
43
|
*/
|
|
44
44
|
length: number | null;
|
|
45
45
|
/**
|
|
46
|
-
* The precision of a DECIMAL, NUMERIC or DECFLOAT,
|
|
47
|
-
* precision of
|
|
46
|
+
* The precision of a DECIMAL, NUMERIC or DECFLOAT, the fractional seconds
|
|
47
|
+
* precision of a TIME or TIMESTAMP (with or without a time zone), or the
|
|
48
|
+
* leading field precision of an interval.
|
|
48
49
|
*/
|
|
49
50
|
precision: number | null;
|
|
50
51
|
/**
|
|
@@ -151,10 +152,21 @@ export class Connection {
|
|
|
151
152
|
* (an identifier, like in SQL), it only rolls back to that savepoint.
|
|
152
153
|
*/
|
|
153
154
|
rollback(savepoint?: string): void;
|
|
155
|
+
/**
|
|
156
|
+
* Makes a savepoint in the transaction. The name is an identifier, like in
|
|
157
|
+
* SQL. It throws a TypeError if there's no transaction.
|
|
158
|
+
*/
|
|
159
|
+
savepoint(name: string): void;
|
|
160
|
+
/**
|
|
161
|
+
* Releases a savepoint (and the savepoints after it), keeping what came
|
|
162
|
+
* after it. It throws a TypeError if there's no transaction.
|
|
163
|
+
*/
|
|
164
|
+
release(name: string): void;
|
|
154
165
|
/**
|
|
155
166
|
* Calls fn in a transaction, which is committed when it returns (and its
|
|
156
167
|
* value is returned), or rolled back if it throws. In another transaction,
|
|
157
|
-
* it's a savepoint instead (and it can't have options).
|
|
168
|
+
* it's a savepoint instead (and it can't have options). fn can't call commit
|
|
169
|
+
* or rollback (without a savepoint), which throw a TypeError.
|
|
158
170
|
*/
|
|
159
171
|
transaction<T>(fn: () => T, options?: TransactionOptions): T;
|
|
160
172
|
|
|
@@ -174,6 +186,7 @@ export class Statement<R = Row> {
|
|
|
174
186
|
/** Whether the statement returns rows (certain once it's executed). */
|
|
175
187
|
readonly reader: boolean;
|
|
176
188
|
|
|
189
|
+
/** Describes each column, as of the last execution (or when prepared). */
|
|
177
190
|
columns(): Column[];
|
|
178
191
|
/** Describes each parameter ("?"), from where it's used. */
|
|
179
192
|
parameters(): ParameterInfo[];
|
|
@@ -214,7 +227,10 @@ export class VsqlError extends Error {
|
|
|
214
227
|
readonly more: boolean;
|
|
215
228
|
}
|
|
216
229
|
|
|
217
|
-
/**
|
|
230
|
+
/**
|
|
231
|
+
* A parameter that's sent as a particular SQL type, made by numeric, date, time
|
|
232
|
+
* or timestamp (the constructor throws a TypeError).
|
|
233
|
+
*/
|
|
218
234
|
export class SqlValue {
|
|
219
235
|
private constructor();
|
|
220
236
|
|
package/lib/connection.js
CHANGED
|
@@ -96,6 +96,10 @@ class Connection {
|
|
|
96
96
|
// when the transaction ends.
|
|
97
97
|
this._cursors = new Set();
|
|
98
98
|
|
|
99
|
+
// Whether fn of transaction is running, when commit and rollback can't be
|
|
100
|
+
// called (transaction ends the transaction, or the savepoint, itself).
|
|
101
|
+
this._inFn = false;
|
|
102
|
+
|
|
99
103
|
const env = lib.allocHandle(lib.SQL_HANDLE_ENV, lib.SQL_NULL_HANDLE);
|
|
100
104
|
let dbc;
|
|
101
105
|
try {
|
|
@@ -229,30 +233,65 @@ class Connection {
|
|
|
229
233
|
|
|
230
234
|
// commit commits the transaction, if there is one.
|
|
231
235
|
commit() {
|
|
236
|
+
this._checkNotInFn('commit');
|
|
232
237
|
this._end(true);
|
|
233
238
|
}
|
|
234
239
|
|
|
240
|
+
_checkNotInFn(name) {
|
|
241
|
+
if (this._inFn) {
|
|
242
|
+
throw new TypeError(`${name}() can't be called in the function of transaction()`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
235
246
|
// rollback rolls back the transaction, if there is one. With the name of a
|
|
236
|
-
// savepoint (
|
|
237
|
-
//
|
|
238
|
-
// SQL (so "sp" is SP, and '"sp"' is sp).
|
|
247
|
+
// savepoint (see savepoint), it only rolls back what came after it, and the
|
|
248
|
+
// transaction continues.
|
|
239
249
|
rollback(savepoint) {
|
|
240
250
|
if (savepoint === undefined) {
|
|
251
|
+
this._checkNotInFn('rollback');
|
|
241
252
|
this._end(false);
|
|
242
253
|
return;
|
|
243
254
|
}
|
|
244
255
|
|
|
256
|
+
this._checkSavepoint(savepoint);
|
|
257
|
+
lib.endSavepoint(this._state.dbc, savepoint, false);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// savepoint makes a savepoint in the transaction, which can be rolled back
|
|
261
|
+
// to (see rollback) or released. The name is an identifier, like it is in
|
|
262
|
+
// SQL (so "sp" is SP, and '"sp"' is sp). A savepoint with the same name
|
|
263
|
+
// replaces it.
|
|
264
|
+
savepoint(name) {
|
|
265
|
+
this._checkSavepoint(name);
|
|
266
|
+
this.run(`SAVEPOINT ${name}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// release releases a savepoint, and the savepoints after it, keeping what
|
|
270
|
+
// came after them in the transaction.
|
|
271
|
+
release(name) {
|
|
272
|
+
this._checkSavepoint(name);
|
|
273
|
+
lib.endSavepoint(this._state.dbc, name, true);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// _checkSavepoint checks the name of a savepoint, and that there's a
|
|
277
|
+
// transaction (vsql accepts SAVEPOINT without one, but it does nothing
|
|
278
|
+
// useful, since each statement is committed on its own).
|
|
279
|
+
_checkSavepoint(name) {
|
|
245
280
|
this._check();
|
|
246
|
-
if (typeof
|
|
281
|
+
if (typeof name !== 'string') {
|
|
247
282
|
throw new TypeError('the savepoint must be a string');
|
|
248
283
|
}
|
|
249
|
-
|
|
284
|
+
if (!this._state.inTransaction) {
|
|
285
|
+
throw new TypeError('there is no transaction');
|
|
286
|
+
}
|
|
250
287
|
}
|
|
251
288
|
|
|
252
289
|
// transaction calls fn in a transaction, which is committed when it returns
|
|
253
290
|
// (and its value is returned), or rolled back if it throws. In another
|
|
254
291
|
// transaction, it uses a savepoint instead, which is released when fn
|
|
255
292
|
// returns, or rolled back to if it throws, so only what fn did is undone.
|
|
293
|
+
// fn can't call commit or rollback (without a savepoint), since then it
|
|
294
|
+
// wouldn't be clear what was committed (like better-sqlite3).
|
|
256
295
|
transaction(fn, options) {
|
|
257
296
|
if (this._state.inTransaction) {
|
|
258
297
|
if (options !== undefined) {
|
|
@@ -263,28 +302,33 @@ class Connection {
|
|
|
263
302
|
|
|
264
303
|
this.begin(options);
|
|
265
304
|
let result;
|
|
305
|
+
this._inFn = true;
|
|
266
306
|
try {
|
|
267
307
|
result = call(fn);
|
|
268
308
|
} catch (e) {
|
|
269
309
|
if (this._state.open) {
|
|
270
|
-
this.
|
|
310
|
+
this._end(false);
|
|
271
311
|
}
|
|
272
312
|
throw e;
|
|
313
|
+
} finally {
|
|
314
|
+
this._inFn = false;
|
|
273
315
|
}
|
|
274
|
-
this.
|
|
316
|
+
this._end(true);
|
|
275
317
|
|
|
276
318
|
return result;
|
|
277
319
|
}
|
|
278
320
|
|
|
279
321
|
_nested(fn) {
|
|
280
322
|
const name = `VSQL_NODE_${++this._savepoints}`;
|
|
323
|
+
const inFn = this._inFn;
|
|
281
324
|
try {
|
|
282
325
|
this.run(`SAVEPOINT ${name}`);
|
|
326
|
+
this._inFn = true;
|
|
283
327
|
let result;
|
|
284
328
|
try {
|
|
285
329
|
result = call(fn);
|
|
286
330
|
} catch (e) {
|
|
287
|
-
//
|
|
331
|
+
// fn may have closed the connection.
|
|
288
332
|
if (this._state.open && this._state.inTransaction) {
|
|
289
333
|
lib.endSavepoint(this._state.dbc, name, false);
|
|
290
334
|
lib.endSavepoint(this._state.dbc, name, true);
|
|
@@ -295,6 +339,7 @@ class Connection {
|
|
|
295
339
|
|
|
296
340
|
return result;
|
|
297
341
|
} finally {
|
|
342
|
+
this._inFn = inFn;
|
|
298
343
|
this._savepoints--;
|
|
299
344
|
}
|
|
300
345
|
}
|
|
@@ -375,10 +420,9 @@ class Statement {
|
|
|
375
420
|
statements.register(this, { state: this._state, stmt: this._stmt }, this);
|
|
376
421
|
}
|
|
377
422
|
|
|
378
|
-
// _describe reads the columns of the statement (if it's a query).
|
|
379
|
-
// again after each execution, in case the columns aren't the same as when it
|
|
380
|
-
// was prepared.
|
|
423
|
+
// _describe reads the columns of the statement (if it's a query).
|
|
381
424
|
_describe(n = lib.numResultCols(this._stmt)) {
|
|
425
|
+
this._described = [];
|
|
382
426
|
this._columns = [];
|
|
383
427
|
this._names = [];
|
|
384
428
|
this._kinds = '';
|
|
@@ -386,7 +430,9 @@ class Statement {
|
|
|
386
430
|
// The columns of the key of the rows (if vsql knows it) have a KEY_MEMBER.
|
|
387
431
|
const keyType = n > 0 ? this._ird(0, lib.SQL_DESC_KEY_TYPE) : null;
|
|
388
432
|
for (let i = 1; i <= n; i++) {
|
|
389
|
-
const
|
|
433
|
+
const described = lib.describeCol(this._stmt, i);
|
|
434
|
+
this._described.push(described);
|
|
435
|
+
const [name, dataType, size, digits, nullable] = described;
|
|
390
436
|
const [type, kind, converter] = types.get(dataType) ?? ['', 's', null];
|
|
391
437
|
const c = column(name, type, size, digits, nullable,
|
|
392
438
|
this._ird(i, lib.SQL_DESC_DATETIME_INTERVAL_PRECISION));
|
|
@@ -401,6 +447,29 @@ class Statement {
|
|
|
401
447
|
}
|
|
402
448
|
}
|
|
403
449
|
|
|
450
|
+
// _changed is true if the columns aren't the same as when they were last
|
|
451
|
+
// described. A table can be replaced (or altered) after the statement was
|
|
452
|
+
// prepared, giving columns with the same count but other names or types.
|
|
453
|
+
// Comparing what describeCol gives is about a third of the work of
|
|
454
|
+
// _describe (which also reads the descriptor fields of each column).
|
|
455
|
+
_changed() {
|
|
456
|
+
const n = lib.numResultCols(this._stmt);
|
|
457
|
+
if (n !== this._described.length) {
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
for (let i = 0; i < n; i++) {
|
|
461
|
+
const was = this._described[i];
|
|
462
|
+
const now = lib.describeCol(this._stmt, i + 1);
|
|
463
|
+
for (let j = 0; j < now.length; j++) {
|
|
464
|
+
if (now[j] !== was[j]) {
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
|
|
404
473
|
// reader is true if the statement returns rows (such as a SELECT). It's
|
|
405
474
|
// only certain once the statement has been executed.
|
|
406
475
|
get reader() {
|
|
@@ -448,7 +517,7 @@ class Statement {
|
|
|
448
517
|
} else if (type === 'DECIMAL' || type === 'NUMERIC') {
|
|
449
518
|
p.precision = ipd(i, lib.SQL_DESC_PRECISION);
|
|
450
519
|
p.scale = ipd(i, lib.SQL_DESC_SCALE);
|
|
451
|
-
} else if (type === 'DECFLOAT') {
|
|
520
|
+
} else if (type === 'DECFLOAT' || datetimes.has(type)) {
|
|
452
521
|
p.precision = ipd(i, lib.SQL_DESC_PRECISION);
|
|
453
522
|
} else if (type?.startsWith('INTERVAL ')) {
|
|
454
523
|
p.precision = ipd(i, lib.SQL_DESC_DATETIME_INTERVAL_PRECISION);
|
|
@@ -493,9 +562,8 @@ class Statement {
|
|
|
493
562
|
this._closeCursor();
|
|
494
563
|
this._execution++;
|
|
495
564
|
lib.execute(this._stmt, values);
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
this._describe(n);
|
|
565
|
+
if (this._changed()) {
|
|
566
|
+
this._describe();
|
|
499
567
|
}
|
|
500
568
|
if (this.reader) {
|
|
501
569
|
this._open = true;
|
|
@@ -624,14 +692,19 @@ class Statement {
|
|
|
624
692
|
}
|
|
625
693
|
}
|
|
626
694
|
|
|
695
|
+
// The types that have a fractional seconds precision (such as TIMESTAMP(3)),
|
|
696
|
+
// which is their precision.
|
|
697
|
+
const datetimes = new Set(['TIME', 'TIME WITH TIME ZONE', 'TIMESTAMP', 'TIMESTAMP WITH TIME ZONE']);
|
|
698
|
+
|
|
627
699
|
// The key that a column is part of, from KEY_TYPE.
|
|
628
700
|
const keyTypes = { 1: 'PRIMARY', 2: 'PREFERRED' };
|
|
629
701
|
|
|
630
702
|
// column describes a column: its name and type, whether it can be NULL, the
|
|
631
703
|
// maximum length of a CHARACTER, CHARACTER VARYING, BINARY or BINARY VARYING,
|
|
632
704
|
// the precision and scale of a DECIMAL or NUMERIC (or the precision of a
|
|
633
|
-
// DECFLOAT),
|
|
634
|
-
//
|
|
705
|
+
// DECFLOAT), the fractional seconds precision of a TIME or TIMESTAMP (with or
|
|
706
|
+
// without a time zone), and the leading field precision and fractional seconds
|
|
707
|
+
// precision of an interval (the others are null). key is the key of the rows that the
|
|
635
708
|
// column is part of ("PRIMARY" or "PREFERRED"), if vsql knows it.
|
|
636
709
|
function column(name, type, size, digits, nullable, intervalPrecision) {
|
|
637
710
|
let length = null;
|
|
@@ -647,6 +720,10 @@ function column(name, type, size, digits, nullable, intervalPrecision) {
|
|
|
647
720
|
scale = digits;
|
|
648
721
|
} else if (type === 'DECFLOAT') {
|
|
649
722
|
precision = size;
|
|
723
|
+
} else if (datetimes.has(type)) {
|
|
724
|
+
// The fractional seconds precision is the decimal digits (the size is the
|
|
725
|
+
// length of the literal).
|
|
726
|
+
precision = digits;
|
|
650
727
|
} else if (type?.startsWith('INTERVAL ')) {
|
|
651
728
|
precision = intervalPrecision;
|
|
652
729
|
scale = type.endsWith('SECOND') ? digits : null;
|
package/lib/convert.js
CHANGED
|
@@ -8,12 +8,21 @@ const lib = require('./binding');
|
|
|
8
8
|
const BIGINT_MIN = -(2n ** 63n);
|
|
9
9
|
const BIGINT_MAX = 2n ** 63n - 1n;
|
|
10
10
|
|
|
11
|
+
// sqlValueToken is given to the constructor of SqlValue by sqlValue, which is
|
|
12
|
+
// only called by numeric, date, time and timestamp (after they check the
|
|
13
|
+
// value). SqlValue is exported (for instanceof), but can't be constructed
|
|
14
|
+
// without it.
|
|
15
|
+
const sqlValueToken = Symbol('SqlValue');
|
|
16
|
+
|
|
11
17
|
// SqlValue is a parameter that's sent as a particular SQL type, from numeric,
|
|
12
18
|
// date, time or timestamp. A string is cast to the type of the parameter
|
|
13
19
|
// (which vsql works out from where it's used), so these are only needed to send
|
|
14
20
|
// a value as a particular type.
|
|
15
21
|
class SqlValue {
|
|
16
|
-
constructor(type, parameterType, columnSize, decimalDigits, value) {
|
|
22
|
+
constructor(token, type, parameterType, columnSize, decimalDigits, value) {
|
|
23
|
+
if (token !== sqlValueToken) {
|
|
24
|
+
throw new TypeError('SqlValue can only be made by numeric, date, time or timestamp');
|
|
25
|
+
}
|
|
17
26
|
this.type = type;
|
|
18
27
|
this.value = value;
|
|
19
28
|
this.param = [lib.SQL_CHAR, parameterType, columnSize, decimalDigits, value];
|
|
@@ -25,6 +34,10 @@ class SqlValue {
|
|
|
25
34
|
}
|
|
26
35
|
}
|
|
27
36
|
|
|
37
|
+
function sqlValue(type, parameterType, columnSize, decimalDigits, value) {
|
|
38
|
+
return new SqlValue(sqlValueToken, type, parameterType, columnSize, decimalDigits, value);
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
const numericRe = /^([+-]?)(\d*)(?:\.(\d*))?$/;
|
|
29
42
|
|
|
30
43
|
// numeric is a NUMERIC with the precision and scale of its digits, from a
|
|
@@ -44,7 +57,7 @@ function numeric(value) {
|
|
|
44
57
|
const fraction = m[3] || '';
|
|
45
58
|
const precision = (integer.replace(/^0+/, '').length || 1) + fraction.length;
|
|
46
59
|
|
|
47
|
-
return
|
|
60
|
+
return sqlValue('NUMERIC', lib.SQL_NUMERIC, precision, fraction.length,
|
|
48
61
|
sign + integer + (fraction && `.${fraction}`));
|
|
49
62
|
}
|
|
50
63
|
|
|
@@ -75,7 +88,7 @@ function offset(s) {
|
|
|
75
88
|
function date(s) {
|
|
76
89
|
checkString('DATE', s, dateRe);
|
|
77
90
|
|
|
78
|
-
return
|
|
91
|
+
return sqlValue('DATE', lib.SQL_TYPE_DATE, s.length, 0, s);
|
|
79
92
|
}
|
|
80
93
|
|
|
81
94
|
// time is a TIME, from "HH:MM:SS" with up to 6 fractional digits. It's WITH
|
|
@@ -84,11 +97,11 @@ function time(s) {
|
|
|
84
97
|
const [, hms, fraction = '', zone] = checkString('TIME', s, timeRe);
|
|
85
98
|
const value = hms + (fraction && `.${fraction}`);
|
|
86
99
|
if (zone === undefined) {
|
|
87
|
-
return
|
|
100
|
+
return sqlValue('TIME', lib.SQL_TYPE_TIME, value.length, fraction.length, value);
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
const withZone = value + offset(zone);
|
|
91
|
-
return
|
|
104
|
+
return sqlValue('TIME WITH TIME ZONE', lib.SQL_TYPE_TIME_WITH_TIMEZONE, withZone.length,
|
|
92
105
|
fraction.length, withZone);
|
|
93
106
|
}
|
|
94
107
|
|
|
@@ -99,11 +112,11 @@ function timestamp(s) {
|
|
|
99
112
|
const [, ymd, hms, fraction = '', zone] = checkString('TIMESTAMP', s, timestampRe);
|
|
100
113
|
const value = `${ymd} ${hms}${fraction && `.${fraction}`}`;
|
|
101
114
|
if (zone === undefined) {
|
|
102
|
-
return
|
|
115
|
+
return sqlValue('TIMESTAMP', lib.SQL_TYPE_TIMESTAMP, value.length, fraction.length, value);
|
|
103
116
|
}
|
|
104
117
|
|
|
105
118
|
const withZone = value + offset(zone);
|
|
106
|
-
return
|
|
119
|
+
return sqlValue('TIMESTAMP WITH TIME ZONE', lib.SQL_TYPE_TIMESTAMP_WITH_TIMEZONE,
|
|
107
120
|
withZone.length, fraction.length, withZone);
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -145,6 +158,11 @@ function toParam(value) {
|
|
|
145
158
|
if (Number.isSafeInteger(value)) {
|
|
146
159
|
return [lib.SQL_BIGINT, lib.SQL_BIGINT, 0, 0, value];
|
|
147
160
|
}
|
|
161
|
+
// vsql doesn't have NaN or infinities (a DOUBLE PRECISION that overflows
|
|
162
|
+
// is an error), and they couldn't be read back.
|
|
163
|
+
if (!Number.isFinite(value)) {
|
|
164
|
+
throw new RangeError(`invalid number: ${value}`);
|
|
165
|
+
}
|
|
148
166
|
return [lib.SQL_DOUBLE, lib.SQL_DOUBLE, 0, 0, value];
|
|
149
167
|
|
|
150
168
|
case 'bigint':
|
|
@@ -154,6 +172,11 @@ function toParam(value) {
|
|
|
154
172
|
return numeric(value).param;
|
|
155
173
|
|
|
156
174
|
case 'string':
|
|
175
|
+
// A lone surrogate can't be converted to UTF-8 (it would be replaced
|
|
176
|
+
// with U+FFFD), so a different string would be stored.
|
|
177
|
+
if (!value.isWellFormed()) {
|
|
178
|
+
throw new RangeError('a string has a lone surrogate');
|
|
179
|
+
}
|
|
157
180
|
return [lib.SQL_CHAR, lib.SQL_VARCHAR, 0, 0, value];
|
|
158
181
|
|
|
159
182
|
default:
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/binding.c
CHANGED
|
@@ -46,11 +46,50 @@ typedef struct {
|
|
|
46
46
|
napi_ref error_factory;
|
|
47
47
|
} instance;
|
|
48
48
|
|
|
49
|
-
//
|
|
50
|
-
//
|
|
49
|
+
// throw_napi_error throws an error for the Node-API call that just failed, unless
|
|
50
|
+
// it threw an exception itself. Most calls (such as napi_get_value_int32) only
|
|
51
|
+
// return a status, and returning without an exception would be undefined in
|
|
52
|
+
// JavaScript, as if the function worked.
|
|
53
|
+
static void throw_napi_error(napi_env env) {
|
|
54
|
+
// The error info is only valid until the next Node-API call. Its message is
|
|
55
|
+
// a constant, so it's still valid after that.
|
|
56
|
+
const napi_extended_error_info *error = NULL;
|
|
57
|
+
const char *message = "Node-API call failed";
|
|
58
|
+
napi_status status = napi_generic_failure;
|
|
59
|
+
if (napi_get_last_error_info(env, &error) == napi_ok && error != NULL) {
|
|
60
|
+
status = error->error_code;
|
|
61
|
+
if (error->error_message != NULL) {
|
|
62
|
+
message = error->error_message;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
bool pending = false;
|
|
67
|
+
if (napi_is_exception_pending(env, &pending) != napi_ok || pending) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// An argument of the wrong type (such as a number that's a string).
|
|
72
|
+
switch (status) {
|
|
73
|
+
case napi_invalid_arg:
|
|
74
|
+
case napi_object_expected:
|
|
75
|
+
case napi_string_expected:
|
|
76
|
+
case napi_number_expected:
|
|
77
|
+
case napi_boolean_expected:
|
|
78
|
+
case napi_array_expected:
|
|
79
|
+
case napi_bigint_expected:
|
|
80
|
+
napi_throw_type_error(env, NULL, message);
|
|
81
|
+
return;
|
|
82
|
+
default:
|
|
83
|
+
napi_throw_error(env, NULL, message);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// NAPI_CALL throws an error (see throw_napi_error) and returns NULL from the
|
|
88
|
+
// function if a Node-API call fails.
|
|
51
89
|
#define NAPI_CALL(call) \
|
|
52
90
|
do { \
|
|
53
91
|
if ((call) != napi_ok) { \
|
|
92
|
+
throw_napi_error(env); \
|
|
54
93
|
return NULL; \
|
|
55
94
|
} \
|
|
56
95
|
} while (0)
|
|
@@ -70,6 +109,7 @@ static void throw_error_fields(napi_env env, const char *sqlstate, const char *m
|
|
|
70
109
|
|
|
71
110
|
napi_value args[3], global, err;
|
|
72
111
|
if (fields == NULL && napi_get_undefined(env, &fields) != napi_ok) {
|
|
112
|
+
throw_napi_error(env);
|
|
73
113
|
return;
|
|
74
114
|
}
|
|
75
115
|
args[2] = fields;
|
|
@@ -77,6 +117,7 @@ static void throw_error_fields(napi_env env, const char *sqlstate, const char *m
|
|
|
77
117
|
napi_create_string_utf8(env, message, length, &args[1]) != napi_ok ||
|
|
78
118
|
napi_get_global(env, &global) != napi_ok ||
|
|
79
119
|
napi_call_function(env, global, factory, 3, args, &err) != napi_ok) {
|
|
120
|
+
throw_napi_error(env);
|
|
80
121
|
return;
|
|
81
122
|
}
|
|
82
123
|
napi_throw(env, err);
|
|
@@ -199,6 +240,7 @@ static int check(napi_env env, SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER
|
|
|
199
240
|
static int args(napi_env env, napi_callback_info info, size_t n, napi_value *argv) {
|
|
200
241
|
size_t argc = n;
|
|
201
242
|
if (napi_get_cb_info(env, info, &argc, argv, NULL, NULL) != napi_ok) {
|
|
243
|
+
throw_napi_error(env);
|
|
202
244
|
return -1;
|
|
203
245
|
}
|
|
204
246
|
if (argc < n) {
|
|
@@ -220,6 +262,7 @@ static int args(napi_env env, napi_callback_info info, size_t n, napi_value *arg
|
|
|
220
262
|
// or NULL if it isn't a string (which has thrown an exception).
|
|
221
263
|
static char *get_string(napi_env env, napi_value value, size_t *length) {
|
|
222
264
|
if (napi_get_value_string_utf8(env, value, NULL, 0, length) != napi_ok) {
|
|
265
|
+
throw_napi_error(env);
|
|
223
266
|
return NULL;
|
|
224
267
|
}
|
|
225
268
|
|
|
@@ -229,6 +272,7 @@ static char *get_string(napi_env env, napi_value value, size_t *length) {
|
|
|
229
272
|
return NULL;
|
|
230
273
|
}
|
|
231
274
|
if (napi_get_value_string_utf8(env, value, s, *length + 1, length) != napi_ok) {
|
|
275
|
+
throw_napi_error(env);
|
|
232
276
|
free(s);
|
|
233
277
|
return NULL;
|
|
234
278
|
}
|
|
@@ -526,6 +570,7 @@ static int bind_param(napi_env env, SQLINTEGER stmt, SQLSMALLINT n, napi_value a
|
|
|
526
570
|
napi_value fields[5];
|
|
527
571
|
for (uint32_t i = 0; i < 5; i++) {
|
|
528
572
|
if (napi_get_element(env, array, i, &fields[i]) != napi_ok) {
|
|
573
|
+
throw_napi_error(env);
|
|
529
574
|
return -1;
|
|
530
575
|
}
|
|
531
576
|
}
|
|
@@ -535,11 +580,13 @@ static int bind_param(napi_env env, SQLINTEGER stmt, SQLSMALLINT n, napi_value a
|
|
|
535
580
|
napi_get_value_int32(env, fields[1], ¶meter_type) != napi_ok ||
|
|
536
581
|
napi_get_value_int32(env, fields[2], &column_size) != napi_ok ||
|
|
537
582
|
napi_get_value_int32(env, fields[3], &decimal_digits) != napi_ok) {
|
|
583
|
+
throw_napi_error(env);
|
|
538
584
|
return -1;
|
|
539
585
|
}
|
|
540
586
|
|
|
541
587
|
napi_valuetype type;
|
|
542
588
|
if (napi_typeof(env, fields[4], &type) != napi_ok) {
|
|
589
|
+
throw_napi_error(env);
|
|
543
590
|
return -1;
|
|
544
591
|
}
|
|
545
592
|
|
|
@@ -551,15 +598,18 @@ static int bind_param(napi_env env, SQLINTEGER stmt, SQLSMALLINT n, napi_value a
|
|
|
551
598
|
if (type == napi_bigint) {
|
|
552
599
|
bool lossless;
|
|
553
600
|
if (napi_get_value_bigint_int64(env, fields[4], &p->i, &lossless) != napi_ok) {
|
|
601
|
+
throw_napi_error(env);
|
|
554
602
|
return -1;
|
|
555
603
|
}
|
|
556
604
|
} else if (napi_get_value_int64(env, fields[4], &p->i) != napi_ok) {
|
|
605
|
+
throw_napi_error(env);
|
|
557
606
|
return -1;
|
|
558
607
|
}
|
|
559
608
|
pointer = &p->i;
|
|
560
609
|
buffer_length = sizeof(SQLBIGINT);
|
|
561
610
|
} else if (value_type == SQL_DOUBLE) {
|
|
562
611
|
if (napi_get_value_double(env, fields[4], &p->d) != napi_ok) {
|
|
612
|
+
throw_napi_error(env);
|
|
563
613
|
return -1;
|
|
564
614
|
}
|
|
565
615
|
pointer = &p->d;
|
|
@@ -572,6 +622,7 @@ static int bind_param(napi_env env, SQLINTEGER stmt, SQLSMALLINT n, napi_value a
|
|
|
572
622
|
void *data;
|
|
573
623
|
if (napi_get_typedarray_info(env, fields[4], &array_type, &length, &data, NULL, NULL) !=
|
|
574
624
|
napi_ok) {
|
|
625
|
+
throw_napi_error(env);
|
|
575
626
|
return -1;
|
|
576
627
|
}
|
|
577
628
|
if (column_size == 0) {
|
|
@@ -625,8 +676,11 @@ static napi_value execute(napi_env env, napi_callback_info info) {
|
|
|
625
676
|
napi_value result = NULL;
|
|
626
677
|
for (uint32_t i = 0; i < n; i++) {
|
|
627
678
|
napi_value array;
|
|
628
|
-
if (napi_get_element(env, argv[1], i, &array) != napi_ok
|
|
629
|
-
|
|
679
|
+
if (napi_get_element(env, argv[1], i, &array) != napi_ok) {
|
|
680
|
+
throw_napi_error(env);
|
|
681
|
+
goto done;
|
|
682
|
+
}
|
|
683
|
+
if (bind_param(env, stmt, (SQLSMALLINT)(i + 1), array, ¶ms[i]) < 0) {
|
|
630
684
|
goto done;
|
|
631
685
|
}
|
|
632
686
|
}
|
|
@@ -749,10 +803,11 @@ static napi_value get_chars(napi_env env, SQLINTEGER stmt, SQLSMALLINT n) {
|
|
|
749
803
|
return result;
|
|
750
804
|
}
|
|
751
805
|
|
|
752
|
-
// The rest of the string (ind) didn't fit, so the buffer
|
|
753
|
-
//
|
|
806
|
+
// The rest of the string (ind) didn't fit, so the buffer is full: a piece
|
|
807
|
+
// of it and the null terminator. The size of the piece comes from the size
|
|
808
|
+
// of the buffer, not strlen, because the string can contain U+0000.
|
|
754
809
|
if (rc == SQL_SUCCESS_WITH_INFO && ind >= size - (SQLINTEGER)length) {
|
|
755
|
-
size_t piece =
|
|
810
|
+
size_t piece = (size_t)size - length - 1;
|
|
756
811
|
size_t new_size = length + piece + (size_t)ind + 1;
|
|
757
812
|
char *grown = malloc(new_size);
|
|
758
813
|
if (grown == NULL) {
|
|
@@ -884,6 +939,33 @@ static napi_value get_value(napi_env env, SQLINTEGER stmt, SQLSMALLINT column, c
|
|
|
884
939
|
return result;
|
|
885
940
|
}
|
|
886
941
|
|
|
942
|
+
// fetch_rows fetches the rows for fetch, with the kind of each column.
|
|
943
|
+
static napi_value fetch_rows(napi_env env, SQLINTEGER stmt, const char *kinds, size_t n,
|
|
944
|
+
int32_t max) {
|
|
945
|
+
napi_value rows;
|
|
946
|
+
NAPI_CALL(napi_create_array(env, &rows));
|
|
947
|
+
for (uint32_t r = 0; max == 0 || r < (uint32_t)max; r++) {
|
|
948
|
+
SQLRETURN rc = SQLFetch(stmt);
|
|
949
|
+
if (rc == SQL_NO_DATA) {
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
CHECK(SQL_HANDLE_STMT, stmt, rc);
|
|
953
|
+
|
|
954
|
+
napi_value row;
|
|
955
|
+
NAPI_CALL(napi_create_array_with_length(env, n, &row));
|
|
956
|
+
for (size_t i = 0; i < n; i++) {
|
|
957
|
+
napi_value value = get_value(env, stmt, (SQLSMALLINT)(i + 1), kinds[i]);
|
|
958
|
+
if (value == NULL) {
|
|
959
|
+
return NULL;
|
|
960
|
+
}
|
|
961
|
+
NAPI_CALL(napi_set_element(env, row, (uint32_t)i, value));
|
|
962
|
+
}
|
|
963
|
+
NAPI_CALL(napi_set_element(env, rows, r, row));
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
return rows;
|
|
967
|
+
}
|
|
968
|
+
|
|
887
969
|
// fetch(stmt, kinds, max) -> rows fetches the next rows (at most max, or all
|
|
888
970
|
// of them if max is 0), each as an array. Fewer than max rows means that there
|
|
889
971
|
// are no more. kinds has a character for how each column is read:
|
|
@@ -903,35 +985,23 @@ static napi_value fetch(napi_env env, napi_callback_info info) {
|
|
|
903
985
|
int32_t stmt, max;
|
|
904
986
|
NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
|
|
905
987
|
NAPI_CALL(napi_get_value_int32(env, argv[2], &max));
|
|
906
|
-
|
|
988
|
+
|
|
989
|
+
// A query can have any number of columns, so kinds is allocated.
|
|
907
990
|
size_t n;
|
|
908
|
-
NAPI_CALL(napi_get_value_string_latin1(env, argv[1],
|
|
909
|
-
|
|
910
|
-
|
|
991
|
+
NAPI_CALL(napi_get_value_string_latin1(env, argv[1], NULL, 0, &n));
|
|
992
|
+
char *kinds = malloc(n + 1);
|
|
993
|
+
if (kinds == NULL) {
|
|
994
|
+
napi_throw_error(env, NULL, "out of memory");
|
|
911
995
|
return NULL;
|
|
912
996
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
SQLRETURN rc = SQLFetch(stmt);
|
|
918
|
-
if (rc == SQL_NO_DATA) {
|
|
919
|
-
break;
|
|
920
|
-
}
|
|
921
|
-
CHECK(SQL_HANDLE_STMT, stmt, rc);
|
|
922
|
-
|
|
923
|
-
napi_value row;
|
|
924
|
-
NAPI_CALL(napi_create_array_with_length(env, n, &row));
|
|
925
|
-
for (size_t i = 0; i < n; i++) {
|
|
926
|
-
napi_value value = get_value(env, stmt, (SQLSMALLINT)(i + 1), kinds[i]);
|
|
927
|
-
if (value == NULL) {
|
|
928
|
-
return NULL;
|
|
929
|
-
}
|
|
930
|
-
NAPI_CALL(napi_set_element(env, row, (uint32_t)i, value));
|
|
931
|
-
}
|
|
932
|
-
NAPI_CALL(napi_set_element(env, rows, r, row));
|
|
997
|
+
if (napi_get_value_string_latin1(env, argv[1], kinds, n + 1, &n) != napi_ok) {
|
|
998
|
+
throw_napi_error(env);
|
|
999
|
+
free(kinds);
|
|
1000
|
+
return NULL;
|
|
933
1001
|
}
|
|
934
1002
|
|
|
1003
|
+
napi_value rows = fetch_rows(env, stmt, kinds, n, max);
|
|
1004
|
+
free(kinds);
|
|
935
1005
|
return rows;
|
|
936
1006
|
}
|
|
937
1007
|
|