优化异常输出

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修复
* 修改连接方式

View File

@ -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)

View File

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