node-postgres/lib/native.js

135 lines
3.2 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-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 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-02-25 12:06:19 +08:00
p.query = function(config, values) {
var q = new NativeQuery(config, values);
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-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 12:41:54 +08:00
//proxy some events to active query
connection.on('_row', function(row) {
connection._activeQuery.emit('row', row);
})
connection.on('_error', function(err) {
if(connection._activeQuery) {
connection._activeQuery.emit('error', err);
} else {
connection.emit('error', err);
}
})
connection.on('_readyForQuery', function() {
connection._activeQuery.emit('end');
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
//event emitter proxy
2011-02-25 11:33:54 +08:00
var NativeQuery = function(text, values) {
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-02-25 11:33:54 +08:00
this.values = values;
2011-02-24 12:41:54 +08:00
}
EventEmitter.call(this);
};
sys.inherits(NativeQuery, EventEmitter);
var p = NativeQuery.prototype;
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
};