Skip to content

Commit 4f1fef5

Browse files
committed
start native code cleanup
1 parent 334e573 commit 4f1fef5

File tree

2 files changed

+98
-91
lines changed

2 files changed

+98
-91
lines changed
Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//require the c++ bindings & export to javascript
22
var sys = require('sys');
33
var EventEmitter = require('events').EventEmitter;
4-
var utils = require(__dirname + "/utils");
4+
var utils = require(__dirname + "/../utils");
55

6+
var binding = require(__dirname + '/../../build/default/binding');
67
var Connection = binding.Connection;
7-
var binding = require(__dirname + '/../build/default/binding');
8-
var types = require(__dirname + "/types");
8+
var types = require(__dirname + "/../types");
9+
var NativeQuery = require(__dirname + '/query');
910

1011
var p = Connection.prototype;
1112

@@ -59,7 +60,7 @@ p._pulseQueryQueue = function(initialConnection) {
5960
}
6061
}
6162

62-
var ctor = function(config) {
63+
var clientBuilder = function(config) {
6364
config = config || {};
6465
var connection = new Connection();
6566
connection._queryQueue = [];
@@ -105,90 +106,4 @@ var ctor = function(config) {
105106
return connection;
106107
};
107108

108-
//event emitter proxy
109-
var NativeQuery = function(text, values, callback) {
110-
//TODO there are better ways to detect overloads
111-
if(typeof text == 'object') {
112-
this.text = text.text;
113-
this.values = text.values;
114-
this.name = text.name;
115-
if(typeof values === 'function') {
116-
this.callback = values;
117-
} else if(values) {
118-
this.values = values;
119-
this.callback = callback;
120-
}
121-
} else {
122-
this.text = text;
123-
this.values = values;
124-
this.callback = callback;
125-
if(typeof values == 'function') {
126-
this.values = null;
127-
this.callback = values;
128-
}
129-
}
130-
if(this.callback) {
131-
this.rows = [];
132-
}
133-
//normalize values
134-
if(this.values) {
135-
for(var i = 0, len = this.values.length; i < len; i++) {
136-
var item = this.values[i];
137-
switch(typeof item) {
138-
case 'undefined':
139-
this.values[i] = null;
140-
break;
141-
case 'object':
142-
this.values[i] = item === null ? null : JSON.stringify(item);
143-
break;
144-
case 'string':
145-
//value already string
146-
break;
147-
default:
148-
//numbers
149-
this.values[i] = item.toString();
150-
}
151-
}
152-
}
153-
154-
EventEmitter.call(this);
155-
};
156-
157-
sys.inherits(NativeQuery, EventEmitter);
158-
var p = NativeQuery.prototype;
159-
160-
//maps from native rowdata into api compatible row object
161-
var mapRowData = function(row) {
162-
var result = {};
163-
for(var i = 0, len = row.length; i < len; i++) {
164-
var item = row[i];
165-
result[item.name] = item.value == null ? null : types.getStringTypeParser(item.type)(item.value);
166-
}
167-
return result;
168-
}
169-
170-
p.handleRow = function(rowData) {
171-
var row = mapRowData(rowData);
172-
if(this.callback) {
173-
this.rows.push(row);
174-
}
175-
this.emit('row', row);
176-
};
177-
178-
p.handleError = function(error) {
179-
if(this.callback) {
180-
this.callback(error);
181-
this.callback = null;
182-
} else {
183-
this.emit('error', error);
184-
}
185-
}
186-
187-
p.handleReadyForQuery = function() {
188-
if(this.callback) {
189-
this.callback(null, { rows: this.rows });
190-
}
191-
this.emit('end');
192-
};
193-
194-
module.exports = ctor;
109+
module.exports = clientBuilder;

lib/native/query.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var sys = require('sys');
2+
var EventEmitter = require('events').EventEmitter;
3+
4+
var types = require(__dirname + "/../types");
5+
6+
//event emitter proxy
7+
var NativeQuery = function(text, values, callback) {
8+
//TODO there are better ways to detect overloads
9+
if(typeof text == 'object') {
10+
this.text = text.text;
11+
this.values = text.values;
12+
this.name = text.name;
13+
if(typeof values === 'function') {
14+
this.callback = values;
15+
} else if(values) {
16+
this.values = values;
17+
this.callback = callback;
18+
}
19+
} else {
20+
this.text = text;
21+
this.values = values;
22+
this.callback = callback;
23+
if(typeof values == 'function') {
24+
this.values = null;
25+
this.callback = values;
26+
}
27+
}
28+
if(this.callback) {
29+
this.rows = [];
30+
}
31+
//normalize values
32+
if(this.values) {
33+
for(var i = 0, len = this.values.length; i < len; i++) {
34+
var item = this.values[i];
35+
switch(typeof item) {
36+
case 'undefined':
37+
this.values[i] = null;
38+
break;
39+
case 'object':
40+
this.values[i] = item === null ? null : JSON.stringify(item);
41+
break;
42+
case 'string':
43+
//value already string
44+
break;
45+
default:
46+
//numbers
47+
this.values[i] = item.toString();
48+
}
49+
}
50+
}
51+
52+
EventEmitter.call(this);
53+
};
54+
55+
sys.inherits(NativeQuery, EventEmitter);
56+
var p = NativeQuery.prototype;
57+
58+
//maps from native rowdata into api compatible row object
59+
var mapRowData = function(row) {
60+
var result = {};
61+
for(var i = 0, len = row.length; i < len; i++) {
62+
var item = row[i];
63+
result[item.name] = item.value == null ? null : types.getStringTypeParser(item.type)(item.value);
64+
}
65+
return result;
66+
}
67+
68+
p.handleRow = function(rowData) {
69+
var row = mapRowData(rowData);
70+
if(this.callback) {
71+
this.rows.push(row);
72+
}
73+
this.emit('row', row);
74+
};
75+
76+
p.handleError = function(error) {
77+
if(this.callback) {
78+
this.callback(error);
79+
this.callback = null;
80+
} else {
81+
this.emit('error', error);
82+
}
83+
}
84+
85+
p.handleReadyForQuery = function() {
86+
if(this.callback) {
87+
this.callback(null, { rows: this.rows });
88+
}
89+
this.emit('end');
90+
};
91+
92+
module.exports = NativeQuery;

0 commit comments

Comments
 (0)