From 4f1fef59334e563dac854ceb0ddefeb7f4291ba0 Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 29 Aug 2011 23:06:07 -0500 Subject: [PATCH] start native code cleanup --- lib/{native.js => native/index.js} | 97 ++---------------------------- lib/native/query.js | 92 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 91 deletions(-) rename lib/{native.js => native/index.js} (56%) create mode 100644 lib/native/query.js diff --git a/lib/native.js b/lib/native/index.js similarity index 56% rename from lib/native.js rename to lib/native/index.js index 9e38513b..22027fbc 100644 --- a/lib/native.js +++ b/lib/native/index.js @@ -1,11 +1,12 @@ //require the c++ bindings & export to javascript var sys = require('sys'); var EventEmitter = require('events').EventEmitter; -var utils = require(__dirname + "/utils"); +var utils = require(__dirname + "/../utils"); +var binding = require(__dirname + '/../../build/default/binding'); var Connection = binding.Connection; -var binding = require(__dirname + '/../build/default/binding'); -var types = require(__dirname + "/types"); +var types = require(__dirname + "/../types"); +var NativeQuery = require(__dirname + '/query'); var p = Connection.prototype; @@ -59,7 +60,7 @@ p._pulseQueryQueue = function(initialConnection) { } } -var ctor = function(config) { +var clientBuilder = function(config) { config = config || {}; var connection = new Connection(); connection._queryQueue = []; @@ -105,90 +106,4 @@ var ctor = function(config) { return connection; }; -//event emitter proxy -var NativeQuery = function(text, values, callback) { - //TODO there are better ways to detect overloads - if(typeof text == 'object') { - this.text = text.text; - this.values = text.values; - this.name = text.name; - if(typeof values === 'function') { - this.callback = values; - } else if(values) { - this.values = values; - this.callback = callback; - } - } else { - this.text = text; - this.values = values; - this.callback = callback; - if(typeof values == 'function') { - this.values = null; - this.callback = values; - } - } - if(this.callback) { - this.rows = []; - } - //normalize values - if(this.values) { - for(var i = 0, len = this.values.length; i < len; i++) { - var item = this.values[i]; - switch(typeof item) { - case 'undefined': - this.values[i] = null; - break; - case 'object': - this.values[i] = item === null ? null : JSON.stringify(item); - break; - case 'string': - //value already string - break; - default: - //numbers - this.values[i] = item.toString(); - } - } - } - - EventEmitter.call(this); -}; - -sys.inherits(NativeQuery, EventEmitter); -var p = NativeQuery.prototype; - -//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]; - result[item.name] = item.value == null ? null : types.getStringTypeParser(item.type)(item.value); - } - return result; -} - -p.handleRow = function(rowData) { - var row = mapRowData(rowData); - if(this.callback) { - this.rows.push(row); - } - this.emit('row', row); -}; - -p.handleError = function(error) { - if(this.callback) { - this.callback(error); - this.callback = null; - } else { - this.emit('error', error); - } -} - -p.handleReadyForQuery = function() { - if(this.callback) { - this.callback(null, { rows: this.rows }); - } - this.emit('end'); -}; - -module.exports = ctor; +module.exports = clientBuilder; diff --git a/lib/native/query.js b/lib/native/query.js new file mode 100644 index 00000000..5ce51c5d --- /dev/null +++ b/lib/native/query.js @@ -0,0 +1,92 @@ +var sys = require('sys'); +var EventEmitter = require('events').EventEmitter; + +var types = require(__dirname + "/../types"); + +//event emitter proxy +var NativeQuery = function(text, values, callback) { + //TODO there are better ways to detect overloads + if(typeof text == 'object') { + this.text = text.text; + this.values = text.values; + this.name = text.name; + if(typeof values === 'function') { + this.callback = values; + } else if(values) { + this.values = values; + this.callback = callback; + } + } else { + this.text = text; + this.values = values; + this.callback = callback; + if(typeof values == 'function') { + this.values = null; + this.callback = values; + } + } + if(this.callback) { + this.rows = []; + } + //normalize values + if(this.values) { + for(var i = 0, len = this.values.length; i < len; i++) { + var item = this.values[i]; + switch(typeof item) { + case 'undefined': + this.values[i] = null; + break; + case 'object': + this.values[i] = item === null ? null : JSON.stringify(item); + break; + case 'string': + //value already string + break; + default: + //numbers + this.values[i] = item.toString(); + } + } + } + + EventEmitter.call(this); +}; + +sys.inherits(NativeQuery, EventEmitter); +var p = NativeQuery.prototype; + +//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]; + result[item.name] = item.value == null ? null : types.getStringTypeParser(item.type)(item.value); + } + return result; +} + +p.handleRow = function(rowData) { + var row = mapRowData(rowData); + if(this.callback) { + this.rows.push(row); + } + this.emit('row', row); +}; + +p.handleError = function(error) { + if(this.callback) { + this.callback(error); + this.callback = null; + } else { + this.emit('error', error); + } +} + +p.handleReadyForQuery = function() { + if(this.callback) { + this.callback(null, { rows: this.rows }); + } + this.emit('end'); +}; + +module.exports = NativeQuery;