This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/components/advancedSearch/TextMode.vue

168 lines
5.8 KiB
Vue
Raw Normal View History

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'
import { ElMessage } from 'element-plus'
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
})
this.codeMirror.setOption('extraKeys', {
Enter: (cm) => {
this.search()
}
})
2022-01-23 23:34:51 +08:00
},
search () {
let originalSql = this.codeMirror.getValue()
if (originalSql) {
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}'`
})
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 {
ElMessage.error(this.$t('tip.invalidExpression'))
}
} 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)}`
})
toRaw(this.codeMirror).setValue(current)
},
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, '')
})
})
toRaw(this.codeMirror).setValue(current)
2022-02-18 10:07:43 +08:00
},
changeParams (params) {
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
})
toRaw(this.codeMirror).setValue(current)
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()
const vm = this
this.emitter.on('advanced-search', function () {
vm.search()
})
2022-01-23 23:34:51 +08:00
}
}
</script>