node-postgres/lib/native/query.js

148 lines
3.5 KiB
JavaScript
Raw Normal View History

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
var NativeQuery = module.exports = function(native) {
EventEmitter.call(this);
this.native = native;
this.text = null;
this.values = null;
this.name = null;
this.callback = null;
this.state = 'new';
};
util.inherits(NativeQuery, EventEmitter);
NativeQuery.prototype.submit = function() {
this.state = 'running';
var self = this;
var after = function(err, rows) {
//handle possible query error
if(err) {
self.state = 'error';
if(self.callback) return self.callback(err);
return self.emit('error', err);
}
//handle successful result
self.state = 'done';
self.emit('done');
if(self.callback) {
self.callback(null, {rows: rows})
}
}
if(this.values) {
this.native.query(this.text, this.values, after);
} else {
this.native.query(this.text, after);
}
};
return;
var utils = require(__dirname + '/../utils');
var Result = require(__dirname + '/../result');
2011-08-30 12:06:07 +08:00
//event emitter proxy
var NativeQuery = function(config, values, callback) {
// use of "new" optional
if (!(this instanceof NativeQuery)) {
return new NativeQuery(config, values, callback);
}
EventEmitter.call(this);
var c = utils.normalizeQueryConfig(config, values, callback);
2013-07-08 21:16:10 +08:00
this.name = c.name;
this.text = c.text;
this.values = c.values;
this.callback = c.callback;
2014-04-12 13:29:20 +08:00
if(process.domain && c.callback) {
this.callback = process.domain.bind(c.callback);
}
this.singleRowMode = false;
if(!this.callback) {
this.singleRowMode = true;
}
2013-07-09 06:45:06 +08:00
this._result = new Result(config.rowMode);
this._addedFields = false;
this._hadError = 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++) {
this.values[i] = utils.prepareValue(this.values[i]);
2011-08-30 12:06:07 +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
NativeQuery.prototype.handleRowDescription = function(rowDescription) {
2013-07-08 22:30:10 +08:00
this._result.addFields(rowDescription);
};
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) {
this._result.addRow(row);
2011-08-30 12:06:07 +08:00
}
this.emit('row', row, this._result);
2011-08-30 12:06:07 +08:00
};
NativeQuery.prototype.handleError = function(error) {
if (this._canceledDueToError) {
error = this._canceledDueToError;
this._canceledDueToError = false;
}
this._hadError = true;
2011-08-30 12:06:07 +08:00
if(this.callback) {
var cb = this.callback;
//remove callback to prevent double call on readyForQuery
2011-08-30 12:06:07 +08:00
this.callback = null;
cb(error);
2011-08-30 12:06:07 +08:00
} else {
this.emit('error', error);
}
};
2011-08-30 12:06:07 +08:00
NativeQuery.prototype.handleReadyForQuery = function(meta) {
if(this._hadError) return;
if (this._canceledDueToError) {
return this.handleError(this._canceledDueToError);
}
if(meta) {
this._result.addCommandComplete(meta);
}
2011-08-30 12:06:07 +08:00
if(this.callback) {
this.callback(null, this._result);
2011-08-30 12:06:07 +08:00
}
this.emit('end', this._result);
2011-08-30 12:06:07 +08:00
};
NativeQuery.prototype.streamData = function (connection) {
if(this.stream) {
this.stream.startStreamingToConnection(connection);
}
else {
connection.sendCopyFail('No source stream defined');
}
};
NativeQuery.prototype.handleCopyFromChunk = function (chunk) {
if(this.stream) {
this.stream.handleChunk(chunk);
}
//if there are no stream (for example when copy to query was sent by
//query method instead of copyTo) error will be handled
//on copyOutResponse event, so silently ignore this error here
};
2011-08-30 12:06:07 +08:00
module.exports = NativeQuery;