Skip to content

Commit

Permalink
feat: add queryOne api (ali-sdk#9)
Browse files Browse the repository at this point in the history
* feat: add queryOne api

* chore: remove unuse comments
  • Loading branch information
fengmk2 authored and dead-horse committed Jun 7, 2016
1 parent 21b1abb commit 19fc1bb
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 96 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,12 @@ TBD

## APIs

`*` Meaning this function is yieldable.
- `*` Meaning this function is yieldable.

### IO queries

- *query(sql[, values])
- *query(sql[, values)
- *queryOne(sql[, values)
- *select(table, options)
- *get(table, where, options)
- *insert(table, row[s], options)
Expand Down Expand Up @@ -331,7 +332,7 @@ let session = new db.literals.Literal('session()');

## TODO

- [ ] MySQL
- [x] MySQL
- [x] Pool
- [ ] Cluster
- [ ] SQL Server
Expand Down
8 changes: 0 additions & 8 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down
10 changes: 0 additions & 10 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**!
* ali-rds - lib/connection.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down
10 changes: 0 additions & 10 deletions lib/literals.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**!
* ali-rds - lib/literals.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down
28 changes: 13 additions & 15 deletions lib/operator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**!
* ali-rds - lib/operator.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

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

module.exports = Operator;

function Operator() {

}
/**
* Operator Interface
*/
function Operator() {}

const proto = Operator.prototype;

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

proto.query = function* (sql, values) {
proto.query = function*(sql, values) {
// query(sql, values)
if (arguments.length >= 2) {
sql = this.format(sql, values);
}
debug('query %j', sql);
try {
return yield this._query(sql);
const rows = yield this._query(sql);
debug('query get %d rows', rows.length);
return rows;
} catch (err) {
err.stack = err.stack + '\n sql: ' + sql;
debug('query error: %s', err);
throw err;
}
};

proto.queryOne = function*(sql, values) {
const rows = yield this.query(sql, values);
return rows && rows[0] || null;
};

proto._query = function (/* sql */) {
throw new Error('SubClass must impl this');
};
Expand Down
14 changes: 2 additions & 12 deletions lib/sqlstring.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
/**!
* ali-rds - lib/sqlstring.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
* Module dependencies.
*/

var SqlString = require('mysql/lib/protocol/SqlString');
var Literal = require('./literals').Literal;
const SqlString = require('mysql/lib/protocol/SqlString');
const Literal = require('./literals').Literal;

module.exports = SqlString;

Expand Down
8 changes: 0 additions & 8 deletions lib/transaction.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"autod": "autod -w --prefix '~'"
},
"dependencies": {
"debug": "~2.2.0",
"mysql": "~2.10.2"
"debug": "^2.2.0",
"mysql": "^2.10.2"
},
"devDependencies": {
"autod": "*",
Expand Down
30 changes: 20 additions & 10 deletions test/client.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand All @@ -16,7 +8,7 @@ const assert = require('assert');
const rds = require('../');
const config = require('./config');

describe('test/client.test.js', function () {
describe('client.test.js', function () {
const prefix = 'prefix-' + process.version + '-';
const table = 'ali-sdk-test-user';
before(function* () {
Expand All @@ -38,6 +30,13 @@ describe('test/client.test.js', function () {
assert(rows);
assert(Array.isArray(rows));
});

it('should connection query one row', function* () {
let conn = yield this.db.getConnection();
let row = yield conn.queryOne('show tables');
conn.release();
assert(row);
});
});

describe('escape()', function () {
Expand All @@ -52,7 +51,7 @@ describe('test/client.test.js', function () {
});
});

describe('query()', function () {
describe('query(), queryOne()', function () {
before(function* () {
yield this.db.query('insert into ??(name, email, gmt_create, gmt_modified) \
values(?, ?, now(), now())',
Expand All @@ -69,6 +68,12 @@ describe('test/client.test.js', function () {
assert.equal(rows[0].name, prefix + 'fengmk2');
assert.equal(rows[1].name, prefix + 'fengmk3');
});

it('should select 1 row', function* () {
const row = yield this.db.queryOne('select * from ?? where email=? order by id',
[table, prefix + 'm@fengmk2.com']);
assert.equal(row.name, prefix + 'fengmk2');
});
});

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

// test query one
const row = yield conn.queryOne('select * from ?? where name=?', [table, prefix + 'beginTransactionScopeCtx1']);
assert(row);
assert.equal(row.name, prefix + 'beginTransactionScopeCtx1');

const fooResult = yield fooInsert();
assert.equal(fooResult, true);
const barResult = yield barInsert();
Expand Down
8 changes: 0 additions & 8 deletions test/operator.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down
10 changes: 0 additions & 10 deletions test/sqlstring.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**!
* ali-rds - test/sqlstring.test.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
Expand Down

0 comments on commit 19fc1bb

Please sign in to comment.