2017-12-14 16:36:39 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @authors yutent (yutent@doui.cc)
|
|
|
|
* @date 2017-12-14 14:01:03
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
const { escape } = require('mysql')
|
|
|
|
const parser = require('./parser')
|
|
|
|
|
2018-03-14 17:42:43 +08:00
|
|
|
class SqlErr extends Error {
|
|
|
|
constructor(msg, sql) {
|
|
|
|
super(msg)
|
|
|
|
this.sql = sql || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:36:39 +08:00
|
|
|
class Method {
|
2018-03-06 20:48:41 +08:00
|
|
|
constructor(pool, slave, db) {
|
|
|
|
this.pool = pool
|
|
|
|
this.slave = slave
|
|
|
|
this.db = db
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
2018-03-15 11:44:32 +08:00
|
|
|
const defer = Promise.defer()
|
2018-03-06 20:48:41 +08:00
|
|
|
this.pool.getConnection(this.slave, (err, conn) => {
|
|
|
|
if (err) {
|
2018-03-15 11:44:32 +08:00
|
|
|
return defer.reject(new SqlErr(`MySQL connect ${err}`))
|
2018-03-06 20:48:41 +08:00
|
|
|
}
|
|
|
|
if (this.db) {
|
|
|
|
conn.query('USE ' + this.db, err => {
|
|
|
|
if (err) {
|
2018-03-15 11:44:32 +08:00
|
|
|
return defer.reject(new SqlErr('Select DB ' + err))
|
2018-03-06 20:48:41 +08:00
|
|
|
}
|
2018-03-15 11:44:32 +08:00
|
|
|
defer.resolve(conn)
|
2018-03-06 20:48:41 +08:00
|
|
|
})
|
|
|
|
} else {
|
2018-03-15 11:44:32 +08:00
|
|
|
defer.resolve(conn)
|
2018-03-06 20:48:41 +08:00
|
|
|
}
|
|
|
|
})
|
2018-03-15 11:44:32 +08:00
|
|
|
return defer.promise
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
listDb() {
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
|
2018-03-06 17:54:10 +08:00
|
|
|
conn.query('SHOW DATABASES', (err, row) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr('SHOW DATABASES ' + err))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
defer.resolve(row.map(it => it.Database))
|
|
|
|
})
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回数据表
|
|
|
|
listTable() {
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
|
2018-03-06 17:54:10 +08:00
|
|
|
conn.query('SHOW TABLES', (err, row) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr('SHOW TABLES ' + err))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
defer.resolve(row.map(it => Object.values(it)[0]))
|
|
|
|
})
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [query sql语句执行]
|
|
|
|
* @param {[type]} sql [sql语句]
|
|
|
|
*/
|
|
|
|
query(sql) {
|
2018-03-06 20:48:41 +08:00
|
|
|
if (typeof sql !== 'string') {
|
2018-03-14 17:42:43 +08:00
|
|
|
return Promise.reject(
|
|
|
|
new SqlErr(
|
|
|
|
`Query error, argument sql must be string. ${typeof sql} given`,
|
|
|
|
sql
|
|
|
|
)
|
|
|
|
)
|
2018-03-06 20:48:41 +08:00
|
|
|
}
|
2017-12-14 16:36:39 +08:00
|
|
|
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
|
|
|
|
conn.query(sql, (err, result) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr(`Query ${err}`, sql))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
defer.resolve(result)
|
|
|
|
})
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:48:51 +08:00
|
|
|
find(condition, select) {
|
2017-12-14 16:36:39 +08:00
|
|
|
const { table, leftJoin, rightJoin, join, where, sort, limit } = condition
|
|
|
|
if (!table) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return Promise.reject(new SqlErr('Find Error: empty table'))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:42:43 +08:00
|
|
|
let sql = parser.select(select)
|
2018-03-03 20:10:31 +08:00
|
|
|
sql += `FROM ${table} `
|
2017-12-14 16:36:39 +08:00
|
|
|
if (leftJoin) {
|
|
|
|
sql += parser.leftJoin(leftJoin)
|
|
|
|
}
|
|
|
|
if (rightJoin) {
|
|
|
|
sql += parser.rightJoin(rightJoin)
|
|
|
|
}
|
|
|
|
if (join) {
|
|
|
|
sql += parser.join(join)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (where) {
|
|
|
|
sql += parser.where(where)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort) {
|
|
|
|
sql += parser.sort(sort)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit) {
|
|
|
|
sql += parser.limit(limit)
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
|
|
|
|
conn.query(sql, (err, result) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr(`Find ${err}`, sql))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
defer.resolve(result)
|
|
|
|
})
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:48:51 +08:00
|
|
|
findOne(condition, select) {
|
2017-12-14 16:36:39 +08:00
|
|
|
condition.limit = [1]
|
2018-03-06 17:54:10 +08:00
|
|
|
return this.find(condition, select).then(row => {
|
2017-12-14 16:36:39 +08:00
|
|
|
return row[0] || null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
count(condition) {
|
|
|
|
delete condition.limit
|
2018-03-06 17:54:10 +08:00
|
|
|
return this.find(condition, ['count(*) AS total']).then(row => {
|
2017-12-14 16:36:39 +08:00
|
|
|
return (row[0] && row[0].total) || 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
insert({ table }, doc) {
|
|
|
|
if (!table) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return Promise.reject(new SqlErr('Insert Error: empty table'))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:42:43 +08:00
|
|
|
let sql = `INSERT INTO ${table} `
|
2017-12-14 16:36:39 +08:00
|
|
|
let keys = []
|
|
|
|
let vals = []
|
|
|
|
|
|
|
|
for (let i in doc) {
|
|
|
|
keys.push(i)
|
|
|
|
vals.push(escape(doc[i]))
|
|
|
|
}
|
|
|
|
sql += `(${keys.join(',')}) VALUES (${vals.join(',')})`
|
|
|
|
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
|
|
|
|
conn.query(sql, (err, result) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr(`Insert ${err}`, sql))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer.resolve(result.insertId)
|
|
|
|
})
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
update({ table, where }, doc) {
|
|
|
|
if (!table) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return Promise.reject(new SqlErr('Update Error: empty table'))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:42:43 +08:00
|
|
|
let sql = `UPDATE ${table} SET `
|
2017-12-14 16:36:39 +08:00
|
|
|
|
|
|
|
let fields = [] //要更新的字段
|
|
|
|
for (let i in doc) {
|
2018-02-22 00:43:10 +08:00
|
|
|
let val = doc[i]
|
|
|
|
if (typeof val === 'object' && val.$sql) {
|
|
|
|
val = `(${val.$sql})`
|
|
|
|
} else {
|
|
|
|
val = escape(val)
|
|
|
|
}
|
|
|
|
fields.push(i + ' = ' + val)
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
sql += fields.join(',')
|
|
|
|
sql += parser.where(where)
|
|
|
|
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
conn.query(sql, (err, res) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr(`Update ${err}`, sql))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer.resolve(res.affectedRows)
|
|
|
|
})
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
remove({ table, where }) {
|
|
|
|
if (!table) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return Promise.reject(new SqlErr('Remove Error: empty table'))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:42:43 +08:00
|
|
|
let sql = `DELETE FROM \`${table}\` `
|
2017-12-14 16:36:39 +08:00
|
|
|
|
|
|
|
if (where) {
|
|
|
|
sql += parser.where(where)
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:48:41 +08:00
|
|
|
return this.connect().then(conn => {
|
2017-12-14 16:36:39 +08:00
|
|
|
const defer = Promise.defer()
|
|
|
|
conn.query(sql, (err, res) => {
|
2018-03-06 20:48:41 +08:00
|
|
|
conn.release()
|
2017-12-14 16:36:39 +08:00
|
|
|
if (err) {
|
2018-03-14 17:42:43 +08:00
|
|
|
return defer.reject(new SqlErr(`Remove ${err}`, sql))
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer.resolve(res.affectedRows)
|
|
|
|
})
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Method
|