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");
|
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;
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldNames = [];
|
|
|
|
this._fieldConverters = [];
|
|
|
|
this._result = new Result();
|
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-02-05 08:51:34 +08:00
|
|
|
//associates row metadata from the supplied
|
|
|
|
//message with this query object
|
|
|
|
//metadata used when parsing row results
|
|
|
|
p.handleRowDescription = function(msg) {
|
|
|
|
this._fieldNames = [];
|
|
|
|
this._fieldConverters = [];
|
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-02-05 08:51:34 +08:00
|
|
|
this._fieldNames[i] = field.name;
|
2011-01-24 09:01:28 +08:00
|
|
|
switch(dataTypeId) {
|
|
|
|
case 20:
|
|
|
|
case 21:
|
|
|
|
case 23:
|
|
|
|
case 26:
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldConverters[i] = parseInt;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
case 1700:
|
|
|
|
case 700:
|
|
|
|
case 701:
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldConverters[i] = parseFloat;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
case 16:
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldConverters[i] = function(val) {
|
2011-01-24 09:01:28 +08:00
|
|
|
return val === 't';
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 1114:
|
|
|
|
case 1184:
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldConverters[i] = dateParser;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-02-05 08:51:34 +08:00
|
|
|
this._fieldConverters[i] = dataTypeParsers[dataTypeId] || noParse;
|
2011-01-24 09:01:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2011-02-05 08:51:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
p.handleDataRow = function(msg) {
|
|
|
|
var self = this;
|
|
|
|
var row = {};
|
|
|
|
for(var i = 0; i < msg.fields.length; i++) {
|
|
|
|
var rawValue = msg.fields[i];
|
|
|
|
if(rawValue === null) {
|
|
|
|
//leave null values alone
|
|
|
|
row[self._fieldNames[i]] = null;
|
|
|
|
} else {
|
|
|
|
//convert value to javascript
|
|
|
|
row[self._fieldNames[i]] = self._fieldConverters[i](rawValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.emit('row', row);
|
|
|
|
|
|
|
|
//if there is a callback collect rows
|
|
|
|
if(self.callback) {
|
|
|
|
self._result.addRow(row);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
p.handleCommandComplete = function(msg) {
|
|
|
|
this._result.addCommandComplete(msg);
|
|
|
|
};
|
2011-01-24 09:01:28 +08:00
|
|
|
|
2011-02-05 09:15:57 +08:00
|
|
|
p.handleReadyForQuery = function() {
|
|
|
|
if(this.callback) {
|
|
|
|
this.callback(null, this._result);
|
|
|
|
}
|
|
|
|
this.emit('end', this._result);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.handleError = function(err) {
|
|
|
|
//if callback supplied do not emit error event as uncaught error
|
|
|
|
//events will bubble up to node process
|
|
|
|
if(this.callback) {
|
|
|
|
this.callback(err)
|
|
|
|
} else {
|
|
|
|
this.emit('error', err);
|
|
|
|
}
|
|
|
|
this.emit('end');
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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) {
|
2011-01-24 13:58:03 +08:00
|
|
|
//TODO this could do w/ a refactor
|
|
|
|
|
|
|
|
var dateMatcher = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/;
|
|
|
|
|
|
|
|
var match = dateMatcher.exec(isoDate);
|
2010-11-03 13:27:11 +08:00
|
|
|
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];
|
2011-01-24 13:58:03 +08:00
|
|
|
var hour = parseInt(match[4],10);
|
|
|
|
var min = parseInt(match[5],10);
|
|
|
|
var seconds = parseInt(match[6], 10);
|
|
|
|
|
|
|
|
var miliString = match[7];
|
|
|
|
var mili = 0;
|
|
|
|
if(miliString) {
|
|
|
|
mili = 1000 * parseFloat(miliString);
|
|
|
|
}
|
2010-11-03 13:27:11 +08:00
|
|
|
|
2011-01-24 13:58:03 +08:00
|
|
|
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(isoDate.split(' ')[1]);
|
2010-11-03 13:27:11 +08:00
|
|
|
//minutes to adjust for timezone
|
|
|
|
var tzAdjust = 0;
|
2011-01-24 13:58:03 +08:00
|
|
|
|
2010-11-03 13:27:11 +08:00
|
|
|
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 = {
|
|
|
|
};
|
|
|
|
|
2011-01-24 09:01:28 +08:00
|
|
|
//TODO document this public method
|
2011-01-22 06:53:24 +08:00
|
|
|
Query.registerParser = function(typeOid, parseFunction) {
|
|
|
|
dataTypeParsers[typeOid] = parseFunction;
|
|
|
|
};
|
|
|
|
|
|
|
|
//parses integer arrays
|
|
|
|
Query.registerParser(1007, function(val) {
|
|
|
|
return JSON.parse(val.replace("{","[").replace("}","]"));
|
|
|
|
});
|
|
|
|
|
|
|
|
//parses string arrays
|
|
|
|
//this only works in happy cases
|
|
|
|
//does not yet support strings with , or { or }
|
|
|
|
Query.registerParser(1009, function(val) {
|
|
|
|
if (!val) return null;
|
|
|
|
if (val[0] !== '{' || val[val.length-1] !== '}')
|
|
|
|
throw "Not postgresql array! (" + arrStr + ")";
|
|
|
|
|
|
|
|
var x = val.substring(1, val.length - 1);
|
|
|
|
x = x.match(/(NULL|[^,]+|"((?:.|\n|\r)*?)(?!\\)"|\{((?:.|\n|\r)*?(?!\\)\}) (,|$))/mg);
|
|
|
|
if (x === null) throw "Not postgre array";
|
|
|
|
return x.map(function (el) {
|
|
|
|
if (el === 'NULL') return null;
|
|
|
|
if (el[0] === '{') return arguments.callee(el);
|
|
|
|
if (el[0] === '\"') return el.substring(1, el.length - 1).replace('\\\"', '\"');
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
});
|
2010-11-03 13:27:11 +08:00
|
|
|
|
|
|
|
module.exports = Query;
|