From 587dc51db23fb3dfac22bf3f0ab9a6a8b1a27247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Tue, 6 Mar 2018 20:48:41 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D;=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 1 + index.js | 19 +------------- lib/method.js | 70 ++++++++++++++++++++++++++++++++++++--------------- package.json | 2 +- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/History.md b/History.md index 3bd193e..55526e1 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ 2.1.1 / 2018-03-06 ================== * Bug修复 + * 修改连接方式 * 异常返回的格式改为对象 diff --git a/index.js b/index.js index c308928..fe27909 100644 --- a/index.js +++ b/index.js @@ -59,25 +59,8 @@ class Mysqli { } emit(fromSlave = false, db) { - const defer = Promise.defer() const slave = fromSlave && this.useSlaveDB ? 'SLAVE*' : 'MASTER' - - this.pool.getConnection(slave, (err, conn) => { - if (err) { - return defer.reject({ err: `MySQL connect ${err}`, sql: '' }) - } - if (db) { - conn.query('USE ' + db, err => { - if (err) { - return defer.reject({ err: 'Select DB ' + err, sql: '' }) - } - defer.resolve(conn) - }) - } else { - defer.resolve(conn) - } - }) - return new Method(defer.promise) + return new Method(this.pool, slave, db) } } diff --git a/lib/method.js b/lib/method.js index f1bdc27..f929098 100644 --- a/lib/method.js +++ b/lib/method.js @@ -9,15 +9,38 @@ const { escape } = require('mysql') const parser = require('./parser') class Method { - constructor(defer) { - this.defer = defer + constructor(pool, slave, db) { + this.pool = pool + this.slave = slave + this.db = db + } + + connect() { + this.defer = Promise.defer() + this.pool.getConnection(this.slave, (err, conn) => { + if (err) { + return this.defer.reject({ err: `MySQL connect ${err}`, sql: '' }) + } + if (this.db) { + conn.query('USE ' + this.db, err => { + if (err) { + return this.defer.reject({ err: 'Select DB ' + err, sql: '' }) + } + this.defer.resolve(conn) + }) + } else { + this.defer.resolve(conn) + } + }) + return this.defer.promise } listDb() { - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query('SHOW DATABASES', (err, row) => { + conn.release() if (err) { return defer.reject({ err: 'SHOW DATABASES ' + err, @@ -33,10 +56,11 @@ class Method { //返回数据表 listTable() { - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query('SHOW TABLES', (err, row) => { + conn.release() if (err) { return defer.reject({ err: 'SHOW TABLES ' + err, @@ -55,18 +79,20 @@ class Method { * @param {[type]} sql [sql语句] */ query(sql) { - if (typeof sql !== 'string') + if (typeof sql !== 'string') { return Promise.reject({ err: `Query error, argument sql must be string. ${typeof sql} given`, - sql + sql: sql }) + } - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query(sql, (err, result) => { + conn.release() if (err) { - return defer.reject({ err: `Query ${err}`, sql }) + return defer.reject({ err: `Query ${err}`, sql: sql }) } defer.resolve(result) }) @@ -78,7 +104,7 @@ class Method { const { table, leftJoin, rightJoin, join, where, sort, limit } = condition let sql = '' if (!table) { - return Promise.reject({ err: 'Find Error: empty table', sql }) + return Promise.reject({ err: 'Find Error: empty table', sql: sql }) } sql = parser.select(select) @@ -105,12 +131,13 @@ class Method { sql += parser.limit(limit) } - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query(sql, (err, result) => { + conn.release() if (err) { - return defer.reject({ err: `Find ${err}`, sql }) + return defer.reject({ err: `Find ${err}`, sql: sql }) } defer.resolve(result) }) @@ -135,7 +162,7 @@ class Method { insert({ table }, doc) { let sql = '' if (!table) { - return Promise.reject({ err: 'Insert Error: empty table', sql }) + return Promise.reject({ err: 'Insert Error: empty table', sql: sql }) } sql = `INSERT INTO ${table} ` @@ -148,12 +175,13 @@ class Method { } sql += `(${keys.join(',')}) VALUES (${vals.join(',')})` - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query(sql, (err, result) => { + conn.release() if (err) { - return defer.reject({ err: `Insert ${err}`, sql }) + return defer.reject({ err: `Insert ${err}`, sql: sql }) } defer.resolve(result.insertId) @@ -166,7 +194,7 @@ class Method { update({ table, where }, doc) { let sql = '' if (!table) { - return Promise.reject({ err: 'Update Error: empty table', sql }) + return Promise.reject({ err: 'Update Error: empty table', sql: sql }) } sql = `UPDATE ${table} SET ` @@ -184,11 +212,12 @@ class Method { sql += fields.join(',') sql += parser.where(where) - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query(sql, (err, res) => { + conn.release() if (err) { - return defer.reject({ err: `Update ${err}`, sql }) + return defer.reject({ err: `Update ${err}`, sql: sql }) } defer.resolve(res.affectedRows) @@ -200,7 +229,7 @@ class Method { remove({ table, where }) { let sql = '' if (!table) { - return Promise.reject({ err: 'Remove Error: empty table', sql }) + return Promise.reject({ err: 'Remove Error: empty table', sql: sql }) } sql = `DELETE FROM \`${table}\` ` @@ -209,11 +238,12 @@ class Method { sql += parser.where(where) } - return this.defer.then(conn => { + return this.connect().then(conn => { const defer = Promise.defer() conn.query(sql, (err, res) => { + conn.release() if (err) { - return defer.reject({ err: `Remove ${err}`, sql }) + return defer.reject({ err: `Remove ${err}`, sql: sql }) } defer.resolve(res.affectedRows) diff --git a/package.json b/package.json index ca97f7a..34f2fbb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mysqli", - "version": "2.1.1", + "version": "2.1.2", "description": "MySQL tool", "main": "index.js", "dependencies": {