2010-10-11 12:02:11 +08:00
|
|
|
var sys = require('sys');
|
2010-10-24 01:46:27 +08:00
|
|
|
var net = require('net');
|
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;
|
|
|
|
|
2010-10-24 01:45:37 +08:00
|
|
|
var utils = require(__dirname + '/utils');
|
2010-10-24 03:50:28 +08:00
|
|
|
var BufferList = require(__dirname + '/buffer-list');
|
2010-10-24 08:02:13 +08:00
|
|
|
var Connection = require(__dirname + '/connection');
|
2010-10-24 01:46:27 +08:00
|
|
|
|
2010-10-11 11:37:30 +08:00
|
|
|
var Client = function(config) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
config = config || {};
|
|
|
|
this.user = config.user;
|
|
|
|
this.database = config.database;
|
|
|
|
this.port = config.port || 5432;
|
|
|
|
this.host = config.host;
|
|
|
|
this.queryQueue = [];
|
2010-10-24 08:21:01 +08:00
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
this.connection = config.connection || new Connection({stream: config.stream || new net.Stream()});
|
2010-10-11 11:37:30 +08:00
|
|
|
this.queryQueue = [];
|
2010-10-20 12:29:23 +08:00
|
|
|
this.password = config.password || '';
|
2010-10-20 13:34:16 +08:00
|
|
|
this.lastBuffer = false;
|
|
|
|
this.lastOffset = 0;
|
|
|
|
this.buffer = null;
|
|
|
|
this.offset = null;
|
|
|
|
this.encoding = 'utf8';
|
2010-10-11 11:37:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
sys.inherits(Client, EventEmitter);
|
|
|
|
|
2010-10-11 11:44:13 +08:00
|
|
|
var p = Client.prototype;
|
|
|
|
|
|
|
|
p.connect = function() {
|
2010-10-11 11:37:30 +08:00
|
|
|
var self = this;
|
2010-10-24 08:02:13 +08:00
|
|
|
var con = this.connection;
|
2010-10-24 09:26:24 +08:00
|
|
|
con.connect(this.port, this.host);
|
|
|
|
|
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() {
|
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
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
con.on('readyForQuery', function() {
|
|
|
|
self.readyForQuery = true;
|
2010-10-24 02:12:49 +08:00
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
self.pulseQueryQueue();
|
|
|
|
});
|
2010-10-26 14:51:12 +08:00
|
|
|
|
|
|
|
con.on('error', function(error) {
|
|
|
|
self.emit('error', error);
|
|
|
|
});
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
p.pulseQueryQueue = function() {
|
|
|
|
if(this.readyForQuery===true && this.queryQueue.length > 0) {
|
|
|
|
this.readyForQuery = false;
|
|
|
|
var query = this.queryQueue.shift();
|
2010-10-25 12:32:18 +08:00
|
|
|
query.submit(this.connection);
|
2010-10-11 11:37:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
p.query = function(config) {
|
2010-10-28 07:58:58 +08:00
|
|
|
//can take in strings or config objects
|
2010-10-29 08:09:40 +08:00
|
|
|
var query = new Query((config.text || config.name) ? config : { text: config });
|
2010-10-25 12:32:18 +08:00
|
|
|
this.queryQueue.push(query);
|
2010-10-25 11:52:12 +08:00
|
|
|
this.pulseQueryQueue();
|
2010-10-25 12:32:18 +08:00
|
|
|
return query;
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2010-10-26 10:24:17 +08:00
|
|
|
p.end = function() {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2010-10-25 12:32:18 +08:00
|
|
|
var Query = function(config) {
|
|
|
|
this.text = config.text;
|
2010-10-28 07:58:58 +08:00
|
|
|
this.values = config.values;
|
|
|
|
this.rows = config.rows;
|
|
|
|
this.types = config.types;
|
|
|
|
this.name = config.name;
|
2010-10-27 12:57:36 +08:00
|
|
|
//for code clarity purposes we'll declare this here though it's not
|
|
|
|
//set or used until a rowDescription message comes in
|
|
|
|
this.rowDescription = null;
|
2010-10-25 12:32:18 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
};
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-28 07:58:58 +08:00
|
|
|
sys.inherits(Query, EventEmitter);
|
2010-10-25 12:32:18 +08:00
|
|
|
var p = Query.prototype;
|
|
|
|
|
2010-10-28 13:50:45 +08:00
|
|
|
p.requiresPreparation = function() {
|
2010-10-28 07:58:58 +08:00
|
|
|
return (this.values || 0).length > 0 || this.name || this.rows;
|
|
|
|
};
|
|
|
|
|
2010-10-25 12:32:18 +08:00
|
|
|
p.submit = function(connection) {
|
|
|
|
var self = this;
|
2010-10-28 13:50:45 +08:00
|
|
|
if(this.requiresPreparation()) {
|
|
|
|
this.prepare(connection);
|
2010-10-28 07:58:58 +08:00
|
|
|
} else {
|
|
|
|
connection.query(this.text);
|
|
|
|
}
|
2010-10-25 12:32:18 +08:00
|
|
|
var handleRowDescription = function(msg) {
|
2010-10-27 12:57:36 +08:00
|
|
|
self.onRowDescription(msg);
|
2010-10-25 12:32:18 +08:00
|
|
|
};
|
|
|
|
var handleDatarow = function(msg) {
|
2010-10-27 12:57:36 +08:00
|
|
|
self.onDataRow(msg);
|
2010-10-25 12:32:18 +08:00
|
|
|
};
|
|
|
|
connection.on('rowDescription', handleRowDescription);
|
|
|
|
connection.on('dataRow', handleDatarow);
|
|
|
|
connection.once('readyForQuery', function() {
|
|
|
|
//remove all listeners
|
|
|
|
connection.removeListener('rowDescription', handleRowDescription);
|
|
|
|
connection.removeListener('dataRow', handleDatarow);
|
|
|
|
self.emit('end');
|
|
|
|
});
|
|
|
|
};
|
2010-10-26 10:24:17 +08:00
|
|
|
|
2010-10-29 08:09:40 +08:00
|
|
|
p.hasBeenParsed = function(connection) {
|
|
|
|
return this.name && connection.parsedStatements[this.name];
|
|
|
|
};
|
|
|
|
|
2010-10-28 13:50:45 +08:00
|
|
|
p.prepare = function(connection) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var onParseComplete = function() {
|
|
|
|
connection.bind({
|
|
|
|
portal: self.name,
|
|
|
|
statement: self.name,
|
|
|
|
values: self.values
|
|
|
|
});
|
|
|
|
connection.flush();
|
|
|
|
};
|
|
|
|
|
2010-10-29 08:09:40 +08:00
|
|
|
|
|
|
|
if(this.hasBeenParsed(connection)) {
|
|
|
|
onParseComplete();
|
|
|
|
} else {
|
|
|
|
connection.parsedStatements[this.name] = true;
|
|
|
|
connection.parse({
|
|
|
|
text: self.text,
|
|
|
|
name: self.name,
|
|
|
|
types: self.types
|
|
|
|
});
|
|
|
|
connection.flush();
|
|
|
|
connection.once('parseComplete', onParseComplete);
|
|
|
|
}
|
|
|
|
|
2010-10-28 13:50:45 +08:00
|
|
|
|
|
|
|
var onBindComplete = function() {
|
|
|
|
connection.describe({
|
|
|
|
type: 'P',
|
|
|
|
name: self.name || ""
|
|
|
|
});
|
|
|
|
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
|
|
|
|
//TODO get ourselves a rowDescription for result type coercion
|
|
|
|
connection.execute({
|
|
|
|
portal: self.name,
|
|
|
|
rows: self.rows
|
|
|
|
});
|
|
|
|
connection.flush();
|
|
|
|
};
|
|
|
|
|
|
|
|
connection.once('bindComplete', onBindComplete);
|
|
|
|
|
|
|
|
//TODO support EmptyQueryResponse, ErrorResponse, and PortalSuspended
|
|
|
|
var onCommandComplete = function() {
|
|
|
|
connection.sync();
|
|
|
|
};
|
|
|
|
connection.once('commandComplete', onCommandComplete);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-10-27 12:57:36 +08:00
|
|
|
p.onRowDescription = function(msg) {
|
|
|
|
var typeIds = msg.fields.map(function(field) {
|
|
|
|
return field.dataTypeID;
|
|
|
|
});
|
|
|
|
var noParse = function(val) {
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.converters = typeIds.map(function(typeId) {
|
|
|
|
return Client.dataTypeParser[typeId] || noParse;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
//handles the raw 'dataRow' event from the connection does type coercion
|
|
|
|
p.onDataRow = function(msg) {
|
|
|
|
var fields = msg.fields;
|
|
|
|
var converters = this.converters || [];
|
|
|
|
var len = msg.fields.length;
|
|
|
|
for(var i = 0; i < len; i++) {
|
|
|
|
fields[i] = this.converters[i] (fields[i]);
|
|
|
|
}
|
|
|
|
msg.fields = fields;
|
|
|
|
this.emit('row', msg);
|
|
|
|
};
|
|
|
|
|
2010-10-26 10:24:17 +08:00
|
|
|
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
// var intParser = {
|
|
|
|
// fromDbValue: parseInt
|
|
|
|
// };
|
|
|
|
|
|
|
|
// var floatParser = {
|
|
|
|
// fromDbValue: parseFloat
|
|
|
|
// };
|
|
|
|
|
|
|
|
// var timeParser = {
|
|
|
|
// fromDbValue: function(isoTime) {
|
|
|
|
// var when = new Date();
|
|
|
|
// var split = isoTime.split(':');
|
|
|
|
// when.setHours(split[0]);
|
|
|
|
// when.setMinutes(split[1]);
|
|
|
|
// when.setSeconds(split[2].split('-') [0]);
|
|
|
|
// return when;
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// var dateParser = {
|
|
|
|
// fromDbValue: function(isoDate) {
|
|
|
|
// return Date.parse(isoDate);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
2010-10-27 12:57:36 +08:00
|
|
|
Client.dataTypeParser = {
|
|
|
|
20: parseInt,
|
|
|
|
21: parseInt,
|
|
|
|
23: parseInt,
|
2010-10-27 13:15:58 +08:00
|
|
|
26: parseInt,
|
|
|
|
1700: parseFloat,
|
|
|
|
700: parseFloat,
|
2010-10-27 13:31:34 +08:00
|
|
|
701: parseFloat,
|
|
|
|
16: function(dbVal) {
|
|
|
|
return dbVal === 't';
|
|
|
|
}
|
2010-10-27 12:57:36 +08:00
|
|
|
// 1083: timeParser,
|
|
|
|
// 1266: timeParser,
|
|
|
|
// 1114: dateParser,
|
|
|
|
// 1184: dateParser
|
|
|
|
};
|
2010-10-25 11:52:12 +08:00
|
|
|
|
|
|
|
// p.processRowDescription = function(description) {
|
|
|
|
// this.fields = description.fields;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// p.processDataRow = function(dataRow) {
|
|
|
|
// var row = dataRow.fields;
|
|
|
|
// var fields = this.fields || [];
|
|
|
|
// var field, dataType;
|
|
|
|
// for(var i = 0, len = row.length; i < len; i++) {
|
|
|
|
// field = fields[i] || 0
|
|
|
|
// var dataType = Client.dataTypes[field.dataTypeID];
|
|
|
|
// if(dataType) {
|
|
|
|
// row[i] = dataType.fromDbValue(row[i]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// this.emit('row',row);
|
|
|
|
// };
|
2010-10-24 02:12:49 +08:00
|
|
|
|
2010-10-20 13:21:13 +08:00
|
|
|
//end parsing methods
|
2010-10-11 11:37:30 +08:00
|
|
|
module.exports = Client;
|