2011-08-30 12:06:07 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2011-08-30 12:06:07 +08:00
|
|
|
|
2012-07-06 11:08:18 +08:00
|
|
|
var types = require(__dirname + '/../types');
|
|
|
|
var utils = require(__dirname + '/../utils');
|
2012-09-10 10:13:36 +08:00
|
|
|
var Result = require(__dirname + '/../result');
|
2011-08-30 12:06:07 +08:00
|
|
|
|
|
|
|
//event emitter proxy
|
2012-12-11 14:34:55 +08:00
|
|
|
var NativeQuery = function(config, values, callback) {
|
|
|
|
// use of "new" optional
|
|
|
|
if (!(this instanceof NativeQuery)) return new NativeQuery(config, values, callback);
|
|
|
|
|
2012-09-10 10:13:36 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2012-12-11 14:34:55 +08:00
|
|
|
config = utils.normalizeQueryConfig(config, values, callback);
|
|
|
|
|
|
|
|
this.name = config.name;
|
|
|
|
this.text = config.text;
|
|
|
|
this.values = config.values;
|
|
|
|
this.callback = config.callback;
|
2012-09-10 10:13:36 +08:00
|
|
|
|
2012-12-11 13:25:26 +08:00
|
|
|
this._result = new Result();
|
2011-08-30 12:06:07 +08:00
|
|
|
//normalize values
|
|
|
|
if(this.values) {
|
|
|
|
for(var i = 0, len = this.values.length; i < len; i++) {
|
2012-07-06 11:08:18 +08:00
|
|
|
this.values[i] = utils.prepareValue(this.values[i]);
|
2011-08-30 12:06:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(NativeQuery, EventEmitter);
|
2011-08-30 12:06:07 +08:00
|
|
|
var p = NativeQuery.prototype;
|
|
|
|
|
|
|
|
//maps from native rowdata into api compatible row object
|
|
|
|
var mapRowData = function(row) {
|
|
|
|
var result = {};
|
|
|
|
for(var i = 0, len = row.length; i < len; i++) {
|
|
|
|
var item = row[i];
|
2011-11-21 18:37:45 +08:00
|
|
|
result[item.name] = item.value == null ? null : types.getTypeParser(item.type, 'text')(item.value);
|
2011-08-30 12:06:07 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.handleRow = function(rowData) {
|
|
|
|
var row = mapRowData(rowData);
|
|
|
|
if(this.callback) {
|
2012-12-11 13:25:26 +08:00
|
|
|
this._result.addRow(row);
|
2011-08-30 12:06:07 +08:00
|
|
|
}
|
2012-12-11 13:25:26 +08:00
|
|
|
this.emit('row', row, this._result);
|
2011-08-30 12:06:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
p.handleError = function(error) {
|
|
|
|
if(this.callback) {
|
|
|
|
this.callback(error);
|
|
|
|
this.callback = null;
|
|
|
|
} else {
|
|
|
|
this.emit('error', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 12:38:03 +08:00
|
|
|
p.handleReadyForQuery = function(meta) {
|
2012-12-11 13:25:26 +08:00
|
|
|
if(meta) {
|
|
|
|
this._result.addCommandComplete(meta);
|
|
|
|
}
|
2011-08-30 12:06:07 +08:00
|
|
|
if(this.callback) {
|
2012-12-11 13:25:26 +08:00
|
|
|
this.callback(null, this._result);
|
2011-08-30 12:06:07 +08:00
|
|
|
}
|
2012-12-11 13:25:26 +08:00
|
|
|
this.emit('end', this._result);
|
2011-08-30 12:06:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = NativeQuery;
|