start native code cleanup

This commit is contained in:
brianc 2011-08-29 23:06:07 -05:00
parent 334e5739d4
commit 4f1fef5933
2 changed files with 98 additions and 91 deletions

View File

@ -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;

92
lib/native/query.js Normal file
View File

@ -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;