修复$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')
}
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 => {
let defer = Promise.defer()
if (this.debug) {
console.log('[debug]', sql)
}
conn.query(sql, (err, result) => {
conn.release()
if (err) {
@ -74,52 +79,22 @@ class Api {
if (!this.db && db) {
return Promise.reject('No database selected.')
}
this.db = db || this.db
let defer = Promise.defer()
this.connect().then(conn => {
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
return this.query(`DROP DATABASE IF EXISTS ${db || this.db}`)
}
dbList() {
return this.connect().then(conn => {
let defer = Promise.defer()
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
})
return this.query('SHOW DATABASES').then(list =>
list.map(it => it.Database)
)
}
//返回数据表
tableList() {
return this.connect().then(conn => {
let defer = Promise.defer()
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
})
return this.query('SHOW TABLES').then(list =>
list.map(it => it[Object.keys(it)[0]])
)
}
// 创建新的数据库
@ -127,21 +102,9 @@ class Api {
if (!name) {
return Promise.reject('Empty database name.')
}
let sql = `CREATE DATABASE \`${name}\` DEFAULT CHARACTER SET = \`${charset}\``
return this.connect().then(conn => {
let defer = Promise.defer()
conn.query(sql, (err, result) => {
conn.release()
if (err) {
return defer.reject(new SqlErr('Create database ' + err))
}
defer.resolve(true)
})
return defer.promise
})
return this.query(sql)
}
// 创建新的表,
@ -160,19 +123,7 @@ class Api {
sql += ` ENGINE=${engine} DEFAULT CHARSET=${charset}`
return this.connect().then(conn => {
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
})
return this.query(sql)
}
}

View File

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

View File

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

View File

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