From 2b0c88fd66b541a00124cb3516327df7889b96ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 14 Mar 2018 17:42:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 6 ++++- lib/method.js | 61 +++++++++++++++++++++++++-------------------------- package.json | 2 +- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/History.md b/History.md index 55526e1..d0e1556 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,8 @@ -2.1.1 / 2018-03-06 +2.1.3 / 2018-03-14 +================== + * 优化异常输出 + +2.1.2 / 2018-03-06 ================== * Bug修复 * 修改连接方式 diff --git a/lib/method.js b/lib/method.js index f929098..2bcfc10 100644 --- a/lib/method.js +++ b/lib/method.js @@ -8,6 +8,13 @@ const { escape } = require('mysql') const parser = require('./parser') +class SqlErr extends Error { + constructor(msg, sql) { + super(msg) + this.sql = sql || '' + } +} + class Method { constructor(pool, slave, db) { this.pool = pool @@ -19,12 +26,12 @@ class Method { this.defer = Promise.defer() this.pool.getConnection(this.slave, (err, conn) => { if (err) { - return this.defer.reject({ err: `MySQL connect ${err}`, sql: '' }) + return this.defer.reject(new SqlErr(`MySQL connect ${err}`)) } if (this.db) { conn.query('USE ' + this.db, err => { if (err) { - return this.defer.reject({ err: 'Select DB ' + err, sql: '' }) + return this.defer.reject(new SqlErr('Select DB ' + err)) } this.defer.resolve(conn) }) @@ -42,10 +49,7 @@ class Method { conn.query('SHOW DATABASES', (err, row) => { conn.release() if (err) { - return defer.reject({ - err: 'SHOW DATABASES ' + err, - sql: '' - }) + return defer.reject(new SqlErr('SHOW DATABASES ' + err)) } defer.resolve(row.map(it => it.Database)) }) @@ -62,10 +66,7 @@ class Method { conn.query('SHOW TABLES', (err, row) => { conn.release() if (err) { - return defer.reject({ - err: 'SHOW TABLES ' + err, - sql: '' - }) + return defer.reject(new SqlErr('SHOW TABLES ' + err)) } defer.resolve(row.map(it => Object.values(it)[0])) }) @@ -80,10 +81,12 @@ class Method { */ query(sql) { if (typeof sql !== 'string') { - return Promise.reject({ - err: `Query error, argument sql must be string. ${typeof sql} given`, - sql: sql - }) + return Promise.reject( + new SqlErr( + `Query error, argument sql must be string. ${typeof sql} given`, + sql + ) + ) } return this.connect().then(conn => { @@ -92,7 +95,7 @@ class Method { conn.query(sql, (err, result) => { conn.release() if (err) { - return defer.reject({ err: `Query ${err}`, sql: sql }) + return defer.reject(new SqlErr(`Query ${err}`, sql)) } defer.resolve(result) }) @@ -102,12 +105,11 @@ class Method { find(condition, select) { const { table, leftJoin, rightJoin, join, where, sort, limit } = condition - let sql = '' if (!table) { - return Promise.reject({ err: 'Find Error: empty table', sql: sql }) + return Promise.reject(new SqlErr('Find Error: empty table')) } - sql = parser.select(select) + let sql = parser.select(select) sql += `FROM ${table} ` if (leftJoin) { sql += parser.leftJoin(leftJoin) @@ -137,7 +139,7 @@ class Method { conn.query(sql, (err, result) => { conn.release() if (err) { - return defer.reject({ err: `Find ${err}`, sql: sql }) + return defer.reject(new SqlErr(`Find ${err}`, sql)) } defer.resolve(result) }) @@ -160,12 +162,11 @@ class Method { } insert({ table }, doc) { - let sql = '' if (!table) { - return Promise.reject({ err: 'Insert Error: empty table', sql: sql }) + return Promise.reject(new SqlErr('Insert Error: empty table')) } - sql = `INSERT INTO ${table} ` + let sql = `INSERT INTO ${table} ` let keys = [] let vals = [] @@ -181,7 +182,7 @@ class Method { conn.query(sql, (err, result) => { conn.release() if (err) { - return defer.reject({ err: `Insert ${err}`, sql: sql }) + return defer.reject(new SqlErr(`Insert ${err}`, sql)) } defer.resolve(result.insertId) @@ -192,12 +193,11 @@ class Method { } update({ table, where }, doc) { - let sql = '' if (!table) { - return Promise.reject({ err: 'Update Error: empty table', sql: sql }) + return Promise.reject(new SqlErr('Update Error: empty table')) } - sql = `UPDATE ${table} SET ` + let sql = `UPDATE ${table} SET ` let fields = [] //要更新的字段 for (let i in doc) { @@ -217,7 +217,7 @@ class Method { conn.query(sql, (err, res) => { conn.release() if (err) { - return defer.reject({ err: `Update ${err}`, sql: sql }) + return defer.reject(new SqlErr(`Update ${err}`, sql)) } defer.resolve(res.affectedRows) @@ -227,12 +227,11 @@ class Method { } remove({ table, where }) { - let sql = '' if (!table) { - return Promise.reject({ err: 'Remove Error: empty table', sql: sql }) + return Promise.reject(new SqlErr('Remove Error: empty table')) } - sql = `DELETE FROM \`${table}\` ` + let sql = `DELETE FROM \`${table}\` ` if (where) { sql += parser.where(where) @@ -243,7 +242,7 @@ class Method { conn.query(sql, (err, res) => { conn.release() if (err) { - return defer.reject({ err: `Remove ${err}`, sql: sql }) + return defer.reject(new SqlErr(`Remove ${err}`, sql)) } defer.resolve(res.affectedRows) diff --git a/package.json b/package.json index 34f2fbb..6891276 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mysqli", - "version": "2.1.2", + "version": "2.1.3", "description": "MySQL tool", "main": "index.js", "dependencies": {