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
|
|
|
|
2013-03-06 22:57:38 +08:00
|
|
|
var types = require(__dirname + '/../types/');
|
2012-07-06 11:08:18 +08:00
|
|
|
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
|
2013-01-21 21:13:36 +08:00
|
|
|
if (!(this instanceof NativeQuery)) {
|
|
|
|
return new NativeQuery(config, values, callback);
|
|
|
|
}
|
2012-12-11 14:34:55 +08:00
|
|
|
|
2012-09-10 10:13:36 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2013-03-07 00:26:40 +08:00
|
|
|
var c = utils.normalizeQueryConfig(config, values, callback);
|
2012-12-11 14:34:55 +08:00
|
|
|
|
2013-07-08 21:16:10 +08:00
|
|
|
this.name = c.name;
|
2013-03-07 00:26:40 +08:00
|
|
|
this.text = c.text;
|
|
|
|
this.values = c.values;
|
|
|
|
this.callback = c.callback;
|
2013-01-24 07:52:02 +08:00
|
|
|
|
2013-07-09 06:45:06 +08:00
|
|
|
this._result = new Result(config.rowMode);
|
2013-07-08 22:19:30 +08:00
|
|
|
this._addedFields = false;
|
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
|
|
|
}
|
|
|
|
}
|
2013-01-18 06:24:08 +08:00
|
|
|
this._canceledDueToError = false;
|
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
|
|
|
|
2013-07-08 22:19:30 +08:00
|
|
|
NativeQuery.prototype.handleRowDescription = function(rowDescription) {
|
2013-07-08 22:30:10 +08:00
|
|
|
this._result.addFields(rowDescription);
|
2013-07-08 22:19:30 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
NativeQuery.prototype.handleRow = function(rowData) {
|
2013-07-08 22:30:10 +08:00
|
|
|
var row = this._result.parseRow(rowData);
|
2011-08-30 12:06:07 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
NativeQuery.prototype.handleError = function(error) {
|
2013-01-18 06:24:08 +08:00
|
|
|
if (this._canceledDueToError) {
|
|
|
|
error = this._canceledDueToError;
|
|
|
|
this._canceledDueToError = false;
|
|
|
|
}
|
2011-08-30 12:06:07 +08:00
|
|
|
if(this.callback) {
|
2013-04-19 22:25:53 +08:00
|
|
|
var cb = this.callback;
|
|
|
|
//remove callback to prevent double call on readyForQuery
|
2011-08-30 12:06:07 +08:00
|
|
|
this.callback = null;
|
2013-04-19 22:25:53 +08:00
|
|
|
cb(error);
|
2011-08-30 12:06:07 +08:00
|
|
|
} else {
|
|
|
|
this.emit('error', error);
|
|
|
|
}
|
2013-01-21 21:13:36 +08:00
|
|
|
};
|
2011-08-30 12:06:07 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
NativeQuery.prototype.handleReadyForQuery = function(meta) {
|
2013-01-18 20:04:21 +08:00
|
|
|
if (this._canceledDueToError) {
|
|
|
|
return this.handleError(this._canceledDueToError);
|
|
|
|
}
|
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
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
NativeQuery.prototype.streamData = function (connection) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(this.stream) {
|
|
|
|
this.stream.startStreamingToConnection(connection);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connection.sendCopyFail('No source stream defined');
|
|
|
|
}
|
2012-09-27 18:28:00 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
NativeQuery.prototype.handleCopyFromChunk = function (chunk) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(this.stream) {
|
2013-01-18 06:24:08 +08:00
|
|
|
this.stream.handleChunk(chunk);
|
2013-01-19 04:11:16 +08:00
|
|
|
}
|
2013-01-18 06:24:08 +08:00
|
|
|
//if there are no stream (for example when copy to query was sent by
|
2013-01-24 07:52:02 +08:00
|
|
|
//query method instead of copyTo) error will be handled
|
|
|
|
//on copyOutResponse event, so silently ignore this error here
|
2013-01-21 21:13:36 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
2011-08-30 12:06:07 +08:00
|
|
|
module.exports = NativeQuery;
|