node-postgres/lib/query.js

54 lines
1.1 KiB
JavaScript
Raw Normal View History

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
};
Query.dataTypes = {
20: intParser,
21: intParser,
23: intParser
};
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;