2011-02-21 06:12:06 +08:00
|
|
|
//require the c++ bindings & export to javascript
|
2011-02-24 10:02:51 +08:00
|
|
|
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-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) {
|
2011-02-24 10:02:51 +08:00
|
|
|
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 if (typeof config == 'string') {
|
|
|
|
getLibpgConString(utils.parseConnectionString(config), callback)
|
|
|
|
} 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);
|
2011-02-24 10:02:51 +08:00
|
|
|
this._queryQueue.push(q);
|
2011-02-23 13:52:25 +08:00
|
|
|
this._pulseQueryQueue();
|
2011-02-24 10:02:51 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-02-24 10:02:51 +08:00
|
|
|
this._activeQuery = query;
|
2011-02-25 11:33:54 +08:00
|
|
|
if(query.values) {
|
|
|
|
//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) {
|
|
|
|
var connection = new Connection();
|
|
|
|
connection._queryQueue = [];
|
|
|
|
connection._activeQuery = null;
|
|
|
|
connection._config = config;
|
|
|
|
connection.on('connect', function() {
|
|
|
|
connection._connected = true;
|
|
|
|
connection._pulseQueryQueue();
|
|
|
|
});
|
2011-02-24 10:02:51 +08:00
|
|
|
|
2011-02-24 12:41:54 +08:00
|
|
|
//proxy some events to active query
|
2011-02-24 10:02:51 +08:00
|
|
|
connection.on('_row', function(row) {
|
2011-03-01 13:09:09 +08:00
|
|
|
connection._activeQuery.handleRow(row);
|
2011-02-24 10:02:51 +08:00
|
|
|
})
|
|
|
|
connection.on('_error', function(err) {
|
|
|
|
if(connection._activeQuery) {
|
|
|
|
connection._activeQuery.emit('error', err);
|
|
|
|
} else {
|
|
|
|
connection.emit('error', err);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
connection.on('_readyForQuery', function() {
|
2011-03-01 13:09:09 +08:00
|
|
|
connection._activeQuery.handleReadyForQuery();
|
2011-02-24 10:02:51 +08:00
|
|
|
connection._activeQuery = null;
|
2011-02-23 13:52:25 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
var connect = function(config, callback) {
|
|
|
|
var client = new ctor(config);
|
|
|
|
client.connect();
|
|
|
|
client.on('connect', function() {
|
|
|
|
callback(null, client);
|
|
|
|
})
|
|
|
|
};
|
2011-02-23 13:52:25 +08:00
|
|
|
|
2011-02-24 10:02:51 +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-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-02-24 10:02:51 +08:00
|
|
|
EventEmitter.call(this);
|
2011-03-01 12:57:29 +08:00
|
|
|
this._translateValues();
|
2011-02-24 10:02:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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];
|
|
|
|
switch(item.type) {
|
|
|
|
case 23:
|
|
|
|
result[item.name] = parseInt(item.value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result[item.name] = item.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2011-02-24 10:02:51 +08:00
|
|
|
|
2011-02-23 13:52:25 +08:00
|
|
|
module.exports = {
|
2011-02-24 09:40:52 +08:00
|
|
|
Client: ctor,
|
|
|
|
connect: connect
|
2011-02-23 13:52:25 +08:00
|
|
|
};
|