mssql 3.1.1 → 3.3.0
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/.travis.yml +1 -0
- package/CHANGELOG.txt +17 -1
- package/README.md +154 -125
- package/appveyor.yml +1 -0
- package/lib/connectionstring.js +15 -6
- package/lib/main.js +90 -1
- package/lib/msnodesql.js +40 -4
- package/lib/msnodesqlv8.js +40 -4
- package/lib/tds.js +11 -0
- package/lib/tedious.js +39 -3
- package/package.json +5 -3
- package/src/connectionstring.coffee +7 -3
- package/src/main.coffee +69 -2
- package/src/msnodesql.coffee +34 -2
- package/src/msnodesqlv8.coffee +34 -2
- package/src/tds.coffee +10 -0
- package/src/tedious.coffee +30 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# node-mssql
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Microsoft SQL Server client for Node.js
|
|
4
4
|
|
|
5
5
|
[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Package Quality][quality-image]][quality-url] [![Travis CI][travis-image]][travis-url] [![Appveyor CI][appveyor-image]][appveyor-url] [](https://gitter.im/patriksimek/node-mssql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
6
6
|
|
|
@@ -12,6 +12,7 @@ An easy-to-use MSSQL database connector for Node.js.
|
|
|
12
12
|
- Supports serialization of Geography and Geometry CLR types.
|
|
13
13
|
- Has smart JS data type to SQL data type mapper.
|
|
14
14
|
- Supports Promises, Streams and standard callbacks.
|
|
15
|
+
- Supports ES6 tagged template literals.
|
|
15
16
|
- Is stable and tested in production environment.
|
|
16
17
|
- Is well documented.
|
|
17
18
|
|
|
@@ -54,88 +55,96 @@ sql.connect("mssql://username:password@localhost/database").then(function() {
|
|
|
54
55
|
}).catch(function(err) {
|
|
55
56
|
// ... execute error checks
|
|
56
57
|
});
|
|
58
|
+
|
|
59
|
+
// ES6 Tagged template literals (experimental)
|
|
60
|
+
|
|
61
|
+
sql.query`select * from mytable where id = ${value}`.then(function(recordset) {
|
|
62
|
+
console.dir(recordset);
|
|
63
|
+
}).catch(function(err) {
|
|
64
|
+
// ... query error checks
|
|
65
|
+
});
|
|
57
66
|
}).catch(function(err) {
|
|
58
67
|
// ... connect error checks
|
|
59
68
|
});
|
|
60
69
|
```
|
|
61
70
|
|
|
62
|
-
If you're on Windows Azure, add `?encrypt=true` to your connection string. See [docs](#
|
|
71
|
+
If you're on Windows Azure, add `?encrypt=true` to your connection string. See [docs](#configuration) to learn more.
|
|
63
72
|
|
|
64
73
|
## Documentation
|
|
65
74
|
|
|
66
|
-
* [2.x to 3.x changes](#
|
|
75
|
+
* [2.x to 3.x changes](#2x-to-3x-changes)
|
|
67
76
|
|
|
68
77
|
### Examples
|
|
69
78
|
|
|
70
79
|
* [Promises](#promises)
|
|
71
|
-
* [Nested callbacks](#callbacks)
|
|
80
|
+
* [Nested callbacks](#nested-callbacks)
|
|
72
81
|
* [Streaming](#streaming)
|
|
73
82
|
* [Multiple Connections](#multiple-connections)
|
|
74
83
|
|
|
75
84
|
### Configuration
|
|
76
85
|
|
|
77
|
-
* [General](#
|
|
78
|
-
* [Formats](#
|
|
86
|
+
* [General](#general-same-for-all-drivers)
|
|
87
|
+
* [Formats](#formats)
|
|
79
88
|
|
|
80
89
|
### Drivers
|
|
81
90
|
|
|
82
|
-
* [Tedious](#
|
|
83
|
-
* [Microsoft / Contributors Node V8 Driver for Node.js for SQL Server](#
|
|
84
|
-
* [Microsoft Driver for Node.js for SQL Server](#
|
|
85
|
-
* [node-tds](#
|
|
91
|
+
* [Tedious](#tedious)
|
|
92
|
+
* [Microsoft / Contributors Node V8 Driver for Node.js for SQL Server](#microsoft--contributors-node-v8-driver-for-nodejs-for-sql-server)
|
|
93
|
+
* [Microsoft Driver for Node.js for SQL Server](#microsoft-driver-for-nodejs-for-sql-server)
|
|
94
|
+
* [node-tds](#node-tds)
|
|
86
95
|
|
|
87
96
|
### Connections
|
|
88
97
|
|
|
89
98
|
* [Connection](#connection)
|
|
90
|
-
* [connect](#connect)
|
|
99
|
+
* [connect](#connect-callback)
|
|
91
100
|
* [close](#close)
|
|
92
101
|
|
|
93
102
|
### Requests
|
|
94
103
|
|
|
95
104
|
* [Request](#request)
|
|
96
|
-
* [execute](#execute)
|
|
97
|
-
* [input](#input)
|
|
98
|
-
* [output](#output)
|
|
99
|
-
* [pipe](#pipe)
|
|
100
|
-
* [query](#query)
|
|
101
|
-
* [batch](#batch)
|
|
102
|
-
* [bulk](#bulk)
|
|
105
|
+
* [execute](#execute-procedure-callback)
|
|
106
|
+
* [input](#input-name-type-value)
|
|
107
|
+
* [output](#output-name-type-value)
|
|
108
|
+
* [pipe](#pipe-stream)
|
|
109
|
+
* [query](#query-command-callback)
|
|
110
|
+
* [batch](#batch-batch-callback)
|
|
111
|
+
* [bulk](#bulk-table-callback)
|
|
103
112
|
* [cancel](#cancel)
|
|
104
113
|
|
|
105
114
|
### Transactions
|
|
106
115
|
|
|
107
116
|
* [Transaction](#transaction)
|
|
108
|
-
* [begin](#begin)
|
|
109
|
-
* [commit](#commit)
|
|
110
|
-
* [rollback](#rollback)
|
|
117
|
+
* [begin](#begin-isolationlevel-callback)
|
|
118
|
+
* [commit](#commit-callback)
|
|
119
|
+
* [rollback](#rollback-callback)
|
|
111
120
|
|
|
112
121
|
### Prepared Statements
|
|
113
122
|
|
|
114
123
|
* [PreparedStatement](#prepared-statement)
|
|
115
|
-
* [input](#
|
|
116
|
-
* [output](#
|
|
117
|
-
* [prepare](#prepare)
|
|
118
|
-
* [execute](#
|
|
119
|
-
* [unprepare](#unprepare)
|
|
124
|
+
* [input](#input-name-type)
|
|
125
|
+
* [output](#output-name-type)
|
|
126
|
+
* [prepare](#prepare-statement-callback)
|
|
127
|
+
* [execute](#execute-values-callback)
|
|
128
|
+
* [unprepare](#unprepare-callback)
|
|
120
129
|
|
|
121
130
|
### Other
|
|
122
131
|
|
|
123
132
|
* [CLI](#cli)
|
|
124
|
-
* [Geography and Geometry](#geography)
|
|
125
|
-
* [Table-Valued Parameter](#tvp)
|
|
133
|
+
* [Geography and Geometry](#geography-and-geometry)
|
|
134
|
+
* [Table-Valued Parameter](#table-valued-parameter-tvp)
|
|
126
135
|
* [Affected Rows](#affected-rows)
|
|
127
|
-
* [JSON support](#json)
|
|
136
|
+
* [JSON support](#json-support)
|
|
128
137
|
* [Errors](#errors)
|
|
129
|
-
* [
|
|
138
|
+
* [Informational messages](#informational-messages)
|
|
139
|
+
* [Metadata](#metadata)
|
|
130
140
|
* [Data Types](#data-types)
|
|
131
|
-
* [SQL injection](#injection)
|
|
132
|
-
* [Verbose Mode](#verbose)
|
|
133
|
-
* [Known Issues](#issues)
|
|
141
|
+
* [SQL injection](#sql-injection)
|
|
142
|
+
* [Verbose Mode](#verbose-mode)
|
|
143
|
+
* [Known Issues](#known-issues)
|
|
134
144
|
* [Contributing](https://github.com/patriksimek/node-mssql/wiki/Contributing)
|
|
135
145
|
|
|
136
146
|
## Examples
|
|
137
147
|
|
|
138
|
-
<a name="promises" />
|
|
139
148
|
### Promises
|
|
140
149
|
|
|
141
150
|
```javascript
|
|
@@ -155,8 +164,9 @@ var config = {
|
|
|
155
164
|
sql.connect(config).then(function() {
|
|
156
165
|
// Query
|
|
157
166
|
|
|
158
|
-
|
|
159
|
-
|
|
167
|
+
new sql.Request();
|
|
168
|
+
.input('input_parameter', sql.Int, value);
|
|
169
|
+
.query('select * from mytable where id = @input_parameter').then(function(recordset) {
|
|
160
170
|
console.dir(recordset);
|
|
161
171
|
}).catch(function(err) {
|
|
162
172
|
// ... error checks
|
|
@@ -164,10 +174,10 @@ sql.connect(config).then(function() {
|
|
|
164
174
|
|
|
165
175
|
// Stored Procedure
|
|
166
176
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
177
|
+
new sql.Request();
|
|
178
|
+
.input('input_parameter', sql.Int, value);
|
|
179
|
+
.output('output_parameter', sql.VarChar(50));
|
|
180
|
+
.execute('procedure_name').then(function(recordsets) {
|
|
171
181
|
console.dir(recordsets);
|
|
172
182
|
}).catch(function(err) {
|
|
173
183
|
// ... error checks
|
|
@@ -179,7 +189,22 @@ sql.connect(config).then(function() {
|
|
|
179
189
|
|
|
180
190
|
Native Promise is used by default. You can easily change this with `sql.Promise = require('myownpromisepackage')`.
|
|
181
191
|
|
|
182
|
-
|
|
192
|
+
**ES6 Tagged template literals (experimental)**
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
sql.connect(config).then(function() {
|
|
196
|
+
sql.query`select * from mytable where id = ${value}`.then(function(recordset) {
|
|
197
|
+
console.dir(recordset);
|
|
198
|
+
}).catch(function(err) {
|
|
199
|
+
// ... error checks
|
|
200
|
+
});
|
|
201
|
+
}).catch(function(err) {
|
|
202
|
+
// ... error checks
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
All values are automatically sanitized against sql injection.
|
|
207
|
+
|
|
183
208
|
### Nested callbacks
|
|
184
209
|
|
|
185
210
|
```javascript
|
|
@@ -201,8 +226,7 @@ sql.connect(config, function(err) {
|
|
|
201
226
|
|
|
202
227
|
// Query
|
|
203
228
|
|
|
204
|
-
|
|
205
|
-
request.query('select 1 as number', function(err, recordset) {
|
|
229
|
+
new sql.Request().query('select 1 as number', function(err, recordset) {
|
|
206
230
|
// ... error checks
|
|
207
231
|
|
|
208
232
|
console.dir(recordset);
|
|
@@ -210,10 +234,10 @@ sql.connect(config, function(err) {
|
|
|
210
234
|
|
|
211
235
|
// Stored Procedure
|
|
212
236
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
237
|
+
new sql.Request()
|
|
238
|
+
.input('input_parameter', sql.Int, value)
|
|
239
|
+
.output('output_parameter', sql.VarChar(50))
|
|
240
|
+
.execute('procedure_name', function(err, recordsets, returnValue) {
|
|
217
241
|
// ... error checks
|
|
218
242
|
|
|
219
243
|
console.dir(recordsets);
|
|
@@ -225,7 +249,6 @@ sql.on('error', function(err) {
|
|
|
225
249
|
});
|
|
226
250
|
```
|
|
227
251
|
|
|
228
|
-
<a name="streaming" />
|
|
229
252
|
### Streaming
|
|
230
253
|
|
|
231
254
|
If you plan to work with large amount of rows, you should always use streaming. Once you enable this, you must listen for events to receive data.
|
|
@@ -274,7 +297,6 @@ sql.on('error', function(err) {
|
|
|
274
297
|
});
|
|
275
298
|
```
|
|
276
299
|
|
|
277
|
-
<a name="multiple-connections" />
|
|
278
300
|
## Multiple Connections
|
|
279
301
|
|
|
280
302
|
```javascript
|
|
@@ -329,7 +351,22 @@ connection2.on('error', function(err) {
|
|
|
329
351
|
});
|
|
330
352
|
```
|
|
331
353
|
|
|
332
|
-
|
|
354
|
+
**ES6 Tagged template literals (experimental)**
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
new sql.Connection(config).connect().then(function(conn) {
|
|
358
|
+
conn.query`select * from mytable where id = ${value}`.then(function(recordset) {
|
|
359
|
+
console.dir(recordset);
|
|
360
|
+
}).catch(function(err) {
|
|
361
|
+
// ... error checks
|
|
362
|
+
});
|
|
363
|
+
}).catch(function(err) {
|
|
364
|
+
// ... error checks
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
All values are automatically sanitized against sql injection.
|
|
369
|
+
|
|
333
370
|
## Configuration
|
|
334
371
|
|
|
335
372
|
```javascript
|
|
@@ -346,7 +383,6 @@ var config = {
|
|
|
346
383
|
}
|
|
347
384
|
```
|
|
348
385
|
|
|
349
|
-
<a name="cfg-general" />
|
|
350
386
|
### General (same for all drivers)
|
|
351
387
|
|
|
352
388
|
- **driver** - Driver to use (default: `tedious`). Possible values: `tedious`, `msnodesqlv8` or `msnodesql` or `tds`.
|
|
@@ -359,12 +395,11 @@ var config = {
|
|
|
359
395
|
- **connectionTimeout** - Connection timeout in ms (default: `15000`).
|
|
360
396
|
- **requestTimeout** - Request timeout in ms (default: `15000`).
|
|
361
397
|
- **stream** - Stream recordsets/rows instead of returning them all at once as an argument of callback (default: `false`). You can also enable streaming for each request independently (`request.stream = true`). Always set to `true` if you plan to work with large amount of rows.
|
|
362
|
-
- **parseJSON** - Parse JSON recordsets to JS objects (default: `false`). For more information please see section [JSON support](#json).
|
|
398
|
+
- **parseJSON** - Parse JSON recordsets to JS objects (default: `false`). For more information please see section [JSON support](#json-support).
|
|
363
399
|
- **pool.max** - The maximum number of connections there can be in the pool (default: `10`).
|
|
364
400
|
- **pool.min** - The minimum of connections there can be in the pool (default: `0`).
|
|
365
401
|
- **pool.idleTimeoutMillis** - The Number of milliseconds before closing an unused connection (default: `30000`).
|
|
366
402
|
|
|
367
|
-
<a name="cfg-formats" />
|
|
368
403
|
### Formats
|
|
369
404
|
|
|
370
405
|
In addition to configuration object there is an option to pass config as a connection string. Two formats of connection string are supported.
|
|
@@ -389,7 +424,6 @@ __Version__
|
|
|
389
424
|
|
|
390
425
|
## Drivers
|
|
391
426
|
|
|
392
|
-
<a name="cfg-tedious" />
|
|
393
427
|
### Tedious
|
|
394
428
|
|
|
395
429
|
Default driver, actively maintained and production ready. Platform independent, runs everywhere Node.js runs.
|
|
@@ -405,7 +439,6 @@ Default driver, actively maintained and production ready. Platform independent,
|
|
|
405
439
|
|
|
406
440
|
More information about Tedious specific options: http://pekim.github.io/tedious/api-connection.html
|
|
407
441
|
|
|
408
|
-
<a name="cfg-msnodesqlv8" />
|
|
409
442
|
### Microsoft / Contributors Node V8 Driver for Node.js for SQL Server
|
|
410
443
|
|
|
411
444
|
**Requires Node.js 0.12.x/4.2.0. Windows only.** This driver is not part of the default package and must be installed separately by `npm install msnodesqlv8`.
|
|
@@ -427,7 +460,6 @@ Default connection string when connecting to named instance:
|
|
|
427
460
|
Driver={SQL Server Native Client 11.0};Server={#{server}\\#{instance}};Database={#{database}};Uid={#{user}};Pwd={#{password}};Trusted_Connection={#{trusted}};
|
|
428
461
|
```
|
|
429
462
|
|
|
430
|
-
<a name="cfg-msnodesql" />
|
|
431
463
|
### Microsoft Driver for Node.js for SQL Server
|
|
432
464
|
|
|
433
465
|
**Requires Node.js 0.6.x/0.8.x/0.10.x. Windows only.** This driver is not part of the default package and must be installed separately by `npm install msnodesql`. If you are looking for compiled binaries, see [node-sqlserver-binary](https://github.com/jorgeazevedo/node-sqlserver-unofficial).
|
|
@@ -449,15 +481,13 @@ Default connection string when connecting to named instance:
|
|
|
449
481
|
Driver={SQL Server Native Client 11.0};Server={#{server}\\#{instance}};Database={#{database}};Uid={#{user}};Pwd={#{password}};Trusted_Connection={#{trusted}};
|
|
450
482
|
```
|
|
451
483
|
|
|
452
|
-
<a name="cfg-node-tds" />
|
|
453
484
|
### node-tds
|
|
454
485
|
|
|
455
486
|
**Legacy support, don't use this driver for new projects.** This driver is not part of the default package and must be installed separately by `npm install tds`.
|
|
456
487
|
|
|
457
488
|
_node-mssql updates this driver with extra features and bug fixes by overriding some of its internal functions. If you want to disable this, require module with `var sql = require('mssql/nofix')`._
|
|
458
489
|
|
|
459
|
-
|
|
460
|
-
## Connections
|
|
490
|
+
## Connection
|
|
461
491
|
|
|
462
492
|
Internally, each `Connection` instance is a separate pool of TDS connections. Once you create a new `Request`/`Transaction`/`Prepared Statement`, a new TDS connection is acquired from the pool and reserved for desired action. Once the action is complete, connection is released back to the pool. Connection health check is built-in so once the dead connection is discovered, it is immediately replaced with a new one.
|
|
463
493
|
|
|
@@ -478,8 +508,7 @@ __Errors__
|
|
|
478
508
|
|
|
479
509
|
---------------------------------------
|
|
480
510
|
|
|
481
|
-
|
|
482
|
-
### connect([callback])
|
|
511
|
+
### connect ([callback])
|
|
483
512
|
|
|
484
513
|
Create a new connection pool with one active connection. This one initial connection serves as a probe to find out whether the configuration is valid.
|
|
485
514
|
|
|
@@ -512,7 +541,6 @@ __Errors__
|
|
|
512
541
|
|
|
513
542
|
---------------------------------------
|
|
514
543
|
|
|
515
|
-
<a name="close" />
|
|
516
544
|
### close()
|
|
517
545
|
|
|
518
546
|
Close all active connections in the pool.
|
|
@@ -523,8 +551,7 @@ __Example__
|
|
|
523
551
|
connection.close();
|
|
524
552
|
```
|
|
525
553
|
|
|
526
|
-
|
|
527
|
-
## Requests
|
|
554
|
+
## Request
|
|
528
555
|
|
|
529
556
|
```javascript
|
|
530
557
|
var request = new sql.Request(/* [connection] */);
|
|
@@ -538,11 +565,11 @@ If you omit connection argument, global connection is used instead.
|
|
|
538
565
|
- **row(row)** - Dispatched when new row is parsed.
|
|
539
566
|
- **done(returnValue)** - Dispatched when request is complete.
|
|
540
567
|
- **error(err)** - Dispatched on error.
|
|
568
|
+
- **info(message)** - Dispatched on informational message.
|
|
541
569
|
|
|
542
570
|
---------------------------------------
|
|
543
571
|
|
|
544
|
-
|
|
545
|
-
### execute(procedure, [callback])
|
|
572
|
+
### execute (procedure, [callback])
|
|
546
573
|
|
|
547
574
|
Call a stored procedure.
|
|
548
575
|
|
|
@@ -585,8 +612,7 @@ __Errors__
|
|
|
585
612
|
|
|
586
613
|
---------------------------------------
|
|
587
614
|
|
|
588
|
-
|
|
589
|
-
### input(name, [type], value)
|
|
615
|
+
### input (name, [type], value)
|
|
590
616
|
|
|
591
617
|
Add an input parameter to the request.
|
|
592
618
|
|
|
@@ -632,8 +658,7 @@ __Errors__ (synchronous)
|
|
|
632
658
|
|
|
633
659
|
---------------------------------------
|
|
634
660
|
|
|
635
|
-
|
|
636
|
-
### output(name, type, [value])
|
|
661
|
+
### output (name, type, [value])
|
|
637
662
|
|
|
638
663
|
Add an output parameter to the request.
|
|
639
664
|
|
|
@@ -656,8 +681,7 @@ __Errors__ (synchronous)
|
|
|
656
681
|
|
|
657
682
|
---------------------------------------
|
|
658
683
|
|
|
659
|
-
|
|
660
|
-
### pipe(stream)
|
|
684
|
+
### pipe (stream)
|
|
661
685
|
|
|
662
686
|
Sets request to `stream` mode and pulls all rows from all recordsets to a given stream.
|
|
663
687
|
|
|
@@ -685,10 +709,9 @@ __Version__
|
|
|
685
709
|
|
|
686
710
|
---------------------------------------
|
|
687
711
|
|
|
688
|
-
|
|
689
|
-
### query(command, [callback])
|
|
712
|
+
### query (command, [callback])
|
|
690
713
|
|
|
691
|
-
Execute the SQL command. To execute commands like `create procedure` or if you plan to work with local temporary tables, use [batch](#batch) instead.
|
|
714
|
+
Execute the SQL command. To execute commands like `create procedure` or if you plan to work with local temporary tables, use [batch](#batch-batch-callback) instead.
|
|
692
715
|
|
|
693
716
|
__Arguments__
|
|
694
717
|
|
|
@@ -736,10 +759,9 @@ request.query('select 1 as number; select 2 as number', function(err, recordsets
|
|
|
736
759
|
|
|
737
760
|
---------------------------------------
|
|
738
761
|
|
|
739
|
-
|
|
740
|
-
### batch(batch, [callback])
|
|
762
|
+
### batch (batch, [callback])
|
|
741
763
|
|
|
742
|
-
Execute the SQL command. Unlike [query](#query), it doesn't use `sp_executesql`, so is not likely that SQL Server will reuse the execution plan it generates for the SQL. Use this only in special cases, for example when you need to execute commands like `create procedure` which can't be executed with [query](#query) or if you're executing statements longer than 4000 chars on SQL Server 2000. Also you should use this if you're plan to work with local temporary tables ([more information here](http://weblogs.sqlteam.com/mladenp/archive/2006/11/03/17197.aspx)).
|
|
764
|
+
Execute the SQL command. Unlike [query](#query-command-callback), it doesn't use `sp_executesql`, so is not likely that SQL Server will reuse the execution plan it generates for the SQL. Use this only in special cases, for example when you need to execute commands like `create procedure` which can't be executed with [query](#query-command-callback) or if you're executing statements longer than 4000 chars on SQL Server 2000. Also you should use this if you're plan to work with local temporary tables ([more information here](http://weblogs.sqlteam.com/mladenp/archive/2006/11/03/17197.aspx)).
|
|
743
765
|
|
|
744
766
|
NOTE: Table-Valued Parameter (TVP) is not supported in batch.
|
|
745
767
|
|
|
@@ -771,7 +793,6 @@ You can enable multiple recordsets in queries with the `request.multiple = true`
|
|
|
771
793
|
|
|
772
794
|
---------------------------------------
|
|
773
795
|
|
|
774
|
-
<a name="bulk" />
|
|
775
796
|
### bulk(table, [callback])
|
|
776
797
|
|
|
777
798
|
Perform a bulk insert.
|
|
@@ -815,7 +836,6 @@ __Errors__
|
|
|
815
836
|
|
|
816
837
|
---------------------------------------
|
|
817
838
|
|
|
818
|
-
<a name="cancel" />
|
|
819
839
|
### cancel()
|
|
820
840
|
|
|
821
841
|
Cancel currently executing request. Return `true` if cancellation packet was send successfully.
|
|
@@ -835,8 +855,7 @@ request.query('waitfor delay \'00:00:05\'; select 1 as number', function(err, re
|
|
|
835
855
|
request.cancel();
|
|
836
856
|
```
|
|
837
857
|
|
|
838
|
-
|
|
839
|
-
## Transactions
|
|
858
|
+
## Transaction
|
|
840
859
|
|
|
841
860
|
**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. Transaction also contains a queue to make sure your requests are executed in series. After you call `commit` or `rollback`, connection is then released back to the connection pool.
|
|
842
861
|
|
|
@@ -912,8 +931,7 @@ transaction.begin(function(err) {
|
|
|
912
931
|
|
|
913
932
|
---------------------------------------
|
|
914
933
|
|
|
915
|
-
|
|
916
|
-
### begin([isolationLevel], [callback])
|
|
934
|
+
### begin ([isolationLevel], [callback])
|
|
917
935
|
|
|
918
936
|
Begin a transaction.
|
|
919
937
|
|
|
@@ -937,8 +955,7 @@ __Errors__
|
|
|
937
955
|
|
|
938
956
|
---------------------------------------
|
|
939
957
|
|
|
940
|
-
|
|
941
|
-
### commit([callback])
|
|
958
|
+
### commit ([callback])
|
|
942
959
|
|
|
943
960
|
Commit a transaction.
|
|
944
961
|
|
|
@@ -965,8 +982,7 @@ __Errors__
|
|
|
965
982
|
|
|
966
983
|
---------------------------------------
|
|
967
984
|
|
|
968
|
-
|
|
969
|
-
### rollback([callback])
|
|
985
|
+
### rollback ([callback])
|
|
970
986
|
|
|
971
987
|
Rollback a transaction. If the queue isn't empty, all queued requests will be Cancelled and the transaction will be marked as aborted.
|
|
972
988
|
|
|
@@ -991,8 +1007,7 @@ __Errors__
|
|
|
991
1007
|
- ENOTBEGUN (`TransactionError`) - Transaction has not begun.
|
|
992
1008
|
- EREQINPROG (`TransactionError`) - Can't rollback transaction. There is a request in progress.
|
|
993
1009
|
|
|
994
|
-
|
|
995
|
-
## PreparedStatement
|
|
1010
|
+
## Prepared Statement
|
|
996
1011
|
|
|
997
1012
|
**IMPORTANT:** always use `PreparedStatement` class to create prepared statements - it ensures that all your executions of prepared statement are executed on one connection. Once you call `prepare`, a single connection is acquired from the connection pool and all subsequent executions are executed exclusively on this connection. Prepared Statement also contains a queue to make sure your executions are executed in series. After you call `unprepare`, the connection is then released back to the connection pool.
|
|
998
1013
|
|
|
@@ -1027,8 +1042,7 @@ ps.prepare('select @param as value', function(err) {
|
|
|
1027
1042
|
|
|
1028
1043
|
---------------------------------------
|
|
1029
1044
|
|
|
1030
|
-
|
|
1031
|
-
### input(name, type)
|
|
1045
|
+
### input (name, type)
|
|
1032
1046
|
|
|
1033
1047
|
Add an input parameter to the prepared statement.
|
|
1034
1048
|
|
|
@@ -1050,8 +1064,7 @@ __Errors__ (synchronous)
|
|
|
1050
1064
|
|
|
1051
1065
|
---------------------------------------
|
|
1052
1066
|
|
|
1053
|
-
|
|
1054
|
-
### output(name, type)
|
|
1067
|
+
### output (name, type)
|
|
1055
1068
|
|
|
1056
1069
|
Add an output parameter to the prepared statement.
|
|
1057
1070
|
|
|
@@ -1073,8 +1086,7 @@ __Errors__ (synchronous)
|
|
|
1073
1086
|
|
|
1074
1087
|
---------------------------------------
|
|
1075
1088
|
|
|
1076
|
-
|
|
1077
|
-
### prepare(statement, [callback])
|
|
1089
|
+
### prepare (statement, [callback])
|
|
1078
1090
|
|
|
1079
1091
|
Prepare a statement.
|
|
1080
1092
|
|
|
@@ -1099,8 +1111,7 @@ __Errors__
|
|
|
1099
1111
|
|
|
1100
1112
|
---------------------------------------
|
|
1101
1113
|
|
|
1102
|
-
|
|
1103
|
-
### execute(values, [callback])
|
|
1114
|
+
### execute (values, [callback])
|
|
1104
1115
|
|
|
1105
1116
|
Execute a prepared statement.
|
|
1106
1117
|
|
|
@@ -1189,6 +1200,8 @@ ps.prepare('select @param as value', function(err) {
|
|
|
1189
1200
|
|
|
1190
1201
|
**TIP**: To learn more about how number of affected rows works, see section [Affected Rows](#affected-rows).
|
|
1191
1202
|
|
|
1203
|
+
**TIP**: To access number of affected rows when using Prepared Statement with Promises, use `ps.lastRequest.affectedRows`.
|
|
1204
|
+
|
|
1192
1205
|
__Errors__
|
|
1193
1206
|
- ENOTPREPARED (`PreparedStatementError`) - Statement is not prepared.
|
|
1194
1207
|
- ETIMEOUT (`RequestError`) - Request timeout.
|
|
@@ -1197,8 +1210,7 @@ __Errors__
|
|
|
1197
1210
|
|
|
1198
1211
|
---------------------------------------
|
|
1199
1212
|
|
|
1200
|
-
|
|
1201
|
-
### unprepare([callback])
|
|
1213
|
+
### unprepare ([callback])
|
|
1202
1214
|
|
|
1203
1215
|
Unprepare a prepared statement.
|
|
1204
1216
|
|
|
@@ -1224,7 +1236,6 @@ ps.prepare('select @param as value', function(err, recordsets) {
|
|
|
1224
1236
|
__Errors__
|
|
1225
1237
|
- ENOTPREPARED (`PreparedStatementError`) - Statement is not prepared.
|
|
1226
1238
|
|
|
1227
|
-
<a name="cli" />
|
|
1228
1239
|
## CLI
|
|
1229
1240
|
|
|
1230
1241
|
Before you can start using CLI, you must install `mssql` globally with `npm install mssql -g`. Once you do that you will be able to execute `mssql` command.
|
|
@@ -1268,7 +1279,6 @@ __Version__
|
|
|
1268
1279
|
|
|
1269
1280
|
2.0
|
|
1270
1281
|
|
|
1271
|
-
<a name="geography" />
|
|
1272
1282
|
## Geography and Geometry
|
|
1273
1283
|
|
|
1274
1284
|
node-mssql has built-in serializer for Geography and Geometry CLR data types.
|
|
@@ -1299,7 +1309,6 @@ Results in:
|
|
|
1299
1309
|
segments: [] }
|
|
1300
1310
|
```
|
|
1301
1311
|
|
|
1302
|
-
<a name="tvp" />
|
|
1303
1312
|
## Table-Valued Parameter (TVP)
|
|
1304
1313
|
|
|
1305
1314
|
Supported on SQL Server 2008 and later. You can pass a data table as a parameter to stored procedure. First, we have to create custom type in our database.
|
|
@@ -1341,7 +1350,6 @@ request.execute('MyCustomStoredProcedure', function(err, recordsets, returnValue
|
|
|
1341
1350
|
|
|
1342
1351
|
**TIP**: You can also create Table variable from any recordset with `recordset.toTable()`.
|
|
1343
1352
|
|
|
1344
|
-
<a name="affected-rows">
|
|
1345
1353
|
## Affected Rows
|
|
1346
1354
|
|
|
1347
1355
|
If you're performing `INSERT`, `UPDATE` or `DELETE` in a query, you can read number of affected rows.
|
|
@@ -1381,8 +1389,7 @@ __Version__
|
|
|
1381
1389
|
|
|
1382
1390
|
3.0
|
|
1383
1391
|
|
|
1384
|
-
|
|
1385
|
-
## JSON support (experimental, works only with Tedious driver)
|
|
1392
|
+
## JSON support
|
|
1386
1393
|
|
|
1387
1394
|
SQL Server 2016 introduced built-in JSON serialization. By default, JSON is returned as a plain text in a special column named `JSON_F52E2B61-18A1-11d1-B105-00805F49916B`.
|
|
1388
1395
|
|
|
@@ -1414,7 +1421,6 @@ __Version__
|
|
|
1414
1421
|
|
|
1415
1422
|
2.3
|
|
1416
1423
|
|
|
1417
|
-
<a name="errors" />
|
|
1418
1424
|
## Errors
|
|
1419
1425
|
|
|
1420
1426
|
There are 4 types of errors you can handle:
|
|
@@ -1463,13 +1469,40 @@ Name | Code | Message
|
|
|
1463
1469
|
SQL errors (`RequestError` with `err.code` equal to `EREQUEST`) contains additional details.
|
|
1464
1470
|
|
|
1465
1471
|
- **err.number** - The error number.
|
|
1466
|
-
- **err.state** - The error state, used as a modifier to the
|
|
1472
|
+
- **err.state** - The error state, used as a modifier to the number.
|
|
1467
1473
|
- **err.class** - The class (severity) of the error. A class of less than 10 indicates an informational message. Detailed explanation can be found [here](https://msdn.microsoft.com/en-us/library/dd304156.aspx).
|
|
1468
1474
|
- **err.lineNumber** - The line number in the SQL batch or stored procedure that caused the error. Line numbers begin at 1; therefore, if the line number is not applicable to the message, the value of LineNumber will be 0.
|
|
1469
1475
|
- **err.serverName** - The server name.
|
|
1470
1476
|
- **err.procName** - The stored procedure name.
|
|
1471
1477
|
|
|
1472
|
-
|
|
1478
|
+
## Informational messages
|
|
1479
|
+
|
|
1480
|
+
To receive informational messages generated by `PRINT` or `RAISERROR` commands use:
|
|
1481
|
+
|
|
1482
|
+
```javascript
|
|
1483
|
+
var request = new sql.Request();
|
|
1484
|
+
request.on('info', function(info) {
|
|
1485
|
+
console.dir(info);
|
|
1486
|
+
});
|
|
1487
|
+
request.query('print \'Hello world.\';', function(err, recordset) {
|
|
1488
|
+
// ...
|
|
1489
|
+
});
|
|
1490
|
+
```
|
|
1491
|
+
|
|
1492
|
+
Structure of informational message:
|
|
1493
|
+
|
|
1494
|
+
- **info.message** - Message.
|
|
1495
|
+
- **info.number** - The message number.
|
|
1496
|
+
- **info.state** - The message state, used as a modifier to the number.
|
|
1497
|
+
- **info.class** - The class (severity) of the message. Equal or lower than 10. Detailed explanation can be found [here](https://msdn.microsoft.com/en-us/library/dd304156.aspx).
|
|
1498
|
+
- **info.lineNumber** - The line number in the SQL batch or stored procedure that generated the message. Line numbers begin at 1; therefore, if the line number is not applicable to the message, the value of LineNumber will be 0.
|
|
1499
|
+
- **info.serverName** - The server name.
|
|
1500
|
+
- **info.procName** - The stored procedure name.
|
|
1501
|
+
|
|
1502
|
+
__Version__
|
|
1503
|
+
|
|
1504
|
+
3.3
|
|
1505
|
+
|
|
1473
1506
|
## Metadata
|
|
1474
1507
|
|
|
1475
1508
|
Recordset metadata are accessible through the `recordset.columns` property.
|
|
@@ -1513,7 +1546,6 @@ Columns structure for example above:
|
|
|
1513
1546
|
}
|
|
1514
1547
|
```
|
|
1515
1548
|
|
|
1516
|
-
<a name="data-types" />
|
|
1517
1549
|
## Data Types
|
|
1518
1550
|
|
|
1519
1551
|
You can define data types with length/precision/scale:
|
|
@@ -1578,7 +1610,6 @@ sql.Geometry
|
|
|
1578
1610
|
|
|
1579
1611
|
To setup MAX length for `VarChar`, `NVarChar` and `VarBinary` use `sql.MAX` length. Types `sql.XML` and `sql.Variant` are not supported as input parameters.
|
|
1580
1612
|
|
|
1581
|
-
<a name="injection" />
|
|
1582
1613
|
## SQL injection
|
|
1583
1614
|
|
|
1584
1615
|
This module has built-in SQL injection protection. Always use parameters to pass sanitized values to your queries.
|
|
@@ -1591,7 +1622,6 @@ request.query('select @myval as myval', function(err, recordset) {
|
|
|
1591
1622
|
});
|
|
1592
1623
|
```
|
|
1593
1624
|
|
|
1594
|
-
<a name="verbose" />
|
|
1595
1625
|
## Verbose Mode
|
|
1596
1626
|
|
|
1597
1627
|
You can enable verbose mode by `request.verbose = true` command.
|
|
@@ -1626,35 +1656,36 @@ Output for the example above could look similar to this.
|
|
|
1626
1656
|
---------- completed ----------
|
|
1627
1657
|
```
|
|
1628
1658
|
|
|
1629
|
-
<a name="issues" />
|
|
1630
1659
|
## Known issues
|
|
1631
1660
|
|
|
1632
1661
|
### Tedious
|
|
1633
1662
|
|
|
1634
1663
|
- If you're facing problems with connecting SQL Server 2000, try setting the default TDS version to 7.1 with `config.options.tdsVersion = '7_1'` ([issue](https://github.com/patriksimek/node-mssql/issues/36))
|
|
1635
|
-
- If you're executing a statement longer than 4000 chars on SQL Server 2000, always use [batch](#batch) instead of [query](#query) ([issue](https://github.com/patriksimek/node-mssql/issues/68))
|
|
1664
|
+
- If you're executing a statement longer than 4000 chars on SQL Server 2000, always use [batch](#batch-batch-callback) instead of [query](#query-command-callback) ([issue](https://github.com/patriksimek/node-mssql/issues/68))
|
|
1636
1665
|
|
|
1637
1666
|
### msnodesqlv8
|
|
1638
1667
|
|
|
1639
1668
|
- msnodesqlv8 has problem with errors during transactions - [reported](https://github.com/patriksimek/node-mssql/issues/77).
|
|
1640
1669
|
- msnodesqlv8 doesn't timeout the connection reliably - [reported](https://github.com/TimelordUK/node-sqlserver-v8/issues/9).
|
|
1641
|
-
- msnodesqlv8 doesn't support [TVP](#tvp) data type.
|
|
1670
|
+
- msnodesqlv8 doesn't support [TVP](#table-valued-parameter-tvp) data type.
|
|
1642
1671
|
- msnodesqlv8 doesn't support Variant data type.
|
|
1643
1672
|
- msnodesqlv8 doesn't support request timeout.
|
|
1644
1673
|
- msnodesqlv8 doesn't support request cancellation.
|
|
1645
1674
|
- msnodesqlv8 doesn't support [detailed SQL errors](#detailed-sql-errors).
|
|
1675
|
+
- msnodesqlv8 doesn't support [Informational messages](#informational-messages).
|
|
1646
1676
|
|
|
1647
1677
|
### msnodesql
|
|
1648
1678
|
|
|
1649
1679
|
- msnodesql has problem with errors during transactions - [reported](https://github.com/patriksimek/node-mssql/issues/77).
|
|
1650
1680
|
- msnodesql contains bug in DateTimeOffset ([reported](https://github.com/Azure/node-sqlserver/issues/160))
|
|
1651
|
-
- msnodesql doesn't support [Bulk](#bulk) load.
|
|
1652
|
-
- msnodesql doesn't support [TVP](#tvp) data type.
|
|
1681
|
+
- msnodesql doesn't support [Bulk](#bulk-table-callback) load.
|
|
1682
|
+
- msnodesql doesn't support [TVP](#table-valued-parameter-tvp) data type.
|
|
1653
1683
|
- msnodesql doesn't support Variant data type.
|
|
1654
1684
|
- msnodesql doesn't support connection timeout.
|
|
1655
1685
|
- msnodesql doesn't support request timeout.
|
|
1656
1686
|
- msnodesql doesn't support request cancellation.
|
|
1657
1687
|
- msnodesql doesn't support [detailed SQL errors](#detailed-sql-errors).
|
|
1688
|
+
- msnodesql doesn't support [Informational messages](#informational-messages).
|
|
1658
1689
|
- msnodesql reports invalid number of affected rows in some cases.
|
|
1659
1690
|
|
|
1660
1691
|
### node-tds
|
|
@@ -1667,20 +1698,19 @@ Output for the example above could look similar to this.
|
|
|
1667
1698
|
- node-tds doesn't support Binary, VarBinary and Image as parameters.
|
|
1668
1699
|
- node-tds always return date/time values in local time.
|
|
1669
1700
|
- node-tds has serious problems with MAX types.
|
|
1670
|
-
- node-tds doesn't support [Bulk](#bulk) load.
|
|
1671
|
-
- node-tds doesn't support [TVP](#tvp) data type.
|
|
1701
|
+
- node-tds doesn't support [Bulk](#bulk-table-callback) load.
|
|
1702
|
+
- node-tds doesn't support [TVP](#table-valued-parameter-tvp) data type.
|
|
1672
1703
|
- node-tds doesn't support Variant data type.
|
|
1673
1704
|
- node-tds doesn't support request timeout.
|
|
1674
|
-
- node-tds doesn't support [built-in JSON serialization](#json) introduced in SQL Server 2016.
|
|
1705
|
+
- node-tds doesn't support [built-in JSON serialization](#json-support) introduced in SQL Server 2016.
|
|
1675
1706
|
- node-tds doesn't support [detailed SQL errors](#detailed-sql-errors).
|
|
1676
1707
|
- node-tds doesn't support [Affected Rows](#affected-rows)
|
|
1677
1708
|
|
|
1678
|
-
<a name="twotothree" />
|
|
1679
1709
|
## 2.x to 3.x changes
|
|
1680
1710
|
|
|
1681
1711
|
### Prepared Statement
|
|
1682
1712
|
|
|
1683
|
-
* [`execute`](#
|
|
1713
|
+
* [`execute`](#execute-values-callback) method now returns 3 arguments instead of 2.
|
|
1684
1714
|
|
|
1685
1715
|
```javascript
|
|
1686
1716
|
ps.execute(values, function(err, recordset, affected) { });
|
|
@@ -1694,7 +1724,7 @@ Output for the example above could look similar to this.
|
|
|
1694
1724
|
|
|
1695
1725
|
### Request
|
|
1696
1726
|
|
|
1697
|
-
* [`execute`](#execute) method now returns 4 arguments instead of 3.
|
|
1727
|
+
* [`execute`](#execute-procedure-callback) method now returns 4 arguments instead of 3.
|
|
1698
1728
|
|
|
1699
1729
|
```javascript
|
|
1700
1730
|
ps.execute(values, function(err, recordset, returnValue, affected) { });
|
|
@@ -1706,7 +1736,7 @@ Output for the example above could look similar to this.
|
|
|
1706
1736
|
request.on('done', function(returnValue, affected) { });
|
|
1707
1737
|
```
|
|
1708
1738
|
|
|
1709
|
-
* [`query`](#query) method now returns 3 arguments instead of 2.
|
|
1739
|
+
* [`query`](#query-command-callback) method now returns 3 arguments instead of 2.
|
|
1710
1740
|
|
|
1711
1741
|
```javascript
|
|
1712
1742
|
ps.execute(values, function(err, recordset, affected) { });
|
|
@@ -1718,7 +1748,6 @@ Output for the example above could look similar to this.
|
|
|
1718
1748
|
request.on('done', function(affected) { });
|
|
1719
1749
|
```
|
|
1720
1750
|
|
|
1721
|
-
<a name="license" />
|
|
1722
1751
|
## License
|
|
1723
1752
|
|
|
1724
1753
|
Copyright (c) 2013-2016 Patrik Simek
|