2022-01-23 23:34:51 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
ref="textSearch"
|
|
|
|
|
|
></textarea>
|
|
|
|
|
|
<div class="search__suffixes search__suffixes--text-mode">
|
|
|
|
|
|
<div class="search__suffix" @click="changeMode">
|
|
|
|
|
|
<i class="cn-icon cn-icon-search-advance"></i>
|
|
|
|
|
|
</div>
|
2022-01-25 17:16:56 +08:00
|
|
|
|
<div class="search__suffix" @click="search">
|
2022-01-23 23:34:51 +08:00
|
|
|
|
<i class="el-icon-search"></i>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import 'codemirror/theme/ambiance.css'
|
|
|
|
|
|
import 'codemirror/addon/hint/show-hint'
|
|
|
|
|
|
import 'codemirror/addon/hint/show-hint.css'
|
2022-01-25 17:16:56 +08:00
|
|
|
|
import 'codemirror/addon/display/placeholder'
|
2022-01-23 23:34:51 +08:00
|
|
|
|
import 'codemirror/mode/sql/sql'
|
2022-02-18 10:07:43 +08:00
|
|
|
|
import SqlParser, { stringInQuot, handleOperatorSpace } from '@/components/advancedSearch/meta/sql-parser'
|
2022-01-25 17:16:56 +08:00
|
|
|
|
import CodeMirror from 'codemirror'
|
|
|
|
|
|
import { toRaw } from 'vue'
|
|
|
|
|
|
import { columnType } from '@/components/advancedSearch/meta/meta'
|
2022-01-27 10:38:11 +08:00
|
|
|
|
import { ElMessage } from 'element-plus'
|
2022-03-12 16:56:46 +08:00
|
|
|
|
import { reg } from '@/utils/constants'
|
2022-01-25 17:16:56 +08:00
|
|
|
|
|
2022-01-23 23:34:51 +08:00
|
|
|
|
export default {
|
|
|
|
|
|
name: 'TextMode',
|
2022-01-25 17:16:56 +08:00
|
|
|
|
props: {
|
|
|
|
|
|
columnList: Array,
|
|
|
|
|
|
sql: String
|
|
|
|
|
|
},
|
|
|
|
|
|
data () {
|
|
|
|
|
|
return {
|
|
|
|
|
|
codeMirror: null
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-01-23 23:34:51 +08:00
|
|
|
|
methods: {
|
|
|
|
|
|
initCodeMirror () {
|
2022-01-25 17:16:56 +08:00
|
|
|
|
this.codeMirror = CodeMirror.fromTextArea(this.$refs.textSearch, {
|
2022-01-23 23:34:51 +08:00
|
|
|
|
mode: {
|
|
|
|
|
|
name: 'sql'
|
|
|
|
|
|
},
|
2022-01-25 17:16:56 +08:00
|
|
|
|
placeholder: 'Enter...',
|
2022-01-23 23:34:51 +08:00
|
|
|
|
lineNumbers: false
|
|
|
|
|
|
})
|
2022-01-25 22:58:23 +08:00
|
|
|
|
this.codeMirror.setOption('extraKeys', {
|
|
|
|
|
|
Enter: (cm) => {
|
|
|
|
|
|
this.search()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2022-01-23 23:34:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
search () {
|
2022-03-30 15:37:55 +08:00
|
|
|
|
let originalSql = this.codeMirror.getValue().trim()
|
2022-01-25 22:58:23 +08:00
|
|
|
|
if (originalSql) {
|
2022-03-12 16:56:46 +08:00
|
|
|
|
originalSql = originalSql.replaceAll(/"/g, '')
|
|
|
|
|
|
// 为解决ip无法校验通过的问题,先将带引号的ip转为不带引号的,再把不带引号的转为带引号的
|
|
|
|
|
|
originalSql = originalSql.replaceAll(reg.notStrictWithQuotIpv4, function (word) {
|
|
|
|
|
|
return word.replaceAll(/'/g, '')
|
|
|
|
|
|
})
|
|
|
|
|
|
originalSql = originalSql.replaceAll(reg.notStrictIpv4, function (word) {
|
|
|
|
|
|
return `'${word}'`
|
|
|
|
|
|
})
|
|
|
|
|
|
originalSql = originalSql.replaceAll(reg.notStrictWithQuotIpv6, function (word) {
|
|
|
|
|
|
return word.replaceAll(/'/g, '')
|
|
|
|
|
|
})
|
|
|
|
|
|
originalSql = originalSql.replaceAll(reg.notStrictIpv6, function (word) {
|
|
|
|
|
|
return `'${word}'`
|
|
|
|
|
|
})
|
2022-01-25 22:58:23 +08:00
|
|
|
|
const parser = new SqlParser(originalSql, this.columnList)
|
|
|
|
|
|
const errorList = parser.validate()
|
|
|
|
|
|
if (this.$_.isEmpty(errorList)) {
|
|
|
|
|
|
const { metaList, formatSql } = parser.formatSql()
|
|
|
|
|
|
toRaw(this.codeMirror).setValue(formatSql)
|
|
|
|
|
|
this.$emit('search', metaList, formatSql)
|
|
|
|
|
|
} else {
|
2022-01-27 10:38:11 +08:00
|
|
|
|
ElMessage.error(this.$t('tip.invalidExpression'))
|
2022-01-25 22:58:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.$emit('search')
|
2022-01-25 17:16:56 +08:00
|
|
|
|
}
|
2022-01-23 23:34:51 +08:00
|
|
|
|
},
|
2022-01-26 18:13:40 +08:00
|
|
|
|
focus () {
|
|
|
|
|
|
this.codeMirror.focus()
|
|
|
|
|
|
},
|
2022-01-23 23:34:51 +08:00
|
|
|
|
changeMode () {
|
2022-01-25 17:16:56 +08:00
|
|
|
|
const originalSql = this.codeMirror.getValue()
|
|
|
|
|
|
const parser = new SqlParser(originalSql, this.columnList)
|
|
|
|
|
|
const errorList = parser.validate()
|
|
|
|
|
|
if (this.$_.isEmpty(errorList)) {
|
|
|
|
|
|
const { metaList, formatSql } = parser.formatSql()
|
|
|
|
|
|
toRaw(this.codeMirror).setValue(formatSql)
|
|
|
|
|
|
this.$emit('changeMode', 'tag', metaList)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.$emit('changeMode', 'tag', [])
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-02-18 10:07:43 +08:00
|
|
|
|
// 处理value,例如转换IN的值
|
|
|
|
|
|
handleValue (value, column, operator) {
|
|
|
|
|
|
const isArray = ['IN', 'NOT IN'].indexOf(operator) > -1
|
|
|
|
|
|
if (isArray) {
|
|
|
|
|
|
if (this.$_.isArray(value)) {
|
|
|
|
|
|
value = value.map(v => column.type === columnType.string ? stringInQuot(v) : v)
|
|
|
|
|
|
return `(${value.join(',')})`
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return value
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return column.type === columnType.string ? stringInQuot(value) : value
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-01-25 17:16:56 +08:00
|
|
|
|
addParams (params) {
|
2022-02-18 10:07:43 +08:00
|
|
|
|
let current = this.codeMirror.getValue()
|
|
|
|
|
|
params.forEach(param => {
|
|
|
|
|
|
const column = this.columnList.find(c => c.name === param.column)
|
|
|
|
|
|
current = `${current ? current + ' AND ' : ''}${param.column}${handleOperatorSpace(param.operator)}${this.handleValue(param.value, column, param.operator)}`
|
|
|
|
|
|
})
|
2022-03-30 15:37:55 +08:00
|
|
|
|
toRaw(this.codeMirror).setValue(current.trim())
|
2022-02-18 10:07:43 +08:00
|
|
|
|
},
|
2022-02-18 18:09:44 +08:00
|
|
|
|
removeParams (params) {
|
|
|
|
|
|
let current = this.codeMirror.getValue()
|
|
|
|
|
|
params.forEach(param => {
|
|
|
|
|
|
const column = this.columnList.find(c => c.name === param.column)
|
|
|
|
|
|
// 将对应内容替换为空串
|
|
|
|
|
|
const sqlPiece = `${param.column}${handleOperatorSpace(param.operator)}${this.handleValue(param.value, column, param.operator)}`.trim()
|
2022-02-22 22:22:15 +08:00
|
|
|
|
const sqlPieceWithConnection = [` AND ${sqlPiece}`, ` OR ${sqlPiece}`, `${sqlPiece} AND `, `${sqlPiece} OR `, sqlPiece]
|
|
|
|
|
|
sqlPieceWithConnection.forEach(piece => {
|
|
|
|
|
|
current = current.replace(piece, '')
|
|
|
|
|
|
})
|
2022-02-18 18:09:44 +08:00
|
|
|
|
})
|
2022-03-30 15:37:55 +08:00
|
|
|
|
toRaw(this.codeMirror).setValue(current.trim())
|
2022-02-18 10:07:43 +08:00
|
|
|
|
},
|
|
|
|
|
|
changeParams (params) {
|
2022-02-18 18:09:44 +08:00
|
|
|
|
let current = this.codeMirror.getValue()
|
|
|
|
|
|
params.forEach(param => {
|
|
|
|
|
|
const oldColumn = this.columnList.find(c => c.name === param.oldParam.column)
|
|
|
|
|
|
const newColumn = this.columnList.find(c => c.name === param.newParam.column)
|
|
|
|
|
|
// 将oldParam内容替换为newParam
|
|
|
|
|
|
const oldSqlPiece = `${param.oldParam.column}${handleOperatorSpace(param.oldParam.operator)}${this.handleValue(param.oldParam.value, oldColumn, param.oldParam.operator)}`.trim()
|
|
|
|
|
|
const newSqlPiece = `${param.newParam.column}${handleOperatorSpace(param.newParam.operator)}${this.handleValue(param.newParam.value, newColumn, param.newParam.operator)}`.trim()
|
|
|
|
|
|
current = current.replace(oldSqlPiece, newSqlPiece)
|
2022-01-25 17:16:56 +08:00
|
|
|
|
})
|
2022-03-30 15:37:55 +08:00
|
|
|
|
toRaw(this.codeMirror).setValue(current.trim())
|
2022-01-25 17:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
sql: {
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
handler (n) {
|
|
|
|
|
|
if (n) {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
toRaw(this.codeMirror).setValue(n)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-01-23 23:34:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
mounted () {
|
|
|
|
|
|
this.initCodeMirror()
|
2022-03-12 16:56:46 +08:00
|
|
|
|
const vm = this
|
|
|
|
|
|
this.emitter.on('advanced-search', function () {
|
|
|
|
|
|
vm.search()
|
|
|
|
|
|
})
|
2022-01-23 23:34:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|