UNPKG

eme-flow

Version:

eme flow组件

328 lines (300 loc) 10.8 kB
/** * Created by cc on 2016/9/23 0023. * 查询语句 * 必须先配置好Pooler */ 'use strict'; import mysql from 'mysql'; import tool from '../util/toolkit'; let regex = /:(\D\w+)\b/g; export default class SQL { constructor(name = false) { this._sql = []; this._where = []; this._order = []; this._group = []; this._params = {}; this._keyword = {}; this._begin = -1; this._length = -1; this._name = name; this._and = true; } push(sql) { this._sql.push(sql); return this; } and(sql){ if(sql !== undefined){ this._where.push(` AND ${sql}`); } this._and = true; return this; } or(sql){ if(sql !== undefined){ this._where.push(` OR ${sql}`); } this._and = false; return this; } where(sql) { this._where.push(sql); return this; } order(sql) { this._order.push(sql); return this; } group(sql) { this._group.push(sql); return this; } /** * * @param key like {key1 : value1,key2 : value2} or key * @param value like {key1 : keyword,key2 : keyword} or value * @param keyword like true | false * @returns {SQL} */ set(key, value, keyword = false) { if (typeof key === 'object') { tool.mix(this._params, key); if (typeof value === 'object') { tool.mix(this._keyword, value); } } else { this._params[key] = value; this._keyword[key] = keyword; } return this; } begin(c) { this._begin = c; return this; } length(c) { this._length = c; return this; } jqGrid(req) { //是否是jqgrid请求判断 if(!req || req.param('nd') === undefined) return this; let filters = req.param('filters'), search = req.param('search'), param_req = req.param('params'), __tableName__ = req.param('__tableName__'), __search__ = req.param('__search__'); try { filters = JSON.parse(req.param('filters')); } catch (e) { } let rules = filters && filters.rules || [], groupOp = (filters && filters.groupOp) || 'AND'; search && (rules = rules.concat(search)); __search__ && __search__.forEach(s => rules.push({ field: s[0], op: s[1], data: s[2] })); if (groupOp !== 'AND') this.or(); let param_cache = {}; let paramName = field => { field = field.replace(/\w+\./, ''); if (param_cache[field] === undefined) param_cache[field] = 0; return `_param_${field}_${++param_cache[field]}`; } let dataCheck = value => { if (value === '') return false; if (value === undefined) return false; if (isNaN(value)) return value; if (!isNaN(value)) return +value; } rules.forEach(rule => { rule.data = dataCheck(rule.data); if (__tableName__ !== undefined) { let ta = __tableName__.find(t => t[0] === rule.field); if (ta !== undefined) rule.field = `${ta[1]}.${rule.field}` } switch (rule.op) { case 'nu': this.where(`${groupOp} ${rule.field} IS NULL`); break; case 'nn': this.where(`${groupOp} ${rule.field} IS NOT NULL`); break; case 'gt': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} > :${p}`); this.set(p, rule.data); } break; case 'ge': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} >= :${p}`); this.set(p, rule.data); } break; case 'le': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} <= :${p}`); this.set(p, rule.data); } break; case 'lt': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} < :${p}`); this.set(p, rule.data); } break; case 'eq': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} = :${p}`); this.set(p, rule.data); } break; case 'ne': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} <> :${p}`); this.set(p, rule.data); } break; case 'cn': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} LIKE CONCAT('%',:${p},'%')`); this.set(p, rule.data); } break; case 'nc': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} NOT LIKE CONCAT('%',:${p},'%')`); this.set(p, rule.data); } break; case 'bw': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} LIKE CONCAT(:${p},'%')`); this.set(p, rule.data); } break; case 'bn': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} NOT LIKE CONCAT(:${p},'%')`); this.set(p, rule.data); } break; case 'ew': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} LIKE CONCAT('%',:${p})`); this.set(p, rule.data); } break; case 'en': if (rule.data !== false) { let p = paramName(rule.field); this.where(`${groupOp} ${rule.field} NOT LIKE CONCAT('%',:${p})`); this.set(p, rule.data); } break; } }); param_req !== undefined && this.set(param_req); let order = req.param('sidx') && [req.param('sidx'), req.param('sord')].join(' ').replace('asc, asc','').replace('desc, desc',''); if (order) { if (__tableName__ !== undefined) { order = order.replace(/[^,\s]*?/gi, (m, i) => { let n = m.toLowerCase(); if (n === 'asc' || n === 'desc') { return m; } let ta = __tableName__.find(t => t[0] === m); if (ta !== undefined) return `${ta[1]}.${m}`; return m; }); } this.order(order); } let group = req.param('groupBy'); if (group) { if (__tableName__ !== undefined) { group = group.replace(/[^,\s]*?/gi, (m, i) => { let n = m.toLowerCase(); if (n === 'asc' || n === 'desc') { return m; } let ta = __tableName__.find(t => t[0] === m); if (ta !== undefined) return `${ta[1]}.${m}`; return m; }); } this.group(group); } this.begin((+req.param('page') - 1) * +req.param('rows')); this.length(+req.param('rows')); return this; } compile() { let params = []; let sql = []; this._sql.forEach(e => { let es = []; es.push(e); if (this._where.length > 0) { es.push(' WHERE '); if(this._and === true) es.push(' 1 = 1 '); else es.push(' 1 <> 1 '); es.push(this._where.join(' ')); } if (this._group.length > 0) { es.push(' GROUP BY '); es.push(this._group.join(' ')); } if (this._order.length > 0) { es.push(' ORDER BY '); es.push(this._order.join(' ')); } sql.push(es.join('')); }); sql = sql.join(';'); sql = sql.replace(regex, (a1, a2) => { if (this._params.hasOwnProperty(a2) === false) { throw { message: `参数${a2}没有传递`, error: `参数${a2}没有传递`, state: 111001 }; } params.push(this._params[a2]); return this._keyword[a2] === true ? '??' : '?'; }); sails.log.debug(sql + ' > ' + params); return { sql: sql, values: params } } clear(){ this._sql.length = 0; this._where.length = 0; this._order.length = 0; this._group.length = 0; this._params = {}; this._keyword = {}; this._begin = -1; this._length = -1; this._and = true; return this; } } module.exports = exports.default;