mssql 12.7.4 → 12.7.5
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 +6 -0
- package/lib/msnodesqlv8/request.js +109 -18
- package/lib/msnodesqlv8/transaction.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1246,6 +1246,10 @@ request.query('waitfor delay \'00:00:05\'; select 1 as number', (err, result) =>
|
|
|
1246
1246
|
request.cancel()
|
|
1247
1247
|
```
|
|
1248
1248
|
|
|
1249
|
+
A cancelled stream emits an `error` event with code `ECANCEL`, followed by `done`.
|
|
1250
|
+
|
|
1251
|
+
With the `msnodesqlv8` driver, a request that is already running can only be cancelled from msnodesqlv8 5.6. Earlier versions can cancel a paused stream, but let any other running request run to the end and complete as if `cancel()` had not been called.
|
|
1252
|
+
|
|
1249
1253
|
## Transaction
|
|
1250
1254
|
|
|
1251
1255
|
**IMPORTANT:** always use `Transaction` class to create transactions - it ensures that all your requests are executed on one connection. Once you call `begin`, a single connection is acquired from the connection pool and all subsequent requests (initialized with the `Transaction` object) are executed exclusively on this connection. After you call `commit` or `rollback`, connection is then released back to the connection pool.
|
|
@@ -1316,6 +1320,8 @@ transaction.begin(err => {
|
|
|
1316
1320
|
})
|
|
1317
1321
|
```
|
|
1318
1322
|
|
|
1323
|
+
The msnodesqlv8 driver has no `abortTransactionOnError` option and can't tell when the server has rolled a transaction back. If you turn `XACT_ABORT` on yourself, or the server rolls the transaction back for another reason (for example, when it picks it as a deadlock victim), the `rollback` event isn't emitted, so the `rollback()` call above fails with a `TransactionError` saying there is no transaction to roll back: the server has already rolled it back.
|
|
1324
|
+
|
|
1319
1325
|
### Events
|
|
1320
1326
|
|
|
1321
1327
|
- **begin** - Dispatched when transaction begin.
|
|
@@ -245,7 +245,12 @@ class Request extends BaseRequest {
|
|
|
245
245
|
|
|
246
246
|
const go = () => {
|
|
247
247
|
const tm = connection.tableMgr()
|
|
248
|
-
|
|
248
|
+
// the table manager reports a failed lookup as (null, err), not error first
|
|
249
|
+
return tm.bind(table.path.replace(/\[|\]/g, ''), (mgr, err) => {
|
|
250
|
+
if (err) {
|
|
251
|
+
return done(err)
|
|
252
|
+
}
|
|
253
|
+
|
|
249
254
|
if (mgr.columns.length === 0) {
|
|
250
255
|
return done(new RequestError('Table was not found on the server.', 'ENAME'))
|
|
251
256
|
}
|
|
@@ -334,13 +339,22 @@ class Request extends BaseRequest {
|
|
|
334
339
|
let handleOutput = false
|
|
335
340
|
let isChunkedRecordset = false
|
|
336
341
|
let chunksBuffer = null
|
|
342
|
+
let terminated = null
|
|
343
|
+
// set while the error just collected is being dispatched, along with whatever came with it
|
|
344
|
+
let failing = false
|
|
345
|
+
let cancelRequested = false
|
|
346
|
+
// 'done' ends the request. msnodesqlv8 >= 5.6 emits it as soon as a cancel is through, and
|
|
347
|
+
// the query can go on emitting after it, so the listeners ignore anything that follows
|
|
348
|
+
let finished = false
|
|
337
349
|
|
|
338
350
|
const handleError = (req, connection, info, moreErrors) => {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
351
|
+
// a cancel is reported from 'done'. msnodesqlv8 >= 5.6 also sends an 'Operation canceled'
|
|
352
|
+
// error after a cancel, and one that arrives before 'done' is dropped here
|
|
353
|
+
if (cancelRequested && info.sqlstate === 'HY008') {
|
|
354
|
+
return
|
|
342
355
|
}
|
|
343
356
|
|
|
357
|
+
const doReturn = !moreErrors
|
|
344
358
|
const err = new RequestError(info, 'EREQUEST')
|
|
345
359
|
err.code = 'EREQUEST'
|
|
346
360
|
|
|
@@ -360,13 +374,8 @@ class Request extends BaseRequest {
|
|
|
360
374
|
debug('request(%d): failed', IDS.get(this), err)
|
|
361
375
|
}
|
|
362
376
|
|
|
363
|
-
let previous
|
|
364
377
|
if (errors.length) {
|
|
365
|
-
|
|
366
|
-
if (!err.precedingErrors) {
|
|
367
|
-
err.precedingErrors = []
|
|
368
|
-
}
|
|
369
|
-
err.precedingErrors.push(previous)
|
|
378
|
+
err.precedingErrors = errors.splice(0)
|
|
370
379
|
}
|
|
371
380
|
|
|
372
381
|
hasReturned = true
|
|
@@ -376,6 +385,10 @@ class Request extends BaseRequest {
|
|
|
376
385
|
|
|
377
386
|
// we must collect errors even in stream mode
|
|
378
387
|
errors.push(err)
|
|
388
|
+
if (!failing) {
|
|
389
|
+
failing = true
|
|
390
|
+
queueMicrotask(() => { failing = false })
|
|
391
|
+
}
|
|
379
392
|
}
|
|
380
393
|
|
|
381
394
|
// nested = function is called by this.execute
|
|
@@ -482,8 +495,19 @@ class Request extends BaseRequest {
|
|
|
482
495
|
|
|
483
496
|
this._cancel = () => {
|
|
484
497
|
debug('request(%d): cancel', IDS.get(this))
|
|
498
|
+
// newer msnodesqlv8 versions drop a repeat cancel without calling back, so the
|
|
499
|
+
// outcome of the first one stands
|
|
500
|
+
if (typeof req.getCancelSent === 'function' && req.getCancelSent()) {
|
|
501
|
+
return
|
|
502
|
+
}
|
|
503
|
+
cancelRequested = true
|
|
485
504
|
req.cancelQuery(err => {
|
|
486
|
-
if (err)
|
|
505
|
+
if (err) {
|
|
506
|
+
debug('request(%d): failed to cancel', IDS.get(this), err)
|
|
507
|
+
// msnodesqlv8 < 5.6 refuses to cancel a query that is neither paused nor polling,
|
|
508
|
+
// and the query runs to the end
|
|
509
|
+
if (!hasReturned) cancelRequested = false
|
|
510
|
+
}
|
|
487
511
|
// this fixes an issue where paused connections don't emit a done event
|
|
488
512
|
try {
|
|
489
513
|
if (req.isPaused()) req.emit('done')
|
|
@@ -494,6 +518,8 @@ class Request extends BaseRequest {
|
|
|
494
518
|
}
|
|
495
519
|
|
|
496
520
|
req.on('meta', metadata => {
|
|
521
|
+
if (finished) return
|
|
522
|
+
|
|
497
523
|
if (row) {
|
|
498
524
|
if (isChunkedRecordset) {
|
|
499
525
|
const concatenatedChunks = chunksBuffer.join('')
|
|
@@ -571,7 +597,7 @@ class Request extends BaseRequest {
|
|
|
571
597
|
})
|
|
572
598
|
|
|
573
599
|
req.on('row', rownumber => {
|
|
574
|
-
if (row && isChunkedRecordset) return
|
|
600
|
+
if (finished || (row && isChunkedRecordset)) return
|
|
575
601
|
|
|
576
602
|
if (this.arrayRowMode) {
|
|
577
603
|
row = []
|
|
@@ -583,6 +609,8 @@ class Request extends BaseRequest {
|
|
|
583
609
|
})
|
|
584
610
|
|
|
585
611
|
req.on('column', (idx, data, more) => {
|
|
612
|
+
if (finished) return
|
|
613
|
+
|
|
586
614
|
if (isChunkedRecordset) {
|
|
587
615
|
chunksBuffer.push(data)
|
|
588
616
|
} else {
|
|
@@ -622,13 +650,17 @@ class Request extends BaseRequest {
|
|
|
622
650
|
})
|
|
623
651
|
|
|
624
652
|
req.on('rowcount', rowCount => {
|
|
653
|
+
if (finished) return
|
|
654
|
+
|
|
625
655
|
rowsAffected.push(rowCount)
|
|
626
656
|
if (this.stream) {
|
|
627
657
|
this.emit('rowsaffected', rowCount)
|
|
628
658
|
}
|
|
629
659
|
})
|
|
630
660
|
|
|
631
|
-
req.on('info', msg => {
|
|
661
|
+
req.on('info', (msg, more) => {
|
|
662
|
+
if (finished) return
|
|
663
|
+
|
|
632
664
|
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)
|
|
633
665
|
if (parsedMessage) {
|
|
634
666
|
msg.message = parsedMessage[1]
|
|
@@ -646,16 +678,75 @@ class Request extends BaseRequest {
|
|
|
646
678
|
|
|
647
679
|
// query terminated
|
|
648
680
|
if (msg.code === 3621 && !hasReturned) {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
681
|
+
if (failing && !more) {
|
|
682
|
+
// msnodesqlv8 2.0 ends a statement's error with more=true and this message, and then
|
|
683
|
+
// nothing follows: the error just collected is the one that terminated the statement.
|
|
684
|
+
// A stream has already emitted it
|
|
685
|
+
if (!this.stream) {
|
|
686
|
+
const error = errors.pop()
|
|
687
|
+
handleError(req, connection, error.originalError || error, false)
|
|
688
|
+
}
|
|
689
|
+
} else if (!terminated) {
|
|
690
|
+
// msnodesqlv8 >= 5.6 emits a failed statement's info messages ahead of its error, and
|
|
691
|
+
// carries on after it, so any error collected so far is an earlier statement's. The
|
|
692
|
+
// error still to come, or 'done', settles the request; 'done' falls back to this
|
|
693
|
+
terminated = msg
|
|
694
|
+
}
|
|
653
695
|
}
|
|
654
696
|
})
|
|
655
697
|
|
|
656
|
-
req.on('error', errorHandlers.error =
|
|
698
|
+
req.on('error', errorHandlers.error = (info, moreErrors) => {
|
|
699
|
+
// a dead link is recorded even after 'done', so the pool does not lend the connection out again
|
|
700
|
+
if ((typeof info.sqlstate === 'string') && (info.sqlstate.toLowerCase() === '08s01')) {
|
|
701
|
+
connection.hasError = true
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
if (finished) return
|
|
705
|
+
|
|
706
|
+
handleError(req, connection, info, moreErrors)
|
|
707
|
+
})
|
|
657
708
|
|
|
658
709
|
req.once('done', () => {
|
|
710
|
+
finished = true
|
|
711
|
+
|
|
712
|
+
if (hasReturned) {
|
|
713
|
+
return
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (cancelRequested) {
|
|
717
|
+
// msnodesqlv8 emits 'done' once a cancel is through: for a paused query in every
|
|
718
|
+
// version, and for a running one from 5.6. What was read up to then is not a result.
|
|
719
|
+
// The error listener stays attached and drops the 'Operation canceled' error that can
|
|
720
|
+
// still arrive after 'done'.
|
|
721
|
+
hasReturned = true
|
|
722
|
+
delete this._cancel
|
|
723
|
+
// released now, as at any 'done', rather than at the statement's later 'free':
|
|
724
|
+
// msnodesqlv8 queues whatever the connection is sent next behind the cancelled
|
|
725
|
+
// statement, and a transaction can carry on as soon as the cancel is reported
|
|
726
|
+
this.parent.release(connection)
|
|
727
|
+
|
|
728
|
+
debug('request(%d): canceled', IDS.get(this))
|
|
729
|
+
|
|
730
|
+
const err = new RequestError('Canceled.', 'ECANCEL')
|
|
731
|
+
if (this.stream) {
|
|
732
|
+
// as with tedious, a cancelled stream reports the cancel as an error and still
|
|
733
|
+
// completes with what was read up to it
|
|
734
|
+
this.emit('error', err)
|
|
735
|
+
return callback(null, null, output, rowsAffected, recordsetcolumns)
|
|
736
|
+
}
|
|
737
|
+
return callback(err)
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (terminated && !errors.length) {
|
|
741
|
+
// the statement was terminated, but no error followed to say why
|
|
742
|
+
handleError(req, connection, terminated, false)
|
|
743
|
+
} else if (errors.length && !this.stream) {
|
|
744
|
+
// msnodesqlv8 >= 5.6 reports an error that does not end the batch with more=true and
|
|
745
|
+
// carries on to 'done', so the request has failed although no error said it was the last
|
|
746
|
+
const error = errors.pop()
|
|
747
|
+
handleError(req, connection, error.originalError || error, false)
|
|
748
|
+
}
|
|
749
|
+
|
|
659
750
|
if (hasReturned) {
|
|
660
751
|
return
|
|
661
752
|
}
|
|
@@ -67,13 +67,13 @@ class Transaction extends BaseTransaction {
|
|
|
67
67
|
|
|
68
68
|
if (!err) debug('transaction(%d): commited', IDS.get(this))
|
|
69
69
|
|
|
70
|
-
callback(
|
|
70
|
+
callback(err)
|
|
71
71
|
})
|
|
72
72
|
})
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
_rollback (callback) {
|
|
76
|
-
super.
|
|
76
|
+
super._rollback(err => {
|
|
77
77
|
if (err) return callback(err)
|
|
78
78
|
|
|
79
79
|
debug('transaction(%d): rollback', IDS.get(this))
|
|
@@ -89,7 +89,7 @@ class Transaction extends BaseTransaction {
|
|
|
89
89
|
|
|
90
90
|
if (!err) debug('transaction(%d): rolled back', IDS.get(this))
|
|
91
91
|
|
|
92
|
-
callback(
|
|
92
|
+
callback(err)
|
|
93
93
|
})
|
|
94
94
|
})
|
|
95
95
|
}
|