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');
|
2011-01-19 12:39:07 +08:00
|
|
|
var Result = require(__dirname + "/result");
|
2011-01-29 20:58:18 +08:00
|
|
|
var TextParser = require(__dirname + "/textParser");
|
|
|
|
var BinaryParser = require(__dirname + "/binaryParser");
|
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;
|
|
|
|
};
|
|
|
|
|
2011-01-24 09:01:28 +08:00
|
|
|
//creates datarow metatdata from the supplied
|
|
|
|
//data row information
|
|
|
|
var buildDataRowMetadata = function(msg, converters, names) {
|
2011-01-29 20:58:18 +08:00
|
|
|
var parsers = {
|
|
|
|
text: new TextParser(),
|
|
|
|
binary: new BinaryParser()
|
|
|
|
};
|
|
|
|
|
2011-01-24 09:01:28 +08:00
|
|
|
var len = msg.fields.length;
|
|
|
|
for(var i = 0; i < len; i++) {
|
|
|
|
var field = msg.fields[i];
|
|
|
|
var dataTypeId = field.dataTypeID;
|
2011-01-29 20:58:18 +08:00
|
|
|
var format = field.format;
|
2011-01-24 09:01:28 +08:00
|
|
|
names[i] = field.name;
|
|
|
|
switch(dataTypeId) {
|
|
|
|
case 20:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseInt64;
|
2011-01-29 08:19:33 +08:00
|
|
|
break;
|
2011-01-24 09:01:28 +08:00
|
|
|
case 21:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseInt16;
|
2011-01-29 08:19:33 +08:00
|
|
|
break;
|
2011-01-24 09:01:28 +08:00
|
|
|
case 23:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseInt32;
|
2011-01-29 08:19:33 +08:00
|
|
|
break;
|
2011-01-24 09:01:28 +08:00
|
|
|
case 26:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseInt64;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
case 700:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseFloat32;
|
2011-01-29 08:32:41 +08:00
|
|
|
break;
|
2011-01-24 09:01:28 +08:00
|
|
|
case 701:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseFloat64;
|
|
|
|
break;
|
|
|
|
case 1700:
|
|
|
|
converters[i] = parsers[format].parseNumeric;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
case 16:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseBool;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
case 1114:
|
|
|
|
case 1184:
|
2011-01-29 20:58:18 +08:00
|
|
|
converters[i] = parsers[format].parseDate;
|
2011-01-29 08:19:33 +08:00
|
|
|
break;
|
|
|
|
case 1008:
|
2011-01-29 20:58:18 +08:00
|
|
|
case 1009:
|
|
|
|
converters[i] = parsers[format].parseStringArray;
|
|
|
|
break;
|
|
|
|
case 1007:
|
|
|
|
converters[i] = parsers[format].parseIntArray;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
converters[i] = dataTypeParsers[dataTypeId] || noParse;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-11-03 13:27:11 +08:00
|
|
|
p.submit = function(connection) {
|
|
|
|
var self = this;
|
|
|
|
if(this.requiresPreparation()) {
|
|
|
|
this.prepare(connection);
|
|
|
|
} else {
|
|
|
|
connection.query(this.text);
|
|
|
|
}
|
2011-01-24 09:01:28 +08:00
|
|
|
|
2010-11-04 13:21:29 +08:00
|
|
|
var converters = [];
|
|
|
|
var names = [];
|
2010-11-03 13:27:11 +08:00
|
|
|
var handleRowDescription = function(msg) {
|
2011-01-24 09:01:28 +08:00
|
|
|
buildDataRowMetadata(msg, converters, names);
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
2011-01-24 09:01:28 +08:00
|
|
|
|
|
|
|
var result = new Result();
|
|
|
|
|
2010-11-03 13:27:11 +08:00
|
|
|
var handleDatarow = function(msg) {
|
2011-01-19 13:03:24 +08:00
|
|
|
var row = {};
|
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];
|
2011-01-19 13:03:24 +08:00
|
|
|
row[names[i]] = rawValue === null ? null : converters[i](rawValue);
|
2010-11-03 13:27:11 +08:00
|
|
|
}
|
2011-01-19 13:03:24 +08:00
|
|
|
self.emit('row', row);
|
2010-11-15 14:42:38 +08:00
|
|
|
|
2011-01-19 13:03:24 +08:00
|
|
|
//if there is a callback collect rows
|
2010-11-15 14:10:21 +08:00
|
|
|
if(self.callback) {
|
2011-01-19 13:03:24 +08:00
|
|
|
result.addRow(row);
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|
2010-11-03 13:27:11 +08:00
|
|
|
};
|
2010-11-15 06:50:38 +08:00
|
|
|
|
2011-01-19 13:03:24 +08:00
|
|
|
var onCommandComplete = function(msg) {
|
|
|
|
result.addCommandComplete(msg);
|
|
|
|
};
|
2010-12-03 07:47:54 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
var onError = function(err) {
|
2010-11-03 13:27:11 +08:00
|
|
|
//remove all listeners
|
2011-01-24 09:01:28 +08:00
|
|
|
removeListeners();
|
2010-11-15 14:10:21 +08:00
|
|
|
if(self.callback) {
|
|
|
|
self.callback(err);
|
2010-12-17 13:50:36 +08:00
|
|
|
} else {
|
|
|
|
self.emit('error', err);
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|
2010-11-15 06:50:38 +08:00
|
|
|
self.emit('end');
|
|
|
|
};
|
|
|
|
|
|
|
|
var onReadyForQuery = function() {
|
2011-01-24 09:01:28 +08:00
|
|
|
removeListeners();
|
|
|
|
if(self.callback) {
|
|
|
|
self.callback(null, result);
|
|
|
|
}
|
|
|
|
self.emit('end', result);
|
|
|
|
};
|
|
|
|
|
|
|
|
var removeListeners = 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);
|
2011-01-19 13:03:24 +08:00
|
|
|
connection.removeListener('commandComplete', onCommandComplete);
|
2010-11-15 06:50:38 +08:00
|
|
|
connection.removeListener('error', onError);
|
|
|
|
};
|
|
|
|
|
|
|
|
connection.on('rowDescription', handleRowDescription);
|
|
|
|
connection.on('dataRow', handleDatarow);
|
|
|
|
connection.on('readyForQuery', onReadyForQuery);
|
2011-01-19 13:03:24 +08:00
|
|
|
connection.on('commandComplete', onCommandComplete);
|
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.parse({
|
|
|
|
text: self.text,
|
|
|
|
name: self.name,
|
|
|
|
types: self.types
|
|
|
|
});
|
2011-01-24 09:01:28 +08:00
|
|
|
connection.parsedStatements[this.name] = true;
|
2010-11-03 13:27:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
|
|
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 dataTypeParsers = {
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Query;
|