Skip to content

Commit c93f49b

Browse files
committed
Make almost all tests pass
1 parent 0ed7b57 commit c93f49b

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

lib/native/index.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var Client = module.exports = function(config) {
4646
this.namedQueries = {};
4747
};
4848

49+
Client.Query = NativeQuery;
50+
4951
util.inherits(Client, EventEmitter);
5052

5153
//connect to the backend
@@ -106,34 +108,41 @@ Client.prototype.connect = function(cb) {
106108
// optional string rowMode = 'array' for an array of results
107109
// }
108110
Client.prototype.query = function(config, values, callback) {
109-
var query = new NativeQuery(this.native);
111+
if (typeof config.submit == 'function') {
112+
this._queryQueue.push(config);
113+
this._pulseQueryQueue();
114+
return config;
115+
}
116+
117+
var conf = { };
110118

111119
//support query('text', ...) style calls
112120
if(typeof config == 'string') {
113-
query.text = config;
121+
conf.text = config;
114122
}
115123

116124
//support passing everything in via a config object
117125
if(typeof config == 'object') {
118-
query.text = config.text;
119-
query.values = config.values;
120-
query.name = config.name;
121-
query.callback = config.callback;
122-
query._arrayMode = config.rowMode == 'array';
126+
conf.text = config.text;
127+
conf.values = config.values;
128+
conf.name = config.name;
129+
conf.callback = config.callback;
130+
conf.rowMode = config.rowMode;
123131
}
124132

125133
//support query({...}, function() {}) style calls
126134
//& support query(..., ['values'], ...) style calls
127135
if(typeof values == 'function') {
128-
query.callback = values;
136+
conf.callback = values;
129137
}
130138
else if(util.isArray(values)) {
131-
query.values = values;
139+
conf.values = values;
132140
}
133141
if(typeof callback == 'function') {
134-
query.callback = callback;
142+
conf.callback = callback;
135143
}
136144

145+
var query = new NativeQuery(conf);
137146
this._queryQueue.push(query);
138147
this._pulseQueryQueue();
139148
return query;

lib/native/query.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ var util = require('util');
1111
var utils = require('../utils');
1212
var NativeResult = require('./result');
1313

14-
var NativeQuery = module.exports = function(native) {
14+
var NativeQuery = module.exports = function(config, values) {
1515
EventEmitter.call(this);
16-
this.native = native;
17-
this.text = null;
18-
this.values = null;
19-
this.name = null;
20-
this.callback = null;
16+
if (typeof config == 'string') {
17+
config = {
18+
text: config,
19+
values: values,
20+
};
21+
}
22+
this.text = config.text;
23+
this.values = config.values;
24+
this.name = config.name;
25+
this.callback = config.callback;
2126
this.state = 'new';
22-
this._arrayMode = false;
27+
this._arrayMode = config.rowMode == 'array';
2328

2429
//if the 'row' event is listened for
2530
//then emit them as they come in
@@ -87,6 +92,7 @@ NativeQuery.prototype.handleError = function(err) {
8792
NativeQuery.prototype.submit = function(client) {
8893
this.state = 'running';
8994
var self = this;
95+
this.native = client.native;
9096
client.native.arrayMode = this._arrayMode;
9197

9298
var after = function(err, rows) {
@@ -136,19 +142,19 @@ NativeQuery.prototype.submit = function(client) {
136142
//check if the client has already executed this named query
137143
//if so...just execute it again - skip the planning phase
138144
if(client.namedQueries[this.name]) {
139-
return this.native.execute(this.name, values, after);
145+
return client.native.execute(this.name, values, after);
140146
}
141147
//plan the named query the first time, then execute it
142-
return this.native.prepare(this.name, this.text, values.length, function(err) {
148+
return client.native.prepare(this.name, this.text, values.length, function(err) {
143149
if(err) return after(err);
144150
client.namedQueries[self.name] = true;
145151
return self.native.execute(self.name, values, after);
146152
});
147153
}
148154
else if(this.values) {
149155
var vals = this.values.map(utils.prepareValue);
150-
this.native.query(this.text, vals, after);
156+
client.native.query(this.text, vals, after);
151157
} else {
152-
this.native.query(this.text, after);
158+
client.native.query(this.text, after);
153159
}
154160
};

test/integration/client/no-row-result-tests.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ var pg = helper.pg;
33
var config = helper.config;
44

55
test('can access results when no rows are returned', function() {
6-
console.log('maybe fix this?', __filename)
7-
if(config.native) return false;
6+
if(config.native) {
7+
console.log('maybe fix this?', __filename)
8+
return false
9+
}
810
var checkResult = function(result) {
911
assert(result.fields, 'should have fields definition');
1012
assert.equal(result.fields.length, 1);

test/integration/gh-issues/131-tests.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
var helper = require(__dirname + "/../test-helper");
1+
var helper = require('../test-helper');
22
var pg = helper.pg;
33

4-
test('parsing array results', function() {
5-
pg.connect(helper.config, assert.calls(function(err, client, done) {
4+
test('parsing array results', function () {
5+
pg.connect(helper.config, assert.calls(function (err, client, done) {
66
assert.isNull(err);
77
client.query("CREATE TEMP TABLE why(names text[], numbors integer[], decimals double precision[])");
88
client.query(new pg.Query('INSERT INTO why(names, numbors, decimals) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\', \'{.1, 0.05, 3.654}\')')).on('error', console.log);
9-
test('decimals', function() {
10-
client.query('SELECT decimals FROM why', assert.success(function(result) {
9+
test('decimals', function () {
10+
client.query('SELECT decimals FROM why', assert.success(function (result) {
1111
assert.lengthIs(result.rows[0].decimals, 3);
1212
assert.equal(result.rows[0].decimals[0], 0.1);
1313
assert.equal(result.rows[0].decimals[1], 0.05);

0 commit comments

Comments
 (0)