@@ -7,48 +7,31 @@ const { IDS, INCREMENT } = require('../utils')
77const shared = require ( '../shared' )
88const ConnectionError = require ( '../error/connection-error' )
99const { platform } = require ( 'os' )
10+ const { buildConnectionString } = require ( '@tediousjs/connection-string' )
1011
1112const CONNECTION_DRIVER = [ 'darwin' , 'linux' ] . includes ( platform ( ) ) ? 'ODBC Driver 17 for SQL Server' : 'SQL Server Native Client 11.0'
12- const CONNECTION_STRING_PORT = `Driver=${ CONNECTION_DRIVER } ;Server=#{server},#{port};Database=#{database};Uid=#{user};Pwd=#{password};Trusted_Connection=#{trusted};Encrypt=#{encrypt};`
13- const CONNECTION_STRING_NAMED_INSTANCE = `Driver=${ CONNECTION_DRIVER } ;Server=#{server}\\#{instance};Database=#{database};Uid=#{user};Pwd=#{password};Trusted_Connection=#{trusted};Encrypt=#{encrypt};`
1413
1514class ConnectionPool extends BaseConnectionPool {
1615 _poolCreate ( ) {
1716 return new shared . Promise ( ( resolve , reject ) => {
18- let defaultConnectionString = CONNECTION_STRING_PORT
19-
20- if ( this . config . options . instanceName != null ) {
21- defaultConnectionString = CONNECTION_STRING_NAMED_INSTANCE
22- }
23-
2417 this . config . requestTimeout = this . config . requestTimeout ?? this . config . timeout ?? 15000
2518
2619 const cfg = {
27- conn_str : this . config . connectionString || defaultConnectionString ,
20+ conn_str : this . config . connectionString ,
2821 conn_timeout : ( this . config . connectionTimeout ?? this . config . timeout ?? 15000 ) / 1000
2922 }
3023
31- cfg . conn_str = cfg . conn_str . replace ( / # { ( [ ^ } ] * ) } / 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- let val = this . config [ key ] || ''
43- // quote strings that contain '{' or '}' but not ones that start and end with them (assume they are already quoted)
44- if ( val && typeof val === 'string' && ! ( val . startsWith ( '{' ) && val . endsWith ( '}' ) ) && ( val . indexOf ( '{' ) !== - 1 || val . indexOf ( '}' ) !== - 1 ) ) {
45- // quote values in `{}` and escape any existing ` }` chars
46- val = `{${ val . replace ( / } / g, '}}' ) } }`
47- }
48- return val
49- }
50- }
51- } )
24+ if ( ! this . config . connectionString ) {
25+ cfg . conn_str = buildConnectionString ( {
26+ Driver : CONNECTION_DRIVER ,
27+ Server : this . config . options . instanceName ? `${ this . config . server } \\${ this . config . instanceName } ` : `${ this . config . server } ,${ this . config . port } ` ,
28+ Database : this . config . database ,
29+ Uid : this . config . user ,
30+ Pwd : this . config . password ,
31+ Trusted_Connection : ! ! this . config . options . trustedConnection ,
32+ Encrypt : ! ! this . config . options . encrypt
33+ } )
34+ }
5235
5336 const connedtionId = INCREMENT . Connection ++
5437 debug ( 'pool(%d): connection #%d created' , IDS . get ( this ) , connedtionId )
0 commit comments