Skip to content

Commit

Permalink
feat: add options.needFields, default is true
Browse files Browse the repository at this point in the history
- if needFields = false, return rows result instead of {rows, fields} object result.
- add escape() function
  • Loading branch information
fengmk2 committed Jun 2, 2015
1 parent d19a2c4 commit 18e0dea
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ language: node_js

node_js:
- iojs-1
- iojs-2
- '0.12'
- '0.10'
- '0.11'

env:
global:
Expand Down
16 changes: 14 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,39 @@ function RDSClient(options) {
return new RDSClient(options);
}
this.pool = mysql.createPool(options);
// if needFields = false, return rows result instead of {rows, fields} object result.
this._needFields = options.needFields === false ? false : true;
}

var proto = RDSClient.prototype;

proto.query = function (sql, values) {
var pool = this.pool;
var needFields = this._needFields;
return function (callback) {
pool.query(sql, values, function (err, rows, fields) {
callback(err, { rows: rows, fields: fields });
if (needFields) {
callback(err, { rows: rows, fields: fields });
} else {
callback(err, rows);
}
});
};
};

proto.escape = function (val) {
return this.pool.escape(val);
};

proto.getConnection = function () {
var pool = this.pool;
var needFields = this._needFields;
return function (callback) {
pool.getConnection(function (err, connection) {
if (err) {
return callback(err);
}
var conn = new RDSConnection(connection);
var conn = new RDSConnection(connection, needFields);
callback(null, conn);
});
};
Expand Down
14 changes: 12 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

module.exports = RDSConnection;

function RDSConnection(conn) {
function RDSConnection(conn, needFields) {
this.conn = conn;
this._needFields = needFields;
}

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

proto.query = function (sql, values) {
var conn = this.conn;
var needFields = this._needFields;
return function (callback) {
conn.query(sql, values, function (err, rows, fields) {
callback(err, { rows: rows, fields: fields });
if (needFields) {
callback(err, { rows: rows, fields: fields });
} else {
callback(err, rows);
}
});
};
};

proto.escape = function (val) {
return this.conn.escape(val);
};

proto.beginTransaction = function () {
var conn = this.conn;
return function (callback) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cnpm": "npm install --registry=https://registry.npm.taobao.org"
},
"dependencies": {
"mysql": "~2.5.5"
"mysql": "~2.7.0"
},
"devDependencies": {
"autod": "*",
Expand Down
37 changes: 36 additions & 1 deletion test/ali-rds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var assert = require('assert');
var rds = require('../');
var config = require('./config');

describe.only('ali-rds.test.js', function () {
describe('ali-rds.test.js', function () {
before(function* () {
this.db = rds(config);
yield this.db.query('truncate `ali-sdk-test-user`');
Expand All @@ -33,6 +33,41 @@ describe.only('ali-rds.test.js', function () {
});
});

describe('options.needFields = false', function () {
var options = {};
for (var k in config) {
options[k] = config[k];
}
options.needFields = false;
var db = rds(options);

it('should return rows only', function* () {
var rows = yield db.query('show tables');
assert(rows);
assert(Array.isArray(rows));
});

it('should connection query return rows only', function* () {
var conn = yield db.getConnection();
var rows = yield conn.query('show tables');
conn.release();
assert(rows);
assert(Array.isArray(rows));
});
});

describe('escape()', function () {
it('should client return escape string', function () {
assert.equal(this.db.escape('\'\"?<//\\'), '\'\\\'\\"?<//\\\\\'');
});

it('should connection return escape string', function* () {
var conn = yield this.db.getConnection();
assert.equal(conn.escape('\'\"?<//\\'), '\'\\\'\\"?<//\\\\\'');
conn.release();
});
});

describe('query()', function () {
before(function* () {
yield this.db.query('insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified) \
Expand Down

0 comments on commit 18e0dea

Please sign in to comment.