2010-10-11 11:37:30 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-10-11 12:01:30 +08:00
|
|
|
var sys = require('sys');
|
2010-10-11 11:37:30 +08:00
|
|
|
|
|
|
|
var Query = function() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
sys.inherits(Query, EventEmitter);
|
|
|
|
|
2010-10-19 11:12:26 +08:00
|
|
|
var intParser = {
|
|
|
|
fromDbValue: parseInt
|
|
|
|
};
|
|
|
|
|
2010-10-19 11:17:58 +08:00
|
|
|
var floatParser = {
|
|
|
|
fromDbValue: parseFloat
|
|
|
|
};
|
|
|
|
|
2010-10-19 12:19:17 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-19 12:35:51 +08:00
|
|
|
var dateParser = {
|
|
|
|
fromDbValue: function(isoDate) {
|
|
|
|
return Date.parse(isoDate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-19 11:12:26 +08:00
|
|
|
Query.dataTypes = {
|
|
|
|
20: intParser,
|
|
|
|
21: intParser,
|
2010-10-19 11:17:58 +08:00
|
|
|
23: intParser,
|
2010-10-19 11:42:48 +08:00
|
|
|
26: intParser,
|
2010-10-19 11:17:58 +08:00
|
|
|
1700: floatParser,
|
|
|
|
700: floatParser,
|
2010-10-19 12:19:17 +08:00
|
|
|
701: floatParser,
|
|
|
|
1083: timeParser,
|
2010-10-19 12:35:51 +08:00
|
|
|
1266: timeParser,
|
|
|
|
1114: dateParser,
|
|
|
|
1184: dateParser
|
2010-10-19 11:12:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var p = Query.prototype;
|
|
|
|
|
|
|
|
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 = Query.dataTypes[field.dataTypeID];
|
|
|
|
if(dataType) {
|
|
|
|
row[i] = dataType.fromDbValue(row[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.emit('row',row);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.toBuffer = function() {
|
2010-10-11 11:37:30 +08:00
|
|
|
var textBuffer = new Buffer(this.text+'\0','utf8');
|
|
|
|
var len = textBuffer.length + 4;
|
|
|
|
var fullBuffer = new Buffer(len + 1);
|
|
|
|
fullBuffer[0] = 0x51;
|
|
|
|
fullBuffer[1] = len >>> 24;
|
|
|
|
fullBuffer[2] = len >>> 16;
|
|
|
|
fullBuffer[3] = len >>> 8;
|
|
|
|
fullBuffer[4] = len >>> 0;
|
|
|
|
textBuffer.copy(fullBuffer,5,0);
|
|
|
|
return fullBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Query;
|