bug修复;优化异常返回

master 2.1.2
宇天 2018-03-06 20:48:41 +08:00
parent 3f45448101
commit 587dc51db2
4 changed files with 53 additions and 39 deletions

View File

@ -1,6 +1,7 @@
2.1.1 / 2018-03-06
==================
* Bug修复
* 修改连接方式
* 异常返回的格式改为对象

View File

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

View File

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

View File

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