2014-09-14 00:32:53 +08:00
|
|
|
var Native = require('pg-native');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var NativeQuery = require('./query');
|
|
|
|
|
2014-09-14 10:37:30 +08:00
|
|
|
var Client = module.exports = function(config) {
|
2014-09-14 00:32:53 +08:00
|
|
|
EventEmitter.call(this);
|
2014-09-14 10:37:30 +08:00
|
|
|
if(typeof config === 'string') {
|
|
|
|
this.connectionString = config;
|
|
|
|
}
|
2014-09-14 00:32:53 +08:00
|
|
|
this.native = new Native();
|
|
|
|
this._queryQueue = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
util.inherits(Client, EventEmitter);
|
|
|
|
|
|
|
|
//connect to the backend
|
|
|
|
//pass an optional callback to be called once connected
|
|
|
|
//or with an error if there was a connection error
|
|
|
|
//if no callback is passed and there is a connection error
|
|
|
|
//the client will emit an error event.
|
|
|
|
Client.prototype.connect = function(cb) {
|
|
|
|
var self = this;
|
2014-09-14 10:37:30 +08:00
|
|
|
this.native.connect(this.connectionString, function(err) {
|
2014-09-14 00:32:53 +08:00
|
|
|
//error handling
|
|
|
|
if(err) {
|
|
|
|
if(cb) return cb(err);
|
2014-09-14 10:37:30 +08:00
|
|
|
return self.emit('error', err);
|
2014-09-14 00:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//set internal states to connected
|
|
|
|
self._connected = true;
|
|
|
|
self.emit('connect');
|
|
|
|
self._pulseQueryQueue(true);
|
|
|
|
|
|
|
|
//possibly call the optional callback
|
|
|
|
if(cb) cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.query = function(config, values, callback) {
|
|
|
|
var query = new NativeQuery(this.native);
|
|
|
|
|
|
|
|
//support query('text', ...) style calls
|
|
|
|
if(typeof config == 'string') {
|
|
|
|
query.text = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
//support passing everything in via a config object
|
|
|
|
if(typeof config == 'object') {
|
|
|
|
query.text = config.text;
|
|
|
|
query.values = config.values;
|
|
|
|
query.name = config.name;
|
|
|
|
query.callback = config.callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
//support query({...}, function() {}) style calls
|
|
|
|
//& support query(..., ['values'], ...) style calls
|
|
|
|
if(typeof values == 'function') {
|
|
|
|
query.callback = values;
|
|
|
|
}
|
|
|
|
else if(util.isArray(values)) {
|
|
|
|
query.values = values;
|
|
|
|
}
|
|
|
|
if(typeof callback == 'function') {
|
|
|
|
query.callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queryQueue.push(query);
|
|
|
|
this._pulseQueryQueue();
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.end = function(cb) {
|
|
|
|
this.native.end(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype._pulseQueryQueue = function(initialConnection) {
|
|
|
|
if(!this._connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this._activeQuery) {
|
2014-09-14 10:37:30 +08:00
|
|
|
if(this._activeQuery.state != 'error' && this._activeQuery.state != 'end') {
|
2014-09-14 00:32:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var query = this._queryQueue.shift();
|
|
|
|
if(!query) {
|
|
|
|
if(!initialConnection) {
|
|
|
|
this.emit('drain');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._activeQuery = query;
|
|
|
|
query.submit();
|
2014-09-14 10:37:30 +08:00
|
|
|
var self = this;
|
|
|
|
query.once('_done', function() {
|
|
|
|
self._pulseQueryQueue();
|
|
|
|
});
|
2014-09-14 00:32:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
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;
|
2013-01-23 13:23:47 +08:00
|
|
|
|
|
|
|
var ConnectionParameters = require(__dirname + '/../connection-parameters');
|
2012-09-27 18:28:00 +08:00
|
|
|
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
|
|
|
|
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
|
2013-07-24 02:04:03 +08:00
|
|
|
var JsClient = require(__dirname + '/../client'); // used to import JS escape functions
|
2011-02-24 10:02:51 +08:00
|
|
|
|
2011-10-11 11:03:27 +08:00
|
|
|
var binding;
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
//TODO remove on v1.0.0
|
2013-01-24 07:52:55 +08:00
|
|
|
try {
|
2011-10-11 11:03:27 +08:00
|
|
|
//v0.5.x
|
2013-01-24 07:52:55 +08:00
|
|
|
binding = require(__dirname + '/../../build/Release/binding.node');
|
2011-10-11 11:03:27 +08:00
|
|
|
} catch(e) {
|
|
|
|
//v0.4.x
|
2013-01-24 07:52:55 +08:00
|
|
|
binding = require(__dirname + '/../../build/default/binding');
|
2011-10-11 11:03:27 +08:00
|
|
|
}
|
|
|
|
|
2011-08-30 11:53:38 +08:00
|
|
|
var Connection = binding.Connection;
|
2011-08-30 12:06:07 +08:00
|
|
|
var NativeQuery = require(__dirname + '/query');
|
2011-08-30 11:53:38 +08:00
|
|
|
|
2011-10-11 11:03:27 +08:00
|
|
|
for(var k in EventEmitter.prototype) {
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype[k] = EventEmitter.prototype[k];
|
2011-10-11 11:03:27 +08:00
|
|
|
}
|
2011-02-23 13:52:25 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
var nativeConnect = Connection.prototype.connect;
|
2011-02-23 13:52:25 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.connect = function(cb) {
|
2011-02-23 13:52:25 +08:00
|
|
|
var self = this;
|
2013-01-23 13:23:47 +08:00
|
|
|
this.connectionParameters.getLibpqConnectionString(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);
|
2013-01-21 21:16:41 +08:00
|
|
|
};
|
2011-10-03 16:25:38 +08:00
|
|
|
errCallback = function(err) {
|
|
|
|
//remove singel-fire connection success callback
|
|
|
|
self.removeListener('connect', connectCallback);
|
|
|
|
cb(err);
|
2013-01-21 21:16:41 +08:00
|
|
|
};
|
2011-10-03 16:25:38 +08:00
|
|
|
self.once('connect', connectCallback);
|
|
|
|
self.once('error', errCallback);
|
|
|
|
}
|
2012-07-06 21:55:10 +08:00
|
|
|
nativeConnect.call(self, conString);
|
2013-01-21 21:16:41 +08:00
|
|
|
});
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype._copy = function (text, stream) {
|
2012-09-27 18:28:00 +08:00
|
|
|
var q = new NativeQuery(text, function (error) {
|
|
|
|
if (error) {
|
|
|
|
q.stream.error(error);
|
|
|
|
} else {
|
|
|
|
q.stream.close();
|
|
|
|
}
|
2013-01-24 07:52:55 +08:00
|
|
|
});
|
2012-09-27 18:28:00 +08:00
|
|
|
q.stream = stream;
|
|
|
|
this._queryQueue.push(q);
|
|
|
|
this._pulseQueryQueue();
|
|
|
|
return q.stream;
|
2013-01-21 21:16:41 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.copyFrom = function (text) {
|
2012-09-27 18:28:00 +08:00
|
|
|
return this._copy(text, new CopyFromStream());
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.copyTo = function (text) {
|
2012-09-27 18:28:00 +08:00
|
|
|
return this._copy(text, new CopyToStream());
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.sendCopyFromChunk = function (chunk) {
|
2013-01-24 07:52:55 +08:00
|
|
|
this._sendCopyFromChunk(chunk);
|
2012-09-27 18:28:00 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.endCopyFrom = function (msg) {
|
2013-01-18 06:24:08 +08:00
|
|
|
this._endCopyFrom(msg);
|
2012-09-27 18:28:00 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
2013-07-24 02:04:03 +08:00
|
|
|
// use JS version if native version undefined
|
|
|
|
// happens when PG version < 9.0.0
|
|
|
|
if (!Connection.prototype.escapeIdentifier) {
|
|
|
|
Connection.prototype.escapeIdentifier = JsClient.prototype.escapeIdentifier;
|
|
|
|
}
|
|
|
|
if (!Connection.prototype.escapeLiteral) {
|
|
|
|
Connection.prototype.escapeLiteral = JsClient.prototype.escapeLiteral;
|
|
|
|
}
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.query = function(config, values, callback) {
|
2013-01-24 07:52:55 +08:00
|
|
|
var query = (config instanceof NativeQuery) ? config :
|
|
|
|
new NativeQuery(config, values, callback);
|
2012-12-11 14:34:55 +08:00
|
|
|
this._queryQueue.push(query);
|
2011-02-23 13:52:25 +08:00
|
|
|
this._pulseQueryQueue();
|
2012-12-11 14:34:55 +08:00
|
|
|
return query;
|
2013-01-21 21:16:41 +08:00
|
|
|
};
|
2011-02-23 13:52:25 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
var nativeCancel = Connection.prototype.cancel;
|
2011-11-03 02:30:44 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.cancel = function(client, query) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if (client._activeQuery == query) {
|
2011-11-03 02:30:44 +08:00
|
|
|
this.connect(nativeCancel.bind(client));
|
2013-03-07 00:26:40 +08:00
|
|
|
} else if (client._queryQueue.indexOf(query) != -1) {
|
2011-11-03 02:30:44 +08:00
|
|
|
client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
|
2013-03-07 00:26:40 +08:00
|
|
|
}
|
2011-11-03 02:30:44 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.sendCopyFail = function(msg) {
|
2013-01-18 06:24:08 +08:00
|
|
|
this.endCopyFrom(msg);
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
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();
|
2013-05-20 22:31:55 +08:00
|
|
|
EventEmitter.call(connection);
|
2011-02-23 13:52:25 +08:00
|
|
|
connection._queryQueue = [];
|
2011-03-07 12:27:35 +08:00
|
|
|
connection._namedQueries = {};
|
2011-02-23 13:52:25 +08:00
|
|
|
connection._activeQuery = null;
|
2013-01-23 13:23:47 +08:00
|
|
|
connection.connectionParameters = new ConnectionParameters(config);
|
2011-08-29 15:35:08 +08:00
|
|
|
//attach properties to normalize interface with pure js client
|
2013-01-23 13:23:47 +08:00
|
|
|
connection.user = connection.connectionParameters.user;
|
|
|
|
connection.password = connection.connectionParameters.password;
|
|
|
|
connection.database = connection.connectionParameters.database;
|
|
|
|
connection.host = connection.connectionParameters.host;
|
|
|
|
connection.port = connection.connectionParameters.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
|
|
|
|
2013-07-08 22:19:30 +08:00
|
|
|
connection.on('_rowDescription', function(rowDescription) {
|
|
|
|
connection._activeQuery.handleRowDescription(rowDescription);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2013-03-29 02:24:33 +08:00
|
|
|
connection.on('_end', function() {
|
|
|
|
process.nextTick(function() {
|
|
|
|
if(connection._activeQuery) {
|
|
|
|
connection._activeQuery.handleError(new Error("Connection was ended during query"));
|
|
|
|
}
|
|
|
|
connection.emit('end');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-02-24 10:02:51 +08:00
|
|
|
connection.on('_readyForQuery', function() {
|
2013-04-19 22:09:28 +08:00
|
|
|
var error;
|
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 {
|
2013-04-19 22:09:28 +08:00
|
|
|
//try/catch/rethrow to ensure exceptions don't prevent the queryQueue from
|
|
|
|
//being processed
|
|
|
|
try{
|
|
|
|
connection._activeQuery.handleReadyForQuery(connection._lastMeta);
|
|
|
|
} catch(e) {
|
|
|
|
error = e;
|
|
|
|
}
|
2011-03-07 11:32:58 +08:00
|
|
|
connection._activeQuery = null;
|
|
|
|
connection._pulseQueryQueue();
|
2013-04-19 22:09:28 +08:00
|
|
|
if(error) throw error;
|
2011-03-07 11:32:58 +08:00
|
|
|
}
|
2011-02-24 09:40:52 +08:00
|
|
|
});
|
2012-11-06 22:55:43 +08:00
|
|
|
connection.on('copyInResponse', function () {
|
2012-10-08 01:12:30 +08:00
|
|
|
//connection is ready to accept chunks
|
2013-01-24 07:52:55 +08:00
|
|
|
//start to send data from stream
|
2012-09-27 18:28:00 +08:00
|
|
|
connection._activeQuery.streamData(connection);
|
|
|
|
});
|
2013-01-18 06:24:08 +08:00
|
|
|
connection.on('copyOutResponse', function(msg) {
|
2013-01-24 07:52:55 +08:00
|
|
|
if (connection._activeQuery.stream === undefined) {
|
2013-03-07 00:26:40 +08:00
|
|
|
connection._activeQuery._canceledDueToError = new Error('No destination stream defined');
|
|
|
|
(new clientBuilder({port: connection.port, host: connection.host})).cancel(connection, connection._activeQuery);
|
2013-01-24 07:52:55 +08:00
|
|
|
}
|
2013-01-18 06:24:08 +08:00
|
|
|
});
|
2012-11-06 22:55:43 +08:00
|
|
|
connection.on('copyData', function (chunk) {
|
2012-10-08 01:12:30 +08:00
|
|
|
//recieve chunk from connection
|
|
|
|
//move it to stream
|
2012-09-27 18:28:00 +08:00
|
|
|
connection._activeQuery.handleCopyFromChunk(chunk);
|
|
|
|
});
|
2011-02-23 13:52:25 +08:00
|
|
|
return connection;
|
2011-02-24 09:40:52 +08:00
|
|
|
};
|
|
|
|
|
2012-12-11 14:50:29 +08:00
|
|
|
// expose a Query constructor
|
|
|
|
clientBuilder.Query = NativeQuery;
|
|
|
|
|
2011-08-30 12:06:07 +08:00
|
|
|
module.exports = clientBuilder;
|