此代码可适用 职责链模式 封装提高扩展性
analysisSQL() {
if (this.apiListForm.apiConfiguration) {
let flag, selectOrFromFlag = false
let tabs = []
this.apiListForm.apiConfiguration.split('\n').forEach((sql) => {
sql.split(' ').map(n => {
//关键字 select 到 from 中的表字段不遍历
if(n === 'select' || n === 'Select' || n === 'SELECT' || n === '(select' || n === '(Select' || n === '(SELECT') selectOrFromFlag = false
selectOrFromFlag = selectOrFromFlag || n === 'from' || n === 'From' || n === 'FROM'
//selectOrFromFlag为true是遍历每行sql提取表名
if ((flag || n.startsWith(',')) && selectOrFromFlag) {
n.split(',').map(m => {
m = m.indexOf(')') > 0 ? m.slice(0, m.indexOf(')')) : m
m = m.indexOf('(') > -1 ? m.slice(m.indexOf('(') + 1, m.length) : m
if (m !== '') {
tabs.push({tableNm: m, record: null, volume: null})
//去重
let num = 0
tabs.filter((value) =>{
//num 为 1 时 为重复出现
num === 1 ? tabs.splice(-1) ? num = 0 : '' : m === value.tableNm ? num ++ : ''
})
flag = false
}
})
}
//标记位
flag = n === 'from' || n === 'From' || n === 'FROM' || n === 'join' || n === 'Join' || n === 'JOIN' || n.endsWith(',')
})
})
this.apiListForm.takesUpResources = tabs.length === 0 ? [{tableNm: null, record: null, volume: null}] : tabs
} else {
//文本框为空且失焦时执行
this.apiListForm.takesUpResources = [{tableNm: null, record: null, volume: null}]
}
}
适用于编写比较规范的sql语句,可根据自己需求来修改辨别字段适用新需求