Skip to content

Commit 063ad8d

Browse files
alsotangdougwilson
authored andcommitted
Add "query" method to PoolNamespace
closes #1256 closes #1505 closes #1506
1 parent 69aa44f commit 063ad8d

File tree

7 files changed

+186
-1
lines changed

7 files changed

+186
-1
lines changed

Changes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ you spot any mistakes.
66

77
## HEAD
88

9+
* Add `query` method to `PoolNamespace` #1256 #1505 #1506
10+
- Used as `cluster.of(...).query(...)`
911
* Update `bignumber.js` to 2.4.0
1012
* Update `sqlstring` to 2.1.0
1113
- Accept numbers and other value types in `escapeId`

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ poolCluster.of('*').getConnection(function (err, connection) {});
468468
var pool = poolCluster.of('SLAVE*', 'RANDOM');
469469
pool.getConnection(function (err, connection) {});
470470
pool.getConnection(function (err, connection) {});
471+
pool.query(function (err, result) {});
471472

472473
// close all connections
473474
poolCluster.end(function (err) {
@@ -538,7 +539,7 @@ space for a new connection to be created on the next getConnection call.
538539
## Performing queries
539540

540541
The most basic way to perform a query is to call the `.query()` method on an object
541-
(like a `Connection` or `Pool` instance).
542+
(like a `Connection`, `Pool`, or `PoolNamespace` instance).
542543

543544
The simplest form of .`query()` is `.query(sqlString, callback)`, where a SQL string
544545
is the first argument and the second is a callback:

lib/PoolNamespace.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Connection = require('./Connection');
12
var PoolSelector = require('./PoolSelector');
23

34
module.exports = PoolNamespace;
@@ -54,6 +55,65 @@ PoolNamespace.prototype.getConnection = function(cb) {
5455
});
5556
};
5657

58+
PoolNamespace.prototype.query = function (sql, values, cb) {
59+
var cluster = this._cluster;
60+
var clusterNode = this._getClusterNode();
61+
var query = Connection.createQuery(sql, values, cb);
62+
var namespace = this;
63+
64+
if (clusterNode === null) {
65+
var err = null;
66+
67+
if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
68+
err = new Error('Pool does not have online node.');
69+
err.code = 'POOL_NONEONLINE';
70+
} else {
71+
err = new Error('Pool does not exist.');
72+
err.code = 'POOL_NOEXIST';
73+
}
74+
75+
process.nextTick(function () {
76+
query.on('error', function () {});
77+
query.end(err);
78+
});
79+
return query;
80+
}
81+
82+
if (!(typeof sql === 'object' && 'typeCast' in sql)) {
83+
query.typeCast = clusterNode.pool.config.connectionConfig.typeCast;
84+
}
85+
86+
if (clusterNode.pool.config.connectionConfig.trace) {
87+
// Long stack trace support
88+
query._callSite = new Error;
89+
}
90+
91+
cluster._getConnection(clusterNode, function (err, conn) {
92+
var retry = err && cluster._canRetry
93+
&& cluster._findNodeIds(namespace._pattern).length !== 0;
94+
95+
if (retry) {
96+
namespace.query(query);
97+
return;
98+
}
99+
100+
if (err) {
101+
query.on('error', function () {});
102+
query.end(err);
103+
return;
104+
}
105+
106+
// Release connection based off event
107+
query.once('end', function() {
108+
conn.release();
109+
});
110+
111+
conn.query(query);
112+
});
113+
114+
return query;
115+
};
116+
57117
PoolNamespace.prototype._getClusterNode = function _getClusterNode() {
58118
var foundNodeIds = this._cluster._findNodeIds(this._pattern);
59119
var nodeId;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
4+
var cluster = common.createPoolCluster();
5+
var server = common.createFakeServer();
6+
7+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
8+
cluster.add('SLAVE1', poolConfig);
9+
// cluster.add('SLAVE2', poolConfig);
10+
11+
server.listen(common.fakeServerPort, function (err) {
12+
assert.ifError(err);
13+
14+
var pool = cluster.of('SLAVE*', 'ORDER');
15+
16+
pool.query('SELECT 1', function (err, rows) {
17+
assert.ok(err, 'got error');
18+
assert.equal(err.code, 'ER_HOST_NOT_PRIVILEGED');
19+
server.destroy();
20+
});
21+
});
22+
23+
server.on('connection', function (conn) {
24+
conn.deny('You suck.', common.Errors.ER_HOST_NOT_PRIVILEGED);
25+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
4+
var cluster = common.createPoolCluster();
5+
var server = common.createFakeServer();
6+
7+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
8+
9+
server.listen(common.fakeServerPort, function (err) {
10+
assert.ifError(err);
11+
12+
var pool = cluster.of('SLAVE*', 'ORDER');
13+
14+
pool.query('SELECT 1', function (err, rows) {
15+
assert.ok(err, 'got error');
16+
assert.equal(err.code, 'POOL_NOEXIST');
17+
server.destroy();
18+
});
19+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster({
4+
canRetry : true,
5+
removeNodeErrorCount : 2,
6+
restoreNodeTimeout : 100
7+
});
8+
var server = common.createFakeServer();
9+
10+
var connCount = 0;
11+
var offline = true;
12+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
13+
cluster.add('MASTER', poolConfig);
14+
15+
server.listen(common.fakeServerPort, function (err) {
16+
assert.ifError(err);
17+
18+
var pool = cluster.of('MASTER', 'ORDER');
19+
20+
pool.query('SELECT 1', function (err) {
21+
assert.ok(err);
22+
assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST');
23+
assert.equal(err.fatal, true);
24+
assert.equal(connCount, 2);
25+
26+
pool.query('SELECT 1', function (err) {
27+
assert.ok(err);
28+
assert.equal(err.code, 'POOL_NONEONLINE');
29+
30+
offline = false;
31+
});
32+
33+
setTimeout(function () {
34+
pool.query('SELECT 1', function (err) {
35+
assert.ifError(err);
36+
cluster.end(function (err) {
37+
assert.ifError(err);
38+
server.destroy();
39+
});
40+
});
41+
}, 200);
42+
});
43+
});
44+
45+
server.on('connection', function (conn) {
46+
connCount += 1;
47+
48+
if (offline) {
49+
conn.destroy();
50+
} else {
51+
conn.handshake();
52+
}
53+
});

test/unit/pool-cluster/test-query.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster();
4+
var server = common.createFakeServer();
5+
6+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
7+
cluster.add('SLAVE1', poolConfig);
8+
cluster.add('SLAVE2', poolConfig);
9+
10+
server.listen(common.fakeServerPort, function(err) {
11+
assert.ifError(err);
12+
13+
var pool = cluster.of('SLAVE*', 'ORDER');
14+
15+
pool.query('SELECT 1', function (err, rows) {
16+
assert.ifError(err);
17+
assert.equal(rows.length, 1);
18+
assert.equal(rows[0]['1'], 1);
19+
20+
cluster.end(function (err) {
21+
assert.ifError(err);
22+
server.destroy();
23+
});
24+
});
25+
});

0 commit comments

Comments
 (0)