1
- // Copyright 2013 SAP AG.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http: //www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing,
10
- // software distributed under the License is distributed on an
11
- // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12
- // either express or implied. See the License for the specific
13
- // language governing permissions and limitations under the License.
14
- 'use strict' ;
15
-
16
- var State = require ( './ConnectionManagerState' ) ;
17
- var util = require ( '../util' ) ;
18
- var debug = util . debuglog ( 'hdb_conn' ) ;
19
-
20
- module . exports = ConnectionManager ;
21
-
22
- function ConnectionManager ( settings ) {
23
- this . state = new State ( settings ) ;
24
- }
25
-
26
- ConnectionManager . prototype . updateState = function updateState ( newOptions ) {
27
- this . state . update ( newOptions ) ;
28
- } ;
29
-
30
- ConnectionManager . prototype . getConnectOptions = function getConnectOptions ( ) {
31
- return this . state . options . connect ;
32
- } ;
33
-
34
- ConnectionManager . prototype . openConnection = function openConnection ( conn , cb ) {
35
- var dbHosts = this . state . dbHosts ;
36
- debug ( 'Opening connection. Hosts:' , dbHosts ) ;
37
-
38
- this . _openConnectionToHost ( conn , dbHosts , 0 , [ ] , cb ) ;
39
- } ;
40
-
41
- ConnectionManager . prototype . _openConnectionToHost = function _openConnectionToHost ( conn , dbHosts , whichHost , errorStats , cb ) {
42
- if ( whichHost === dbHosts . length ) {
43
- return cb ( couldNotOpenConnectionError ( errorStats ) ) ;
44
- }
45
-
46
- var self = this ;
47
- var currentHostOptions = dbHosts [ whichHost ] ;
48
- var openOptions = util . extend ( { } , currentHostOptions , this . state . options . encryption ) ;
49
- this . _openConnection ( conn , openOptions , function ( err ) {
50
- if ( ! err ) {
51
- debug ( 'Successful connection to %s:%d' , openOptions . host , openOptions . port ) ;
52
- return cb ( null ) ;
53
- }
54
- debug ( 'Connection to %s:%d failed with:' , openOptions . host , openOptions . port , err . message ) ;
55
- errorStats . push ( { host : openOptions . host , port : openOptions . port , err : err } ) ;
56
- self . _openConnectionToHost ( conn , dbHosts , ( whichHost + 1 ) , errorStats , cb ) ;
57
- } ) ;
58
- } ;
59
-
60
- ConnectionManager . prototype . _openConnection = function _openConnection ( conn , openOptions , cb ) {
61
- var dbName = this . state . options . multiDb . databaseName ;
62
- if ( util . isString ( dbName ) && dbName . length ) {
63
- this . _openConnectionMultiDbCase ( conn , openOptions , cb ) ;
64
- } else {
65
- conn . open ( openOptions , cb ) ;
66
- }
67
- } ;
68
-
69
- ConnectionManager . prototype . _openConnectionMultiDbCase = function _openConnectionMultiDbCase ( conn , openOptions , cb ) {
70
- var multiDbOptions = this . state . options . multiDb ;
71
-
72
- if ( ! openOptions . port ) {
73
- var instanceNum = extractInstanceNumber ( multiDbOptions . instanceNumber ) ;
74
- if ( isNaN ( instanceNum ) ) {
75
- return cb ( new Error ( 'Instance Number is not valid' ) ) ;
76
- } else {
77
- openOptions . port = 30013 + ( instanceNum * 100 ) ;
78
- }
79
- }
80
-
81
- function handleError ( err ) {
82
- conn . close ( ) ;
83
- cb ( err ) ;
84
- }
85
-
86
- conn . open ( openOptions , function ( err ) {
87
- if ( err ) {
88
- return handleError ( err ) ;
89
- }
90
-
91
- conn . fetchDbConnectInfo ( multiDbOptions , function ( err , info ) {
92
- if ( err ) {
93
- return handleError ( err ) ;
94
- }
95
-
96
- if ( info . isConnected ) {
97
- cb ( null ) ;
98
- } else {
99
- conn . _closeSilently ( ) ;
100
- openOptions . host = info . host ;
101
- openOptions . port = info . port ;
102
- debug ( 'Connecting to tenant-db %s on %s:%d' , multiDbOptions . databaseName , openOptions . host , openOptions . port ) ;
103
- conn . open ( openOptions , cb ) ;
104
- }
105
- } ) ;
106
- } ) ;
107
- } ;
108
-
109
- function extractInstanceNumber ( instanceNumber ) {
110
- if ( util . isNumber ( instanceNumber ) ) {
111
- return instanceNumber ;
112
- }
113
- if ( util . isString ( instanceNumber ) && instanceNumber . length ) {
114
- return parseInt ( instanceNumber , 10 ) ;
115
- }
116
- return NaN ;
117
- }
118
-
119
- function couldNotOpenConnectionError ( errorStats ) {
120
- var message = 'Could not connect to any host:' ;
121
- errorStats . forEach ( function ( stats ) {
122
- message += util . format ( ' [ %s:%d - %s ]' , stats . host , stats . port , stats . err . message ) ;
123
- } ) ;
124
- var err = new Error ( message ) ;
125
- err . code = 'EHDBOPENCONN' ;
126
- return err ;
1
+ // Copyright 2013 SAP AG.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http: //www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing,
10
+ // software distributed under the License is distributed on an
11
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12
+ // either express or implied. See the License for the specific
13
+ // language governing permissions and limitations under the License.
14
+ 'use strict' ;
15
+
16
+ var State = require ( './ConnectionManagerState' ) ;
17
+ var util = require ( '../util' ) ;
18
+ var debug = util . debuglog ( 'hdb_conn' ) ;
19
+
20
+ module . exports = ConnectionManager ;
21
+
22
+ function ConnectionManager ( settings ) {
23
+ this . state = new State ( settings ) ;
24
+ }
25
+
26
+ ConnectionManager . prototype . updateState = function updateState ( newOptions ) {
27
+ this . state . update ( newOptions ) ;
28
+ } ;
29
+
30
+ ConnectionManager . prototype . getConnectOptions = function getConnectOptions ( ) {
31
+ return this . state . options . connect ;
32
+ } ;
33
+
34
+ ConnectionManager . prototype . openConnection = function openConnection ( conn , cb ) {
35
+ var dbHosts = this . state . dbHosts ;
36
+ debug ( 'Opening connection. Hosts:' , dbHosts ) ;
37
+
38
+ this . _openConnectionToHost ( conn , dbHosts , 0 , [ ] , cb ) ;
39
+ } ;
40
+
41
+ ConnectionManager . prototype . _openConnectionToHost = function _openConnectionToHost ( conn , dbHosts , whichHost , errorStats , cb ) {
42
+ if ( whichHost === dbHosts . length ) {
43
+ return cb ( couldNotOpenConnectionError ( errorStats ) ) ;
44
+ }
45
+
46
+ var self = this ;
47
+ var currentHostOptions = dbHosts [ whichHost ] ;
48
+ var openOptions = util . extend ( { } , currentHostOptions , this . state . options . encryption ) ;
49
+ this . _openConnection ( conn , openOptions , function ( err ) {
50
+ if ( ! err ) {
51
+ debug ( 'Successful connection to %s:%d' , openOptions . host , openOptions . port ) ;
52
+ return cb ( null ) ;
53
+ }
54
+ debug ( 'Connection to %s:%d failed with:' , openOptions . host , openOptions . port , err . message ) ;
55
+ errorStats . push ( { host : openOptions . host , port : openOptions . port , err : err } ) ;
56
+ self . _openConnectionToHost ( conn , dbHosts , ( whichHost + 1 ) , errorStats , cb ) ;
57
+ } ) ;
58
+ } ;
59
+
60
+ ConnectionManager . prototype . _openConnection = function _openConnection ( conn , openOptions , cb ) {
61
+ var dbName = this . state . options . multiDb . databaseName ;
62
+ if ( util . isString ( dbName ) && dbName . length ) {
63
+ this . _openConnectionMultiDbCase ( conn , openOptions , cb ) ;
64
+ } else {
65
+ conn . open ( openOptions , cb ) ;
66
+ }
67
+ } ;
68
+
69
+ ConnectionManager . prototype . _openConnectionMultiDbCase = function _openConnectionMultiDbCase ( conn , openOptions , cb ) {
70
+ var multiDbOptions = this . state . options . multiDb ;
71
+
72
+ if ( ! openOptions . port ) {
73
+ var instanceNum = extractInstanceNumber ( multiDbOptions . instanceNumber ) ;
74
+ if ( isNaN ( instanceNum ) ) {
75
+ return cb ( new Error ( 'Instance Number is not valid' ) ) ;
76
+ } else {
77
+ openOptions . port = 30013 + ( instanceNum * 100 ) ;
78
+ }
79
+ }
80
+
81
+ function handleError ( err ) {
82
+ conn . close ( ) ;
83
+ cb ( err ) ;
84
+ }
85
+
86
+ conn . open ( openOptions , function ( err ) {
87
+ if ( err ) {
88
+ return handleError ( err ) ;
89
+ }
90
+
91
+ conn . fetchDbConnectInfo ( multiDbOptions , function ( err , info ) {
92
+ if ( err ) {
93
+ return handleError ( err ) ;
94
+ }
95
+
96
+ if ( info . isConnected ) {
97
+ cb ( null ) ;
98
+ } else {
99
+ conn . _closeSilently ( ) ;
100
+ openOptions . host = info . host ;
101
+ openOptions . port = info . port ;
102
+ debug ( 'Connecting to tenant-db %s on %s:%d' , multiDbOptions . databaseName , openOptions . host , openOptions . port ) ;
103
+ conn . open ( openOptions , cb ) ;
104
+ }
105
+ } ) ;
106
+ } ) ;
107
+ } ;
108
+
109
+ function extractInstanceNumber ( instanceNumber ) {
110
+ if ( util . isNumber ( instanceNumber ) ) {
111
+ return instanceNumber ;
112
+ }
113
+ if ( util . isString ( instanceNumber ) && instanceNumber . length ) {
114
+ return parseInt ( instanceNumber , 10 ) ;
115
+ }
116
+ return NaN ;
117
+ }
118
+
119
+ function couldNotOpenConnectionError ( errorStats ) {
120
+ var message = 'Could not connect to any host:' ;
121
+ errorStats . forEach ( function ( stats ) {
122
+ message += util . format ( ' [ %s:%d - %s ]' , stats . host , stats . port , stats . err . message ) ;
123
+ } ) ;
124
+ var err = new Error ( message ) ;
125
+ err . code = 'EHDBOPENCONN' ;
126
+ return err ;
127
127
}
0 commit comments