diff --git a/README.md b/README.md index f530e05..716d26d 100644 --- a/README.md +++ b/README.md @@ -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) @@ -331,7 +332,7 @@ let session = new db.literals.Literal('session()'); ## TODO -- [ ] MySQL +- [x] MySQL - [x] Pool - [ ] Cluster - [ ] SQL Server diff --git a/lib/client.js b/lib/client.js index 27aa5cd..c85183f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,11 +1,3 @@ -/** - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** diff --git a/lib/connection.js b/lib/connection.js index eeb35f4..bae5710 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,13 +1,3 @@ -/**! - * ali-rds - lib/connection.js - * - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** diff --git a/lib/literals.js b/lib/literals.js index 41a7af8..f7bd471 100644 --- a/lib/literals.js +++ b/lib/literals.js @@ -1,13 +1,3 @@ -/**! - * ali-rds - lib/literals.js - * - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** diff --git a/lib/operator.js b/lib/operator.js index a79cba4..7449313 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -1,13 +1,3 @@ -/**! - * ali-rds - lib/operator.js - * - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** @@ -20,9 +10,10 @@ const literals = require('./literals'); module.exports = Operator; -function Operator() { - -} +/** + * Operator Interface + */ +function Operator() {} const proto = Operator.prototype; @@ -40,14 +31,16 @@ 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); @@ -55,6 +48,11 @@ proto.query = function* (sql, values) { } }; +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'); }; diff --git a/lib/sqlstring.js b/lib/sqlstring.js index 1c0c796..9f8ada7 100644 --- a/lib/sqlstring.js +++ b/lib/sqlstring.js @@ -1,21 +1,11 @@ -/**! - * ali-rds - lib/sqlstring.js - * - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (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; diff --git a/lib/transaction.js b/lib/transaction.js index 58686ca..899bed4 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -1,11 +1,3 @@ -/** - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** diff --git a/package.json b/package.json index b922676..39725b3 100644 --- a/package.json +++ b/package.json @@ -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": "*", diff --git a/test/client.test.js b/test/client.test.js index 1cd55a7..f1f8df3 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -1,11 +1,3 @@ -/** - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** @@ -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* () { @@ -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 () { @@ -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())', @@ -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 () { @@ -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(); diff --git a/test/operator.test.js b/test/operator.test.js index 36b0382..31ec999 100644 --- a/test/operator.test.js +++ b/test/operator.test.js @@ -1,11 +1,3 @@ -/** - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /** diff --git a/test/sqlstring.test.js b/test/sqlstring.test.js index 0795a4d..386de93 100644 --- a/test/sqlstring.test.js +++ b/test/sqlstring.test.js @@ -1,13 +1,3 @@ -/**! - * ali-rds - test/sqlstring.test.js - * - * Copyright(c) ali-sdk and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; /**