node-postgres/lib/result.js
2013-07-08 09:30:10 -05:00

75 lines
1.9 KiB
JavaScript

var types = require(__dirname + '/types/');
//result object returned from query
//in the 'end' event and also
//passed as second argument to provided callback
var Result = function() {
this.command = null;
this.rowCount = null;
this.oid = null;
this.rows = [];
this.fields = [];
};
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
//adds a command complete message
Result.prototype.addCommandComplete = function(msg) {
var match;
if(msg.text) {
//pure javascript
match = matchRegexp.exec(msg.text);
} else {
//native bindings
match = matchRegexp.exec(msg.command);
}
if(match) {
this.command = match[1];
//match 3 will only be existing on insert commands
if(match[3]) {
//msg.value is from native bindings
this.rowCount = parseInt(match[3] || msg.value, 10);
this.oid = parseInt(match[2], 10);
} else {
this.rowCount = parseInt(match[2], 10);
}
}
};
//rowData is an array of text or binary values
//this turns the row into a JavaScript object
Result.prototype.parseRow = function(rowData) {
var row = {};
for(var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i];
var field = this.fields[i];
var fieldType = field.dataTypeID;
var parsedValue = null;
if(rawValue !== null) {
parsedValue = types.getTypeParser(fieldType, field.format || 'text')(rawValue);
}
var fieldName = field.name;
row[fieldName] = parsedValue;
}
return row;
};
Result.prototype.addRow = function(row) {
this.rows.push(row);
};
Result.prototype.addFields = function(fieldDescriptions) {
//clears field definitions
//multiple query statements in 1 action can result in multiple sets
//of rowDescriptions...eg: 'select NOW(); select 1::int;'
//you need to reset the fields
if(this.fields.length) {
this.fields = [];
}
for(var i = 0; i < fieldDescriptions.length; i++) {
this.fields.push(fieldDescriptions[i]);
}
};
module.exports = Result;