king
2023-11-05 e01d39d4a2c3137fdce5f18f9589a34df13b7963
src/utils/utils-custom.js
@@ -1,4 +1,5 @@
import md5 from 'md5'
import { notification } from 'antd'
export default class MenuUtils {
  /**
@@ -1703,3 +1704,149 @@
  return errors
}
/**
 * @description 检测sql
 */
export function checkSQL(sql, type) {
  if (!sql) return true
  let label = '数据源中'
  if (type === 'customscript') {
    label = '自定义sql语句中'
  }
  let _quot = sql.match(/'{1}/g)
  let _lparen = sql.match(/\({1}/g)
  let _rparen = sql.match(/\){1}/g)
  _quot = _quot ? _quot.length : 0
  _lparen = _lparen ? _lparen.length : 0
  _rparen = _rparen ? _rparen.length : 0
  if (_quot % 2 !== 0) {
    notification.warning({
      top: 92,
      message: 'sql中\'必须成对出现',
      duration: 5
    })
    return false
  } else if (_lparen !== _rparen) {
    notification.warning({
      top: 92,
      message: 'sql中()必须成对出现',
      duration: 5
    })
    return false
  } else if (/--/ig.test(sql)) {
    let lines = []
    sql.split(/\n/).forEach((s, i) => {
      if (/--/ig.test(s)) {
        lines.push(i + 1)
      }
    })
    lines = lines.join('、')
    lines = lines ? '(第' + lines + '行)' : ''
    notification.warning({
      top: 92,
      message: label + `${lines},不可出现字符 -- ,注释请用 /*内容*/`,
      duration: 5
    })
    return false
  } else if (/,,/ig.test(sql)) {
    let lines = []
    sql.split(/\n/).forEach((s, i) => {
      if (/,,/ig.test(s)) {
        lines.push(i + 1)
      }
    })
    lines = lines.join('、')
    lines = lines ? '(第' + lines + '行)' : ''
    notification.warning({
      top: 92,
      message: label + `${lines},不可出现连续的英文逗号,,`,
      duration: 5
    })
    return false
  } else if (/‘|’/ig.test(sql)) {
    let lines = []
    sql.split(/\n/).forEach((s, i) => {
      if (/‘|’/ig.test(s)) {
        lines.push(i + 1)
      }
    })
    lines = lines.join('、')
    lines = lines ? '(第' + lines + '行)' : ''
    notification.warning({
      top: 92,
      message: label + `${lines},不可出现中文单引号`,
      duration: 5
    })
    return false
  } else if (type === 'customscript' && /\son\s+[a-z0-9_]+\.[a-z0-9_]+\s*=\s*[a-z0-9_]+\.[a-z0-9_]+/ig.test(sql)) {
    let list = sql.match(/\son\s+[a-z0-9_]+\.[a-z0-9_]+\s*=\s*[a-z0-9_]+\.[a-z0-9_]+/ig)
    let errors = []
    list.forEach(str => {
      str = str.replace(/^\s/, '')
      let strs = str.match(/(\s|=)[a-z0-9_]+\./ig)
      if (strs.length === 2 && (strs[0].replace(/\s|\./g, '') === strs[1].replace(/\s|\./g, ''))) {
        errors.push(str)
      }
    })
    if (errors.length > 0) {
      notification.warning({
        top: 92,
        message: '不可使用同一个表字段进行关联:' + errors.join('、'),
        duration: 5
      })
      return false
    }
  }
  let error = ''
  let chars = [
    {key: 'create', reg: /(^|\s|\(|\))create\s/ig},
    {key: 'insert', reg: /(^|\s|\(|\))insert\s/ig},
    {key: 'delete', reg: /(^|\s|\(|\))delete\s/ig},
    {key: 'update', reg: /(^|\s|\(|\))update\s/ig},
    {key: 'set', reg: /(^|\s|\(|\))set\s/ig},
    {key: 'drop', reg: /(^|\s|\(|\))drop\s/ig},
    {key: 'alter', reg: /(^|\s|\(|\))alter\s/ig},
    {key: 'truncate', reg: /(^|\s|\(|\))truncate\s/ig},
    {key: 'if', reg: /(^|\s|\(|\))if\s/ig},
    {key: 'exec', reg: /(^|\s|\(|\))exec(\s|\()/ig},
    {key: 'OBJECT', reg: /(^|\s|\(|\))object(\s|\()/ig},
    {key: 'sys.', reg: /(^|\s|\(|\))sys\./ig},
    {key: 'kill', reg: /(^|\s|\(|\))kill\s/ig}
  ]
  if (type === 'customscript') {
    chars = chars.filter(char => !['create', 'insert', 'delete', 'update', 'set', 'drop', 'if', 'exec'].includes(char.key))
  }
  sql = sql.replace(/sys\.fn_/ig, '') // 跳过sys.fn_验证
  chars.forEach(char => {
    if (!error && char.reg.test(sql)) {
      error = char.key
    }
  })
  if (error) {
    notification.warning({
      top: 92,
      message: 'sql中不可使用' + error,
      duration: 5
    })
    return false
  }
  return true
}