From a580c8ab8de9241208e3e7ae43b12ef7acf88fd9 Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 2 May 2011 00:32:30 -0500 Subject: [PATCH] code cleanup --- lib/index.js | 2 +- lib/native.js | 63 +++++++++++++++++++++++---------------------------- lib/query.js | 5 +--- lib/writer.js | 3 +++ 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/lib/index.js b/lib/index.js index d3eddeb..2f84f0a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'); }) diff --git a/lib/native.js b/lib/native.js index 9db260c..867ac64 100644 --- a/lib/native.js +++ b/lib/native.js @@ -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"); } } diff --git a/lib/query.js b/lib/query.js index 0346bd0..f68c343 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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; diff --git a/lib/writer.js b/lib/writer.js index 0faf344..d3f34d2 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -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);