2013-01-22 05:09:44 +08:00
|
|
|
var dns = require('dns');
|
2013-01-22 05:44:55 +08:00
|
|
|
var path = require('path');
|
2013-01-22 05:09:44 +08:00
|
|
|
|
2013-01-18 08:14:13 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
|
|
|
|
|
|
|
var val = function(key, config) {
|
|
|
|
return config[key] ||
|
|
|
|
process.env['PG' + key.toUpperCase()] ||
|
|
|
|
defaults[key];
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
2013-01-18 08:14:13 +08:00
|
|
|
|
|
|
|
var url = require('url');
|
|
|
|
//parses a connection string
|
|
|
|
var parse = function(str) {
|
|
|
|
//unix socket
|
|
|
|
if(str.charAt(0) === '/') {
|
|
|
|
return { host: str };
|
|
|
|
}
|
|
|
|
var result = url.parse(str);
|
|
|
|
var config = {};
|
|
|
|
config.host = result.hostname;
|
2013-01-21 21:36:45 +08:00
|
|
|
config.database = result.pathname ? result.pathname.slice(1) : null;
|
2013-01-18 08:14:13 +08:00
|
|
|
var auth = (result.auth || ':').split(':');
|
|
|
|
config.user = auth[0];
|
|
|
|
config.password = auth[1];
|
|
|
|
config.port = result.port;
|
|
|
|
return config;
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var ConnectionParameters = function(config) {
|
|
|
|
config = typeof config == 'string' ? parse(config) : (config || {});
|
|
|
|
this.user = val('user', config);
|
|
|
|
this.database = val('database', config);
|
2013-01-21 21:36:45 +08:00
|
|
|
this.port = parseInt(val('port', config), 10);
|
2013-01-18 08:27:48 +08:00
|
|
|
this.host = val('host', config);
|
|
|
|
this.password = val('password', config);
|
|
|
|
this.binary = val('binary', config);
|
|
|
|
this.ssl = config.ssl || defaults.ssl;
|
2013-01-22 05:44:55 +08:00
|
|
|
//a domain socket begins with '/'
|
2013-01-22 05:09:44 +08:00
|
|
|
this.isDomainSocket = (!(this.host||'').indexOf('/'));
|
|
|
|
};
|
|
|
|
|
|
|
|
var add = function(params, config, paramName) {
|
|
|
|
var value = config[paramName];
|
|
|
|
if(value) {
|
|
|
|
params.push(paramName+"='"+value+"'");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
|
|
|
|
var params = []
|
|
|
|
add(params, this, 'user');
|
|
|
|
add(params, this, 'password');
|
|
|
|
add(params, this, 'port');
|
|
|
|
if(this.database) {
|
|
|
|
params.push("dbname='" + this.database + "'");
|
|
|
|
}
|
|
|
|
if(this.isDomainSocket) {
|
2013-01-22 05:44:55 +08:00
|
|
|
params.push("host=" + this.getDomainSocketName());
|
2013-01-22 05:09:44 +08:00
|
|
|
return cb(null, params.join(' '));
|
|
|
|
}
|
|
|
|
dns.lookup(this.host, function(err, address) {
|
|
|
|
if(err) return cb(err, null);
|
|
|
|
params.push("hostaddr=" + address);
|
|
|
|
return cb(null, params.join(' '));
|
|
|
|
});
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
2013-01-18 08:14:13 +08:00
|
|
|
|
2013-01-22 05:44:55 +08:00
|
|
|
ConnectionParameters.prototype.getDomainSocketName = function() {
|
|
|
|
var filename = '.s.PGSQL.' + this.port;
|
|
|
|
|
|
|
|
//if host is full path to socket fd with port number, just return it
|
|
|
|
if(this.host.indexOf(filename) > -1) return this.host;
|
|
|
|
|
|
|
|
//otherwise, build it from host + standard filename + port
|
|
|
|
return path.join(this.host, filename);
|
|
|
|
};
|
|
|
|
|
2013-01-18 08:14:13 +08:00
|
|
|
module.exports = ConnectionParameters;
|