2020-05-04 12:18:57 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="data-executor-vue">
|
2020-05-11 22:37:30 +08:00
|
|
|
|
<div style="padding: 0 10px 10px;height: 100%;box-sizing: border-box;">
|
|
|
|
|
|
<el-card style="margin-bottom: 10px;">
|
2021-05-20 00:04:07 +08:00
|
|
|
|
<pre id="sqlExecutorEditor" style="width: 100%;height: 500px;margin-top: 0;margin-bottom: 10px;"></pre>
|
2020-05-11 22:37:30 +08:00
|
|
|
|
<div>
|
|
|
|
|
|
<el-button v-if="sqlExecuting" v-on:click="cancelExecutorSql" type="primary" plain size="small" icon="el-icon-video-pause">取消执行</el-button>
|
|
|
|
|
|
<el-tooltip v-else effect="dark" content="Ctrl+R、Ctrl+Enter" placement="top">
|
2021-05-20 00:04:07 +08:00
|
|
|
|
<el-button v-on:click="doExecutorClick" type="primary" plain size="small" icon="el-icon-video-play">筛选</el-button>
|
2020-05-11 22:37:30 +08:00
|
|
|
|
</el-tooltip>
|
2020-05-12 22:29:29 +08:00
|
|
|
|
<el-button icon="el-icon-refresh-left" size="small" @click="refreshData">重置</el-button>
|
2020-05-11 22:37:30 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</el-card>
|
2020-05-04 12:18:57 +08:00
|
|
|
|
<el-card>
|
|
|
|
|
|
<div v-if="!!executeError" style="color: #f00;">{{executeError}}</div>
|
2020-05-08 22:32:54 +08:00
|
|
|
|
<div v-else-if="sqlExecuting" v-loading="sqlExecuting" style="padding: 20px 0;">数据加载中...</div>
|
|
|
|
|
|
<div v-else-if="executeResultList.length <= 0" v-loading="sqlExecuting" style="padding: 20px 0;">暂无数据</div>
|
2020-05-04 12:18:57 +08:00
|
|
|
|
<div v-else>
|
|
|
|
|
|
<el-tabs :value="executeShowTable">
|
|
|
|
|
|
<el-tab-pane label="信息" name="table0">
|
|
|
|
|
|
<pre>{{executeResultInfo}}</pre>
|
|
|
|
|
|
</el-tab-pane>
|
|
|
|
|
|
<el-tab-pane :label="'结果'+resultItem.index" :name="'table'+resultItem.index" v-for="resultItem in executeResultList" v-if="!!resultItem.index">
|
|
|
|
|
|
<div v-if="!!resultItem.errMsg" style="color: #f00;">{{resultItem.errMsg}}</div>
|
2020-05-04 20:15:18 +08:00
|
|
|
|
<el-table v-else :data="resultItem.dataList" stripe border
|
|
|
|
|
|
style="width: 100%; margin-bottom: 5px;"
|
2020-05-11 22:37:30 +08:00
|
|
|
|
class="execute-result-table" :max-height="tableMaxHeight"
|
2020-05-04 20:15:18 +08:00
|
|
|
|
@sort-change="tableSortChange"
|
|
|
|
|
|
:default-sort="tableSort">
|
2020-05-04 12:18:57 +08:00
|
|
|
|
<el-table-column width="60px" v-if="resultItem.dataCols.length > 0">
|
|
|
|
|
|
<template slot-scope="scope">{{scope.row._index}}</template>
|
|
|
|
|
|
</el-table-column>
|
2020-05-04 20:15:18 +08:00
|
|
|
|
<el-table-column sortable v-for="item in resultItem.dataCols" :prop="item.prop" :label="item.prop" :width="item.width">
|
2020-05-04 12:18:57 +08:00
|
|
|
|
<template slot-scope="scope">
|
2020-05-08 22:32:54 +08:00
|
|
|
|
<textarea readonly :value="scope.row[item.prop]" class="el-textarea__inner" rows="1"></textarea>
|
2020-05-04 12:18:57 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
</el-table-column>
|
|
|
|
|
|
</el-table>
|
|
|
|
|
|
<el-pagination
|
2020-05-08 22:32:54 +08:00
|
|
|
|
style="margin-top: 10px;"
|
2020-05-04 12:18:57 +08:00
|
|
|
|
@size-change="handlePageSizeChange"
|
|
|
|
|
|
@current-change="handleCurrentChange"
|
|
|
|
|
|
:current-page="currentPage"
|
|
|
|
|
|
:page-sizes="[50, 100, 300, 500]"
|
|
|
|
|
|
:page-size="pageSize"
|
|
|
|
|
|
layout="total, sizes, prev, pager, next, jumper"
|
|
|
|
|
|
:total="tableTotalCount">
|
|
|
|
|
|
</el-pagination>
|
|
|
|
|
|
</el-tab-pane>
|
|
|
|
|
|
</el-tabs>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</el-card>
|
|
|
|
|
|
</div>
|
2020-05-12 22:29:29 +08:00
|
|
|
|
<span id="widthCalculate" style="visibility: hidden; white-space: nowrap;position: fixed;"></span>
|
2020-05-04 12:18:57 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import '../../common/lib/ace/ace'
|
|
|
|
|
|
import '../../common/lib/ace/theme-monokai'
|
|
|
|
|
|
import '../../common/lib/ace/mode-sql'
|
|
|
|
|
|
import '../../common/lib/ace/ext-language_tools'
|
|
|
|
|
|
import '../../common/lib/ace/snippets/sql'
|
2020-05-23 12:21:23 +08:00
|
|
|
|
import datasourceApi from '../../common/api/datasource'
|
2020-05-12 22:29:29 +08:00
|
|
|
|
import sqlFormatter from "sql-formatter";
|
|
|
|
|
|
|
2020-05-04 12:18:57 +08:00
|
|
|
|
export default {
|
|
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
sqlExecuting: false,
|
|
|
|
|
|
executeResultList: [],
|
|
|
|
|
|
executeResultInfo: "",
|
|
|
|
|
|
executeShowTable: "table1",
|
|
|
|
|
|
sqlExecutorEditor: {},
|
|
|
|
|
|
nowExecutorId: 1,
|
|
|
|
|
|
executeError: "",
|
|
|
|
|
|
vueQueryParam: {},
|
|
|
|
|
|
pageSize: 50,
|
|
|
|
|
|
currentPage: 1,
|
|
|
|
|
|
tableTotalCount: 0,
|
2020-05-04 20:15:18 +08:00
|
|
|
|
tableSort: {},
|
|
|
|
|
|
tableMaxHeight: 600,
|
2020-05-04 12:18:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2020-05-11 22:37:30 +08:00
|
|
|
|
mounted: function () {
|
|
|
|
|
|
let that = this;
|
2021-05-20 00:04:07 +08:00
|
|
|
|
this.sqlExecutorEditor = this.initAceEditor("sqlExecutorEditor", 5);
|
2020-05-16 08:54:11 +08:00
|
|
|
|
this.sqlExecutorEditor.setFontSize(16);
|
2020-05-11 22:37:30 +08:00
|
|
|
|
this.sqlExecutorEditor.commands.addCommand({
|
|
|
|
|
|
name: "execute-sql",
|
|
|
|
|
|
bindKey: {win: "Ctrl-R|Ctrl-Shift-R|Ctrl-Enter", mac: "Command-R|Command-Shift-R|Command-Enter"},
|
|
|
|
|
|
exec: function (editor) {
|
|
|
|
|
|
that.doExecutorClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2020-05-23 12:21:23 +08:00
|
|
|
|
// 延迟设置展开的目录,edit比app先初始化
|
|
|
|
|
|
setTimeout(() => {
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-23 12:21:23 +08:00
|
|
|
|
this.$emit('initLoadDataList', {
|
|
|
|
|
|
sourceId: this.vueQueryParam.sourceId,
|
|
|
|
|
|
host: this.vueQueryParam.host,
|
|
|
|
|
|
dbName: this.vueQueryParam.dbName
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 500);
|
2020-05-04 12:18:57 +08:00
|
|
|
|
},
|
2020-05-11 22:37:30 +08:00
|
|
|
|
activated: function () {
|
|
|
|
|
|
this.initQueryParam(this.$route);
|
|
|
|
|
|
},
|
2020-05-04 12:18:57 +08:00
|
|
|
|
methods: {
|
|
|
|
|
|
initQueryParam(to) {
|
|
|
|
|
|
this.vueQueryParam = to.query;
|
2020-05-04 20:15:18 +08:00
|
|
|
|
let newName = {key: this.$route.fullPath, val: '数据-'+this.vueQueryParam.tableName};
|
2020-05-04 12:18:57 +08:00
|
|
|
|
this.$store.commit('global/addTableName', newName);
|
|
|
|
|
|
},
|
|
|
|
|
|
handleCurrentChange(to) {
|
|
|
|
|
|
this.currentPage = to;
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-04 12:18:57 +08:00
|
|
|
|
},
|
|
|
|
|
|
handlePageSizeChange(to) {
|
|
|
|
|
|
this.pageSize = to;
|
2021-06-01 22:34:21 +08:00
|
|
|
|
this.currentPage = 1;
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-04 12:18:57 +08:00
|
|
|
|
},
|
2020-05-04 20:15:18 +08:00
|
|
|
|
tableSortChange(sort) {
|
|
|
|
|
|
if (this.tableSort.prop === sort.prop && this.tableSort.order === sort.order) return;
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.tableSort = {orderColumn: sort.prop, orderType: (sort.order === 'ascending' ? 'asc' : 'desc')};
|
|
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-04 20:15:18 +08:00
|
|
|
|
},
|
|
|
|
|
|
refreshData() {
|
|
|
|
|
|
this.tableSort = {};
|
|
|
|
|
|
this.currentPage = 1;
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.sqlExecutorEditor.setValue('', 1);
|
|
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-04 20:15:18 +08:00
|
|
|
|
},
|
2020-05-04 12:18:57 +08:00
|
|
|
|
cancelExecutorSql() {
|
2020-05-23 12:21:23 +08:00
|
|
|
|
datasourceApi.executeSqlCancel({executeId: this.nowExecutorId}).then(() => {
|
|
|
|
|
|
this.$message.success("取消成功");
|
2020-05-04 12:18:57 +08:00
|
|
|
|
});
|
|
|
|
|
|
},
|
2020-05-11 22:37:30 +08:00
|
|
|
|
doExecutorClick() {
|
2021-05-24 22:42:01 +08:00
|
|
|
|
this.tableSort = {};
|
|
|
|
|
|
this.currentPage = 1;
|
2021-05-20 00:04:07 +08:00
|
|
|
|
this.doExecutorSqlCommon();
|
2020-05-11 22:37:30 +08:00
|
|
|
|
},
|
2021-05-24 22:42:01 +08:00
|
|
|
|
doExecutorSqlCommon() {
|
2020-05-04 12:18:57 +08:00
|
|
|
|
if (!this.vueQueryParam.sourceId) {
|
|
|
|
|
|
this.$message.error("请先选择数据源");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-05-24 22:42:01 +08:00
|
|
|
|
if (!this.tableSort.orderColumn) {
|
|
|
|
|
|
this.tableSort = {orderColumn: this.vueQueryParam.orderColumn, orderType: 'asc'};
|
|
|
|
|
|
}
|
|
|
|
|
|
let conditionSql = this.sqlExecutorEditor.getSelectedText();
|
|
|
|
|
|
conditionSql = conditionSql || this.sqlExecutorEditor.getValue();
|
|
|
|
|
|
conditionSql = conditionSql || "";
|
2020-05-04 12:18:57 +08:00
|
|
|
|
this.executeError = "";
|
|
|
|
|
|
this.executeUseTime = "";
|
|
|
|
|
|
this.executeResultList = [];
|
2021-05-20 00:04:07 +08:00
|
|
|
|
this.tableMaxHeight = document.body.clientHeight - 400;
|
2020-05-04 12:18:57 +08:00
|
|
|
|
this.nowExecutorId = (new Date()).getTime() + Math.ceil(Math.random() * 1000);
|
|
|
|
|
|
this.sqlExecuting = true;
|
|
|
|
|
|
let param = {
|
|
|
|
|
|
sourceId: this.vueQueryParam.sourceId,
|
2021-05-20 00:04:07 +08:00
|
|
|
|
dbName: this.vueQueryParam.dbName,
|
|
|
|
|
|
tableName: this.vueQueryParam.tableName,
|
2020-05-04 12:18:57 +08:00
|
|
|
|
executeId: this.nowExecutorId,
|
2021-05-20 00:04:07 +08:00
|
|
|
|
condition: conditionSql,
|
|
|
|
|
|
pageNum: this.currentPage,
|
|
|
|
|
|
pageSize: this.pageSize,
|
2021-05-24 22:42:01 +08:00
|
|
|
|
orderColumn: this.tableSort.orderColumn,
|
|
|
|
|
|
orderType: this.tableSort.orderType,
|
2020-05-04 12:18:57 +08:00
|
|
|
|
params: '',
|
|
|
|
|
|
};
|
2021-05-20 00:04:07 +08:00
|
|
|
|
datasourceApi.dataViewQuery(param).then(json => {
|
|
|
|
|
|
if (json.errCode !== 200) {
|
2020-05-23 12:21:23 +08:00
|
|
|
|
this.executeError = json.errMsg;
|
|
|
|
|
|
this.sqlExecuting = false;
|
2020-05-04 12:18:57 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-05-23 12:21:23 +08:00
|
|
|
|
let resultList = json.data || [];
|
|
|
|
|
|
let executeResultList = [];
|
|
|
|
|
|
let executeResultInfo = "", itemIndex = 1;
|
|
|
|
|
|
for (let i = 0; i < resultList.length; i++) {
|
|
|
|
|
|
let objItem = JSON.parse(resultList[i]);
|
|
|
|
|
|
executeResultInfo += this.getExecuteInfoStr(objItem);
|
|
|
|
|
|
let resultItem = this.dealExecuteResult(objItem);
|
2020-05-04 12:18:57 +08:00
|
|
|
|
if (resultItem.updateCount < 0) {
|
|
|
|
|
|
resultItem.index = itemIndex;
|
|
|
|
|
|
itemIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
executeResultList.push(resultItem);
|
2021-05-20 00:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (!!json.total) {
|
|
|
|
|
|
this.tableTotalCount = json.total || 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.executeShowTable = (itemIndex === 1) ? "table0" : "table1";
|
|
|
|
|
|
this.executeResultInfo = executeResultInfo;
|
|
|
|
|
|
this.executeResultList = executeResultList;
|
2020-05-23 12:21:23 +08:00
|
|
|
|
this.sqlExecuting = false;
|
2020-05-04 12:18:57 +08:00
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
getExecuteInfoStr(resultData) {
|
|
|
|
|
|
var resultStr = resultData.sql;
|
|
|
|
|
|
resultStr += "\n> 状态:" + ((!!resultData.errMsg) ? "ERROR" : "OK");
|
|
|
|
|
|
if (resultData.updateCount >= 0) {
|
|
|
|
|
|
resultStr += "\n> 影响行数:" + resultData.updateCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
resultStr += "\n> 耗时:" + (resultData.useTime || 0) / 1000 + "s";
|
|
|
|
|
|
resultStr += "\n\n";
|
|
|
|
|
|
return resultStr;
|
|
|
|
|
|
},
|
|
|
|
|
|
dealExecuteResult(resultData) {
|
|
|
|
|
|
var dataList = resultData.result || [];
|
|
|
|
|
|
var executeResultCols = [];
|
|
|
|
|
|
if (dataList.length > 0) {
|
|
|
|
|
|
var propData = dataList[0];
|
|
|
|
|
|
for (var key in propData) {
|
|
|
|
|
|
// 动态计算宽度~自己想的一个方法,666
|
|
|
|
|
|
document.getElementById("widthCalculate").innerText = key;
|
|
|
|
|
|
var width1 = document.getElementById("widthCalculate").offsetWidth;
|
|
|
|
|
|
document.getElementById("widthCalculate").innerText = propData[key];
|
|
|
|
|
|
var width2 = document.getElementById("widthCalculate").offsetWidth;
|
|
|
|
|
|
var width = (width1 > width2) ? width1 : width2;
|
|
|
|
|
|
width = (width < 50) ? 50 : width;
|
|
|
|
|
|
width = (width > 200) ? 200 : width;
|
2020-05-04 20:15:18 +08:00
|
|
|
|
executeResultCols.push({prop: key, width: width + 50});
|
2020-05-04 12:18:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
for (var i = 0; i < dataList.length; i++) {
|
|
|
|
|
|
dataList[i]._index = i + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
var resultObj = {};
|
|
|
|
|
|
resultObj.dataList = dataList;
|
|
|
|
|
|
resultObj.dataCols = executeResultCols;
|
|
|
|
|
|
resultObj.useTime = resultData.useTime || 0;
|
|
|
|
|
|
resultObj.errMsg = resultData.errMsg || "";
|
|
|
|
|
|
resultObj.updateCount = resultData.updateCount;
|
|
|
|
|
|
return resultObj;
|
|
|
|
|
|
},
|
2020-05-11 22:37:30 +08:00
|
|
|
|
initAceEditor(editor, minLines) {
|
|
|
|
|
|
return ace.edit(editor, {
|
|
|
|
|
|
theme: "ace/theme/monokai",
|
|
|
|
|
|
mode: "ace/mode/sql",
|
|
|
|
|
|
wrap: true,
|
|
|
|
|
|
autoScrollEditorIntoView: true,
|
|
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
|
|
enableSnippets: true,
|
|
|
|
|
|
enableLiveAutocompletion: true,
|
|
|
|
|
|
minLines: minLines,
|
2021-05-20 00:04:07 +08:00
|
|
|
|
maxLines: 5
|
2020-05-11 22:37:30 +08:00
|
|
|
|
});
|
|
|
|
|
|
},
|
2020-05-04 12:18:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
.data-executor-vue .ace-monokai .ace_print-margin{
|
|
|
|
|
|
display: none;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue .el-card__body{
|
|
|
|
|
|
padding: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue .el-table td, .el-table th{
|
|
|
|
|
|
padding: 6px 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue .execute-result-table .el-input__inner{
|
|
|
|
|
|
height: 25px;
|
|
|
|
|
|
line-height: 25px;
|
|
|
|
|
|
padding: 0 5px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue .execute-result-table .el-textarea__inner{
|
2020-05-08 22:32:54 +08:00
|
|
|
|
height: 27px;
|
|
|
|
|
|
min-height: 27px;
|
2020-05-04 12:18:57 +08:00
|
|
|
|
line-height: 25px;
|
|
|
|
|
|
padding: 0 5px;
|
2020-05-08 22:32:54 +08:00
|
|
|
|
resize: none;
|
2020-05-04 12:18:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue .execute-use-time{
|
|
|
|
|
|
font-size: 12px;margin-right: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue-out .el-tabs__nav-scroll{
|
|
|
|
|
|
padding-left: 20px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue-out .el-button+.el-button{
|
|
|
|
|
|
margin-left: 0px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.data-executor-vue-out .el-table__body-wrapper{
|
|
|
|
|
|
height: calc(100vh - 180px);
|
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|