connection-pool.js 2.7 KB

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