|
1 | 1 | //require the c++ bindings & export to javascript
|
2 | 2 | var sys = require('sys');
|
3 | 3 | var EventEmitter = require('events').EventEmitter;
|
4 |
| -var utils = require(__dirname + "/utils"); |
| 4 | +var utils = require(__dirname + "/../utils"); |
5 | 5 |
|
| 6 | +var binding = require(__dirname + '/../../build/default/binding'); |
6 | 7 | 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'); |
9 | 10 |
|
10 | 11 | var p = Connection.prototype;
|
11 | 12 |
|
@@ -59,7 +60,7 @@ p._pulseQueryQueue = function(initialConnection) {
|
59 | 60 | }
|
60 | 61 | }
|
61 | 62 |
|
62 |
| -var ctor = function(config) { |
| 63 | +var clientBuilder = function(config) { |
63 | 64 | config = config || {};
|
64 | 65 | var connection = new Connection();
|
65 | 66 | connection._queryQueue = [];
|
@@ -105,90 +106,4 @@ var ctor = function(config) {
|
105 | 106 | return connection;
|
106 | 107 | };
|
107 | 108 |
|
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; |
0 commit comments