Skip to content

Commit 19fc1bb

Browse files
fengmk2dead-horse
authored andcommitted
feat: add queryOne api (#9)
* feat: add queryOne api * chore: remove unuse comments
1 parent 21b1abb commit 19fc1bb

File tree

11 files changed

+41
-96
lines changed

11 files changed

+41
-96
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,12 @@ TBD
287287

288288
## APIs
289289

290-
`*` Meaning this function is yieldable.
290+
- `*` Meaning this function is yieldable.
291291

292292
### IO queries
293293

294-
- *query(sql[, values])
294+
- *query(sql[, values)
295+
- *queryOne(sql[, values)
295296
- *select(table, options)
296297
- *get(table, where, options)
297298
- *insert(table, row[s], options)
@@ -331,7 +332,7 @@ let session = new db.literals.Literal('session()');
331332

332333
## TODO
333334

334-
- [ ] MySQL
335+
- [x] MySQL
335336
- [x] Pool
336337
- [ ] Cluster
337338
- [ ] SQL Server

lib/client.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Copyright(c) ali-sdk and other contributors.
3-
* MIT Licensed
4-
*
5-
* Authors:
6-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
7-
*/
8-
91
'use strict';
102

113
/**

lib/connection.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**!
2-
* ali-rds - lib/connection.js
3-
*
4-
* Copyright(c) ali-sdk and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
9-
*/
10-
111
'use strict';
122

133
/**

lib/literals.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**!
2-
* ali-rds - lib/literals.js
3-
*
4-
* Copyright(c) ali-sdk and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
9-
*/
10-
111
'use strict';
122

133
/**

lib/operator.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**!
2-
* ali-rds - lib/operator.js
3-
*
4-
* Copyright(c) ali-sdk and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
9-
*/
10-
111
'use strict';
122

133
/**
@@ -20,9 +10,10 @@ const literals = require('./literals');
2010

2111
module.exports = Operator;
2212

23-
function Operator() {
24-
25-
}
13+
/**
14+
* Operator Interface
15+
*/
16+
function Operator() {}
2617

2718
const proto = Operator.prototype;
2819

@@ -40,21 +31,28 @@ proto.format = function (sql, values, stringifyObjects, timeZone) {
4031
return SqlString.format(sql, values, stringifyObjects, timeZone);
4132
};
4233

43-
proto.query = function* (sql, values) {
34+
proto.query = function*(sql, values) {
4435
// query(sql, values)
4536
if (arguments.length >= 2) {
4637
sql = this.format(sql, values);
4738
}
4839
debug('query %j', sql);
4940
try {
50-
return yield this._query(sql);
41+
const rows = yield this._query(sql);
42+
debug('query get %d rows', rows.length);
43+
return rows;
5144
} catch (err) {
5245
err.stack = err.stack + '\n sql: ' + sql;
5346
debug('query error: %s', err);
5447
throw err;
5548
}
5649
};
5750

51+
proto.queryOne = function*(sql, values) {
52+
const rows = yield this.query(sql, values);
53+
return rows && rows[0] || null;
54+
};
55+
5856
proto._query = function (/* sql */) {
5957
throw new Error('SubClass must impl this');
6058
};

lib/sqlstring.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
/**!
2-
* ali-rds - lib/sqlstring.js
3-
*
4-
* Copyright(c) ali-sdk and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
9-
*/
10-
111
'use strict';
122

133
/**
144
* Module dependencies.
155
*/
166

17-
var SqlString = require('mysql/lib/protocol/SqlString');
18-
var Literal = require('./literals').Literal;
7+
const SqlString = require('mysql/lib/protocol/SqlString');
8+
const Literal = require('./literals').Literal;
199

2010
module.exports = SqlString;
2111

lib/transaction.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Copyright(c) ali-sdk and other contributors.
3-
* MIT Licensed
4-
*
5-
* Authors:
6-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
7-
*/
8-
91
'use strict';
102

113
/**

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"autod": "autod -w --prefix '~'"
1414
},
1515
"dependencies": {
16-
"debug": "~2.2.0",
17-
"mysql": "~2.10.2"
16+
"debug": "^2.2.0",
17+
"mysql": "^2.10.2"
1818
},
1919
"devDependencies": {
2020
"autod": "*",

test/client.test.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Copyright(c) ali-sdk and other contributors.
3-
* MIT Licensed
4-
*
5-
* Authors:
6-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
7-
*/
8-
91
'use strict';
102

113
/**
@@ -16,7 +8,7 @@ const assert = require('assert');
168
const rds = require('../');
179
const config = require('./config');
1810

19-
describe('test/client.test.js', function () {
11+
describe('client.test.js', function () {
2012
const prefix = 'prefix-' + process.version + '-';
2113
const table = 'ali-sdk-test-user';
2214
before(function* () {
@@ -38,6 +30,13 @@ describe('test/client.test.js', function () {
3830
assert(rows);
3931
assert(Array.isArray(rows));
4032
});
33+
34+
it('should connection query one row', function* () {
35+
let conn = yield this.db.getConnection();
36+
let row = yield conn.queryOne('show tables');
37+
conn.release();
38+
assert(row);
39+
});
4140
});
4241

4342
describe('escape()', function () {
@@ -52,7 +51,7 @@ describe('test/client.test.js', function () {
5251
});
5352
});
5453

55-
describe('query()', function () {
54+
describe('query(), queryOne()', function () {
5655
before(function* () {
5756
yield this.db.query('insert into ??(name, email, gmt_create, gmt_modified) \
5857
values(?, ?, now(), now())',
@@ -69,6 +68,12 @@ describe('test/client.test.js', function () {
6968
assert.equal(rows[0].name, prefix + 'fengmk2');
7069
assert.equal(rows[1].name, prefix + 'fengmk3');
7170
});
71+
72+
it('should select 1 row', function* () {
73+
const row = yield this.db.queryOne('select * from ?? where email=? order by id',
74+
[table, prefix + 'm@fengmk2.com']);
75+
assert.equal(row.name, prefix + 'fengmk2');
76+
});
7277
});
7378

7479
describe('transactions', function () {
@@ -323,6 +328,11 @@ describe('test/client.test.js', function () {
323328
values(?, ?, now(), now())',
324329
[table, prefix + 'beginTransactionScopeCtx2', prefix + 'm@beginTransactionScopeCtx1.com']);
325330

331+
// test query one
332+
const row = yield conn.queryOne('select * from ?? where name=?', [table, prefix + 'beginTransactionScopeCtx1']);
333+
assert(row);
334+
assert.equal(row.name, prefix + 'beginTransactionScopeCtx1');
335+
326336
const fooResult = yield fooInsert();
327337
assert.equal(fooResult, true);
328338
const barResult = yield barInsert();

test/operator.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Copyright(c) ali-sdk and other contributors.
3-
* MIT Licensed
4-
*
5-
* Authors:
6-
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
7-
*/
8-
91
'use strict';
102

113
/**

0 commit comments

Comments
 (0)