2011-02-05 10:06:52 +08:00
|
|
|
var url = require('url');
|
2011-03-02 04:35:14 +08:00
|
|
|
var defaults = require(__dirname + "/defaults");
|
2010-10-24 01:45:37 +08:00
|
|
|
var events = require('events');
|
2010-12-11 07:32:34 +08:00
|
|
|
var sys = require('sys');
|
2010-10-24 01:45:37 +08:00
|
|
|
|
2011-08-12 10:30:10 +08:00
|
|
|
//compatibility for old nodes
|
2010-10-24 01:45:37 +08:00
|
|
|
if(typeof events.EventEmitter.prototype.once !== 'function') {
|
|
|
|
events.EventEmitter.prototype.once = function (type, listener) {
|
|
|
|
var self = this;
|
|
|
|
self.on(type, function g () {
|
|
|
|
self.removeListener(type, g);
|
|
|
|
listener.apply(this, arguments);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2011-02-24 09:40:52 +08:00
|
|
|
|
2011-03-02 04:35:14 +08:00
|
|
|
var parseConnectionString = function(str) {
|
|
|
|
//unix socket
|
|
|
|
if(str.charAt(0) === '/') {
|
|
|
|
return { host: str };
|
|
|
|
}
|
|
|
|
var result = url.parse(str);
|
|
|
|
var config = {};
|
|
|
|
config.host = result.hostname;
|
|
|
|
config.database = result.pathname ? result.pathname.slice(1) : null
|
|
|
|
var auth = (result.auth || ':').split(':');
|
|
|
|
config.user = auth[0];
|
|
|
|
config.password = auth[1];
|
|
|
|
config.port = result.port;
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
|
|
|
//allows passing false as property to remove it from config
|
|
|
|
var norm = function(config, propName) {
|
|
|
|
config[propName] = (config[propName] || (config[propName] === false ? undefined : defaults[propName]))
|
|
|
|
};
|
|
|
|
|
|
|
|
//normalizes connection info
|
|
|
|
//which can be in the form of an object
|
|
|
|
//or a connection string
|
|
|
|
var normalizeConnectionInfo = function(config) {
|
|
|
|
switch(typeof config) {
|
|
|
|
case 'object':
|
|
|
|
norm(config, 'user');
|
|
|
|
norm(config, 'password');
|
|
|
|
norm(config, 'host');
|
|
|
|
norm(config, 'port');
|
|
|
|
norm(config, 'database');
|
|
|
|
return config;
|
|
|
|
case 'string':
|
|
|
|
return normalizeConnectionInfo(parseConnectionString(config));
|
|
|
|
default:
|
|
|
|
throw new Error("Unrecognized connection config parameter: " + config);
|
|
|
|
}
|
|
|
|
};
|
2010-12-10 08:10:42 +08:00
|
|
|
|
2011-05-06 08:13:43 +08:00
|
|
|
|
|
|
|
var add = function(params, config, paramName) {
|
|
|
|
var value = config[paramName];
|
|
|
|
if(value) {
|
|
|
|
params.push(paramName+"='"+value+"'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//builds libpq specific connection string
|
|
|
|
//from a supplied config object
|
|
|
|
//the config object conforms to the interface of the config object
|
|
|
|
//accepted by the pure javascript client
|
|
|
|
var getLibpgConString = function(config, callback) {
|
|
|
|
if(typeof config == 'object') {
|
|
|
|
var params = []
|
|
|
|
add(params, config, 'user');
|
|
|
|
add(params, config, 'password');
|
|
|
|
add(params, config, 'port');
|
|
|
|
if(config.database) {
|
|
|
|
params.push("dbname='" + config.database + "'");
|
|
|
|
}
|
|
|
|
if(config.host) {
|
|
|
|
if(config.host != 'localhost' && config.host != '127.0.0.1') {
|
|
|
|
//do dns lookup
|
|
|
|
return require('dns').lookup(config.host, 4, function(err, address) {
|
|
|
|
if(err) return callback(err, null);
|
|
|
|
params.push("hostaddr="+address)
|
|
|
|
callback(null, params.join(" "))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
params.push("hostaddr=127.0.0.1 ");
|
|
|
|
}
|
|
|
|
callback(null, params.join(" "));
|
|
|
|
} else {
|
|
|
|
throw new Error("Unrecognized config type for connection");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-10 08:10:42 +08:00
|
|
|
module.exports = {
|
2011-05-06 08:13:43 +08:00
|
|
|
normalizeConnectionInfo: normalizeConnectionInfo,
|
|
|
|
//only exported here to make testing of this method possible
|
|
|
|
//since it contains quite a bit of logic and testing for
|
|
|
|
//each connection scenario in an integration test is impractical
|
|
|
|
buildLibpqConnectionString: getLibpgConString
|
2010-12-10 08:10:42 +08:00
|
|
|
}
|