mssql 12.7.10 → 12.7.12
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 +2 -0
- package/lib/error/request-error.js +2 -4
- package/lib/msnodesqlv8/request.js +28 -12
- package/lib/utils.js +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2318,6 +2318,8 @@ Columns structure for example above:
|
|
|
2318
2318
|
}
|
|
2319
2319
|
```
|
|
2320
2320
|
|
|
2321
|
+
The MSNodeSQLv8 driver describes a column only by `index`, `name`, `length`, `type`, `nullable` and `identity` (and `udt.name` for a CLR type); it has no `scale`, `precision`, `caseSensitive` or `readOnly` to give. Its `length` is the size ODBC reports for the column, which for a number is its precision rather than its length in bytes. ODBC names the type of a column of an alias type (`CREATE TYPE dbo.MyInt FROM int`) after the alias, so with this driver such a column has no `type`, and is not marked as an `identity` column even when it is one.
|
|
2322
|
+
|
|
2321
2323
|
## Data Types
|
|
2322
2324
|
|
|
2323
2325
|
You can define data types with length/precision/scale:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const MSSQLError = require('./mssql-error')
|
|
4
|
+
const { stripOdbcPrefix } = require('../utils')
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Class RequestError.
|
|
@@ -43,10 +44,7 @@ class RequestError extends MSSQLError {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
this.name = 'RequestError'
|
|
46
|
-
|
|
47
|
-
if (parsedMessage) {
|
|
48
|
-
this.message = parsedMessage[1]
|
|
49
|
-
}
|
|
47
|
+
this.message = stripOdbcPrefix(this.message)
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
50
|
|
|
@@ -4,7 +4,7 @@ const msnodesql = require('msnodesqlv8')
|
|
|
4
4
|
const debug = require('debug')('mssql:msv8')
|
|
5
5
|
const BaseRequest = require('../base/request')
|
|
6
6
|
const RequestError = require('../error/request-error')
|
|
7
|
-
const { IDS, objectHasProperty, assertSafeColumnName, assertSafeTypeName, assertSafeTypeSize, assertSafeProcedureName } = require('../utils')
|
|
7
|
+
const { IDS, objectHasProperty, assertSafeColumnName, assertSafeTypeName, assertSafeTypeSize, assertSafeProcedureName, stripOdbcPrefix } = require('../utils')
|
|
8
8
|
const { TYPES, DECLARATIONS, declare } = require('../datatypes')
|
|
9
9
|
const { PARSERS: UDT } = require('../udt')
|
|
10
10
|
const Table = require('../table')
|
|
@@ -111,6 +111,18 @@ const castParameter = function (value, type) {
|
|
|
111
111
|
return value
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
// ODBC names the type of an identity column after the type it holds, as 'int identity', and
|
|
115
|
+
// gives decimal and numeric the brackets of their precision, as 'decimal() identity'
|
|
116
|
+
const IDENTITY_SQL_TYPE = /^(tinyint|smallint|int|bigint|decimal|numeric)(?:\(\))? identity$/
|
|
117
|
+
|
|
118
|
+
// msnodesqlv8 describes a column by the name ODBC gives its type. An identity column is described
|
|
119
|
+
// by the type it holds instead, so that it is described, and its values read, as any other column
|
|
120
|
+
// of that type.
|
|
121
|
+
const describeColumn = function (column) {
|
|
122
|
+
const identity = typeof column.sqlType === 'string' ? IDENTITY_SQL_TYPE.exec(column.sqlType) : null
|
|
123
|
+
return identity ? { ...column, sqlType: identity[1], identity: true } : column
|
|
124
|
+
}
|
|
125
|
+
|
|
114
126
|
const createColumns = function (metadata, arrayRowMode) {
|
|
115
127
|
let out = {}
|
|
116
128
|
if (arrayRowMode) out = []
|
|
@@ -122,7 +134,8 @@ const createColumns = function (metadata, arrayRowMode) {
|
|
|
122
134
|
name: column.name,
|
|
123
135
|
length: column.size,
|
|
124
136
|
type: DECLARATIONS[column.sqlType],
|
|
125
|
-
nullable: column.nullable
|
|
137
|
+
nullable: column.nullable,
|
|
138
|
+
identity: column.identity === true
|
|
126
139
|
}
|
|
127
140
|
|
|
128
141
|
if (column.udtType != null) {
|
|
@@ -147,6 +160,16 @@ const createColumns = function (metadata, arrayRowMode) {
|
|
|
147
160
|
const numericStringSqlTypes = new Set(['int', 'smallint', 'tinyint', 'float', 'real', 'numeric', 'decimal', 'money', 'smallmoney'])
|
|
148
161
|
|
|
149
162
|
const valueCorrection = function (value, metadata) {
|
|
163
|
+
// a number msnodesqlv8 hands over as a string is read as a number before a value handler is
|
|
164
|
+
// given it, as it is with the tedious driver
|
|
165
|
+
if (typeof value === 'string') {
|
|
166
|
+
if (numericStringSqlTypes.has(metadata.sqlType)) {
|
|
167
|
+
value = Number(value)
|
|
168
|
+
} else if (metadata.sqlType === 'sql_variant' && value !== '' && !isNaN(Number(value))) {
|
|
169
|
+
value = Number(value)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
150
173
|
const type = metadata && objectHasProperty(metadata, 'sqlType') && objectHasProperty(DECLARATIONS, metadata.sqlType)
|
|
151
174
|
? DECLARATIONS[metadata.sqlType]
|
|
152
175
|
: null
|
|
@@ -161,10 +184,6 @@ const valueCorrection = function (value, metadata) {
|
|
|
161
184
|
} else {
|
|
162
185
|
return value
|
|
163
186
|
}
|
|
164
|
-
} else if (typeof value === 'string' && numericStringSqlTypes.has(metadata.sqlType)) {
|
|
165
|
-
return Number(value)
|
|
166
|
-
} else if (typeof value === 'string' && metadata.sqlType === 'sql_variant' && value !== '' && !isNaN(Number(value))) {
|
|
167
|
-
return Number(value)
|
|
168
187
|
} else {
|
|
169
188
|
return value
|
|
170
189
|
}
|
|
@@ -743,13 +762,13 @@ class Request extends BaseRequest {
|
|
|
743
762
|
|
|
744
763
|
row = null
|
|
745
764
|
rowsInRecordset = 0
|
|
746
|
-
columns = metadata
|
|
765
|
+
columns = metadata.map(describeColumn)
|
|
747
766
|
recordset = []
|
|
748
767
|
|
|
749
768
|
Object.defineProperty(recordset, 'columns', {
|
|
750
769
|
enumerable: false,
|
|
751
770
|
configurable: true,
|
|
752
|
-
value: createColumns(
|
|
771
|
+
value: createColumns(columns, this.arrayRowMode)
|
|
753
772
|
})
|
|
754
773
|
|
|
755
774
|
Object.defineProperty(recordset, 'toTable', {
|
|
@@ -860,10 +879,7 @@ class Request extends BaseRequest {
|
|
|
860
879
|
req.on('info', (msg, more) => {
|
|
861
880
|
if (finished) return
|
|
862
881
|
|
|
863
|
-
|
|
864
|
-
if (parsedMessage) {
|
|
865
|
-
msg.message = parsedMessage[1]
|
|
866
|
-
}
|
|
882
|
+
msg.message = stripOdbcPrefix(msg.message)
|
|
867
883
|
|
|
868
884
|
const info = {
|
|
869
885
|
message: msg.message,
|
package/lib/utils.js
CHANGED
|
@@ -50,6 +50,13 @@ const TYPE_SIZE = /^\w*$/
|
|
|
50
50
|
// is the documented way to include one literally.
|
|
51
51
|
const escapesQuotedIdentifier = (name) => String(name).replace(/]]/g, '').includes(']')
|
|
52
52
|
|
|
53
|
+
// ODBC puts the components a message came through in front of it: the vendor and the driver,
|
|
54
|
+
// such as `[Microsoft][ODBC Driver 18 for SQL Server]`, then `[SQL Server]` when the server
|
|
55
|
+
// raised it. The driver manager's own messages name it instead, followed by a space on Windows,
|
|
56
|
+
// `[Microsoft][ODBC Driver Manager] `, and by nothing with unixODBC, `[unixODBC][Driver Manager]`.
|
|
57
|
+
// Only that one prefix is taken off, so what a PRINT or RAISERROR sent is kept as it was sent.
|
|
58
|
+
const ODBC_PREFIX = /^(?:\[Microsoft\]\[ODBC Driver Manager\] |\[unixODBC\]\[Driver Manager\]|\[Microsoft\]\[[^\]]+\](?:\[SQL Server\])?)/
|
|
59
|
+
|
|
53
60
|
module.exports = {
|
|
54
61
|
objectHasProperty: (object, property) => Object.prototype.hasOwnProperty.call(object, property),
|
|
55
62
|
isValidParameterName: (name) => {
|
|
@@ -107,6 +114,7 @@ module.exports = {
|
|
|
107
114
|
}
|
|
108
115
|
return column
|
|
109
116
|
},
|
|
117
|
+
stripOdbcPrefix: (message) => typeof message === 'string' ? message.replace(ODBC_PREFIX, '') : message,
|
|
110
118
|
INCREMENT,
|
|
111
119
|
IDS: {
|
|
112
120
|
get: IDS.get.bind(IDS),
|