node-postgres/lib/native.js

205 lines
4.8 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 add = function(params, config, paramName) {
var value = config[paramName];
if(value) {
params.push(paramName+"='"+value+"'");
}
}
var getLibpgConString = function(config, callback) {
if(typeof config == 'object') {
2011-02-24 09:40:52 +08:00
var params = []
2011-02-23 13:52:25 +08:00
add(params, config, 'user');
add(params, config, 'password');
add(params, config, 'port');
if(config.database) {
params.push("dbname='" + config.database + "'");
}
if(config.host) {
if(config.host != 'localhost' && config.host != '127.0.0.1') {
throw new Error("Need to use node to do async DNS on host");
2011-02-23 13:52:25 +08:00
}
params.push("hostaddr=127.0.0.1 ");
}
2011-02-24 09:40:52 +08:00
callback(params.join(" "));
} else {
throw new Error("Unrecognized config type for connection");
2011-02-23 13:52:25 +08:00
}
}
var nativeConnect = p.connect;
p.connect = function() {
var self = this;
getLibpgConString(this._config, function(conString) {
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() {
if(!this._connected) {
return;
}
if(this._activeQuery) {
return;
}
var query = this._queryQueue.shift();
if(!query) {
this.emit('drain');
return;
}
this._activeQuery = query;
2011-03-06 02:01:57 +08:00
if(query.name) {
2011-03-07 11:32:58 +08:00
this._namedQuery = true;
2011-03-06 02:01:57 +08:00
this._sendPrepare(query.name, query.text, (query.values||[]).length);
}
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._activeQuery = null;
2011-03-04 02:46:24 +08:00
connection._config = utils.normalizeConnectionInfo(config);
2011-02-23 13:52:25 +08:00
connection.on('connect', function() {
connection._connected = true;
connection._pulseQueryQueue();
});
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() {
2011-03-07 11:32:58 +08:00
//a named query finished being prepared
if(this._namedQuery) {
this._namedQuery = false;
connection.
} 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-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-02-24 12:41:54 +08:00
} else {
this.text = text;
2011-03-02 03:51:25 +08:00
this.callback = callback;
this.values = values;
}
if(typeof values == 'function') {
this.values = null;
this.callback = values;
}
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];
if(item instanceof Date) {
this.values[i] = JSON.stringify(item);
}
}
}
EventEmitter.call(this);
2011-03-01 12:57:29 +08:00
this._translateValues();
};
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++) {
var item = row[i];
2011-03-04 02:05:29 +08:00
var parser = types.getStringTypeParser(item.type);
result[item.name] = parser(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');
};
2011-03-01 12:57:29 +08:00
//translates values into strings
p._translateValues = function() {
if(this.values) {
this.values = this.values.map(function(val) {
return val.toString();
});
}
}
var pool = require(__dirname + '/client-pool').init(ctor);
2011-02-23 13:52:25 +08:00
module.exports = {
2011-02-24 09:40:52 +08:00
Client: ctor,
connect: pool.connect,
end: pool.end
2011-02-23 13:52:25 +08:00
};