Skip to content

Commit 8a958f9

Browse files
committed
Split msnodesqlv8
1 parent c39c49f commit 8a958f9

File tree

5 files changed

+231
-208
lines changed

5 files changed

+231
-208
lines changed

lib/msnodesqlv8/connection-pool.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
3+
const msnodesql = require('msnodesqlv8')
4+
const debug = require('debug')('mssql:msv8')
5+
const BaseConnectionPool = require('../base/connection-pool')
6+
const { IDS, INCREMENT } = require('../utils')
7+
const shared = require('../shared')
8+
const ConnectionError = require('../error/connection-error')
9+
10+
const CONNECTION_STRING_PORT = 'Driver=SQL Server Native Client 11.0;Server=#{server},#{port};Database=#{database};Uid=#{user};Pwd=#{password};Trusted_Connection=#{trusted};Encrypt=#{encrypt};'
11+
const CONNECTION_STRING_NAMED_INSTANCE = 'Driver=SQL Server Native Client 11.0;Server=#{server}\\#{instance};Database=#{database};Uid=#{user};Pwd=#{password};Trusted_Connection=#{trusted};Encrypt=#{encrypt};'
12+
13+
class ConnectionPool extends BaseConnectionPool {
14+
_poolCreate () {
15+
return new shared.Promise((resolve, reject) => {
16+
let defaultConnectionString = CONNECTION_STRING_PORT
17+
18+
if (this.config.options.instanceName != null) {
19+
defaultConnectionString = CONNECTION_STRING_NAMED_INSTANCE
20+
}
21+
22+
if (this.config.requestTimeout == null) {
23+
this.config.requestTimeout = 15000
24+
}
25+
26+
const cfg = {
27+
conn_str: this.config.connectionString || defaultConnectionString,
28+
conn_timeout: (this.config.connectionTimeout || 15000) / 1000
29+
}
30+
31+
cfg.conn_str = cfg.conn_str.replace(new RegExp('#{([^}]*)}', 'g'), (p) => {
32+
const key = p.substr(2, p.length - 3)
33+
34+
switch (key) {
35+
case 'instance':
36+
return this.config.options.instanceName
37+
case 'trusted':
38+
return this.config.options.trustedConnection ? 'Yes' : 'No'
39+
case 'encrypt':
40+
return this.config.options.encrypt ? 'Yes' : 'No'
41+
default:
42+
return this.config[key] != null ? this.config[key] : ''
43+
}
44+
})
45+
46+
const connedtionId = INCREMENT.Connection++
47+
debug('pool(%d): connection #%d created', IDS.get(this), connedtionId)
48+
debug('connection(%d): establishing', connedtionId)
49+
50+
if (typeof this.config.beforeConnect === 'function') {
51+
this.config.beforeConnect(cfg)
52+
}
53+
54+
msnodesql.open(cfg, (err, tds) => {
55+
if (err) {
56+
err = new ConnectionError(err)
57+
return reject(err)
58+
}
59+
60+
IDS.add(tds, 'Connection', connedtionId)
61+
debug('connection(%d): established', IDS.get(tds))
62+
resolve(tds)
63+
})
64+
})
65+
}
66+
67+
_poolValidate (tds) {
68+
return tds && !tds.hasError
69+
}
70+
71+
_poolDestroy (tds) {
72+
return new shared.Promise((resolve, reject) => {
73+
if (!tds) {
74+
resolve()
75+
return
76+
}
77+
debug('connection(%d): destroying', IDS.get(tds))
78+
tds.close(() => {
79+
resolve()
80+
})
81+
debug('connection(%d): destroyed', IDS.get(tds))
82+
})
83+
}
84+
}
85+
86+
module.exports = ConnectionPool

lib/msnodesqlv8/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
const base = require('../base')
4+
const ConnectionPool = require('./connection-pool')
5+
const Transaction = require('./transaction')
6+
const Request = require('./request')
7+
8+
module.exports = Object.assign({
9+
ConnectionPool,
10+
Transaction,
11+
Request,
12+
PreparedStatement: base.PreparedStatement
13+
}, base.exports)
14+
15+
Object.defineProperty(module.exports, 'Promise', {
16+
enumerable: true,
17+
get: () => {
18+
return base.Promise
19+
},
20+
set: (value) => {
21+
base.Promise = value
22+
}
23+
})
24+
25+
base.driver.name = 'msnodesqlv8'
26+
base.driver.ConnectionPool = ConnectionPool
27+
base.driver.Transaction = Transaction
28+
base.driver.Request = Request

0 commit comments

Comments
 (0)