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 2.1.1 / 2018-03-06
================== ==================
* Bug修复 * Bug修复
* 修改连接方式
* 异常返回的格式改为对象 * 异常返回的格式改为对象

View File

@ -59,25 +59,8 @@ class Mysqli {
} }
emit(fromSlave = false, db) { emit(fromSlave = false, db) {
const defer = Promise.defer()
const slave = fromSlave && this.useSlaveDB ? 'SLAVE*' : 'MASTER' const slave = fromSlave && this.useSlaveDB ? 'SLAVE*' : 'MASTER'
return new Method(this.pool, slave, db)
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)
} }
} }

View File

@ -9,15 +9,38 @@ const { escape } = require('mysql')
const parser = require('./parser') const parser = require('./parser')
class Method { class Method {
constructor(defer) { constructor(pool, slave, db) {
this.defer = defer 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() { listDb() {
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query('SHOW DATABASES', (err, row) => { conn.query('SHOW DATABASES', (err, row) => {
conn.release()
if (err) { if (err) {
return defer.reject({ return defer.reject({
err: 'SHOW DATABASES ' + err, err: 'SHOW DATABASES ' + err,
@ -33,10 +56,11 @@ class Method {
//返回数据表 //返回数据表
listTable() { listTable() {
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query('SHOW TABLES', (err, row) => { conn.query('SHOW TABLES', (err, row) => {
conn.release()
if (err) { if (err) {
return defer.reject({ return defer.reject({
err: 'SHOW TABLES ' + err, err: 'SHOW TABLES ' + err,
@ -55,18 +79,20 @@ class Method {
* @param {[type]} sql [sql语句] * @param {[type]} sql [sql语句]
*/ */
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`, 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() const defer = Promise.defer()
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release()
if (err) { if (err) {
return defer.reject({ err: `Query ${err}`, sql }) return defer.reject({ err: `Query ${err}`, sql: sql })
} }
defer.resolve(result) defer.resolve(result)
}) })
@ -78,7 +104,7 @@ class Method {
const { table, leftJoin, rightJoin, join, where, sort, limit } = condition const { table, leftJoin, rightJoin, join, where, sort, limit } = condition
let sql = '' let sql = ''
if (!table) { 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) sql = parser.select(select)
@ -105,12 +131,13 @@ class Method {
sql += parser.limit(limit) sql += parser.limit(limit)
} }
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release()
if (err) { if (err) {
return defer.reject({ err: `Find ${err}`, sql }) return defer.reject({ err: `Find ${err}`, sql: sql })
} }
defer.resolve(result) defer.resolve(result)
}) })
@ -135,7 +162,7 @@ class Method {
insert({ table }, doc) { insert({ table }, doc) {
let sql = '' let sql = ''
if (!table) { 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} ` sql = `INSERT INTO ${table} `
@ -148,12 +175,13 @@ class Method {
} }
sql += `(${keys.join(',')}) VALUES (${vals.join(',')})` sql += `(${keys.join(',')}) VALUES (${vals.join(',')})`
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release()
if (err) { if (err) {
return defer.reject({ err: `Insert ${err}`, sql }) return defer.reject({ err: `Insert ${err}`, sql: sql })
} }
defer.resolve(result.insertId) defer.resolve(result.insertId)
@ -166,7 +194,7 @@ class Method {
update({ table, where }, doc) { update({ table, where }, doc) {
let sql = '' let sql = ''
if (!table) { 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 ` sql = `UPDATE ${table} SET `
@ -184,11 +212,12 @@ class Method {
sql += fields.join(',') sql += fields.join(',')
sql += parser.where(where) sql += parser.where(where)
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query(sql, (err, res) => { conn.query(sql, (err, res) => {
conn.release()
if (err) { if (err) {
return defer.reject({ err: `Update ${err}`, sql }) return defer.reject({ err: `Update ${err}`, sql: sql })
} }
defer.resolve(res.affectedRows) defer.resolve(res.affectedRows)
@ -200,7 +229,7 @@ class Method {
remove({ table, where }) { remove({ table, where }) {
let sql = '' let sql = ''
if (!table) { 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}\` ` sql = `DELETE FROM \`${table}\` `
@ -209,11 +238,12 @@ class Method {
sql += parser.where(where) sql += parser.where(where)
} }
return this.defer.then(conn => { return this.connect().then(conn => {
const defer = Promise.defer() const defer = Promise.defer()
conn.query(sql, (err, res) => { conn.query(sql, (err, res) => {
conn.release()
if (err) { if (err) {
return defer.reject({ err: `Remove ${err}`, sql }) return defer.reject({ err: `Remove ${err}`, sql: sql })
} }
defer.resolve(res.affectedRows) defer.resolve(res.affectedRows)

View File

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