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">
|
2023-06-16 10:14:44 +08:00
|
|
|
|
<i class="cn-icon cn-icon-search-advance"></i>
|
2022-01-23 23:34:51 +08:00
|
|
|
|
</div>
|
2023-06-16 10:14:44 +08:00
|
|
|
|
<!-- <div class="search__suffix-close" @click="cleanParams">-->
|
|
|
|
|
|
<!-- <i class="el-icon-error"></i>-->
|
|
|
|
|
|
<!-- </div>-->
|
|
|
|
|
|
<!-- <div class="search__suffix new-search__suffix" @click="search">-->
|
|
|
|
|
|
<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-06-06 17:34:55 +08:00
|
|
|
|
import Parser, { stringInQuot, handleOperatorSpace } from '@/components/advancedSearch/meta/parser'
|
2022-01-25 17:16:56 +08:00
|
|
|
|
import CodeMirror from 'codemirror'
|
|
|
|
|
|
import { toRaw } from 'vue'
|
2022-06-06 17:34:55 +08:00
|
|
|
|
import _ from 'lodash'
|
2022-01-25 17:16:56 +08:00
|
|
|
|
import { columnType } from '@/components/advancedSearch/meta/meta'
|
2022-12-08 16:09:46 +08:00
|
|
|
|
import { handleErrorTip } from '@/components/advancedSearch/meta/error'
|
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,
|
2022-06-06 17:34:55 +08:00
|
|
|
|
str: String
|
2022-01-25 17:16:56 +08:00
|
|
|
|
},
|
|
|
|
|
|
data () {
|
|
|
|
|
|
return {
|
|
|
|
|
|
codeMirror: null
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-12-14 15:01:48 +08:00
|
|
|
|
emits: ['changeMode', 'search'],
|
2022-01-23 23:34:51 +08:00
|
|
|
|
methods: {
|
2023-06-16 10:14:44 +08:00
|
|
|
|
// cleanParams () {
|
|
|
|
|
|
// toRaw(this.codeMirror).setValue('')
|
|
|
|
|
|
// },
|
2022-01-23 23:34:51 +08:00
|
|
|
|
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', {
|
2022-04-13 10:14:36 +08:00
|
|
|
|
Enter: (cm) => {}
|
2022-01-25 22:58:23 +08:00
|
|
|
|
})
|
2022-01-23 23:34:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
search () {
|
2022-06-06 17:34:55 +08:00
|
|
|
|
const str = this.codeMirror.getValue().trim()
|
|
|
|
|
|
if (str) {
|
|
|
|
|
|
const parser = new Parser(this.columnList)
|
|
|
|
|
|
const errorList = parser.validateStr(str)
|
|
|
|
|
|
if (_.isEmpty(errorList)) {
|
|
|
|
|
|
this.$emit('search', parser.parseStr(str))
|
2022-01-25 22:58:23 +08:00
|
|
|
|
} else {
|
2022-06-15 20:41:21 +08:00
|
|
|
|
this.$message.error(handleErrorTip(errorList[0]))
|
2022-01-25 22:58:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2022-06-06 17:34:55 +08:00
|
|
|
|
this.$emit('search', { q: '', str: '', metaList: [] })
|
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-06-06 17:34:55 +08:00
|
|
|
|
const str = this.codeMirror.getValue().trim()
|
|
|
|
|
|
if (str) {
|
|
|
|
|
|
const parser = new Parser(this.columnList)
|
|
|
|
|
|
const errorList = parser.validateStr(str)
|
|
|
|
|
|
if (_.isEmpty(errorList)) {
|
2022-06-15 20:41:21 +08:00
|
|
|
|
const metaList = parser.parseStr(str)
|
|
|
|
|
|
this.$emit('changeMode', 'tag', metaList)
|
2022-06-06 17:34:55 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.$emit('changeMode', 'tag', { metaList: [], str: '' })
|
|
|
|
|
|
}
|
2022-01-25 17:16:56 +08:00
|
|
|
|
} else {
|
2022-06-06 17:34:55 +08:00
|
|
|
|
this.$emit('changeMode', 'tag', { str: '', metaList: [] })
|
2022-01-25 17:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-02-18 10:07:43 +08:00
|
|
|
|
// 处理value,例如转换IN的值
|
|
|
|
|
|
handleValue (value, column, operator) {
|
|
|
|
|
|
const isArray = ['IN', 'NOT IN'].indexOf(operator) > -1
|
|
|
|
|
|
if (isArray) {
|
2022-06-06 17:34:55 +08:00
|
|
|
|
if (_.isArray(value)) {
|
2022-02-18 10:07:43 +08:00
|
|
|
|
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: {
|
2022-06-06 17:34:55 +08:00
|
|
|
|
str: {
|
2022-01-25 17:16:56 +08:00
|
|
|
|
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>
|
2023-06-15 18:48:47 +08:00
|
|
|
|
|
2023-06-16 10:14:44 +08:00
|
|
|
|
<!--<style lang="scss">-->
|
|
|
|
|
|
<!--.search__suffix-close {-->
|
|
|
|
|
|
<!-- .el-icon-error {-->
|
|
|
|
|
|
<!-- font-size: 17px;-->
|
|
|
|
|
|
<!-- color: #C4C4C4;-->
|
|
|
|
|
|
<!-- margin: 0 12px;-->
|
|
|
|
|
|
<!-- cursor: pointer;-->
|
|
|
|
|
|
<!-- }-->
|
|
|
|
|
|
<!--}-->
|
2023-06-15 18:48:47 +08:00
|
|
|
|
|
2023-06-16 10:14:44 +08:00
|
|
|
|
<!--.new-search__suffix {-->
|
|
|
|
|
|
<!-- width: 41px;-->
|
|
|
|
|
|
<!-- height: 41px;-->
|
|
|
|
|
|
<!-- line-height: 41px;-->
|
|
|
|
|
|
<!-- background: #38ACD2;-->
|
|
|
|
|
|
<!-- text-align: center;-->
|
|
|
|
|
|
<!-- margin-top: -10px;-->
|
|
|
|
|
|
<!-- margin-right: -10px;-->
|
2023-06-15 18:48:47 +08:00
|
|
|
|
|
2023-06-16 10:14:44 +08:00
|
|
|
|
<!-- .el-icon-search {-->
|
|
|
|
|
|
<!-- color: #fff !important;-->
|
|
|
|
|
|
<!-- margin-top: 9px !important;-->
|
|
|
|
|
|
<!-- }-->
|
|
|
|
|
|
<!--}-->
|
|
|
|
|
|
<!--</style>-->
|