2011-02-21 06:12:06 +08:00
|
|
|
//require the c++ bindings & export to javascript
|
2011-02-24 10:02:51 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-08-30 12:06:07 +08:00
|
|
|
var utils = require(__dirname + "/../utils");
|
2011-02-24 10:02:51 +08:00
|
|
|
|
2011-10-11 11:03:27 +08:00
|
|
|
var binding;
|
|
|
|
|
|
|
|
try{
|
|
|
|
//v0.5.x
|
|
|
|
binding = require(__dirname + '/../../build/Release/binding.node');
|
|
|
|
} catch(e) {
|
|
|
|
//v0.4.x
|
|
|
|
binding = require(__dirname + '/../../build/default/binding');
|
|
|
|
}
|
|
|
|
|
2011-08-30 11:53:38 +08:00
|
|
|
var Connection = binding.Connection;
|
2011-08-30 12:06:07 +08:00
|
|
|
var types = require(__dirname + "/../types");
|
|
|
|
var NativeQuery = require(__dirname + '/query');
|
2011-08-30 11:53:38 +08:00
|
|
|
|
2011-10-11 11:03:27 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-02-23 13:52:25 +08:00
|
|
|
var p = Connection.prototype;
|
2011-10-11 11:03:27 +08:00
|
|
|
for(var k in EventEmitter.prototype) {
|
|
|
|
p[k] = EventEmitter.prototype[k];
|
|
|
|
}
|
2011-02-23 13:52:25 +08:00
|
|
|
|
|
|
|
var nativeConnect = p.connect;
|
|
|
|
|
2011-10-03 16:25:38 +08:00
|
|
|
p.connect = function(cb) {
|
2011-02-23 13:52:25 +08:00
|
|
|
var self = this;
|
2011-05-06 08:13:43 +08:00
|
|
|
utils.buildLibpqConnectionString(this._config, function(err, conString) {
|
2011-10-03 16:25:38 +08:00
|
|
|
if(err) {
|
|
|
|
return cb ? cb(err) : self.emit('error', err);
|
|
|
|
}
|
|
|
|
if(cb) {
|
|
|
|
var errCallback;
|
|
|
|
var connectCallback = function() {
|
|
|
|
//remove single-fire connection error callback
|
|
|
|
self.removeListener('error', errCallback);
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
errCallback = function(err) {
|
|
|
|
//remove singel-fire connection success callback
|
|
|
|
self.removeListener('connect', connectCallback);
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
self.once('connect', connectCallback);
|
|
|
|
self.once('error', errCallback);
|
|
|
|
}
|
2012-07-06 21:55:10 +08:00
|
|
|
nativeConnect.call(self, conString);
|
2011-02-23 13:52:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2011-03-01 13:09:09 +08:00
|
|
|
p.query = function(config, values, callback) {
|
|
|
|
var q = new NativeQuery(config, values, callback);
|
2011-02-24 10:02:51 +08:00
|
|
|
this._queryQueue.push(q);
|
2011-02-23 13:52:25 +08:00
|
|
|
this._pulseQueryQueue();
|
2011-02-24 10:02:51 +08:00
|
|
|
return q;
|
2011-02-23 13:52:25 +08:00
|
|
|
}
|
|
|
|
|
2011-11-03 02:30:44 +08:00
|
|
|
var nativeCancel = p.cancel;
|
|
|
|
|
|
|
|
p.cancel = function(client, query) {
|
|
|
|
if (client._activeQuery == query)
|
|
|
|
this.connect(nativeCancel.bind(client));
|
|
|
|
else if (client._queryQueue.indexOf(query) != -1)
|
|
|
|
client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
|
|
|
|
};
|
|
|
|
|
2011-08-16 09:31:31 +08:00
|
|
|
p._pulseQueryQueue = function(initialConnection) {
|
2011-02-23 13:52:25 +08:00
|
|
|
if(!this._connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this._activeQuery) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var query = this._queryQueue.shift();
|
|
|
|
if(!query) {
|
2011-08-16 09:31:31 +08:00
|
|
|
if(!initialConnection) {
|
2011-08-30 12:43:36 +08:00
|
|
|
this._drainPaused ? this._drainPaused++ : this.emit('drain');
|
2011-08-16 09:31:31 +08:00
|
|
|
}
|
2011-02-23 13:52:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-24 10:02:51 +08:00
|
|
|
this._activeQuery = query;
|
2011-03-06 02:01:57 +08:00
|
|
|
if(query.name) {
|
2011-03-07 12:27:35 +08:00
|
|
|
if(this._namedQueries[query.name]) {
|
|
|
|
this._sendQueryPrepared(query.name, query.values||[]);
|
|
|
|
} else {
|
|
|
|
this._namedQuery = true;
|
|
|
|
this._namedQueries[query.name] = true;
|
|
|
|
this._sendPrepare(query.name, query.text, (query.values||[]).length);
|
|
|
|
}
|
2011-03-06 02:01:57 +08:00
|
|
|
}
|
|
|
|
else if(query.values) {
|
2011-02-25 11:33:54 +08:00
|
|
|
//call native function
|
|
|
|
this._sendQueryWithParams(query.text, query.values)
|
|
|
|
} else {
|
|
|
|
//call native function
|
|
|
|
this._sendQuery(query.text);
|
|
|
|
}
|
2011-02-23 13:52:25 +08:00
|
|
|
}
|
|
|
|
|
2011-08-30 12:43:36 +08:00
|
|
|
p.pauseDrain = function() {
|
|
|
|
this._drainPaused = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
p.resumeDrain = function() {
|
2011-11-03 02:30:44 +08:00
|
|
|
if(this._drainPaused > 1) {
|
|
|
|
this.emit('drain')
|
2011-08-30 12:43:36 +08:00
|
|
|
};
|
|
|
|
this._drainPaused = 0;
|
|
|
|
};
|
|
|
|
|
2011-08-30 12:06:07 +08:00
|
|
|
var clientBuilder = function(config) {
|
2011-03-04 02:46:24 +08:00
|
|
|
config = config || {};
|
2011-02-23 13:52:25 +08:00
|
|
|
var connection = new Connection();
|
|
|
|
connection._queryQueue = [];
|
2011-03-07 12:27:35 +08:00
|
|
|
connection._namedQueries = {};
|
2011-02-23 13:52:25 +08:00
|
|
|
connection._activeQuery = null;
|
2011-03-04 02:46:24 +08:00
|
|
|
connection._config = utils.normalizeConnectionInfo(config);
|
2011-08-29 15:35:08 +08:00
|
|
|
//attach properties to normalize interface with pure js client
|
|
|
|
connection.user = connection._config.user;
|
|
|
|
connection.password = connection._config.password;
|
|
|
|
connection.database = connection._config.database;
|
|
|
|
connection.host = connection._config.host;
|
|
|
|
connection.port = connection._config.port;
|
2011-02-23 13:52:25 +08:00
|
|
|
connection.on('connect', function() {
|
|
|
|
connection._connected = true;
|
2011-08-16 09:31:31 +08:00
|
|
|
connection._pulseQueryQueue(true);
|
2011-02-23 13:52:25 +08:00
|
|
|
});
|
2011-02-24 10:02:51 +08:00
|
|
|
|
2011-02-24 12:41:54 +08:00
|
|
|
//proxy some events to active query
|
2011-02-24 10:02:51 +08:00
|
|
|
connection.on('_row', function(row) {
|
2011-03-01 13:09:09 +08:00
|
|
|
connection._activeQuery.handleRow(row);
|
2011-10-04 12:43:28 +08:00
|
|
|
});
|
|
|
|
|
2012-05-31 12:38:03 +08:00
|
|
|
connection.on('_cmdStatus', function(status) {
|
2012-09-10 10:13:36 +08:00
|
|
|
//set this here so we can pass it to the query
|
|
|
|
//when the query completes
|
|
|
|
connection._lastMeta = status;
|
2012-05-31 12:38:03 +08:00
|
|
|
});
|
|
|
|
|
2011-10-04 12:43:28 +08:00
|
|
|
//TODO: emit more native error properties (make it match js error)
|
2011-02-24 10:02:51 +08:00
|
|
|
connection.on('_error', function(err) {
|
2011-10-04 12:43:28 +08:00
|
|
|
//create Error object from object literal
|
|
|
|
var error = new Error(err.message || "Unknown native driver error");
|
|
|
|
for(var key in err) {
|
|
|
|
error[key] = err[key];
|
|
|
|
}
|
|
|
|
|
2011-03-07 11:32:58 +08:00
|
|
|
//give up on trying to wait for named query prepare
|
|
|
|
this._namedQuery = false;
|
2011-02-24 10:02:51 +08:00
|
|
|
if(connection._activeQuery) {
|
2011-10-04 12:43:28 +08:00
|
|
|
connection._activeQuery.handleError(error);
|
2011-02-24 10:02:51 +08:00
|
|
|
} else {
|
2011-10-04 12:43:28 +08:00
|
|
|
connection.emit('error', error);
|
2011-02-24 10:02:51 +08:00
|
|
|
}
|
2011-10-04 12:43:28 +08:00
|
|
|
});
|
|
|
|
|
2011-02-24 10:02:51 +08:00
|
|
|
connection.on('_readyForQuery', function() {
|
2011-03-07 12:27:35 +08:00
|
|
|
var q = this._activeQuery;
|
2011-03-07 11:32:58 +08:00
|
|
|
//a named query finished being prepared
|
|
|
|
if(this._namedQuery) {
|
|
|
|
this._namedQuery = false;
|
2011-03-07 12:27:35 +08:00
|
|
|
this._sendQueryPrepared(q.name, q.values||[]);
|
2011-03-07 11:32:58 +08:00
|
|
|
} else {
|
2012-05-31 12:38:03 +08:00
|
|
|
connection._activeQuery.handleReadyForQuery(connection._lastMeta);
|
2011-03-07 11:32:58 +08:00
|
|
|
connection._activeQuery = null;
|
|
|
|
connection._pulseQueryQueue();
|
|
|
|
}
|
2011-02-24 09:40:52 +08:00
|
|
|
});
|
2011-10-04 12:43:28 +08:00
|
|
|
|
2011-02-23 13:52:25 +08:00
|
|
|
return connection;
|
2011-02-24 09:40:52 +08:00
|
|
|
};
|
|
|
|
|
2011-08-30 12:06:07 +08:00
|
|
|
module.exports = clientBuilder;
|