dataease/frontend/src/views/dataset/add/AddSQL.vue

307 lines
8.0 KiB
Vue
Raw Normal View History

2021-03-08 11:35:28 +08:00
<template>
<el-col>
<el-row>
<el-row style="height: 26px;">
<span style="line-height: 26px;">
{{ param.tableId?$t('dataset.edit_sql'):$t('dataset.add_sql_table') }}
2021-03-08 11:35:28 +08:00
</span>
<el-row style="float: right">
<el-button size="mini" @click="cancel">
{{ $t('dataset.cancel') }}
</el-button>
<el-button size="mini" type="primary" @click="save">
2021-03-08 11:35:28 +08:00
{{ $t('dataset.confirm') }}
</el-button>
</el-row>
</el-row>
2021-03-08 12:29:11 +08:00
<el-divider />
2021-03-08 11:35:28 +08:00
<el-row>
<el-form :inline="true">
<el-form-item class="form-item">
<el-select v-model="dataSource" filterable :placeholder="$t('dataset.pls_slc_data_source')" size="mini">
<el-option
v-for="item in options"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
2021-03-12 17:49:41 +08:00
<el-form-item class="form-item">
<el-input v-model="name" size="mini" :placeholder="$t('commons.name')" />
</el-form-item>
<el-form-item class="form-item">
<el-select v-model="mode" filterable :placeholder="$t('dataset.connect_mode')" size="mini">
<el-option :label="$t('dataset.direct_connect')" value="0" />
<el-option :label="$t('dataset.sync_data')" value="1" />
</el-select>
2021-03-12 17:49:41 +08:00
</el-form-item>
2021-03-08 11:35:28 +08:00
</el-form>
</el-row>
2021-03-12 17:49:41 +08:00
<el-row>
2021-03-12 18:18:51 +08:00
<el-col style="min-width: 200px;">
2021-03-12 17:49:41 +08:00
<codemirror
ref="myCm"
v-model="sql"
class="codemirror"
:options="sqlOption"
@ready="onCmReady"
@focus="onCmFocus"
@input="onCmCodeChange"
/>
</el-col>
</el-row>
<el-row style="margin-top: 10px;">
<el-card class="box-card dataPreview" shadow="never">
<div slot="header" class="clearfix">
<span>{{ $t('dataset.data_preview') }}</span>
<el-button style="float: right; padding: 3px 0" type="text" size="mini" @click="getSQLPreview">{{ $t('dataset.preview') }}</el-button>
</div>
<div class="text item">
<ux-grid
ref="plxTable"
size="mini"
style="width: 100%;"
:height="height"
:checkbox-config="{highlight: true}"
2021-03-16 11:55:22 +08:00
:width-resize="true"
>
<ux-table-column
v-for="field in fields"
:key="field.fieldName"
min-width="200px"
:field="field.fieldName"
:title="field.remarks"
:resizable="true"
/>
</ux-grid>
</div>
</el-card>
</el-row>
2021-03-08 11:35:28 +08:00
</el-row>
</el-col>
</template>
<script>
import { post, listDatasource } from '@/api/dataset/dataset'
2021-03-12 17:49:41 +08:00
import { codemirror } from 'vue-codemirror'
import { getTable } from '@/api/dataset/dataset'
2021-03-12 17:49:41 +08:00
// 核心样式
import 'codemirror/lib/codemirror.css'
// 引入主题后还需要在 options 中指定主题才会生效
2021-03-12 18:18:51 +08:00
import 'codemirror/theme/solarized.css'
2021-03-12 17:49:41 +08:00
import 'codemirror/mode/sql/sql.js'
// require active-line.js
import 'codemirror/addon/selection/active-line.js'
// closebrackets
import 'codemirror/addon/edit/closebrackets.js'
// keyMap
import 'codemirror/mode/clike/clike.js'
import 'codemirror/addon/edit/matchbrackets.js'
import 'codemirror/addon/comment/comment.js'
import 'codemirror/addon/dialog/dialog.js'
import 'codemirror/addon/dialog/dialog.css'
import 'codemirror/addon/search/searchcursor.js'
import 'codemirror/addon/search/search.js'
import 'codemirror/keymap/emacs.js'
// 引入代码自动提示插件
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/hint/sql-hint'
import 'codemirror/addon/hint/show-hint'
2021-03-08 11:35:28 +08:00
export default {
name: 'AddSQL',
2021-03-12 17:49:41 +08:00
components: { codemirror },
2021-03-08 11:35:28 +08:00
props: {
2021-03-12 17:49:41 +08:00
param: {
type: Object,
required: true
}
2021-03-08 11:35:28 +08:00
},
data() {
return {
dataSource: '',
2021-03-12 17:49:41 +08:00
options: [],
name: '',
sql: '',
sqlOption: {
tabSize: 2,
styleActiveLine: true,
lineNumbers: true,
line: true,
2021-03-12 18:18:51 +08:00
mode: 'text/x-sql',
theme: 'solarized',
hintOptions: { // 自定义提示选项
completeSingle: false // 当匹配只有一项的时候是否自动补全
2021-03-12 17:49:41 +08:00
}
},
data: [],
fields: [],
mode: '0',
height: 500
2021-03-12 17:49:41 +08:00
}
},
computed: {
codemirror() {
return this.$refs.myCm.codemirror
2021-03-08 11:35:28 +08:00
}
},
watch: {
'param.tableId': {
handler: function() {
this.initTableInfo()
}
}
},
2021-03-08 11:35:28 +08:00
mounted() {
window.onresize = () => {
return (() => {
this.height = window.innerHeight / 2
})()
}
this.height = window.innerHeight / 2
2021-03-08 11:35:28 +08:00
this.initDataSource()
this.$refs.myCm.codemirror.on('keypress', () => {
this.$refs.myCm.codemirror.showHint()
})
this.initTableInfo()
2021-03-08 11:35:28 +08:00
},
methods: {
initDataSource() {
listDatasource().then(response => {
this.options = response.data
})
},
2021-03-12 17:49:41 +08:00
initTableInfo() {
if (this.param.tableId) {
getTable(this.param.tableId).then(response => {
const table = response.data
this.name = table.name
this.dataSource = table.dataSourceId
this.mode = table.mode + ''
this.sql = JSON.parse(table.info.replace(/\n/g, '\\n').replace(/\r/g, '\\r')).sql
this.getSQLPreview()
})
}
},
getSQLPreview() {
if (!this.dataSource || this.datasource === '') {
this.$message({
showClose: true,
message: this.$t('dataset.pls_slc_data_source'),
type: 'error'
})
return
}
post('/dataset/table/sqlPreview', {
dataSourceId: this.dataSource,
type: 'sql',
info: '{"sql":"' + this.sql + '"}'
}).then(response => {
this.fields = response.data.fields
this.data = response.data.data
const datas = this.data
this.$refs.plxTable.reloadData(datas)
})
},
save() {
if (!this.dataSource || this.datasource === '') {
this.$message({
showClose: true,
message: this.$t('dataset.pls_slc_data_source'),
type: 'error'
})
return
}
if (!this.name || this.name === '') {
this.$message({
showClose: true,
message: this.$t('dataset.pls_input_name'),
type: 'error'
})
return
}
const table = {
id: this.param.tableId,
name: this.name,
sceneId: this.param.id,
dataSourceId: this.dataSource,
type: 'sql',
mode: parseInt(this.mode),
info: '{"sql":"' + this.sql + '"}'
}
post('/dataset/table/update', table).then(response => {
this.$store.dispatch('dataset/setSceneData', new Date().getTime())
this.cancel()
})
},
cancel() {
// this.dataReset()
this.$emit('switchComponent', { name: '' })
},
2021-03-12 17:49:41 +08:00
showSQL(val) {
this.sql = val || ''
},
onCmReady(cm) {
this.codemirror.setSize('-webkit-fill-available', 'auto')
},
onCmFocus(cm) {
// console.log('the editor is focus!', cm)
},
onCmCodeChange(newCode) {
// console.log(newCode)
2021-03-12 17:49:41 +08:00
this.sql = newCode
this.$emit('codeChange', this.sql)
2021-03-08 11:35:28 +08:00
}
}
}
</script>
2021-03-26 12:14:18 +08:00
<style scoped>
2021-03-08 11:35:28 +08:00
.el-divider--horizontal {
margin: 12px 0;
}
.form-item {
margin-bottom: 6px;
}
.el-checkbox {
margin-bottom: 14px;
margin-left: 0;
margin-right: 14px;
}
.el-checkbox.is-bordered + .el-checkbox.is-bordered {
margin-left: 0;
}
2021-03-12 17:49:41 +08:00
.codemirror {
height: 160px;
overflow-y: auto;
2021-03-12 17:49:41 +08:00
}
.codemirror >>> .CodeMirror-scroll {
height: 160px;
overflow-y: auto;
}
.dataPreview>>>.el-card__header{
padding: 6px 8px;
}
.dataPreview>>>.el-card__body{
padding:10px;
}
span{
font-size: 14px;
2021-03-12 17:49:41 +08:00
}
2021-03-08 11:35:28 +08:00
</style>