vsql-node 1.2.8 → 1.2.9
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 +7 -0
- package/index.d.ts +7 -1
- package/lib/connection.js +69 -10
- 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 +17 -0
package/README.md
CHANGED
|
@@ -148,6 +148,13 @@ db.release('a');
|
|
|
148
148
|
db.commit(); // commits 1
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
+
vsql rolls back the whole transaction for some errors, such as a `40001`
|
|
152
|
+
serialization failure (other errors only undo the statement). After that,
|
|
153
|
+
`db.inTransaction` is `false` and each statement is committed on its own again.
|
|
154
|
+
If `fn` of `db.transaction(fn)` catches the error, the statements after it throw
|
|
155
|
+
a `TypeError` (rather than being committed one at a time), and so does
|
|
156
|
+
`db.transaction(fn)` when `fn` returns.
|
|
157
|
+
|
|
151
158
|
`db.begin(options)` and `db.transaction(fn, options)` take `readOnly` (`false`
|
|
152
159
|
by default) and `isolationLevel` (`"READ COMMITTED"` by default, or
|
|
153
160
|
`"READ UNCOMMITTED"`, `"REPEATABLE READ"` or `"SERIALIZABLE"`):
|
package/index.d.ts
CHANGED
|
@@ -133,6 +133,10 @@ export class Connection {
|
|
|
133
133
|
readonly name: string;
|
|
134
134
|
readonly bigint: boolean;
|
|
135
135
|
readonly open: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Whether there's a transaction. It's false after vsql rolls back the
|
|
138
|
+
* transaction (such as for a 40001 serialization failure).
|
|
139
|
+
*/
|
|
136
140
|
readonly inTransaction: boolean;
|
|
137
141
|
|
|
138
142
|
/** Prepares a statement, with a "?" for each parameter. */
|
|
@@ -166,7 +170,9 @@ export class Connection {
|
|
|
166
170
|
* Calls fn in a transaction, which is committed when it returns (and its
|
|
167
171
|
* value is returned), or rolled back if it throws. In another transaction,
|
|
168
172
|
* 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.
|
|
173
|
+
* or rollback (without a savepoint), which throw a TypeError. If vsql rolls
|
|
174
|
+
* back the transaction and fn catches the error, the statements after it,
|
|
175
|
+
* and transaction itself, throw a TypeError.
|
|
170
176
|
*/
|
|
171
177
|
transaction<T>(fn: () => T, options?: TransactionOptions): T;
|
|
172
178
|
|
package/lib/connection.js
CHANGED
|
@@ -96,8 +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
|
|
100
|
-
// called (transaction ends the transaction, or the
|
|
99
|
+
// Whether fn of transaction is running (in any transaction), when commit
|
|
100
|
+
// and rollback can't be called (transaction ends the transaction, or the
|
|
101
|
+
// savepoint, itself), and a statement after vsql rolled back the
|
|
102
|
+
// transaction fails instead of being committed on its own.
|
|
101
103
|
this._inFn = false;
|
|
102
104
|
|
|
103
105
|
const env = lib.allocHandle(lib.SQL_HANDLE_ENV, lib.SQL_NULL_HANDLE);
|
|
@@ -227,8 +229,38 @@ class Connection {
|
|
|
227
229
|
for (const stmt of this._cursors) {
|
|
228
230
|
stmt._closeCursor();
|
|
229
231
|
}
|
|
232
|
+
try {
|
|
233
|
+
lib.endTran(this._state.dbc, commit);
|
|
234
|
+
} finally {
|
|
235
|
+
// A commit that fails may leave the transaction active, or roll it back.
|
|
236
|
+
this._state.inTransaction = lib.transactionActive(this._state.dbc);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// _sync is called after an error. vsql rolls back the whole transaction for
|
|
241
|
+
// some errors (such as a 40001 serialization failure). Each statement after
|
|
242
|
+
// that is committed on its own.
|
|
243
|
+
_sync() {
|
|
244
|
+
if (!this._state.open || !this._state.inTransaction ||
|
|
245
|
+
lib.transactionActive(this._state.dbc)) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// The rows of the transaction's queries are closed, like they are by
|
|
250
|
+
// commit and rollback. An open cursor would stop the next statement from
|
|
251
|
+
// being committed.
|
|
230
252
|
this._state.inTransaction = false;
|
|
231
|
-
|
|
253
|
+
for (const stmt of this._cursors) {
|
|
254
|
+
stmt._closeCursor();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// _checkRolledBack throws if vsql rolled back the transaction while fn of
|
|
259
|
+
// transaction was running (and fn caught the error).
|
|
260
|
+
_checkRolledBack() {
|
|
261
|
+
if (this._inFn && !this._state.inTransaction) {
|
|
262
|
+
throw new TypeError('the transaction was rolled back by vsql');
|
|
263
|
+
}
|
|
232
264
|
}
|
|
233
265
|
|
|
234
266
|
// commit commits the transaction, if there is one.
|
|
@@ -254,7 +286,7 @@ class Connection {
|
|
|
254
286
|
}
|
|
255
287
|
|
|
256
288
|
this._checkSavepoint(savepoint);
|
|
257
|
-
|
|
289
|
+
this._endSavepoint(savepoint, false);
|
|
258
290
|
}
|
|
259
291
|
|
|
260
292
|
// savepoint makes a savepoint in the transaction, which can be rolled back
|
|
@@ -270,7 +302,7 @@ class Connection {
|
|
|
270
302
|
// came after them in the transaction.
|
|
271
303
|
release(name) {
|
|
272
304
|
this._checkSavepoint(name);
|
|
273
|
-
|
|
305
|
+
this._endSavepoint(name, true);
|
|
274
306
|
}
|
|
275
307
|
|
|
276
308
|
// _checkSavepoint checks the name of a savepoint, and that there's a
|
|
@@ -286,12 +318,25 @@ class Connection {
|
|
|
286
318
|
}
|
|
287
319
|
}
|
|
288
320
|
|
|
321
|
+
_endSavepoint(name, release) {
|
|
322
|
+
try {
|
|
323
|
+
lib.endSavepoint(this._state.dbc, name, release);
|
|
324
|
+
} catch (e) {
|
|
325
|
+
this._sync();
|
|
326
|
+
throw e;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
289
330
|
// transaction calls fn in a transaction, which is committed when it returns
|
|
290
331
|
// (and its value is returned), or rolled back if it throws. In another
|
|
291
332
|
// transaction, it uses a savepoint instead, which is released when fn
|
|
292
333
|
// returns, or rolled back to if it throws, so only what fn did is undone.
|
|
293
334
|
// fn can't call commit or rollback (without a savepoint), since then it
|
|
294
335
|
// wouldn't be clear what was committed (like better-sqlite3).
|
|
336
|
+
//
|
|
337
|
+
// If vsql rolls back the transaction (such as for a 40001) and fn catches
|
|
338
|
+
// the error, the statements after it would be committed one at a time, so
|
|
339
|
+
// they throw instead, and so does transaction when fn returns.
|
|
295
340
|
transaction(fn, options) {
|
|
296
341
|
if (this._state.inTransaction) {
|
|
297
342
|
if (options !== undefined) {
|
|
@@ -305,6 +350,7 @@ class Connection {
|
|
|
305
350
|
this._inFn = true;
|
|
306
351
|
try {
|
|
307
352
|
result = call(fn);
|
|
353
|
+
this._checkRolledBack();
|
|
308
354
|
} catch (e) {
|
|
309
355
|
if (this._state.open) {
|
|
310
356
|
this._end(false);
|
|
@@ -330,12 +376,13 @@ class Connection {
|
|
|
330
376
|
} catch (e) {
|
|
331
377
|
// fn may have closed the connection.
|
|
332
378
|
if (this._state.open && this._state.inTransaction) {
|
|
333
|
-
|
|
334
|
-
|
|
379
|
+
this._endSavepoint(name, false);
|
|
380
|
+
this._endSavepoint(name, true);
|
|
335
381
|
}
|
|
336
382
|
throw e;
|
|
337
383
|
}
|
|
338
|
-
|
|
384
|
+
this._checkRolledBack();
|
|
385
|
+
this._endSavepoint(name, true);
|
|
339
386
|
|
|
340
387
|
return result;
|
|
341
388
|
} finally {
|
|
@@ -558,10 +605,16 @@ class Statement {
|
|
|
558
605
|
if (this._iterating) {
|
|
559
606
|
throw new TypeError('the statement is being iterated');
|
|
560
607
|
}
|
|
608
|
+
this.connection._checkRolledBack();
|
|
561
609
|
const values = toParams(params);
|
|
562
610
|
this._closeCursor();
|
|
563
611
|
this._execution++;
|
|
564
|
-
|
|
612
|
+
try {
|
|
613
|
+
lib.execute(this._stmt, values);
|
|
614
|
+
} catch (e) {
|
|
615
|
+
this.connection._sync();
|
|
616
|
+
throw e;
|
|
617
|
+
}
|
|
565
618
|
if (this._changed()) {
|
|
566
619
|
this._describe();
|
|
567
620
|
}
|
|
@@ -598,7 +651,13 @@ class Statement {
|
|
|
598
651
|
}
|
|
599
652
|
|
|
600
653
|
_fetch(max) {
|
|
601
|
-
|
|
654
|
+
let rows;
|
|
655
|
+
try {
|
|
656
|
+
rows = lib.fetch(this._stmt, this._kinds, max);
|
|
657
|
+
} catch (e) {
|
|
658
|
+
this.connection._sync();
|
|
659
|
+
throw e;
|
|
660
|
+
}
|
|
602
661
|
if (max === 0 || rows.length < max) {
|
|
603
662
|
this._closeCursor();
|
|
604
663
|
}
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/binding.c
CHANGED
|
@@ -534,6 +534,22 @@ static napi_value end_savepoint(napi_env env, napi_callback_info info) {
|
|
|
534
534
|
return undefined(env);
|
|
535
535
|
}
|
|
536
536
|
|
|
537
|
+
// transactionActive(dbc) -> bool is whether the connection has a transaction
|
|
538
|
+
// (SQL_DIAG_TRANSACTION_ACTIVE). vsql rolls back the whole transaction for
|
|
539
|
+
// some errors, so this is the only way to know after one.
|
|
540
|
+
static napi_value transaction_active(napi_env env, napi_callback_info info) {
|
|
541
|
+
ARGS(1);
|
|
542
|
+
int32_t dbc;
|
|
543
|
+
NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
|
|
544
|
+
|
|
545
|
+
SQLINTEGER active = 0;
|
|
546
|
+
CHECK(SQL_HANDLE_DBC, dbc,
|
|
547
|
+
SQLGetDiagField(SQL_HANDLE_DBC, dbc, 0, SQL_DIAG_TRANSACTION_ACTIVE, &active, 0, NULL));
|
|
548
|
+
napi_value result;
|
|
549
|
+
NAPI_CALL(napi_get_boolean(env, active != 0, &result));
|
|
550
|
+
return result;
|
|
551
|
+
}
|
|
552
|
+
|
|
537
553
|
// getInfo(dbc, infoType, kind) -> value reads SQLGetInfo, as a string (kind
|
|
538
554
|
// "s") or an SQLSMALLINT ("h") or SQLINTEGER ("i").
|
|
539
555
|
static napi_value get_info(napi_env env, napi_callback_info info) {
|
|
@@ -1229,6 +1245,7 @@ NAPI_MODULE_INIT() {
|
|
|
1229
1245
|
{"startTran", NULL, start_tran, NULL, NULL, NULL, napi_default, NULL},
|
|
1230
1246
|
{"endTran", NULL, end_tran, NULL, NULL, NULL, napi_default, NULL},
|
|
1231
1247
|
{"endSavepoint", NULL, end_savepoint, NULL, NULL, NULL, napi_default, NULL},
|
|
1248
|
+
{"transactionActive", NULL, transaction_active, NULL, NULL, NULL, napi_default, NULL},
|
|
1232
1249
|
{"getInfo", NULL, get_info, NULL, NULL, NULL, napi_default, NULL},
|
|
1233
1250
|
{"descField", NULL, desc_field, NULL, NULL, NULL, napi_default, NULL},
|
|
1234
1251
|
{"execDirect", NULL, exec_direct, NULL, NULL, NULL, napi_default, NULL},
|