node-postgres/lib/native.js

194 lines
4.9 KiB
JavaScript
Raw Normal View History

2011-02-21 06:12:06 +08:00
//require the c++ bindings & export to javascript
var sys = require('sys');
var EventEmitter = require('events').EventEmitter;
2011-02-19 01:38:47 +08:00
var binding = require(__dirname + '/../build/default/binding');
2011-02-24 09:40:52 +08:00
var utils = require(__dirname + "/utils");
2011-03-04 02:05:29 +08:00
var types = require(__dirname + "/types");
2011-02-23 13:52:25 +08:00
var Connection = binding.Connection;
var p = Connection.prototype;
var nativeConnect = p.connect;
p.connect = function() {
var self = this;
utils.buildLibpqConnectionString(this._config, function(err, conString) {
if(err) return self.emit('error', err);
2011-02-23 13:52:25 +08:00
nativeConnect.call(self, conString);
})
}
2011-03-01 13:09:09 +08:00
p.query = function(config, values, callback) {
var q = new NativeQuery(config, values, callback);
this._queryQueue.push(q);
2011-02-23 13:52:25 +08:00
this._pulseQueryQueue();
return q;
2011-02-23 13:52:25 +08:00
}
p._pulseQueryQueue = function(initialConnection) {
2011-02-23 13:52:25 +08:00
if(!this._connected) {
return;
}
if(this._activeQuery) {
return;
}
var query = this._queryQueue.shift();
if(!query) {
if(!initialConnection) {
this.emit('drain');
}
2011-02-23 13:52:25 +08:00
return;
}
this._activeQuery = query;
2011-03-06 02:01:57 +08:00
if(query.name) {
if(this._namedQueries[query.name]) {
this._sendQueryPrepared(query.name, query.values||[]);
} else {
this._namedQuery = true;
this._namedQueries[query.name] = true;
this._sendPrepare(query.name, query.text, (query.values||[]).length);
}
2011-03-06 02:01:57 +08:00
}
else if(query.values) {
2011-02-25 11:33:54 +08:00
//call native function
this._sendQueryWithParams(query.text, query.values)
} else {
//call native function
this._sendQuery(query.text);
}
2011-02-23 13:52:25 +08:00
}
var ctor = function(config) {
2011-03-04 02:46:24 +08:00
config = config || {};
2011-02-23 13:52:25 +08:00
var connection = new Connection();
connection._queryQueue = [];
connection._namedQueries = {};
2011-02-23 13:52:25 +08:00
connection._activeQuery = null;
2011-03-04 02:46:24 +08:00
connection._config = utils.normalizeConnectionInfo(config);
2011-08-29 15:35:08 +08:00
//attach properties to normalize interface with pure js client
connection.user = connection._config.user;
connection.password = connection._config.password;
connection.database = connection._config.database;
connection.host = connection._config.host;
connection.port = connection._config.port;
2011-02-23 13:52:25 +08:00
connection.on('connect', function() {
connection._connected = true;
connection._pulseQueryQueue(true);
2011-02-23 13:52:25 +08:00
});
2011-02-24 12:41:54 +08:00
//proxy some events to active query
connection.on('_row', function(row) {
2011-03-01 13:09:09 +08:00
connection._activeQuery.handleRow(row);
})
connection.on('_error', function(err) {
2011-03-07 11:32:58 +08:00
//give up on trying to wait for named query prepare
this._namedQuery = false;
if(connection._activeQuery) {
2011-03-04 02:05:29 +08:00
connection._activeQuery.handleError(err);
} else {
connection.emit('error', err);
}
})
connection.on('_readyForQuery', function() {
var q = this._activeQuery;
2011-03-07 11:32:58 +08:00
//a named query finished being prepared
if(this._namedQuery) {
this._namedQuery = false;
this._sendQueryPrepared(q.name, q.values||[]);
2011-03-07 11:32:58 +08:00
} else {
connection._activeQuery.handleReadyForQuery();
connection._activeQuery = null;
connection._pulseQueryQueue();
}
2011-02-24 09:40:52 +08:00
});
2011-02-23 13:52:25 +08:00
return connection;
2011-02-24 09:40:52 +08:00
};
//event emitter proxy
2011-03-01 13:09:09 +08:00
var NativeQuery = function(text, values, callback) {
2011-08-16 09:57:39 +08:00
//TODO there are better ways to detect overloads
2011-02-24 12:41:54 +08:00
if(typeof text == 'object') {
this.text = text.text;
2011-02-25 11:33:54 +08:00
this.values = text.values;
2011-03-06 02:01:57 +08:00
this.name = text.name;
2011-08-16 09:57:39 +08:00
if(typeof values === 'function') {
this.callback = values;
2011-08-17 12:58:54 +08:00
} else if(values) {
2011-08-16 09:57:39 +08:00
this.values = values;
this.callback = callback;
}
2011-02-24 12:41:54 +08:00
} else {
this.text = text;
2011-03-02 03:51:25 +08:00
this.values = values;
this.callback = callback;
if(typeof values == 'function') {
this.values = null;
this.callback = values;
}
2011-03-02 03:51:25 +08:00
}
if(this.callback) {
this.rows = [];
2011-02-24 12:41:54 +08:00
}
2011-03-04 02:46:24 +08:00
//normalize values
if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) {
var item = this.values[i];
2011-05-20 09:46:27 +08:00
switch(typeof item) {
case 'undefined':
this.values[i] = null;
break;
case 'object':
this.values[i] = item === null ? null : JSON.stringify(item);
break;
case 'string':
//value already string
break;
default:
//numbers
this.values[i] = item.toString();
2011-03-04 02:46:24 +08:00
}
}
}
EventEmitter.call(this);
};
sys.inherits(NativeQuery, EventEmitter);
var p = NativeQuery.prototype;
2011-03-02 05:03:51 +08:00
//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++) {
2011-05-20 09:46:27 +08:00
var item = row[i];
result[item.name] = item.value == null ? null : types.getStringTypeParser(item.type)(item.value);
2011-03-02 05:03:51 +08:00
}
return result;
}
p.handleRow = function(rowData) {
var row = mapRowData(rowData);
2011-03-01 13:09:09 +08:00
if(this.callback) {
this.rows.push(row);
}
this.emit('row', row);
};
2011-03-04 02:05:29 +08:00
p.handleError = function(error) {
if(this.callback) {
this.callback(error);
this.callback = null;
} else {
this.emit('error', error);
}
}
2011-03-01 13:09:09 +08:00
p.handleReadyForQuery = function() {
if(this.callback) {
2011-03-02 03:51:25 +08:00
this.callback(null, { rows: this.rows });
2011-03-01 13:09:09 +08:00
}
this.emit('end');
};
module.exports = ctor;