修复$gt异常的bug;增加debug模式

master
宇天 2021-06-21 19:55:09 +08:00
parent 662769b244
commit 9d0f169f17
4 changed files with 62 additions and 216 deletions

View File

@ -39,7 +39,8 @@ class Api {
throw new SqlErr('Query Error: empty table') throw new SqlErr('Query Error: empty table')
} }
name = fixtable(name) name = fixtable(name)
return new Method(this.pool, this.slave, this.db, name)
return new Method(this.pool, this.slave, this.db, name, this.debug)
} }
/** /**
@ -59,6 +60,10 @@ class Api {
return this.connect().then(conn => { return this.connect().then(conn => {
let defer = Promise.defer() let defer = Promise.defer()
if (this.debug) {
console.log('[debug]', sql)
}
conn.query(sql, (err, result) => { conn.query(sql, (err, result) => {
conn.release() conn.release()
if (err) { if (err) {
@ -74,52 +79,22 @@ class Api {
if (!this.db && db) { if (!this.db && db) {
return Promise.reject('No database selected.') return Promise.reject('No database selected.')
} }
this.db = db || this.db
let defer = Promise.defer() let defer = Promise.defer()
this.connect().then(conn => { return this.query(`DROP DATABASE IF EXISTS ${db || this.db}`)
conn.query(`DROP DATABASE IF EXISTS ${db || this.db}`, err => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Drop database ${err}`))
}
defer.resolve(true)
})
})
return defer.promise
} }
dbList() { dbList() {
return this.connect().then(conn => { return this.query('SHOW DATABASES').then(list =>
let defer = Promise.defer() list.map(it => it.Database)
)
conn.query('SHOW DATABASES', (err, row) => {
conn.release()
if (err) {
return defer.reject(new SqlErr('Show databases ' + err))
}
defer.resolve(row.map(it => it.Database))
})
return defer.promise
})
} }
//返回数据表 //返回数据表
tableList() { tableList() {
return this.connect().then(conn => { return this.query('SHOW TABLES').then(list =>
let defer = Promise.defer() list.map(it => it[Object.keys(it)[0]])
)
conn.query('SHOW TABLES', (err, row) => {
conn.release()
if (err) {
return defer.reject(new SqlErr('Show tables ' + err))
}
defer.resolve(row.map(it => it[Object.keys(it)[0]]))
})
return defer.promise
})
} }
// 创建新的数据库 // 创建新的数据库
@ -127,21 +102,9 @@ class Api {
if (!name) { if (!name) {
return Promise.reject('Empty database name.') return Promise.reject('Empty database name.')
} }
let sql = `CREATE DATABASE \`${name}\` DEFAULT CHARACTER SET = \`${charset}\`` let sql = `CREATE DATABASE \`${name}\` DEFAULT CHARACTER SET = \`${charset}\``
return this.connect().then(conn => {
let defer = Promise.defer()
conn.query(sql, (err, result) => { return this.query(sql)
conn.release()
if (err) {
return defer.reject(new SqlErr('Create database ' + err))
}
defer.resolve(true)
})
return defer.promise
})
} }
// 创建新的表, // 创建新的表,
@ -160,19 +123,7 @@ class Api {
sql += ` ENGINE=${engine} DEFAULT CHARSET=${charset}` sql += ` ENGINE=${engine} DEFAULT CHARSET=${charset}`
return this.connect().then(conn => { return this.query(sql)
let defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr('Create table ' + err, sql))
}
defer.resolve(true)
})
return defer.promise
})
} }
} }

View File

@ -7,33 +7,42 @@
const { SqlErr, parser, escape, fixtable } = require('./utils') const { SqlErr, parser, escape, fixtable } = require('./utils')
class Method { class Method {
constructor(pool, slave, db, table) { constructor(pool, slave, db, table, debug = false) {
this.pool = pool this.pool = pool
this.slave = slave this.slave = slave
this.db = db this.db = db
this.cache = { table } this.cache = { table }
this.debug = debug
} }
connect() { _connect() {
let defer = Promise.defer() let defer = Promise.defer()
this.pool.getConnection(this.slave, (err, conn) => { this.pool.getConnection(this.slave, (err, conn) => {
if (err) { if (err) {
return defer.reject(new SqlErr(`MySQL connect ${err}`)) return defer.reject(new SqlErr(`MySQL connect ${err}`))
} }
if (this.db) {
conn.query('USE ' + this.db, err => {
if (err) {
return defer.reject(new SqlErr('Use DB ' + err))
}
defer.resolve(conn) defer.resolve(conn)
}) })
} else {
defer.resolve(conn)
}
})
return defer.promise return defer.promise
} }
_query(sql) {
return this._connect().then(conn => {
let defer = Promise.defer()
if (this.debug) {
console.log('[debug]', sql)
}
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`MySQL ${err}`, sql))
}
defer.resolve(result)
})
return defer.promise
})
}
leftJoin(tables) { leftJoin(tables) {
this.cache.leftJoin = tables this.cache.leftJoin = tables
return this return this
@ -97,7 +106,7 @@ class Method {
* [withFields 选取指定的字段] * [withFields 选取指定的字段]
* @param {string[]} fields [以数组形式传入] * @param {string[]} fields [以数组形式传入]
*/ */
withFields(fields) { withFields(...fields) {
this.cache.fields = fields this.cache.fields = fields
return this return this
} }
@ -166,18 +175,7 @@ class Method {
sql += parser.limit(limit) sql += parser.limit(limit)
} }
return this.connect().then(conn => { return this._query(sql)
let defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Find ${err}`, sql))
}
defer.resolve(result)
})
return defer.promise
})
} }
/** /**
@ -219,20 +217,7 @@ class Method {
} }
sql += `(${keys.join(',')}) VALUES (${vals.join(',')})` sql += `(${keys.join(',')}) VALUES (${vals.join(',')})`
return this.connect().then(conn => { return this._query(sql)
const defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Insert ${err}`, sql))
}
defer.resolve(result.insertId)
})
return defer.promise
})
} }
/** /**
@ -259,20 +244,7 @@ class Method {
sql += fields.join(',') sql += fields.join(',')
sql += parser.filter(filter) sql += parser.filter(filter)
return this.connect().then(conn => { return this._query(sql)
const defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Update ${err}`, sql))
}
defer.resolve(result.affectedRows)
})
return defer.promise
})
} }
/** /**
@ -284,86 +256,34 @@ class Method {
let sql = `DELETE FROM ${table} ` let sql = `DELETE FROM ${table} `
sql += parser.filter(filter) sql += parser.filter(filter)
return this.connect().then(conn => { return this._query(sql)
const defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Remove ${err}`, sql))
}
defer.resolve(result.affectedRows)
})
return defer.promise
})
} }
drop() { drop() {
let sql = `DROP TABLE IF EXISTS ${this.cache.table} ` let sql = `DROP TABLE IF EXISTS ${this.cache.table} `
return this.connect().then(conn => { return this._query(sql)
const defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr(`Drop table ${err}`, sql))
}
defer.resolve(true)
})
return defer.promise
})
} }
// 重命名表 // 重命名表
renameTo(name) { renameTo(name) {
return this.connect().then(conn => {
const defer = Promise.defer()
let sql = `RENAME TABLE ${this.cache.table} TO ${fixtable(name)}` let sql = `RENAME TABLE ${this.cache.table} TO ${fixtable(name)}`
conn.query(sql, (err, result) => { return this._query(sql)
conn.release()
if (err) {
return defer.reject(new SqlErr(`List index ${err}`, sql))
}
defer.resolve(true)
})
return defer.promise
})
} }
// 返回索引列表 // 返回索引列表
indexList() { indexList() {
return this.connect().then(conn => {
const defer = Promise.defer()
let sql = `SHOW INDEX FROM ${this.cache.table}` let sql = `SHOW INDEX FROM ${this.cache.table}`
conn.query(sql, (err, result) => {
conn.release() return this._query(sql).then(list =>
if (err) { list.map(it => ({
return defer.reject(new SqlErr(`List index ${err}`, sql))
}
let list = result.map(it => {
return {
name: it.Key_name, name: it.Key_name,
column: it.Column_name, column: it.Column_name,
unique: !it.Non_unique, unique: !it.Non_unique,
cardinality: it.Cardinality, cardinality: it.Cardinality,
collation: it.Collation collation: it.Collation
} }))
}) )
defer.resolve(list)
})
return defer.promise
})
} }
// 删除指定索引 // 删除指定索引
@ -371,21 +291,9 @@ class Method {
if (!name) { if (!name) {
return Promise.reject('Empty index name') return Promise.reject('Empty index name')
} }
return this.connect().then(conn => {
const defer = Promise.defer()
let sql = `ALTER TABLE ${this.cache.table} DROP INDEX \`${name}\`` let sql = `ALTER TABLE ${this.cache.table} DROP INDEX \`${name}\``
conn.query(sql, (err, result) => { return this._query(sql)
conn.release()
if (err) {
return defer.reject(new SqlErr(`Drop index ${err}`, sql))
}
defer.resolve(true)
})
return defer.promise
})
} }
// 删除指定索引 // 删除指定索引
@ -402,24 +310,11 @@ class Method {
unique = 'UNIQUE' unique = 'UNIQUE'
} }
return this.connect().then(conn => {
const defer = Promise.defer()
let sql = `ALTER TABLE ${ let sql = `ALTER TABLE ${
this.cache.table this.cache.table
} ADD ${unique} INDEX \`${name}\` (${opt.field})` } ADD ${unique} INDEX \`${name}\` (${opt.field})`
conn.query(sql, (err, result) => { return this._query(sql)
conn.release()
if (err) {
return defer.reject(new SqlErr(`Drop index ${err}`, sql))
}
defer.resolve(true)
})
return defer.promise
})
} }
} }

View File

@ -102,7 +102,7 @@ function parse$opt(opt) {
if (tmp.hasOwnProperty('$gt') || tmp.hasOwnProperty('$gte')) { if (tmp.hasOwnProperty('$gt') || tmp.hasOwnProperty('$gte')) {
let oc = tmp.hasOwnProperty('$gt') ? '>' : '>=' let oc = tmp.hasOwnProperty('$gt') ? '>' : '>='
let val = tmp.hasOwnProperty('$gt') ? tmp.$gt : tmp.$gte let val = tmp.hasOwnProperty('$gt') ? tmp.$gt : tmp.$gte
sql += ` ${k} >${oc} ${escape(val)} ` sql += ` ${k} ${oc} ${escape(val)} `
break break
} }

View File

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