Skip to content

Commit 18e0dea

Browse files
committed
feat: add options.needFields, default is true
- if needFields = false, return rows result instead of {rows, fields} object result. - add escape() function
1 parent d19a2c4 commit 18e0dea

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ language: node_js
22

33
node_js:
44
- iojs-1
5+
- iojs-2
56
- '0.12'
6-
- '0.10'
7+
- '0.11'
78

89
env:
910
global:

lib/client.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,39 @@ function RDSClient(options) {
2424
return new RDSClient(options);
2525
}
2626
this.pool = mysql.createPool(options);
27+
// if needFields = false, return rows result instead of {rows, fields} object result.
28+
this._needFields = options.needFields === false ? false : true;
2729
}
2830

2931
var proto = RDSClient.prototype;
3032

3133
proto.query = function (sql, values) {
3234
var pool = this.pool;
35+
var needFields = this._needFields;
3336
return function (callback) {
3437
pool.query(sql, values, function (err, rows, fields) {
35-
callback(err, { rows: rows, fields: fields });
38+
if (needFields) {
39+
callback(err, { rows: rows, fields: fields });
40+
} else {
41+
callback(err, rows);
42+
}
3643
});
3744
};
3845
};
3946

47+
proto.escape = function (val) {
48+
return this.pool.escape(val);
49+
};
50+
4051
proto.getConnection = function () {
4152
var pool = this.pool;
53+
var needFields = this._needFields;
4254
return function (callback) {
4355
pool.getConnection(function (err, connection) {
4456
if (err) {
4557
return callback(err);
4658
}
47-
var conn = new RDSConnection(connection);
59+
var conn = new RDSConnection(connection, needFields);
4860
callback(null, conn);
4961
});
5062
};

lib/connection.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
module.exports = RDSConnection;
1818

19-
function RDSConnection(conn) {
19+
function RDSConnection(conn, needFields) {
2020
this.conn = conn;
21+
this._needFields = needFields;
2122
}
2223

2324
var proto = RDSConnection.prototype;
@@ -28,13 +29,22 @@ proto.release = function () {
2829

2930
proto.query = function (sql, values) {
3031
var conn = this.conn;
32+
var needFields = this._needFields;
3133
return function (callback) {
3234
conn.query(sql, values, function (err, rows, fields) {
33-
callback(err, { rows: rows, fields: fields });
35+
if (needFields) {
36+
callback(err, { rows: rows, fields: fields });
37+
} else {
38+
callback(err, rows);
39+
}
3440
});
3541
};
3642
};
3743

44+
proto.escape = function (val) {
45+
return this.conn.escape(val);
46+
};
47+
3848
proto.beginTransaction = function () {
3949
var conn = this.conn;
4050
return function (callback) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cnpm": "npm install --registry=https://registry.npm.taobao.org"
1616
},
1717
"dependencies": {
18-
"mysql": "~2.5.5"
18+
"mysql": "~2.7.0"
1919
},
2020
"devDependencies": {
2121
"autod": "*",

test/ali-rds.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var assert = require('assert');
1818
var rds = require('../');
1919
var config = require('./config');
2020

21-
describe.only('ali-rds.test.js', function () {
21+
describe('ali-rds.test.js', function () {
2222
before(function* () {
2323
this.db = rds(config);
2424
yield this.db.query('truncate `ali-sdk-test-user`');
@@ -33,6 +33,41 @@ describe.only('ali-rds.test.js', function () {
3333
});
3434
});
3535

36+
describe('options.needFields = false', function () {
37+
var options = {};
38+
for (var k in config) {
39+
options[k] = config[k];
40+
}
41+
options.needFields = false;
42+
var db = rds(options);
43+
44+
it('should return rows only', function* () {
45+
var rows = yield db.query('show tables');
46+
assert(rows);
47+
assert(Array.isArray(rows));
48+
});
49+
50+
it('should connection query return rows only', function* () {
51+
var conn = yield db.getConnection();
52+
var rows = yield conn.query('show tables');
53+
conn.release();
54+
assert(rows);
55+
assert(Array.isArray(rows));
56+
});
57+
});
58+
59+
describe('escape()', function () {
60+
it('should client return escape string', function () {
61+
assert.equal(this.db.escape('\'\"?<//\\'), '\'\\\'\\"?<//\\\\\'');
62+
});
63+
64+
it('should connection return escape string', function* () {
65+
var conn = yield this.db.getConnection();
66+
assert.equal(conn.escape('\'\"?<//\\'), '\'\\\'\\"?<//\\\\\'');
67+
conn.release();
68+
});
69+
});
70+
3671
describe('query()', function () {
3772
before(function* () {
3873
yield this.db.query('insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified) \

0 commit comments

Comments
 (0)