mssql 12.7.4 → 12.7.6
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 +8 -0
- package/lib/msnodesqlv8/request.js +109 -18
- package/lib/msnodesqlv8/transaction.js +3 -3
- package/lib/table.js +4 -0
- package/lib/tedious/request.js +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1213,6 +1213,8 @@ request.bulk(table, (err, result) => {
|
|
|
1213
1213
|
|
|
1214
1214
|
**TIP**: You can also create Table variable from any recordset with `recordset.toTable()`. You can optionally specify table type name in the first argument.
|
|
1215
1215
|
|
|
1216
|
+
**TIP**: A `VarChar`, `NVarChar` or `VarBinary` column declared without a length (e.g. `sql.NVarChar`) is created as `max` when `table.create` is set, and the tedious driver loads it as `max`. It does so whether or not the table already existed, so when `table.create` is set for an existing table, give such a column its length if the table's column is sized (it loads faster) or a `sql_variant` (which cannot hold a `max` value). Without `table.create`, the tedious driver needs a column declared as `max` (e.g. `sql.NVarChar(sql.MAX)`) to bulk load into an existing `varchar(max)`, `nvarchar(max)` or `varbinary(max)` column. `Xml`, `UDT`, `Geography` and `Geometry` columns are loaded as `max` unless you give them a length.
|
|
1217
|
+
|
|
1216
1218
|
__Errors__
|
|
1217
1219
|
- ENAME (`RequestError`) - Table name must be specified for bulk insert.
|
|
1218
1220
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
@@ -1246,6 +1248,10 @@ request.query('waitfor delay \'00:00:05\'; select 1 as number', (err, result) =>
|
|
|
1246
1248
|
request.cancel()
|
|
1247
1249
|
```
|
|
1248
1250
|
|
|
1251
|
+
A cancelled stream emits an `error` event with code `ECANCEL`, followed by `done`.
|
|
1252
|
+
|
|
1253
|
+
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.
|
|
1254
|
+
|
|
1249
1255
|
## Transaction
|
|
1250
1256
|
|
|
1251
1257
|
**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 +1322,8 @@ transaction.begin(err => {
|
|
|
1316
1322
|
})
|
|
1317
1323
|
```
|
|
1318
1324
|
|
|
1325
|
+
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.
|
|
1326
|
+
|
|
1319
1327
|
### Events
|
|
1320
1328
|
|
|
1321
1329
|
- **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
|
}
|
package/lib/table.js
CHANGED
|
@@ -80,14 +80,18 @@ Table.prototype._makeBulk = function _makeBulk () {
|
|
|
80
80
|
}
|
|
81
81
|
break
|
|
82
82
|
|
|
83
|
+
// these are sent as `max` values, so a column without a length is given `max`, which
|
|
84
|
+
// is how Table#declare creates it anyway
|
|
83
85
|
case TYPES.Xml:
|
|
84
86
|
col.type = TYPES.NVarChar(MAX).type
|
|
87
|
+
if (col.length == null) col.length = MAX
|
|
85
88
|
break
|
|
86
89
|
|
|
87
90
|
case TYPES.UDT:
|
|
88
91
|
case TYPES.Geography:
|
|
89
92
|
case TYPES.Geometry:
|
|
90
93
|
col.type = TYPES.VarBinary(MAX).type
|
|
94
|
+
if (col.length == null) col.length = MAX
|
|
91
95
|
break
|
|
92
96
|
|
|
93
97
|
default:
|
package/lib/tedious/request.js
CHANGED
|
@@ -378,10 +378,17 @@ class Request extends BaseRequest {
|
|
|
378
378
|
}
|
|
379
379
|
bulk = connection.newBulkLoad(table.path, bulkOptions, done)
|
|
380
380
|
for (const col of table.columns) {
|
|
381
|
+
const type = getTediousType(col.type)
|
|
382
|
+
// tedious resolves a missing length to 4000 or 8000, but Table#declare creates
|
|
383
|
+
// such a column as `max`, and the server will not load a sized column into a
|
|
384
|
+
// `max` one, so with table.create the column is sent as `max`, even when the
|
|
385
|
+
// table turns out to exist already. Without it the target's column types are
|
|
386
|
+
// not known here, so the column keeps the length tedious gives it.
|
|
387
|
+
const length = assertSafeTypeSize(col.length)
|
|
381
388
|
// tedious emits this name as a quoted identifier of its own when it builds
|
|
382
389
|
// the `insert bulk` statement, so it is checked here as well as in
|
|
383
390
|
// Table#declare, in case the name changed since it was checked above.
|
|
384
|
-
bulk.addColumn(assertSafeColumnName(col.name),
|
|
391
|
+
bulk.addColumn(assertSafeColumnName(col.name), type, { nullable: col.nullable, length: length == null && table.create && MAX_LENGTH_TYPES.has(type) ? MAX : tediousLength(length), scale: assertSafeTypeSize(col.scale), precision: assertSafeTypeSize(col.precision) })
|
|
385
392
|
}
|
|
386
393
|
|
|
387
394
|
if (table.create) {
|