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-01-25 22:58:23 +08:00
|
|
|
import SqlParser, { stringInQuot } 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-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-01-25 22:58:23 +08:00
|
|
|
let originalSql = this.codeMirror.getValue()
|
|
|
|
|
if (originalSql) {
|
|
|
|
|
originalSql = originalSql.replace(/"/g, '')
|
|
|
|
|
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 {
|
|
|
|
|
console.info(errorList)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$emit('search')
|
2022-01-25 17:16:56 +08:00
|
|
|
}
|
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', [])
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
addParams (params) {
|
|
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
|
const column = this.columnList.find(column => {
|
|
|
|
|
return column.name === key
|
|
|
|
|
})
|
|
|
|
|
let current = this.codeMirror.getValue()
|
|
|
|
|
current = `${key}=${(column.type === columnType.string ? stringInQuot(params[key]) : params[key])} ${current ? 'AND' : ''}`
|
|
|
|
|
toRaw(this.codeMirror).setValue(current)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|