@@ -13,88 +13,6 @@ if(typeof events.EventEmitter.prototype.once !== 'function') {
1313 } ;
1414}
1515
16- var parseConnectionString = function ( str ) {
17- //unix socket
18- if ( str . charAt ( 0 ) === '/' ) {
19- return { host : str } ;
20- }
21- var result = url . parse ( str ) ;
22- var config = { } ;
23- config . host = result . hostname ;
24- config . database = result . pathname ? result . pathname . slice ( 1 ) : null
25- var auth = ( result . auth || ':' ) . split ( ':' ) ;
26- config . user = auth [ 0 ] ;
27- config . password = auth [ 1 ] ;
28- config . port = result . port ;
29- return config ;
30- } ;
31-
32- //allows passing false as property to remove it from config
33- var norm = function ( config , propName ) {
34- config [ propName ] = ( config [ propName ] || ( config [ propName ] === false ? undefined : defaults [ propName ] ) )
35- } ;
36-
37- //normalizes connection info
38- //which can be in the form of an object
39- //or a connection string
40- var normalizeConnectionInfo = function ( config ) {
41- switch ( typeof config ) {
42- case 'object' :
43- norm ( config , 'user' ) ;
44- norm ( config , 'password' ) ;
45- norm ( config , 'host' ) ;
46- norm ( config , 'port' ) ;
47- norm ( config , 'database' ) ;
48- return config ;
49- case 'string' :
50- return normalizeConnectionInfo ( parseConnectionString ( config ) ) ;
51- default :
52- throw new Error ( "Unrecognized connection config parameter: " + config ) ;
53- }
54- } ;
55-
56-
57- var add = function ( params , config , paramName ) {
58- var value = config [ paramName ] ;
59- if ( value ) {
60- params . push ( paramName + "='" + value + "'" ) ;
61- }
62- }
63-
64- //builds libpq specific connection string
65- //from a supplied config object
66- //the config object conforms to the interface of the config object
67- //accepted by the pure javascript client
68- var getLibpgConString = function ( config , callback ) {
69- if ( typeof config == 'object' ) {
70- var params = [ ]
71- add ( params , config , 'user' ) ;
72- add ( params , config , 'password' ) ;
73- add ( params , config , 'port' ) ;
74- if ( config . database ) {
75- params . push ( "dbname='" + config . database + "'" ) ;
76- }
77- if ( config . host ) {
78- if ( ! config . host . indexOf ( "/" ) ) {
79- params . push ( "host=" + config . host ) ;
80- } else {
81- if ( config . host != 'localhost' && config . host != '127.0.0.1' ) {
82- //do dns lookup
83- return require ( 'dns' ) . lookup ( config . host , function ( err , address ) {
84- if ( err ) return callback ( err , null ) ;
85- params . push ( "hostaddr=" + address )
86- callback ( null , params . join ( " " ) )
87- } )
88- }
89- params . push ( "hostaddr=127.0.0.1 " ) ;
90- }
91- }
92- callback ( null , params . join ( " " ) ) ;
93- } else {
94- throw new Error ( "Unrecognized config type for connection" ) ;
95- }
96- }
97-
9816//converts values from javascript types
9917//to their 'raw' counterparts for use as a postgres parameter
10018//note: you can override this function to provide your own conversion mechanism
@@ -126,12 +44,6 @@ function normalizeQueryConfig (config, values, callback) {
12644}
12745
12846module . exports = {
129- normalizeConnectionInfo : normalizeConnectionInfo ,
130- //only exported here to make testing of this method possible
131- //since it contains quite a bit of logic and testing for
132- //each connection scenario in an integration test is impractical
133- buildLibpqConnectionString : getLibpgConString ,
134- parseConnectionString : parseConnectionString ,
13547 prepareValue : prepareValue ,
13648 normalizeQueryConfig : normalizeQueryConfig
13749}
0 commit comments