mssql 12.7.3 → 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 +10 -0
- package/lib/msnodesqlv8/request.js +147 -32
- package/lib/msnodesqlv8/transaction.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -920,6 +920,7 @@ request.execute('procedure_name', (err, result) => {
|
|
|
920
920
|
|
|
921
921
|
__Errors__
|
|
922
922
|
- EREQUEST (`RequestError`) - *Message from SQL Server*
|
|
923
|
+
- EPARAM (`RequestError`) - A parameter value is not valid for its type.
|
|
923
924
|
- EINJECT (`RequestError`) - Procedure name or parameter type is not a valid identifier. See [Identifiers](#identifiers).
|
|
924
925
|
- ECANCEL (`RequestError`) - Cancelled.
|
|
925
926
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
@@ -1123,6 +1124,7 @@ request.query('select 1 as number', (err, result) => {
|
|
|
1123
1124
|
__Errors__
|
|
1124
1125
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
1125
1126
|
- EREQUEST (`RequestError`) - *Message from SQL Server*
|
|
1127
|
+
- EPARAM (`RequestError`) - A parameter value is not valid for its type.
|
|
1126
1128
|
- ECANCEL (`RequestError`) - Cancelled.
|
|
1127
1129
|
- ENOCONN (`RequestError`) - No connection is specified for that request.
|
|
1128
1130
|
- ENOTOPEN (`ConnectionError`) - Connection not yet open.
|
|
@@ -1169,6 +1171,7 @@ request.batch('create procedure #temporary as select * from table', (err, result
|
|
|
1169
1171
|
__Errors__
|
|
1170
1172
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
1171
1173
|
- EREQUEST (`RequestError`) - *Message from SQL Server*
|
|
1174
|
+
- EPARAM (`RequestError`) - A parameter value is not valid for its type.
|
|
1172
1175
|
- ECANCEL (`RequestError`) - Cancelled.
|
|
1173
1176
|
- ENOCONN (`RequestError`) - No connection is specified for that request.
|
|
1174
1177
|
- ENOTOPEN (`ConnectionError`) - Connection not yet open.
|
|
@@ -1243,6 +1246,10 @@ request.query('waitfor delay \'00:00:05\'; select 1 as number', (err, result) =>
|
|
|
1243
1246
|
request.cancel()
|
|
1244
1247
|
```
|
|
1245
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
|
+
|
|
1246
1253
|
## Transaction
|
|
1247
1254
|
|
|
1248
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.
|
|
@@ -1313,6 +1320,8 @@ transaction.begin(err => {
|
|
|
1313
1320
|
})
|
|
1314
1321
|
```
|
|
1315
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
|
+
|
|
1316
1325
|
### Events
|
|
1317
1326
|
|
|
1318
1327
|
- **begin** - Dispatched when transaction begin.
|
|
@@ -1577,6 +1586,7 @@ __Errors__
|
|
|
1577
1586
|
- ENOTPREPARED (`PreparedStatementError`) - Statement is not prepared.
|
|
1578
1587
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
1579
1588
|
- EREQUEST (`RequestError`) - *Message from SQL Server*
|
|
1589
|
+
- EPARAM (`RequestError`) - A parameter value is not valid for its type.
|
|
1580
1590
|
- ECANCEL (`RequestError`) - Cancelled.
|
|
1581
1591
|
|
|
1582
1592
|
---------------------------------------
|
|
@@ -14,6 +14,16 @@ const JSON_COLUMN_ID = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B'
|
|
|
14
14
|
const XML_COLUMN_ID = 'XML_F52E2B61-18A1-11d1-B105-00805F49916B'
|
|
15
15
|
const EMPTY_BUFFER = Buffer.alloc(0)
|
|
16
16
|
|
|
17
|
+
// String() converts a Symbol, which only fails to convert implicitly; an object without a
|
|
18
|
+
// working toString falls back to its tag
|
|
19
|
+
const thrownMessage = function (value) {
|
|
20
|
+
try {
|
|
21
|
+
return String(value)
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return Object.prototype.toString.call(value)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
const castParameter = function (value, type) {
|
|
18
28
|
if (value == null) {
|
|
19
29
|
if ((type === TYPES.Binary) || (type === TYPES.VarBinary) || (type === TYPES.Image)) {
|
|
@@ -235,7 +245,12 @@ class Request extends BaseRequest {
|
|
|
235
245
|
|
|
236
246
|
const go = () => {
|
|
237
247
|
const tm = connection.tableMgr()
|
|
238
|
-
|
|
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
|
+
|
|
239
254
|
if (mgr.columns.length === 0) {
|
|
240
255
|
return done(new RequestError('Table was not found on the server.', 'ENAME'))
|
|
241
256
|
}
|
|
@@ -324,13 +339,22 @@ class Request extends BaseRequest {
|
|
|
324
339
|
let handleOutput = false
|
|
325
340
|
let isChunkedRecordset = false
|
|
326
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
|
|
327
349
|
|
|
328
350
|
const handleError = (req, connection, info, moreErrors) => {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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
|
|
332
355
|
}
|
|
333
356
|
|
|
357
|
+
const doReturn = !moreErrors
|
|
334
358
|
const err = new RequestError(info, 'EREQUEST')
|
|
335
359
|
err.code = 'EREQUEST'
|
|
336
360
|
|
|
@@ -350,13 +374,8 @@ class Request extends BaseRequest {
|
|
|
350
374
|
debug('request(%d): failed', IDS.get(this), err)
|
|
351
375
|
}
|
|
352
376
|
|
|
353
|
-
let previous
|
|
354
377
|
if (errors.length) {
|
|
355
|
-
|
|
356
|
-
if (!err.precedingErrors) {
|
|
357
|
-
err.precedingErrors = []
|
|
358
|
-
}
|
|
359
|
-
err.precedingErrors.push(previous)
|
|
378
|
+
err.precedingErrors = errors.splice(0)
|
|
360
379
|
}
|
|
361
380
|
|
|
362
381
|
hasReturned = true
|
|
@@ -366,6 +385,10 @@ class Request extends BaseRequest {
|
|
|
366
385
|
|
|
367
386
|
// we must collect errors even in stream mode
|
|
368
387
|
errors.push(err)
|
|
388
|
+
if (!failing) {
|
|
389
|
+
failing = true
|
|
390
|
+
queueMicrotask(() => { failing = false })
|
|
391
|
+
}
|
|
369
392
|
}
|
|
370
393
|
|
|
371
394
|
// nested = function is called by this.execute
|
|
@@ -417,22 +440,16 @@ class Request extends BaseRequest {
|
|
|
417
440
|
}
|
|
418
441
|
}
|
|
419
442
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
this.parent.release(connection)
|
|
428
|
-
return callback(new RequestError('Canceled.', 'ECANCEL'))
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
// values are bound to the markers by position, so a caller that writes its own
|
|
432
|
-
// markers passes the parameters in the order it wrote them
|
|
433
|
-
const params = []
|
|
443
|
+
// values are bound to the markers by position, so a caller that writes its own
|
|
444
|
+
// markers passes the parameters in the order it wrote them. They are converted before
|
|
445
|
+
// a connection is borrowed, so that a value that cannot be converted is handed back
|
|
446
|
+
// with nothing to release, rather than thrown while a connection is held.
|
|
447
|
+
const params = []
|
|
448
|
+
let converting
|
|
449
|
+
try {
|
|
434
450
|
if (bound) {
|
|
435
451
|
for (const param of bound) {
|
|
452
|
+
converting = param.name
|
|
436
453
|
params.push(castParameter(param.value, param.type))
|
|
437
454
|
}
|
|
438
455
|
} else {
|
|
@@ -442,10 +459,30 @@ class Request extends BaseRequest {
|
|
|
442
459
|
}
|
|
443
460
|
const param = this.parameters[name]
|
|
444
461
|
if (param.io === 1 || (param.io === 2 && param.value != null)) {
|
|
462
|
+
converting = param.name
|
|
445
463
|
params.push(castParameter(param.value, param.type))
|
|
446
464
|
}
|
|
447
465
|
}
|
|
448
466
|
}
|
|
467
|
+
} catch (e) {
|
|
468
|
+
// what was thrown need not be an Error, nor convert to a string implicitly, and may
|
|
469
|
+
// carry a code of its own
|
|
470
|
+
const err = new RequestError(e instanceof Error ? e : thrownMessage(e), 'EPARAM')
|
|
471
|
+
err.code = 'EPARAM'
|
|
472
|
+
err.message = `Validation failed for parameter '${converting}'. ${err.message}`
|
|
473
|
+
return callback(err)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
this.parent.acquire(this, (err, connection, config) => {
|
|
477
|
+
if (err) return callback(err)
|
|
478
|
+
|
|
479
|
+
debug('connection(%d): borrowed to request #%d', IDS.get(connection), IDS.get(this))
|
|
480
|
+
|
|
481
|
+
if (this.canceled) {
|
|
482
|
+
debug('request(%d): canceled', IDS.get(this))
|
|
483
|
+
this.parent.release(connection)
|
|
484
|
+
return callback(new RequestError('Canceled.', 'ECANCEL'))
|
|
485
|
+
}
|
|
449
486
|
|
|
450
487
|
debug('request(%d): query', IDS.get(this), command)
|
|
451
488
|
|
|
@@ -458,8 +495,19 @@ class Request extends BaseRequest {
|
|
|
458
495
|
|
|
459
496
|
this._cancel = () => {
|
|
460
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
|
|
461
504
|
req.cancelQuery(err => {
|
|
462
|
-
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
|
+
}
|
|
463
511
|
// this fixes an issue where paused connections don't emit a done event
|
|
464
512
|
try {
|
|
465
513
|
if (req.isPaused()) req.emit('done')
|
|
@@ -470,6 +518,8 @@ class Request extends BaseRequest {
|
|
|
470
518
|
}
|
|
471
519
|
|
|
472
520
|
req.on('meta', metadata => {
|
|
521
|
+
if (finished) return
|
|
522
|
+
|
|
473
523
|
if (row) {
|
|
474
524
|
if (isChunkedRecordset) {
|
|
475
525
|
const concatenatedChunks = chunksBuffer.join('')
|
|
@@ -547,7 +597,7 @@ class Request extends BaseRequest {
|
|
|
547
597
|
})
|
|
548
598
|
|
|
549
599
|
req.on('row', rownumber => {
|
|
550
|
-
if (row && isChunkedRecordset) return
|
|
600
|
+
if (finished || (row && isChunkedRecordset)) return
|
|
551
601
|
|
|
552
602
|
if (this.arrayRowMode) {
|
|
553
603
|
row = []
|
|
@@ -559,6 +609,8 @@ class Request extends BaseRequest {
|
|
|
559
609
|
})
|
|
560
610
|
|
|
561
611
|
req.on('column', (idx, data, more) => {
|
|
612
|
+
if (finished) return
|
|
613
|
+
|
|
562
614
|
if (isChunkedRecordset) {
|
|
563
615
|
chunksBuffer.push(data)
|
|
564
616
|
} else {
|
|
@@ -598,13 +650,17 @@ class Request extends BaseRequest {
|
|
|
598
650
|
})
|
|
599
651
|
|
|
600
652
|
req.on('rowcount', rowCount => {
|
|
653
|
+
if (finished) return
|
|
654
|
+
|
|
601
655
|
rowsAffected.push(rowCount)
|
|
602
656
|
if (this.stream) {
|
|
603
657
|
this.emit('rowsaffected', rowCount)
|
|
604
658
|
}
|
|
605
659
|
})
|
|
606
660
|
|
|
607
|
-
req.on('info', msg => {
|
|
661
|
+
req.on('info', (msg, more) => {
|
|
662
|
+
if (finished) return
|
|
663
|
+
|
|
608
664
|
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)
|
|
609
665
|
if (parsedMessage) {
|
|
610
666
|
msg.message = parsedMessage[1]
|
|
@@ -622,16 +678,75 @@ class Request extends BaseRequest {
|
|
|
622
678
|
|
|
623
679
|
// query terminated
|
|
624
680
|
if (msg.code === 3621 && !hasReturned) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
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
|
+
}
|
|
629
695
|
}
|
|
630
696
|
})
|
|
631
697
|
|
|
632
|
-
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
|
+
})
|
|
633
708
|
|
|
634
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
|
+
|
|
635
750
|
if (hasReturned) {
|
|
636
751
|
return
|
|
637
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
|
}
|