2010-11-03 13:27:11 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-11-04 05:47:26 +08:00
|
|
|
var sys = require('sys');var sys = require('sys');
|
2010-11-03 13:27:11 +08:00
|
|
|
|
|
|
|
var Query = function(config) {
|
|
|
|
this.text = config.text;
|
|
|
|
this.values = config.values;
|
|
|
|
this.rows = config.rows;
|
|
|
|
this.types = config.types;
|
|
|
|
this.name = config.name;
|
|
|
|
//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-11-15 14:10:21 +08:00
|
|
|
this.callback = config.callback;
|
2010-11-03 13:27:11 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
sys.inherits(Query, EventEmitter);
|
|
|
|
var p = Query.prototype;
|
|
|
|
|
|
|
|
p.requiresPreparation = function() {
|
|
|
|
return (this.values || 0).length > 0 || this.name || this.rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var noParse = function(val) {
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
p.submit = function(connection) {
|
|
|
|
var self = this;
|
|
|
|
if(this.requiresPreparation()) {
|
|
|
|
this.prepare(connection);
|
|
|
|
} else {
|
|
|
|
connection.query(this.text);
|
|
|
|
}
|
2010-11-04 13:21:29 +08:00
|
|
|
var converters = [];
|
|
|
|
var names = [];
|
2010-11-15 14:10:21 +08:00
|
|
|
var rows = [];
|
2010-11-03 13:27:11 +08:00
|
|
|
var handleRowDescription = function(msg) {
|
2010-11-04 13:21:29 +08:00
|
|
|
for(var i = 0; i < msg.fields.length; i++) {
|
|
|
|
converters[i] = dataTypeParsers[msg.fields[i].dataTypeID] || noParse;
|
|
|
|
names[i] = msg.fields[i].name;
|
|
|
|
};
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
|
|
|
var handleDatarow = function(msg) {
|
2010-12-14 07:24:05 +08:00
|
|
|
var result = [];
|
2010-11-03 13:27:11 +08:00
|
|
|
for(var i = 0; i < msg.fields.length; i++) {
|
2010-11-04 12:06:07 +08:00
|
|
|
var rawValue = msg.fields[i];
|
2010-11-04 13:21:29 +08:00
|
|
|
result[names[i]] = rawValue === null ? null : converters[i](rawValue);
|
2010-11-03 13:27:11 +08:00
|
|
|
}
|
2010-11-04 12:06:07 +08:00
|
|
|
self.emit('row', result);
|
2010-11-15 14:42:38 +08:00
|
|
|
|
2010-11-15 14:10:21 +08:00
|
|
|
//if no reciever, buffer rows
|
|
|
|
if(self.callback) {
|
|
|
|
rows.push(result);
|
|
|
|
}
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
2010-11-15 06:50:38 +08:00
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
var onCommandComplete = function() {
|
|
|
|
self.callback(null, {rows: rows});
|
|
|
|
rows = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(self.callback) {
|
|
|
|
connection.on('commandComplete', onCommandComplete)
|
|
|
|
}
|
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
var onError = function(err) {
|
2010-11-03 13:27:11 +08:00
|
|
|
//remove all listeners
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.removeListener('rowDescription', handleDatarow);
|
|
|
|
connection.removeListener('dataRow', handleDatarow);
|
|
|
|
connection.removeListener('error', onError);
|
|
|
|
connection.removeListener('readyForQuery', onReadyForQuery);
|
2010-11-15 14:10:21 +08:00
|
|
|
if(self.callback) {
|
|
|
|
self.callback(err);
|
2010-12-03 07:47:54 +08:00
|
|
|
connection.removeListener('commandComplete', onCommandComplete);
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|
2010-11-15 06:50:38 +08:00
|
|
|
self.emit('error', err);
|
|
|
|
self.emit('end');
|
|
|
|
};
|
|
|
|
|
|
|
|
var onReadyForQuery = function() {
|
2010-11-15 07:53:49 +08:00
|
|
|
//remove all listeners
|
2010-11-03 13:27:11 +08:00
|
|
|
connection.removeListener('rowDescription', handleRowDescription);
|
|
|
|
connection.removeListener('dataRow', handleDatarow);
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.removeListener('readyForQuery', onReadyForQuery);
|
|
|
|
connection.removeListener('error', onError);
|
2010-11-15 14:10:21 +08:00
|
|
|
if(self.callback) {
|
2010-12-03 07:47:54 +08:00
|
|
|
connection.removeListener('commandComplete', onCommandComplete);
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|
2010-11-03 13:27:11 +08:00
|
|
|
self.emit('end');
|
2010-11-15 06:50:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
connection.on('rowDescription', handleRowDescription);
|
|
|
|
connection.on('dataRow', handleDatarow);
|
|
|
|
connection.on('readyForQuery', onReadyForQuery);
|
2010-12-03 07:47:54 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.on('error', onError);
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
p.hasBeenParsed = function(connection) {
|
|
|
|
return this.name && connection.parsedStatements[this.name];
|
|
|
|
};
|
|
|
|
|
|
|
|
p.prepare = function(connection) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if(!this.hasBeenParsed(connection)) {
|
|
|
|
connection.parsedStatements[this.name] = true;
|
|
|
|
connection.parse({
|
|
|
|
text: self.text,
|
|
|
|
name: self.name,
|
|
|
|
types: self.types
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO is there some btter way to prepare values for the database?
|
|
|
|
if(self.values) {
|
|
|
|
self.values = self.values.map(function(val) {
|
|
|
|
return (val instanceof Date) ? JSON.stringify(val) : val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
|
|
|
|
connection.bind({
|
|
|
|
portal: self.name,
|
|
|
|
statement: self.name,
|
|
|
|
values: self.values
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.describe({
|
|
|
|
type: 'P',
|
|
|
|
name: self.name || ""
|
|
|
|
});
|
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
var getRows = function() {
|
|
|
|
connection.execute({
|
|
|
|
portal: self.name,
|
|
|
|
rows: self.rows
|
|
|
|
});
|
|
|
|
connection.flush();
|
|
|
|
};
|
2010-11-03 13:27:11 +08:00
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
getRows();
|
2010-11-03 13:27:11 +08:00
|
|
|
|
|
|
|
//TODO support EmptyQueryResponse, ErrorResponse, and PortalSuspended
|
|
|
|
var onCommandComplete = function() {
|
2010-11-15 07:53:49 +08:00
|
|
|
connection.removeListener('error', onCommandComplete);
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.removeListener('commandComplete', onCommandComplete);
|
2010-11-15 07:53:49 +08:00
|
|
|
connection.removeListener('portalSuspended', getRows);
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.sync();
|
|
|
|
};
|
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
connection.on('portalSuspended', getRows);
|
2010-11-03 13:27:11 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.on('commandComplete', onCommandComplete);
|
2010-11-15 07:53:49 +08:00
|
|
|
connection.on('error', onCommandComplete);
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var dateParser = function(isoDate) {
|
|
|
|
//TODO find some regexp help
|
|
|
|
//this method works but it's ooglay
|
|
|
|
//if you wanna contribute...... ;)
|
|
|
|
var split = isoDate.split(' ');
|
|
|
|
var dateMatcher = /(\d{4})-(\d{2})-(\d{2})/;
|
|
|
|
|
|
|
|
var date = split[0];
|
|
|
|
var time = split[1];
|
|
|
|
var match = dateMatcher.exec(date);
|
|
|
|
var splitDate = date.split('-');
|
|
|
|
var year = match[1];
|
2010-12-11 19:03:29 +08:00
|
|
|
var month = parseInt(match[2],10)-1;
|
2010-11-03 13:27:11 +08:00
|
|
|
var day = match[3];
|
|
|
|
|
|
|
|
var splitTime = time.split(':');
|
2010-12-11 19:03:29 +08:00
|
|
|
var hour = parseInt(splitTime[0],10);
|
|
|
|
var min = parseInt(splitTime[1],10);
|
2010-11-03 13:27:11 +08:00
|
|
|
var end = splitTime[2];
|
|
|
|
var seconds = /(\d{2})/.exec(end);
|
|
|
|
seconds = (seconds ? seconds[1] : 0);
|
2010-12-11 19:03:29 +08:00
|
|
|
seconds = parseInt(seconds,10);
|
2010-11-03 13:27:11 +08:00
|
|
|
var mili = /\.(\d{1,})/.exec(end);
|
|
|
|
mili = mili ? mili[1].slice(0,3) : 0;
|
|
|
|
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(end);
|
|
|
|
//minutes to adjust for timezone
|
|
|
|
var tzAdjust = 0;
|
|
|
|
if(tZone) {
|
|
|
|
var type = tZone[1];
|
|
|
|
switch(type) {
|
|
|
|
case 'Z': break;
|
|
|
|
case '-':
|
2010-12-11 19:03:29 +08:00
|
|
|
tzAdjust = -(((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
|
2010-11-03 13:27:11 +08:00
|
|
|
break;
|
|
|
|
case '+':
|
2010-12-11 19:03:29 +08:00
|
|
|
tzAdjust = (((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
|
2010-11-03 13:27:11 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unidentifed tZone part " + type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
|
|
|
|
|
|
|
|
var date = new Date(utcOffset - (tzAdjust * 60* 1000));
|
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
2010-12-11 19:03:29 +08:00
|
|
|
// To help we test dateParser
|
|
|
|
Query.dateParser = dateParser;
|
|
|
|
|
2010-11-03 13:27:11 +08:00
|
|
|
var dataTypeParsers = {
|
|
|
|
20: parseInt,
|
|
|
|
21: parseInt,
|
|
|
|
23: parseInt,
|
|
|
|
26: parseInt,
|
|
|
|
1700: parseFloat,
|
|
|
|
700: parseFloat,
|
|
|
|
701: parseFloat,
|
|
|
|
16: function(dbVal) { //boolean
|
|
|
|
return dbVal === 't';
|
|
|
|
},
|
|
|
|
1114: dateParser,
|
|
|
|
1184: dateParser
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Query;
|