2010-09-29 12:18:46 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2010-11-15 13:04:41 +08:00
|
|
|
var Client = require(__dirname+'/client');
|
2010-12-11 07:32:34 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
2013-02-21 06:08:48 +08:00
|
|
|
var pool = require(__dirname + '/pool');
|
2013-03-06 22:57:38 +08:00
|
|
|
var types = require(__dirname + '/types/');
|
2013-02-21 06:08:48 +08:00
|
|
|
var Connection = require(__dirname + '/connection');
|
2011-08-12 09:59:56 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
var PG = function(clientConstructor) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.defaults = defaults;
|
2013-02-21 06:08:48 +08:00
|
|
|
this.Client = pool.Client = clientConstructor;
|
|
|
|
this.Query = this.Client.Query;
|
|
|
|
this.pools = pool;
|
|
|
|
this.types = types;
|
|
|
|
this.Connection = Connection;
|
2011-08-30 11:48:17 +08:00
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(PG, EventEmitter);
|
2011-08-12 09:59:56 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
PG.prototype.end = function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var self = this;
|
|
|
|
Object.keys(self.pools.all).forEach(function(key) {
|
|
|
|
var pool = self.pools.all[key];
|
2011-08-12 09:59:56 +08:00
|
|
|
pool.drain(function() {
|
|
|
|
pool.destroyAllNow();
|
|
|
|
});
|
2013-01-21 21:52:00 +08:00
|
|
|
});
|
|
|
|
};
|
2011-03-12 02:29:27 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
PG.prototype.connect = function(config, callback) {
|
2013-02-21 06:08:48 +08:00
|
|
|
if(typeof config == "function") {
|
|
|
|
callback = config;
|
|
|
|
config = null;
|
|
|
|
}
|
|
|
|
var pool = this.pools.getOrCreate(config);
|
|
|
|
pool.connect(callback);
|
|
|
|
if(!pool.listeners('error').length) {
|
|
|
|
//propagate errors up to pg object
|
|
|
|
pool.on('error', this.emit.bind(this, 'error'));
|
2011-08-30 11:48:17 +08:00
|
|
|
}
|
2013-01-21 21:52:00 +08:00
|
|
|
};
|
2011-08-29 15:35:46 +08:00
|
|
|
|
2011-11-02 23:07:14 +08:00
|
|
|
// cancel the query runned by the given client
|
|
|
|
PG.prototype.cancel = function(config, client, query) {
|
|
|
|
var c = config;
|
|
|
|
//allow for no config to be passed
|
2013-01-21 21:52:00 +08:00
|
|
|
if(typeof c === 'function') {
|
2011-11-02 23:07:14 +08:00
|
|
|
c = defaults;
|
2013-01-21 21:52:00 +08:00
|
|
|
}
|
2011-11-02 23:07:14 +08:00
|
|
|
var cancellingClient = new this.Client(c);
|
|
|
|
cancellingClient.cancel(client, query);
|
2013-01-21 21:52:00 +08:00
|
|
|
};
|
2011-11-02 23:07:14 +08:00
|
|
|
|
2013-06-27 04:32:07 +08:00
|
|
|
if (process.env.hasOwnProperty('NODE_PG_FORCE_NATIVE')) {
|
|
|
|
module.exports = new PG(require(__dirname + '/native'));
|
|
|
|
} else {
|
|
|
|
module.exports = new PG(Client);
|
2012-09-19 01:30:50 +08:00
|
|
|
|
2013-06-27 04:32:07 +08:00
|
|
|
//lazy require native module...the native module may not have installed
|
|
|
|
module.exports.__defineGetter__("native", function() {
|
|
|
|
delete module.exports.native;
|
|
|
|
module.exports.native = new PG(require(__dirname + '/native'));
|
|
|
|
return module.exports.native;
|
|
|
|
});
|
|
|
|
}
|