bug修复;优化异常返回

master 2.1.1
宇天 2018-03-06 17:54:10 +08:00
parent 5a3ea70687
commit 3f45448101
4 changed files with 43 additions and 26 deletions

View File

@ -1,3 +1,10 @@
2.1.1 / 2018-03-06
==================
* Bug修复
* 异常返回的格式改为对象
2.1.0 / 2018-03-03
==================
* 大重构, 更加简练, 结构也更加合理

View File

@ -64,12 +64,12 @@ class Mysqli {
this.pool.getConnection(slave, (err, conn) => {
if (err) {
return defer.reject(`MySQL connect ${err}`)
return defer.reject({ err: `MySQL connect ${err}`, sql: '' })
}
if (db) {
conn.query('USE ' + db, err => {
if (err) {
return defer.reject('Select DB ' + err)
return defer.reject({ err: 'Select DB ' + err, sql: '' })
}
defer.resolve(conn)
})

View File

@ -17,9 +17,12 @@ class Method {
return this.defer.then(conn => {
const defer = Promise.defer()
conn.query('SHOW databases', (err, row) => {
conn.query('SHOW DATABASES', (err, row) => {
if (err) {
return defer.reject('SHOW databases ' + err)
return defer.reject({
err: 'SHOW DATABASES ' + err,
sql: ''
})
}
defer.resolve(row.map(it => it.Database))
})
@ -33,9 +36,12 @@ class Method {
return this.defer.then(conn => {
const defer = Promise.defer()
conn.query('SHOW tables', (err, row) => {
conn.query('SHOW TABLES', (err, row) => {
if (err) {
return defer.reject('SHOW tables ' + err)
return defer.reject({
err: 'SHOW TABLES ' + err,
sql: ''
})
}
defer.resolve(row.map(it => Object.values(it)[0]))
})
@ -50,16 +56,17 @@ class Method {
*/
query(sql) {
if (typeof sql !== 'string')
return Promise.reject(
`Query error, argument sql must be string. ${typeof sql} given`
)
return Promise.reject({
err: `Query error, argument sql must be string. ${typeof sql} given`,
sql
})
return this.defer.then(conn => {
const defer = Promise.defer()
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(`Query ${err}; \nLast exec SQL:「${sql}`)
return defer.reject({ err: `Query ${err}`, sql })
}
defer.resolve(result)
})
@ -69,12 +76,12 @@ class Method {
find(condition, select) {
const { table, leftJoin, rightJoin, join, where, sort, limit } = condition
let sql = ''
if (!table) {
return Promise.reject('Filter Error: empty table')
return Promise.reject({ err: 'Find Error: empty table', sql })
}
let sql = parser.select(select)
sql = parser.select(select)
sql += `FROM ${table} `
if (leftJoin) {
sql += parser.leftJoin(leftJoin)
@ -103,7 +110,7 @@ class Method {
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(`Filter ${err}; \nLast exec SQL:「${sql}`)
return defer.reject({ err: `Find ${err}`, sql })
}
defer.resolve(result)
})
@ -113,24 +120,25 @@ class Method {
findOne(condition, select) {
condition.limit = [1]
return this.filter(condition, select).then(row => {
return this.find(condition, select).then(row => {
return row[0] || null
})
}
count(condition) {
delete condition.limit
return this.filter(condition, ['count(*) AS total']).then(row => {
return this.find(condition, ['count(*) AS total']).then(row => {
return (row[0] && row[0].total) || 0
})
}
insert({ table }, doc) {
let sql = ''
if (!table) {
return Promise.reject('Insert Error: empty table')
return Promise.reject({ err: 'Insert Error: empty table', sql })
}
let sql = `INSERT INTO ${table} `
sql = `INSERT INTO ${table} `
let keys = []
let vals = []
@ -145,7 +153,7 @@ class Method {
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(`Insert ${err}; \nLast exec SQL: ${sql}`)
return defer.reject({ err: `Insert ${err}`, sql })
}
defer.resolve(result.insertId)
@ -156,11 +164,12 @@ class Method {
}
update({ table, where }, doc) {
let sql = ''
if (!table) {
return Promise.reject('Update Error: empty table')
return Promise.reject({ err: 'Update Error: empty table', sql })
}
let sql = `UPDATE ${table} SET `
sql = `UPDATE ${table} SET `
let fields = [] //要更新的字段
for (let i in doc) {
@ -179,7 +188,7 @@ class Method {
const defer = Promise.defer()
conn.query(sql, (err, res) => {
if (err) {
return defer.reject(`Update ${err}; \nLast exec SQL: ${sql}`)
return defer.reject({ err: `Update ${err}`, sql })
}
defer.resolve(res.affectedRows)
@ -189,11 +198,12 @@ class Method {
}
remove({ table, where }) {
let sql = ''
if (!table) {
return Promise.reject('Remove Error: empty table')
return Promise.reject({ err: 'Remove Error: empty table', sql })
}
let sql = `DELETE FROM \`${table}\` `
sql = `DELETE FROM \`${table}\` `
if (where) {
sql += parser.where(where)
@ -203,7 +213,7 @@ class Method {
const defer = Promise.defer()
conn.query(sql, (err, res) => {
if (err) {
return defer.reject(`Remove ${err}; \nLast exec SQL: ${sql}`)
return defer.reject({ err: `Remove ${err}`, sql })
}
defer.resolve(res.affectedRows)

View File

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