|
| 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 |
0 commit comments