code cleanup

This commit is contained in:
brianc 2011-05-02 00:32:30 -05:00
parent 6c7b908367
commit a580c8ab8d
4 changed files with 33 additions and 40 deletions

View File

@ -11,7 +11,7 @@ module.exports = {
defaults: defaults
}
//lazy require native as it may not have been built
//lazy require native module...the c++ may not have been compiled
module.exports.__defineGetter__("native", function() {
return require(__dirname + '/native');
})

View File

@ -8,34 +8,6 @@ var types = require(__dirname + "/types");
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') {
var params = []
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");
}
params.push("hostaddr=127.0.0.1 ");
}
callback(params.join(" "));
} else {
throw new Error("Unrecognized config type for connection");
}
}
var nativeConnect = p.connect;
p.connect = function() {
@ -148,12 +120,13 @@ var NativeQuery = function(text, values, callback) {
var item = this.values[i];
if(item instanceof Date) {
this.values[i] = JSON.stringify(item);
} else {
this.values[i] = item.toString();
}
}
}
EventEmitter.call(this);
this._translateValues();
};
sys.inherits(NativeQuery, EventEmitter);
@ -194,12 +167,32 @@ p.handleReadyForQuery = function() {
this.emit('end');
};
//translates values into strings
p._translateValues = function() {
if(this.values) {
this.values = this.values.map(function(val) {
return val.toString();
});
var add = function(params, config, paramName) {
var value = config[paramName];
if(value) {
params.push(paramName+"='"+value+"'");
}
}
//connection string helper
var getLibpgConString = function(config, callback) {
if(typeof config == 'object') {
var params = []
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");
}
params.push("hostaddr=127.0.0.1 ");
}
callback(params.join(" "));
} else {
throw new Error("Unrecognized config type for connection");
}
}

View File

@ -9,9 +9,6 @@ var Query = function(config) {
this.rows = config.rows;
this.types = config.types;
this.name = config.name;
//for code clarity purposes we'll declare this here though it's not
//set or used until a rowDescription message comes in
this.rowDescription = null;
this.callback = config.callback;
this._fieldNames = [];
this._fieldConverters = [];
@ -125,7 +122,7 @@ p.prepare = function(connection) {
connection.parsedStatements[this.name] = true;
}
//TODO is there some btter way to prepare values for the database?
//TODO is there some better way to prepare values for the database?
if(self.values) {
self.values = self.values.map(function(val) {
return (val instanceof Date) ? JSON.stringify(val) : val;

View File

@ -1,3 +1,6 @@
//binary data writer tuned for creating
//postgres message packets as effeciently as possible by reusing the
//same buffer to avoid memcpy and limit memory allocations
var Writer = function(size) {
this.size = size || 1024;
this.buffer = new Buffer(this.size + 5);