Skip to content

Commit bc2f550

Browse files
committed
Remove promise-interop from native query
1 parent 55c3b36 commit bc2f550

File tree

5 files changed

+20
-85
lines changed

5 files changed

+20
-85
lines changed

lib/native/client.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,14 @@ Client.prototype.query = function(config, values, callback) {
119119
}
120120

121121
var query = new NativeQuery(config, values, callback);
122+
var result = query.callback ? query : new global.Promise(function(resolve, reject) {
123+
query.on('end', resolve);
124+
query.on('error', reject);
125+
});
126+
122127
this._queryQueue.push(query);
123128
this._pulseQueryQueue();
124-
return query;
129+
return result;
125130
};
126131

127132
//disconnect from the backend server

lib/native/query.js

-17
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ var NativeQuery = module.exports = function(config, values, callback) {
3434

3535
util.inherits(NativeQuery, EventEmitter);
3636

37-
NativeQuery.prototype.then = function(onSuccess, onFailure) {
38-
return this.promise().then(onSuccess, onFailure);
39-
};
40-
41-
NativeQuery.prototype.catch = function(callback) {
42-
return this.promise().catch(callback);
43-
};
44-
45-
NativeQuery.prototype.promise = function() {
46-
if (this._promise) return this._promise;
47-
this._promise = new Promise(function(resolve, reject) {
48-
this.once('end', resolve);
49-
this.once('error', reject);
50-
}.bind(this));
51-
return this._promise;
52-
};
53-
5437
var errorFieldMap = {
5538
'sqlState': 'code',
5639
'statementPosition': 'position',

test/native/error-tests.js

-28
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
11
var helper = require(__dirname + "/../test-helper");
22
var Client = require(__dirname + "/../../lib/native");
33

4-
test('query with non-text as first parameter throws error', function() {
5-
var client = new Client(helper.config);
6-
client.connect();
7-
assert.emits(client, 'connect', function() {
8-
client.end();
9-
assert.emits(client, 'end', function() {
10-
assert.throws(function() {
11-
client.query({text:{fail: true}});
12-
});
13-
});
14-
});
15-
});
16-
17-
test('parameterized query with non-text as first parameter throws error', function() {
18-
var client = new Client(helper.config);
19-
client.connect();
20-
assert.emits(client, 'connect', function() {
21-
client.end();
22-
assert.emits(client, 'end', function() {
23-
assert.throws(function() {
24-
client.query({
25-
text: {fail: true},
26-
values: [1, 2]
27-
})
28-
});
29-
});
30-
});
31-
});
324

335
var connect = function(callback) {
346
var client = new Client(helper.config);

test/native/evented-api-tests.js

+11-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var helper = require(__dirname + "/../test-helper");
2-
var Client = require(__dirname + "/../../lib/native");
1+
var helper = require("../test-helper");
2+
var Client = require("../../lib/native");
3+
var Query = Client.Query;
34

45
var setupClient = function() {
56
var client = new Client(helper.config);
@@ -10,37 +11,10 @@ var setupClient = function() {
1011
return client;
1112
}
1213

13-
//test('connects', function() {
14-
//var client = new Client(helper.config);
15-
//client.connect();
16-
//test('good query', function() {
17-
//var query = client.query("SELECT 1 as num, 'HELLO' as str");
18-
//assert.emits(query, 'row', function(row) {
19-
//test('has integer data type', function() {
20-
//assert.strictEqual(row.num, 1);
21-
//})
22-
//test('has string data type', function() {
23-
//assert.strictEqual(row.str, "HELLO")
24-
//})
25-
//test('emits end AFTER row event', function() {
26-
//assert.emits(query, 'end');
27-
//test('error query', function() {
28-
//var query = client.query("LSKDJF");
29-
//assert.emits(query, 'error', function(err) {
30-
//assert.ok(err != null, "Should not have emitted null error");
31-
//client.end();
32-
//})
33-
//})
34-
//})
35-
//})
36-
//})
37-
//})
38-
39-
4014
test('multiple results', function() {
4115
test('queued queries', function() {
4216
var client = setupClient();
43-
var q = client.query("SELECT name FROM BOOM");
17+
var q = client.query(new Query("SELECT name FROM BOOM"));
4418
assert.emits(q, 'row', function(row) {
4519
assert.equal(row.name, 'Aaron');
4620
assert.emits(q, 'row', function(row) {
@@ -49,7 +23,7 @@ test('multiple results', function() {
4923
})
5024
assert.emits(q, 'end', function() {
5125
test('query with config', function() {
52-
var q2 = client.query({text:'SELECT 1 as num'});
26+
var q2 = client.query(new Query({text:'SELECT 1 as num'}));
5327
assert.emits(q2, 'row', function(row) {
5428
assert.strictEqual(row.num, 1);
5529
assert.emits(q2, 'end', function() {
@@ -64,7 +38,7 @@ test('multiple results', function() {
6438
test('parameterized queries', function() {
6539
test('with a single string param', function() {
6640
var client = setupClient();
67-
var q = client.query("SELECT * FROM boom WHERE name = $1", ['Aaron']);
41+
var q = client.query(new Query("SELECT * FROM boom WHERE name = $1", ['Aaron']));
6842
assert.emits(q, 'row', function(row) {
6943
assert.equal(row.name, 'Aaron');
7044
})
@@ -75,10 +49,10 @@ test('parameterized queries', function() {
7549

7650
test('with object config for query', function() {
7751
var client = setupClient();
78-
var q = client.query({
52+
var q = client.query(new Query({
7953
text: "SELECT name FROM boom WHERE name = $1",
8054
values: ['Brian']
81-
});
55+
}));
8256
assert.emits(q, 'row', function(row) {
8357
assert.equal(row.name, 'Brian');
8458
})
@@ -89,7 +63,7 @@ test('parameterized queries', function() {
8963

9064
test('multiple parameters', function() {
9165
var client = setupClient();
92-
var q = client.query('SELECT name FROM boom WHERE name = $1 or name = $2 ORDER BY name', ['Aaron', 'Brian']);
66+
var q = client.query(new Query('SELECT name FROM boom WHERE name = $1 or name = $2 ORDER BY name', ['Aaron', 'Brian']));
9367
assert.emits(q, 'row', function(row) {
9468
assert.equal(row.name, 'Aaron');
9569
assert.emits(q, 'row', function(row) {
@@ -100,10 +74,10 @@ test('parameterized queries', function() {
10074
})
10175
})
10276
})
103-
77+
10478
test('integer parameters', function() {
10579
var client = setupClient();
106-
var q = client.query('SELECT * FROM boom WHERE age > $1', [27]);
80+
var q = client.query(new Query('SELECT * FROM boom WHERE age > $1', [27]));
10781
assert.emits(q, 'row', function(row) {
10882
assert.equal(row.name, 'Brian');
10983
assert.equal(row.age, 28);

test/native/stress-tests.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var helper = require(__dirname + "/../test-helper");
22
var Client = require(__dirname + "/../../lib/native");
3+
var Query = Client.Query;
34

45
test('many rows', function() {
56
var client = new Client(helper.config);
67
client.connect();
7-
var q = client.query("SELECT * FROM person");
8+
var q = client.query(new Query("SELECT * FROM person"));
89
var rows = [];
910
q.on('row', function(row) {
1011
rows.push(row)
@@ -21,7 +22,7 @@ test('many queries', function() {
2122
var count = 0;
2223
var expected = 100;
2324
for(var i = 0; i < expected; i++) {
24-
var q = client.query("SELECT * FROM person");
25+
var q = client.query(new Query("SELECT * FROM person"));
2526
assert.emits(q, 'end', function() {
2627
count++;
2728
});

0 commit comments

Comments
 (0)