优化异常输出

master 2.1.3
宇天 2018-03-14 17:42:43 +08:00
parent 587dc51db2
commit 2b0c88fd66
3 changed files with 36 additions and 33 deletions

View File

@ -1,4 +1,8 @@
2.1.1 / 2018-03-06 2.1.3 / 2018-03-14
==================
* 优化异常输出
2.1.2 / 2018-03-06
================== ==================
* Bug修复 * Bug修复
* 修改连接方式 * 修改连接方式

View File

@ -8,6 +8,13 @@
const { escape } = require('mysql') const { escape } = require('mysql')
const parser = require('./parser') const parser = require('./parser')
class SqlErr extends Error {
constructor(msg, sql) {
super(msg)
this.sql = sql || ''
}
}
class Method { class Method {
constructor(pool, slave, db) { constructor(pool, slave, db) {
this.pool = pool this.pool = pool
@ -19,12 +26,12 @@ class Method {
this.defer = Promise.defer() this.defer = Promise.defer()
this.pool.getConnection(this.slave, (err, conn) => { this.pool.getConnection(this.slave, (err, conn) => {
if (err) { if (err) {
return this.defer.reject({ err: `MySQL connect ${err}`, sql: '' }) return this.defer.reject(new SqlErr(`MySQL connect ${err}`))
} }
if (this.db) { if (this.db) {
conn.query('USE ' + this.db, err => { conn.query('USE ' + this.db, err => {
if (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) this.defer.resolve(conn)
}) })
@ -42,10 +49,7 @@ class Method {
conn.query('SHOW DATABASES', (err, row) => { conn.query('SHOW DATABASES', (err, row) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ return defer.reject(new SqlErr('SHOW DATABASES ' + err))
err: 'SHOW DATABASES ' + err,
sql: ''
})
} }
defer.resolve(row.map(it => it.Database)) defer.resolve(row.map(it => it.Database))
}) })
@ -62,10 +66,7 @@ class Method {
conn.query('SHOW TABLES', (err, row) => { conn.query('SHOW TABLES', (err, row) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ return defer.reject(new SqlErr('SHOW TABLES ' + err))
err: 'SHOW TABLES ' + err,
sql: ''
})
} }
defer.resolve(row.map(it => Object.values(it)[0])) defer.resolve(row.map(it => Object.values(it)[0]))
}) })
@ -80,10 +81,12 @@ class Method {
*/ */
query(sql) { query(sql) {
if (typeof sql !== 'string') { if (typeof sql !== 'string') {
return Promise.reject({ return Promise.reject(
err: `Query error, argument sql must be string. ${typeof sql} given`, new SqlErr(
sql: sql `Query error, argument sql must be string. ${typeof sql} given`,
}) sql
)
)
} }
return this.connect().then(conn => { return this.connect().then(conn => {
@ -92,7 +95,7 @@ class Method {
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ err: `Query ${err}`, sql: sql }) return defer.reject(new SqlErr(`Query ${err}`, sql))
} }
defer.resolve(result) defer.resolve(result)
}) })
@ -102,12 +105,11 @@ class Method {
find(condition, select) { find(condition, select) {
const { table, leftJoin, rightJoin, join, where, sort, limit } = condition const { table, leftJoin, rightJoin, join, where, sort, limit } = condition
let sql = ''
if (!table) { 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} ` sql += `FROM ${table} `
if (leftJoin) { if (leftJoin) {
sql += parser.leftJoin(leftJoin) sql += parser.leftJoin(leftJoin)
@ -137,7 +139,7 @@ class Method {
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ err: `Find ${err}`, sql: sql }) return defer.reject(new SqlErr(`Find ${err}`, sql))
} }
defer.resolve(result) defer.resolve(result)
}) })
@ -160,12 +162,11 @@ class Method {
} }
insert({ table }, doc) { insert({ table }, doc) {
let sql = ''
if (!table) { 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 keys = []
let vals = [] let vals = []
@ -181,7 +182,7 @@ class Method {
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ err: `Insert ${err}`, sql: sql }) return defer.reject(new SqlErr(`Insert ${err}`, sql))
} }
defer.resolve(result.insertId) defer.resolve(result.insertId)
@ -192,12 +193,11 @@ class Method {
} }
update({ table, where }, doc) { update({ table, where }, doc) {
let sql = ''
if (!table) { 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 = [] //要更新的字段 let fields = [] //要更新的字段
for (let i in doc) { for (let i in doc) {
@ -217,7 +217,7 @@ class Method {
conn.query(sql, (err, res) => { conn.query(sql, (err, res) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ err: `Update ${err}`, sql: sql }) return defer.reject(new SqlErr(`Update ${err}`, sql))
} }
defer.resolve(res.affectedRows) defer.resolve(res.affectedRows)
@ -227,12 +227,11 @@ class Method {
} }
remove({ table, where }) { remove({ table, where }) {
let sql = ''
if (!table) { 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) { if (where) {
sql += parser.where(where) sql += parser.where(where)
@ -243,7 +242,7 @@ class Method {
conn.query(sql, (err, res) => { conn.query(sql, (err, res) => {
conn.release() conn.release()
if (err) { if (err) {
return defer.reject({ err: `Remove ${err}`, sql: sql }) return defer.reject(new SqlErr(`Remove ${err}`, sql))
} }
defer.resolve(res.affectedRows) defer.resolve(res.affectedRows)

View File

@ -1,6 +1,6 @@
{ {
"name": "mysqli", "name": "mysqli",
"version": "2.1.2", "version": "2.1.3",
"description": "MySQL tool", "description": "MySQL tool",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {