2010-10-23 07:16:40 +08:00
|
|
|
var crypto = require('crypto');
|
2010-10-24 01:46:27 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2011-02-05 10:05:02 +08:00
|
|
|
|
2013-01-22 06:14:19 +08:00
|
|
|
var ConnectionParameters = require(__dirname + '/connection-parameters');
|
2010-11-03 13:27:11 +08:00
|
|
|
var Query = require(__dirname + '/query');
|
2010-11-21 04:09:18 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
2010-10-24 08:02:13 +08:00
|
|
|
var Connection = require(__dirname + '/connection');
|
2012-09-27 18:28:00 +08:00
|
|
|
var CopyFromStream = require(__dirname + '/copystream').CopyFromStream;
|
|
|
|
var CopyToStream = require(__dirname + '/copystream').CopyToStream;
|
2013-01-24 08:15:37 +08:00
|
|
|
|
2010-10-11 11:37:30 +08:00
|
|
|
var Client = function(config) {
|
|
|
|
EventEmitter.call(this);
|
2013-01-22 06:14:19 +08:00
|
|
|
|
|
|
|
this.connectionParameters = new ConnectionParameters(config);
|
|
|
|
this.user = this.connectionParameters.user;
|
|
|
|
this.database = this.connectionParameters.database;
|
|
|
|
this.port = this.connectionParameters.port;
|
|
|
|
this.host = this.connectionParameters.host;
|
|
|
|
this.password = this.connectionParameters.password;
|
|
|
|
|
2013-03-07 00:26:40 +08:00
|
|
|
var c = config || {};
|
2013-01-22 06:14:19 +08:00
|
|
|
|
2013-03-07 00:26:40 +08:00
|
|
|
this.connection = c.connection || new Connection({
|
|
|
|
stream: c.stream,
|
|
|
|
ssl: c.ssl
|
2012-09-11 10:40:41 +08:00
|
|
|
});
|
2010-10-11 11:37:30 +08:00
|
|
|
this.queryQueue = [];
|
2013-03-07 00:26:40 +08:00
|
|
|
this.binary = c.binary || defaults.binary;
|
2010-10-20 13:34:16 +08:00
|
|
|
this.encoding = 'utf8';
|
2011-11-02 23:07:14 +08:00
|
|
|
this.processID = null;
|
|
|
|
this.secretKey = null;
|
2013-03-07 00:26:40 +08:00
|
|
|
this.ssl = c.ssl || false;
|
2010-10-11 11:37:30 +08:00
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(Client, EventEmitter);
|
2010-10-11 11:37:30 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Client.prototype.connect = function(callback) {
|
2010-10-11 11:37:30 +08:00
|
|
|
var self = this;
|
2010-10-24 08:02:13 +08:00
|
|
|
var con = this.connection;
|
2011-03-02 04:35:14 +08:00
|
|
|
if(this.host && this.host.indexOf('/') === 0) {
|
|
|
|
con.connect(this.host + '/.s.PGSQL.' + this.port);
|
|
|
|
} else {
|
|
|
|
con.connect(this.port, this.host);
|
|
|
|
}
|
|
|
|
|
2010-10-24 09:26:24 +08:00
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//once connection is established send startup message
|
2010-10-24 09:26:24 +08:00
|
|
|
con.on('connect', function() {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(self.ssl) {
|
2012-08-07 19:51:37 +08:00
|
|
|
con.requestSsl();
|
|
|
|
} else {
|
|
|
|
con.startup({
|
|
|
|
user: self.user,
|
|
|
|
database: self.database
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-08-07 19:51:37 +08:00
|
|
|
con.on('sslconnect', function() {
|
2010-10-25 10:28:10 +08:00
|
|
|
con.startup({
|
2010-10-24 09:26:24 +08:00
|
|
|
user: self.user,
|
|
|
|
database: self.database
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//password request handling
|
2010-10-24 08:02:13 +08:00
|
|
|
con.on('authenticationCleartextPassword', function() {
|
2010-10-25 10:26:18 +08:00
|
|
|
con.password(self.password);
|
2010-10-20 12:29:23 +08:00
|
|
|
});
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//password request handling
|
2010-10-24 08:21:01 +08:00
|
|
|
con.on('authenticationMD5Password', function(msg) {
|
2010-10-24 09:26:24 +08:00
|
|
|
var inner = Client.md5(self.password + self.user);
|
|
|
|
var outer = Client.md5(inner + msg.salt.toString('binary'));
|
2010-10-24 08:21:01 +08:00
|
|
|
var md5password = "md5" + outer;
|
2010-10-25 10:26:18 +08:00
|
|
|
con.password(md5password);
|
2010-10-23 07:16:40 +08:00
|
|
|
});
|
2010-10-24 08:21:01 +08:00
|
|
|
|
2011-11-02 23:07:14 +08:00
|
|
|
con.once('backendKeyData', function(msg) {
|
2012-03-23 04:32:56 +08:00
|
|
|
self.processID = msg.processID;
|
|
|
|
self.secretKey = msg.secretKey;
|
|
|
|
});
|
2011-11-02 23:07:14 +08:00
|
|
|
|
2011-02-05 10:03:41 +08:00
|
|
|
//hook up query handling events to connection
|
|
|
|
//after the connection initially becomes ready for queries
|
2011-02-05 08:51:23 +08:00
|
|
|
con.once('readyForQuery', function() {
|
2011-02-05 10:03:41 +08:00
|
|
|
//delegate row descript to active query
|
2011-02-05 09:03:23 +08:00
|
|
|
con.on('rowDescription', function(msg) {
|
|
|
|
self.activeQuery.handleRowDescription(msg);
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2011-02-05 10:03:41 +08:00
|
|
|
//delegate datarow to active query
|
2011-02-05 09:03:23 +08:00
|
|
|
con.on('dataRow', function(msg) {
|
|
|
|
self.activeQuery.handleDataRow(msg);
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2011-02-05 10:03:41 +08:00
|
|
|
//TODO should query gain access to connection?
|
2011-02-05 09:30:30 +08:00
|
|
|
con.on('portalSuspended', function(msg) {
|
|
|
|
self.activeQuery.getRows(con);
|
|
|
|
});
|
|
|
|
|
2011-02-05 09:03:23 +08:00
|
|
|
con.on('commandComplete', function(msg) {
|
2011-02-05 10:03:41 +08:00
|
|
|
//delegate command complete to query
|
2011-02-05 09:03:23 +08:00
|
|
|
self.activeQuery.handleCommandComplete(msg);
|
2011-02-05 09:30:30 +08:00
|
|
|
//need to sync after each command complete of a prepared statement
|
|
|
|
if(self.activeQuery.isPreparedStatement) {
|
|
|
|
con.sync();
|
|
|
|
}
|
2011-02-05 09:15:57 +08:00
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-09-27 18:28:00 +08:00
|
|
|
con.on('copyInResponse', function(msg) {
|
|
|
|
self.activeQuery.streamData(self.connection);
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2013-01-18 06:24:08 +08:00
|
|
|
con.on('copyOutResponse', function(msg) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(self.activeQuery.stream === undefined) {
|
2013-01-24 08:15:37 +08:00
|
|
|
self.activeQuery._canceledDueToError =
|
|
|
|
new Error('No destination stream defined');
|
|
|
|
//canceling query requires creation of new connection
|
2013-01-18 19:55:35 +08:00
|
|
|
//look for postgres frontend/backend protocol
|
2013-01-24 08:15:37 +08:00
|
|
|
(new self.constructor({port: self.port, host: self.host}))
|
|
|
|
.cancel(self, self.activeQuery);
|
2013-01-18 06:24:08 +08:00
|
|
|
}
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-09-27 18:28:00 +08:00
|
|
|
con.on('copyData', function (msg) {
|
|
|
|
self.activeQuery.handleCopyFromChunk(msg.chunk);
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2011-09-21 23:41:07 +08:00
|
|
|
if (!callback) {
|
|
|
|
self.emit('connect');
|
|
|
|
} else {
|
|
|
|
callback(null,self);
|
|
|
|
//remove callback for proper error handling after the connect event
|
|
|
|
callback = null;
|
|
|
|
}
|
2011-02-05 09:15:57 +08:00
|
|
|
|
2011-03-05 03:30:19 +08:00
|
|
|
con.on('notification', function(msg) {
|
|
|
|
self.emit('notification', msg);
|
2011-09-21 23:41:07 +08:00
|
|
|
});
|
2011-03-05 03:30:19 +08:00
|
|
|
|
2011-02-05 10:03:41 +08:00
|
|
|
});
|
2011-02-05 08:51:23 +08:00
|
|
|
|
2011-02-05 10:03:41 +08:00
|
|
|
con.on('readyForQuery', function() {
|
2013-04-19 22:09:28 +08:00
|
|
|
var error;
|
2011-02-05 10:03:41 +08:00
|
|
|
if(self.activeQuery) {
|
2013-04-19 22:09:28 +08:00
|
|
|
//try/catch/rethrow to ensure exceptions don't prevent the queryQueue from
|
|
|
|
//being processed
|
|
|
|
try{
|
|
|
|
self.activeQuery.handleReadyForQuery();
|
|
|
|
} catch(e) {
|
|
|
|
error = e;
|
|
|
|
}
|
2011-02-05 10:03:41 +08:00
|
|
|
}
|
2012-05-08 22:07:43 +08:00
|
|
|
self.activeQuery = null;
|
2011-02-05 10:03:41 +08:00
|
|
|
self.readyForQuery = true;
|
2011-02-05 10:07:59 +08:00
|
|
|
self._pulseQueryQueue();
|
2013-04-19 22:09:28 +08:00
|
|
|
if(error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2010-10-25 11:52:12 +08:00
|
|
|
});
|
2010-10-26 14:51:12 +08:00
|
|
|
|
|
|
|
con.on('error', function(error) {
|
2010-11-15 06:50:38 +08:00
|
|
|
if(!self.activeQuery) {
|
2011-09-21 23:41:07 +08:00
|
|
|
if(!callback) {
|
|
|
|
self.emit('error', error);
|
|
|
|
} else {
|
|
|
|
callback(error);
|
|
|
|
}
|
2011-02-05 09:15:57 +08:00
|
|
|
} else {
|
2011-02-05 09:30:30 +08:00
|
|
|
//need to sync after error during a prepared statement
|
|
|
|
if(self.activeQuery.isPreparedStatement) {
|
|
|
|
con.sync();
|
|
|
|
}
|
2013-04-19 22:25:53 +08:00
|
|
|
var activeQuery = self.activeQuery;
|
2011-02-05 09:15:57 +08:00
|
|
|
self.activeQuery = null;
|
2013-04-19 22:25:53 +08:00
|
|
|
activeQuery.handleError(error);
|
2010-11-15 06:50:38 +08:00
|
|
|
}
|
2010-10-26 14:51:12 +08:00
|
|
|
});
|
2011-03-04 07:30:17 +08:00
|
|
|
|
2013-03-29 02:24:33 +08:00
|
|
|
con.once('end', function() {
|
|
|
|
if(self.activeQuery) {
|
|
|
|
self.activeQuery.handleError(new Error('Stream unexpectedly ended during query execution'));
|
|
|
|
self.activeQuery = null;
|
|
|
|
}
|
|
|
|
self.emit('end');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2011-03-04 07:30:17 +08:00
|
|
|
con.on('notice', function(msg) {
|
|
|
|
self.emit('notice', msg);
|
2011-09-21 23:41:07 +08:00
|
|
|
});
|
2011-03-04 07:30:17 +08:00
|
|
|
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Client.prototype.cancel = function(client, query) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(client.activeQuery == query) {
|
2012-03-23 04:32:56 +08:00
|
|
|
var con = this.connection;
|
|
|
|
|
|
|
|
if(this.host && this.host.indexOf('/') === 0) {
|
|
|
|
con.connect(this.host + '/.s.PGSQL.' + this.port);
|
|
|
|
} else {
|
|
|
|
con.connect(this.port, this.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
//once connection is established send cancel message
|
|
|
|
con.on('connect', function() {
|
|
|
|
con.cancel(client.processID, client.secretKey);
|
|
|
|
});
|
2013-03-07 00:26:40 +08:00
|
|
|
} else if(client.queryQueue.indexOf(query) != -1) {
|
2012-03-23 04:32:56 +08:00
|
|
|
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
|
2012-12-11 12:44:58 +08:00
|
|
|
}
|
2011-11-02 23:07:14 +08:00
|
|
|
};
|
|
|
|
|
2013-07-12 05:49:51 +08:00
|
|
|
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
|
|
Client.prototype.escapeIdentifier = function(str) {
|
|
|
|
|
|
|
|
var escaped = '"';
|
|
|
|
|
|
|
|
for(var i = 0; i < str.length; i++) {
|
|
|
|
var c = str[i];
|
|
|
|
if(c === '"') {
|
|
|
|
escaped += c + c;
|
|
|
|
} else {
|
|
|
|
escaped += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped += '"';
|
|
|
|
|
|
|
|
return escaped;
|
2013-07-12 07:23:59 +08:00
|
|
|
};
|
2013-07-12 05:49:51 +08:00
|
|
|
|
|
|
|
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
|
|
Client.prototype.escapeLiteral = function(str) {
|
|
|
|
|
|
|
|
var hasBackslash = false;
|
|
|
|
var escaped = '\'';
|
|
|
|
|
|
|
|
for(var i = 0; i < str.length; i++) {
|
|
|
|
var c = str[i];
|
|
|
|
if(c === '\'') {
|
|
|
|
escaped += c + c;
|
|
|
|
} else if (c === '\\') {
|
|
|
|
escaped += c + c;
|
|
|
|
hasBackslash = true;
|
|
|
|
} else {
|
|
|
|
escaped += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped += '\'';
|
|
|
|
|
|
|
|
if(hasBackslash === true) {
|
|
|
|
escaped = ' E' + escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped;
|
2013-07-12 07:23:59 +08:00
|
|
|
};
|
2013-07-12 05:49:51 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Client.prototype._pulseQueryQueue = function() {
|
2010-10-30 08:48:31 +08:00
|
|
|
if(this.readyForQuery===true) {
|
2011-02-05 10:03:41 +08:00
|
|
|
this.activeQuery = this.queryQueue.shift();
|
|
|
|
if(this.activeQuery) {
|
2010-10-30 08:48:31 +08:00
|
|
|
this.readyForQuery = false;
|
2010-12-03 07:47:54 +08:00
|
|
|
this.hasExecuted = true;
|
2011-02-05 10:03:41 +08:00
|
|
|
this.activeQuery.submit(this.connection);
|
2010-12-03 07:47:54 +08:00
|
|
|
} else if(this.hasExecuted) {
|
2010-12-11 06:56:10 +08:00
|
|
|
this.activeQuery = null;
|
2013-03-08 06:12:09 +08:00
|
|
|
this.emit('drain');
|
2010-10-30 08:48:31 +08:00
|
|
|
}
|
2010-10-11 11:37:30 +08:00
|
|
|
}
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Client.prototype._copy = function (text, stream) {
|
2013-03-07 00:26:40 +08:00
|
|
|
var config = {};
|
2012-09-27 18:28:00 +08:00
|
|
|
config.text = text;
|
|
|
|
config.stream = stream;
|
|
|
|
config.callback = function (error) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(error) {
|
2012-09-27 18:28:00 +08:00
|
|
|
config.stream.error(error);
|
|
|
|
} else {
|
|
|
|
config.stream.close();
|
|
|
|
}
|
2013-01-21 21:40:18 +08:00
|
|
|
};
|
2013-03-07 00:26:40 +08:00
|
|
|
var query = new Query(config);
|
2012-09-27 18:28:00 +08:00
|
|
|
this.queryQueue.push(query);
|
|
|
|
this._pulseQueryQueue();
|
|
|
|
return config.stream;
|
2010-10-11 11:37:30 +08:00
|
|
|
|
2012-09-27 18:28:00 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Client.prototype.copyFrom = function (text) {
|
2012-09-27 18:28:00 +08:00
|
|
|
return this._copy(text, new CopyFromStream());
|
2013-01-21 21:40:18 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Client.prototype.copyTo = function (text) {
|
2012-09-27 18:28:00 +08:00
|
|
|
return this._copy(text, new CopyToStream());
|
2013-01-21 21:40:18 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Client.prototype.query = function(config, values, callback) {
|
2012-08-10 07:31:32 +08:00
|
|
|
//can take in strings, config object or query object
|
2013-01-24 08:15:37 +08:00
|
|
|
var query = (config instanceof Query) ? config :
|
|
|
|
new Query(config, values, callback);
|
2013-03-07 00:26:40 +08:00
|
|
|
if(this.binary && !query.binary) {
|
2012-08-10 07:31:32 +08:00
|
|
|
query.binary = true;
|
2011-11-21 18:45:55 +08:00
|
|
|
}
|
2011-02-05 09:45:30 +08:00
|
|
|
|
2010-10-25 12:32:18 +08:00
|
|
|
this.queryQueue.push(query);
|
2011-02-05 10:07:59 +08:00
|
|
|
this._pulseQueryQueue();
|
2010-10-25 12:32:18 +08:00
|
|
|
return query;
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Client.prototype.end = function() {
|
2010-10-26 10:24:17 +08:00
|
|
|
this.connection.end();
|
|
|
|
};
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
Client.md5 = function(string) {
|
|
|
|
return crypto.createHash('md5').update(string).digest('hex');
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2012-12-11 14:50:29 +08:00
|
|
|
// expose a Query constructor
|
|
|
|
Client.Query = Query;
|
|
|
|
|
2010-10-11 11:37:30 +08:00
|
|
|
module.exports = Client;
|