重构条件解析函数;优化参数传递

master
宇天 2018-02-22 00:43:10 +08:00
parent 637f40a0e7
commit b5330c859c
3 changed files with 105 additions and 19 deletions

View File

@ -73,6 +73,8 @@ class Mysqli {
}
defer.resolve(conn)
})
} else {
defer.resolve(conn)
}
})
return new Method(defer.promise)

View File

@ -22,7 +22,6 @@ class Method {
return defer.reject('SHOW databases ' + err)
}
defer.resolve(row.map(it => it.Database))
// conn.release()
})
return defer.promise
@ -39,7 +38,6 @@ class Method {
return defer.reject('SHOW tables ' + err)
}
defer.resolve(row.map(it => Object.values(it)[0]))
// conn.release()
})
return defer.promise
@ -61,9 +59,7 @@ class Method {
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(
`Query ${err}; \nLast exec SQL:「${this.lastSql}`
)
return defer.reject(`Query ${err}; \nLast exec SQL:「${sql}`)
}
defer.resolve(result)
})
@ -107,9 +103,7 @@ class Method {
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(
`Filter ${err}; \nLast exec SQL:「${this.lastSql}`
)
return defer.reject(`Filter ${err}; \nLast exec SQL:「${sql}`)
}
defer.resolve(result)
})
@ -151,7 +145,7 @@ class Method {
conn.query(sql, (err, result) => {
if (err) {
return defer.reject(`Insert ${err}; Last exec SQL: ${sql}`)
return defer.reject(`Insert ${err}; \nLast exec SQL: ${sql}`)
}
defer.resolve(result.insertId)
@ -166,11 +160,17 @@ class Method {
return Promise.reject('Update Error: empty table')
}
let sql = `UPDATE \`${table}\` SET `
let sql = `UPDATE ${table} SET `
let fields = [] //要更新的字段
for (let i in doc) {
fields.push(i + ' = ' + escape(doc[i]))
let val = doc[i]
if (typeof val === 'object' && val.$sql) {
val = `(${val.$sql})`
} else {
val = escape(val)
}
fields.push(i + ' = ' + val)
}
sql += fields.join(',')
sql += parser.where(where)
@ -179,7 +179,7 @@ class Method {
const defer = Promise.defer()
conn.query(sql, (err, res) => {
if (err) {
return defer.reject(`Update ${err}; Last exec SQL: ${sql}`)
return defer.reject(`Update ${err}; \nLast exec SQL: ${sql}`)
}
defer.resolve(res.affectedRows)
@ -203,7 +203,7 @@ class Method {
const defer = Promise.defer()
conn.query(sql, (err, res) => {
if (err) {
return defer.reject(`Remove ${err}; Last exec SQL: ${sql}`)
return defer.reject(`Remove ${err}; \nLast exec SQL: ${sql}`)
}
defer.resolve(res.affectedRows)

View File

@ -11,7 +11,7 @@ const { escape } = require('mysql')
* @param [array] arr [条件数组]
*/
function parseWhere(arr) {
/*function parseWhere(arr) {
if (typeof arr === 'string' && !!arr) {
return ' WHERE ' + arr
} else if (Array.isArray(arr) && arr.length > 0) {
@ -36,13 +36,82 @@ function parseWhere(arr) {
} else {
return ' '
}
}*/
function getType(val) {
if (val === null) {
return String(val)
}
return Object.prototype.toString
.call(val)
.slice(8, -1)
.toLowerCase()
}
function parse$or(arr) {
let sql = ''
for (let it of arr) {
sql += '('
if (it.$and) {
sql += parse$and(it.$and)
} else {
sql += parse$opt(it)
}
sql += ') OR '
}
sql = sql.slice(0, -3)
return sql
}
function parse$and(arr) {
let sql = ''
for (let it of arr) {
sql += '('
if (it.$or) {
sql += parse$or(it.$or)
} else {
sql += parse$opt(it)
}
sql += ') AND '
}
sql = sql.slice(0, -4)
return sql
}
function parse$opt(opt) {
let sql = ''
for (let k in opt) {
let tmp = opt[k]
switch (getType(tmp)) {
case 'object':
if (tmp.$like) {
sql += ` ${k} LIKE ${escape(tmp.$like)} `
break
}
if (tmp.$sql) {
sql += ` ${k} IN ${tmp.$sql} `
break
}
if (tmp.$in) {
let list = tmp.$in.map(it => {
return escape(it)
})
sql += ` ${k} IN (${list.join(',')}) `
break
}
default:
sql += ` ${k} = ${escape(tmp)}`
}
sql += ' AND '
}
sql = sql.slice(0, -4)
return sql
}
const Parser = {
leftJoin(tables) {
let sql = ''
for (let it of tables) {
sql += ` LEFT JOIN ${it[0]} ON ${it[1]} `
sql += ` LEFT JOIN ${it.table} ON ${it.on} `
}
return sql
},
@ -50,7 +119,7 @@ const Parser = {
rightJoin(tables) {
let sql = ''
for (let it of tables) {
sql += ` RIGHT JOIN ${it[0]} ON ${it[1]} `
sql += ` RIGHT JOIN ${it.table} ON ${it.on} `
}
return sql
},
@ -58,13 +127,28 @@ const Parser = {
join(tables) {
let sql = ''
for (let it of tables) {
sql += ` JOIN ${it[0]} ON ${it[1]} `
sql += ` JOIN ${it[0]} ON ${it.on} `
}
return sql
},
where(where = '') {
return parseWhere(where)
where(opt) {
if (typeof opt === 'string') {
return ` WHERE ${opt} `
}
if (typeof opt === 'function') {
return ` WHERE ${opt()} `
}
if (typeof opt === 'object') {
if (opt.$and) {
return parse$and(opt.$and)
} else if (opt.$or) {
return parse$or(opt.$or)
}
return parse$opt(opt)
}
return ' '
},
select(arr = ['*']) {