mssql 12.7.2 → 12.7.4

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 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.
@@ -1577,6 +1580,7 @@ __Errors__
1577
1580
  - ENOTPREPARED (`PreparedStatementError`) - Statement is not prepared.
1578
1581
  - ETIMEOUT (`RequestError`) - Request timeout.
1579
1582
  - EREQUEST (`RequestError`) - *Message from SQL Server*
1583
+ - EPARAM (`RequestError`) - A parameter value is not valid for its type.
1580
1584
  - ECANCEL (`RequestError`) - Cancelled.
1581
1585
 
1582
1586
  ---------------------------------------
@@ -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)) {
@@ -302,7 +312,7 @@ class Request extends BaseRequest {
302
312
  })
303
313
  }
304
314
 
305
- _query (command, callback) {
315
+ _query (command, callback, bound) {
306
316
  super._query(command, err => {
307
317
  if (err) return callback(err)
308
318
 
@@ -392,7 +402,9 @@ class Request extends BaseRequest {
392
402
  continue
393
403
  }
394
404
  const param = this.parameters[name]
395
- if (param.io === 1) {
405
+ // an output's initial value is bound with the inputs below, so it needs a marker too;
406
+ // an output given no value is null, and 0, '' and false are values like any other
407
+ if (param.io === 1 || (param.io === 2 && param.value != null)) {
396
408
  sets.push(`set @${param.name}=?`)
397
409
  }
398
410
  }
@@ -415,6 +427,39 @@ class Request extends BaseRequest {
415
427
  }
416
428
  }
417
429
 
430
+ // values are bound to the markers by position, so a caller that writes its own
431
+ // markers passes the parameters in the order it wrote them. They are converted before
432
+ // a connection is borrowed, so that a value that cannot be converted is handed back
433
+ // with nothing to release, rather than thrown while a connection is held.
434
+ const params = []
435
+ let converting
436
+ try {
437
+ if (bound) {
438
+ for (const param of bound) {
439
+ converting = param.name
440
+ params.push(castParameter(param.value, param.type))
441
+ }
442
+ } else {
443
+ for (const name in this.parameters) {
444
+ if (!objectHasProperty(this.parameters, name)) {
445
+ continue
446
+ }
447
+ const param = this.parameters[name]
448
+ if (param.io === 1 || (param.io === 2 && param.value != null)) {
449
+ converting = param.name
450
+ params.push(castParameter(param.value, param.type))
451
+ }
452
+ }
453
+ }
454
+ } catch (e) {
455
+ // what was thrown need not be an Error, nor convert to a string implicitly, and may
456
+ // carry a code of its own
457
+ const err = new RequestError(e instanceof Error ? e : thrownMessage(e), 'EPARAM')
458
+ err.code = 'EPARAM'
459
+ err.message = `Validation failed for parameter '${converting}'. ${err.message}`
460
+ return callback(err)
461
+ }
462
+
418
463
  this.parent.acquire(this, (err, connection, config) => {
419
464
  if (err) return callback(err)
420
465
 
@@ -426,17 +471,6 @@ class Request extends BaseRequest {
426
471
  return callback(new RequestError('Canceled.', 'ECANCEL'))
427
472
  }
428
473
 
429
- const params = []
430
- for (const name in this.parameters) {
431
- if (!objectHasProperty(this.parameters, name)) {
432
- continue
433
- }
434
- const param = this.parameters[name]
435
- if (param.io === 1 || (param.io === 2 && param.value)) {
436
- params.push(castParameter(param.value, param.type))
437
- }
438
- }
439
-
440
474
  debug('request(%d): query', IDS.get(this), command)
441
475
 
442
476
  const req = connection.queryRaw({
@@ -726,6 +760,10 @@ class Request extends BaseRequest {
726
760
  return callback(new RequestError(e, 'EREQUEST'))
727
761
  }
728
762
 
763
+ // the parameters in the order their markers are written: output initial values are set
764
+ // before the call and inputs are passed to it, whatever order they were declared in
765
+ const bound = []
766
+
729
767
  // set output params w/ values
730
768
  const sets = []
731
769
  for (const name in this.parameters) {
@@ -733,8 +771,9 @@ class Request extends BaseRequest {
733
771
  continue
734
772
  }
735
773
  const param = this.parameters[name]
736
- if (param.io === 2 && param.value) {
774
+ if (param.io === 2 && param.value != null) {
737
775
  sets.push(`set @${param.name}=?`)
776
+ bound.push(param)
738
777
  }
739
778
  }
740
779
 
@@ -754,6 +793,7 @@ class Request extends BaseRequest {
754
793
  } else {
755
794
  // input parameter
756
795
  spp.push(`@${param.name}=?`)
796
+ bound.push(param)
757
797
  }
758
798
  }
759
799
 
@@ -829,7 +869,7 @@ class Request extends BaseRequest {
829
869
  } else {
830
870
  callback(null, recordsets, output, returnValue, rowsAffected, recordsetcolumns)
831
871
  }
832
- })
872
+ }, bound)
833
873
  })
834
874
  }
835
875
 
@@ -12,6 +12,7 @@ const { valueHandler } = require('../shared')
12
12
 
13
13
  const JSON_COLUMN_ID = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B'
14
14
  const XML_COLUMN_ID = 'XML_F52E2B61-18A1-11d1-B105-00805F49916B'
15
+ const MAX = 65535 // (1 << 16) - 1
15
16
 
16
17
  const N_TYPES = {
17
18
  BitN: 0x68,
@@ -174,6 +175,18 @@ const valueCorrection = function (value, metadata) {
174
175
  }
175
176
  }
176
177
 
178
+ // tedious only compares a length as a number, so `max` is passed as the length it stands
179
+ // for. As a word it compares false against the type's maximum both ways, and tedious then
180
+ // writes a `max` value without its terminator.
181
+ const tediousLength = function (length) {
182
+ return typeof length === 'string' && length.trim().toLowerCase() === 'max' ? MAX : length
183
+ }
184
+
185
+ // tedious writes a TVP column with the length it is given, without resolving a missing one,
186
+ // and a missing length fails the same comparisons as `max`. These types are given `max`,
187
+ // which is how tedious wrote them before it compared the length both ways.
188
+ const MAX_LENGTH_TYPES = new Set([tds.TYPES.NVarChar, tds.TYPES.VarChar, tds.TYPES.VarBinary])
189
+
177
190
  const parameterCorrection = function (value) {
178
191
  if (value instanceof Table) {
179
192
  const tvp = {
@@ -191,7 +204,7 @@ const parameterCorrection = function (value) {
191
204
  tvp.columns.push({
192
205
  name: col.name,
193
206
  type: tediousType,
194
- length: col.length,
207
+ length: col.length == null && MAX_LENGTH_TYPES.has(tediousType) ? MAX : tediousLength(col.length),
195
208
  scale: col.scale,
196
209
  precision: col.precision
197
210
  })
@@ -368,7 +381,7 @@ class Request extends BaseRequest {
368
381
  // tedious emits this name as a quoted identifier of its own when it builds
369
382
  // the `insert bulk` statement, so it is checked here as well as in
370
383
  // Table#declare, in case the name changed since it was checked above.
371
- bulk.addColumn(assertSafeColumnName(col.name), getTediousType(col.type), { nullable: col.nullable, length: assertSafeTypeSize(col.length), scale: assertSafeTypeSize(col.scale), precision: assertSafeTypeSize(col.precision) })
384
+ bulk.addColumn(assertSafeColumnName(col.name), getTediousType(col.type), { nullable: col.nullable, length: tediousLength(assertSafeTypeSize(col.length)), scale: assertSafeTypeSize(col.scale), precision: assertSafeTypeSize(col.precision) })
372
385
  }
373
386
 
374
387
  if (table.create) {
@@ -773,9 +786,9 @@ class Request extends BaseRequest {
773
786
  // tedious builds the sp_executesql declaration from these, so a rejected size
774
787
  // has to be reported through the path that releases the connection
775
788
  if (param.io === 1) {
776
- req.addParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: assertSafeTypeSize(param.length), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
789
+ req.addParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: tediousLength(assertSafeTypeSize(param.length)), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
777
790
  } else {
778
- req.addOutputParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: assertSafeTypeSize(param.length), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
791
+ req.addOutputParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: tediousLength(assertSafeTypeSize(param.length)), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
779
792
  }
780
793
  }
781
794
  }
@@ -1093,9 +1106,9 @@ class Request extends BaseRequest {
1093
1106
  }
1094
1107
  const param = this.parameters[name]
1095
1108
  if (param.io === 1) {
1096
- req.addParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: assertSafeTypeSize(param.length), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
1109
+ req.addParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: tediousLength(assertSafeTypeSize(param.length)), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
1097
1110
  } else {
1098
- req.addOutputParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: assertSafeTypeSize(param.length), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
1111
+ req.addOutputParameter(param.name, getTediousType(param.type), parameterCorrection(param.value), { length: tediousLength(assertSafeTypeSize(param.length)), scale: assertSafeTypeSize(param.scale), precision: assertSafeTypeSize(param.precision) })
1099
1112
  }
1100
1113
  }
1101
1114
 
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "azure",
22
22
  "node-mssql"
23
23
  ],
24
- "version": "12.7.2",
24
+ "version": "12.7.4",
25
25
  "main": "index.js",
26
26
  "type": "commonjs",
27
27
  "repository": {