2016-06-24 23:56:43 +08:00
|
|
|
/**
|
2017-05-18 05:47:07 +08:00
|
|
|
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
2016-06-24 23:56:43 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* README.md file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 04:36:27 +08:00
|
|
|
var types = require('pg-types');
|
2013-07-08 22:30:10 +08:00
|
|
|
|
2011-01-27 22:10:45 +08:00
|
|
|
//result object returned from query
|
|
|
|
//in the 'end' event and also
|
|
|
|
//passed as second argument to provided callback
|
2013-07-09 06:45:06 +08:00
|
|
|
var Result = function(rowMode) {
|
2012-09-10 10:13:36 +08:00
|
|
|
this.command = null;
|
|
|
|
this.rowCount = null;
|
|
|
|
this.oid = null;
|
2011-01-27 22:10:45 +08:00
|
|
|
this.rows = [];
|
2013-06-04 01:14:47 +08:00
|
|
|
this.fields = [];
|
2013-07-08 22:32:53 +08:00
|
|
|
this._parsers = [];
|
2013-08-08 04:33:57 +08:00
|
|
|
this.RowCtor = null;
|
2014-05-20 22:44:53 +08:00
|
|
|
this.rowAsArray = rowMode == "array";
|
|
|
|
if(this.rowAsArray) {
|
2013-07-09 06:45:06 +08:00
|
|
|
this.parseRow = this._parseRowAsArray;
|
|
|
|
}
|
2011-01-27 22:10:45 +08:00
|
|
|
};
|
|
|
|
|
2013-04-17 23:29:21 +08:00
|
|
|
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
|
2011-01-27 22:10:45 +08:00
|
|
|
|
|
|
|
//adds a command complete message
|
2013-03-06 22:48:52 +08:00
|
|
|
Result.prototype.addCommandComplete = function(msg) {
|
2013-01-21 21:38:04 +08:00
|
|
|
var match;
|
2012-12-11 13:25:26 +08:00
|
|
|
if(msg.text) {
|
|
|
|
//pure javascript
|
2013-01-21 21:38:04 +08:00
|
|
|
match = matchRegexp.exec(msg.text);
|
2012-12-11 13:25:26 +08:00
|
|
|
} else {
|
|
|
|
//native bindings
|
2013-01-21 21:38:04 +08:00
|
|
|
match = matchRegexp.exec(msg.command);
|
2012-12-11 13:25:26 +08:00
|
|
|
}
|
2011-01-27 22:10:45 +08:00
|
|
|
if(match) {
|
|
|
|
this.command = match[1];
|
|
|
|
//match 3 will only be existing on insert commands
|
|
|
|
if(match[3]) {
|
2012-12-11 13:25:26 +08:00
|
|
|
//msg.value is from native bindings
|
2013-01-21 21:38:04 +08:00
|
|
|
this.rowCount = parseInt(match[3] || msg.value, 10);
|
|
|
|
this.oid = parseInt(match[2], 10);
|
2011-01-27 22:10:45 +08:00
|
|
|
} else {
|
2013-01-21 21:38:04 +08:00
|
|
|
this.rowCount = parseInt(match[2], 10);
|
2011-01-27 22:10:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-09 06:45:06 +08:00
|
|
|
Result.prototype._parseRowAsArray = function(rowData) {
|
|
|
|
var row = [];
|
|
|
|
for(var i = 0, len = rowData.length; i < len; i++) {
|
|
|
|
var rawValue = rowData[i];
|
|
|
|
if(rawValue !== null) {
|
|
|
|
row.push(this._parsers[i](rawValue));
|
|
|
|
} else {
|
|
|
|
row.push(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
};
|
|
|
|
|
2013-07-08 22:30:10 +08:00
|
|
|
//rowData is an array of text or binary values
|
|
|
|
//this turns the row into a JavaScript object
|
|
|
|
Result.prototype.parseRow = function(rowData) {
|
2013-08-08 04:33:57 +08:00
|
|
|
return new this.RowCtor(this._parsers, rowData);
|
2013-07-08 22:30:10 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Result.prototype.addRow = function(row) {
|
2011-01-27 22:10:45 +08:00
|
|
|
this.rows.push(row);
|
|
|
|
};
|
|
|
|
|
2013-08-18 03:21:19 +08:00
|
|
|
var inlineParser = function(fieldName, i) {
|
2014-01-22 22:38:29 +08:00
|
|
|
return "\nthis['" +
|
|
|
|
//fields containing single quotes will break
|
|
|
|
//the evaluated javascript unless they are escaped
|
|
|
|
//see https://github.com/brianc/node-postgres/issues/507
|
2016-02-11 04:52:43 +08:00
|
|
|
//Addendum: However, we need to make sure to replace all
|
|
|
|
//occurences of apostrophes, not just the first one.
|
|
|
|
//See https://github.com/brianc/node-postgres/issues/934
|
|
|
|
fieldName.replace(/'/g, "\\'") +
|
2014-01-22 22:38:29 +08:00
|
|
|
"'] = " +
|
|
|
|
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
|
2013-08-08 04:33:57 +08:00
|
|
|
};
|
|
|
|
|
2013-07-08 22:30:10 +08:00
|
|
|
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 = [];
|
2013-07-08 22:32:53 +08:00
|
|
|
this._parsers = [];
|
2013-07-08 22:30:10 +08:00
|
|
|
}
|
2013-08-08 04:33:57 +08:00
|
|
|
var ctorBody = "";
|
2013-07-08 22:30:10 +08:00
|
|
|
for(var i = 0; i < fieldDescriptions.length; i++) {
|
2013-07-08 22:32:53 +08:00
|
|
|
var desc = fieldDescriptions[i];
|
|
|
|
this.fields.push(desc);
|
2014-11-14 07:57:00 +08:00
|
|
|
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text');
|
2013-08-08 04:33:57 +08:00
|
|
|
this._parsers.push(parser);
|
2013-08-08 04:35:07 +08:00
|
|
|
//this is some craziness to compile the row result parsing
|
|
|
|
//results in ~60% speedup on large query result sets
|
2013-08-18 03:21:19 +08:00
|
|
|
ctorBody += inlineParser(desc.name, i);
|
2013-07-08 22:30:10 +08:00
|
|
|
}
|
2014-05-20 22:44:53 +08:00
|
|
|
if(!this.rowAsArray) {
|
|
|
|
this.RowCtor = Function("parsers", "rowData", ctorBody);
|
|
|
|
}
|
2013-06-04 01:14:47 +08:00
|
|
|
};
|
|
|
|
|
2014-11-14 07:57:00 +08:00
|
|
|
Result.prototype._getTypeParser = types.getTypeParser;
|
|
|
|
|
2011-01-27 22:10:45 +08:00
|
|
|
module.exports = Result;
|