2021-06-07 17:22:07 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-row type="flex">
|
2023-02-06 14:50:57 +08:00
|
|
|
|
<el-col :span="24">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-button type="primary" icon="plus" @click="addQueryTab" size="small">新建查询</el-button>
|
2023-02-06 14:50:57 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col :span="4" style="border-left: 1px solid #eee; margin-top: 10px" >
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<InstanceTree
|
|
|
|
|
|
:instance-menu-max-height="state.instanceMenuMaxHeight"
|
|
|
|
|
|
:instances="state.instances"
|
|
|
|
|
|
@init-load-instances="loadInstances"
|
|
|
|
|
|
@change-instance="changeInstance"
|
|
|
|
|
|
@change-schema="changeSchema"
|
|
|
|
|
|
@load-table-names="loadSchemaTables"
|
|
|
|
|
|
@load-table-data="loadTableData"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-col>
|
2023-02-06 14:50:57 +08:00
|
|
|
|
<el-col :span="20">
|
|
|
|
|
|
<el-container id="data-exec" style="border-left: 1px solid #eee; margin-top: 10px">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-tabs @tab-remove="removeDataTab" @tab-click="onDataTabClick" style="width: 100%" v-model="state.activeName">
|
|
|
|
|
|
<el-tab-pane closable v-for="q in state.queryTabs" :key="q.id" :label="q.label" :name="q.name" >
|
2022-01-19 15:10:17 +08:00
|
|
|
|
<div>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div class="toolbar">
|
|
|
|
|
|
<div class="fl">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onRunSql(q.dbId, q.db)" :underline="false" class="ml15" icon="VideoPlay">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
|
|
|
|
|
<el-tooltip class="box-item" effect="dark" content="format sql" placement="top">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="formatSql(q.dbId, q.db)" type="primary" :underline="false" icon="MagicStick">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-tooltip>
|
|
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
|
|
|
|
|
<el-tooltip class="box-item" effect="dark" content="commit" placement="top">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onCommit(q.dbId, q.db)" type="success" :underline="false" icon="CircleCheck">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-tooltip>
|
|
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
2022-11-22 00:34:42 +08:00
|
|
|
|
<el-upload class="sql-file-exec" :before-upload="beforeUpload"
|
2023-01-31 10:37:40 +08:00
|
|
|
|
:on-success="execSqlFileSuccess" :headers="{ Authorization: token }" :data="{ dbId: q.dbId }" :action="getUploadSqlFileUrl(q.dbId, q.db)" :show-file-list="false" name="file" multiple
|
2022-10-29 20:08:15 +08:00
|
|
|
|
:limit="100">
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-tooltip class="box-item" effect="dark" content="SQL脚本执行" placement="top">
|
|
|
|
|
|
<el-link type="success" :underline="false" icon="Document"></el-link>
|
|
|
|
|
|
</el-tooltip>
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</el-upload>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div style="float: right" class="fl">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-select v-model="state.sqlName[q.dbId+q.db]" placeholder="选择or输入SQL模板名" @change="changeSqlTemplate(q.dbId, q.db)"
|
2022-10-29 20:08:15 +08:00
|
|
|
|
filterable allow-create default-first-option size="small" class="mr10">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-option v-for="item in state.sqlNames[q.dbId+q.db]" :key="item" :label="item.database"
|
2022-10-29 20:08:15 +08:00
|
|
|
|
:value="item">
|
2022-01-19 15:10:17 +08:00
|
|
|
|
{{ item }}
|
|
|
|
|
|
</el-option>
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-button @click="saveSql(q.dbId, q.db)" type="primary" icon="document-add" plain size="small">保存
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-button>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-button @click="deleteSql(q.dbId, q.db)" type="danger" icon="delete" plain size="small">删除
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-button>
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<div class="mt5 sqlEditor">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<div :id="'MonacoTextarea-'+ q.id" :style="{ height: state.monacoOptions.height }"></div>
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</div>
|
2022-03-15 22:27:39 +08:00
|
|
|
|
<div class="mt5">
|
2022-09-23 14:27:50 +08:00
|
|
|
|
<el-row>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link v-if="q.nowTableName" @click="onDeleteData(q.dbId, q.db, q.nowTableName)" class="ml5" type="danger"
|
2022-10-29 20:08:15 +08:00
|
|
|
|
icon="delete" :underline="false"></el-link>
|
2022-09-23 14:27:50 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<span v-if="q.execRes.data.length > 0">
|
2022-09-23 14:27:50 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-link type="success" :underline="false" @click="exportData"><span
|
|
|
|
|
|
style="font-size: 12px">导出</span></el-link>
|
2022-09-23 14:27:50 +08:00
|
|
|
|
</span>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<span v-if="q.updatedFields.length > 0">
|
2022-11-22 00:34:42 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link type="success" :underline="false" @click="submitUpdateFields(q.dbId, q.db, q.nowTableName)"><span
|
2022-11-22 00:34:42 +08:00
|
|
|
|
style="font-size: 12px">提交</span></el-link>
|
|
|
|
|
|
</span>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<span v-if="q.updatedFields.length > 0">
|
2022-11-22 15:54:11 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
<el-link type="warning" :underline="false" @click="cancelUpdateFields"><span
|
|
|
|
|
|
style="font-size: 12px">取消</span></el-link>
|
|
|
|
|
|
</span>
|
2022-01-20 18:01:11 +08:00
|
|
|
|
</el-row>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-table @cell-dblclick="(row, column, cell, event) => cellClick(row, column, cell, event, {dbId: q.dbId, db: q.db, tableName: q.nowTableName})"
|
|
|
|
|
|
@selection-change="onDataSelectionChange" size="small"
|
|
|
|
|
|
:data="q.execRes.data" v-loading="q.loading" element-loading-text="查询中..."
|
|
|
|
|
|
empty-text="tips: select *开头的单表查询或点击表名默认查询的数据,可双击数据在线修改" stripe border class="mt5">
|
|
|
|
|
|
<el-table-column v-if="q.execRes.tableColumn.length > 0 && q.nowTableName"
|
2022-10-29 20:08:15 +08:00
|
|
|
|
type="selection" width="35" />
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-table-column min-width="100" :width="flexColumnWidth(item, q.execRes.data)"
|
|
|
|
|
|
align="center" v-for="item in q.execRes.tableColumn" :key="item" :prop="item"
|
2022-10-29 20:08:15 +08:00
|
|
|
|
:label="item" show-overflow-tooltip>
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</el-table-column>
|
|
|
|
|
|
</el-table>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-row type="flex" class="mt5" justify="center">
|
|
|
|
|
|
<el-pagination
|
|
|
|
|
|
v-show="q.execRes.showPage" small
|
|
|
|
|
|
:total="q.execRes.total"
|
|
|
|
|
|
@current-change="doRunSql(q.dbId,q.db,q.sql)"
|
|
|
|
|
|
layout="prev,pager,next,total,jumper"
|
|
|
|
|
|
v-model:current-page="q.execRes.pageNum"
|
|
|
|
|
|
:page-size="defalutLimit">
|
|
|
|
|
|
</el-pagination>
|
|
|
|
|
|
</el-row>
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</div>
|
2022-11-05 15:13:40 +08:00
|
|
|
|
|
2022-01-19 15:10:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</el-tab-pane>
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-tab-pane closable v-for="dt in state.dataTabs" :key="dt.key" :label="dt.label" :name="dt.key">
|
|
|
|
|
|
<el-row>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-col :span="8">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onRefresh(dt.dbId, dt.db,dt.name)" icon="refresh" :underline="false" class="ml5">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="addRow(dt.dbId, dt.db, dt.name)" type="primary" icon="plus" :underline="false"></el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onDeleteData(dt.dbId, dt.db, dt.name)" type="danger" icon="delete" :underline="false"></el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
|
|
|
|
|
<el-tooltip class="box-item" effect="dark" content="commit" placement="top">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onCommit(dt.dbId, dt.db)" type="success" icon="CircleCheck" :underline="false">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-tooltip>
|
|
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
|
|
|
|
|
<el-tooltip class="box-item" effect="dark" content="生成insert sql" placement="top">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="onGenerateInsertSql(dt.dbId, dt.db, dt.name)" type="success" :underline="false">gi</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-tooltip>
|
2022-11-22 00:34:42 +08:00
|
|
|
|
<el-divider direction="vertical" border-style="dashed" />
|
|
|
|
|
|
|
2022-11-22 17:15:08 +08:00
|
|
|
|
<el-tooltip v-if="state.updatedFields[state.nowTableName]?.length > 0" class="box-item"
|
|
|
|
|
|
effect="dark" content="提交修改" placement="top">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-link @click="submitUpdateFields(dt.dbId, dt.db, dt.name)" type="success" :underline="false">提交</el-link>
|
2022-11-22 00:34:42 +08:00
|
|
|
|
</el-tooltip>
|
2022-11-22 17:15:08 +08:00
|
|
|
|
<el-divider v-if="state.updatedFields[state.nowTableName]?.length > 0" direction="vertical"
|
|
|
|
|
|
border-style="dashed" />
|
|
|
|
|
|
<el-tooltip v-if="state.updatedFields[state.nowTableName]?.length > 0" class="box-item"
|
|
|
|
|
|
effect="dark" content="取消修改" placement="top">
|
|
|
|
|
|
<el-link @click="cancelUpdateFields" type="warning" :underline="false">取消</el-link>
|
2022-11-22 15:54:11 +08:00
|
|
|
|
</el-tooltip>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col :span="16">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-input v-model="dt.condition" placeholder="若需条件过滤,可选择列并点击对应的字段并输入需要过滤的内容点击查询按钮即可"
|
|
|
|
|
|
clearable size="small" style="width: 100%">
|
2022-08-26 20:15:36 +08:00
|
|
|
|
<template #prepend>
|
|
|
|
|
|
<el-popover trigger="click" :width="320" placement="right">
|
|
|
|
|
|
<template #reference>
|
2022-09-23 14:27:50 +08:00
|
|
|
|
<el-link type="success" :underline="false">选择列</el-link>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</template>
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-table :data="getColumns4Map(dt.name)" max-height="500" size="small"
|
2022-08-26 20:15:36 +08:00
|
|
|
|
@row-click="
|
2022-10-29 20:08:15 +08:00
|
|
|
|
(...event: any) => {
|
2022-08-26 20:15:36 +08:00
|
|
|
|
onConditionRowClick(event, dt);
|
|
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
" style="cursor: pointer">
|
|
|
|
|
|
<el-table-column property="columnName" label="列名" show-overflow-tooltip>
|
|
|
|
|
|
</el-table-column>
|
|
|
|
|
|
<el-table-column property="columnComment" label="备注" show-overflow-tooltip>
|
|
|
|
|
|
</el-table-column>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</el-table>
|
|
|
|
|
|
</el-popover>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<template #append>
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-button @click="selectByCondition(dt.name, dt.condition)" icon="search"
|
|
|
|
|
|
size="small"></el-button>
|
2022-08-26 20:15:36 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
</el-input>
|
|
|
|
|
|
</el-col>
|
2022-02-07 17:49:48 +08:00
|
|
|
|
</el-row>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-table @cell-dblclick="(row, column, cell, event) => cellClick(row, column, cell, event, {dbId: dt.dbId, db: dt.db, tableName: dt.name})"
|
|
|
|
|
|
@sort-change="sort => onTableSortChange(dt.dbId, dt.db, sort, dt.name)"
|
|
|
|
|
|
@selection-change="onDataSelectionChange" :data="dt.datas" size="small"
|
|
|
|
|
|
:max-height="state.dataTabsTableHeight" v-loading="dt.loading" element-loading-text="查询中..."
|
|
|
|
|
|
empty-text="暂无数据" stripe border class="mt5">
|
2022-07-02 18:59:46 +08:00
|
|
|
|
<el-table-column v-if="dt.datas.length > 0" type="selection" width="35" />
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-table-column min-width="100" :width="flexColumnWidth(item, dt.datas)" align="center"
|
|
|
|
|
|
v-for="item in dt.columnNames" :key="item" :prop="item" :label="item" show-overflow-tooltip
|
2023-01-31 10:37:40 +08:00
|
|
|
|
:sortable="state.nowTableName !== '' ? 'custom' : false">
|
2022-03-03 11:29:33 +08:00
|
|
|
|
<template #header>
|
2022-05-27 15:45:12 +08:00
|
|
|
|
<el-tooltip raw-content placement="top" effect="customized">
|
2022-03-03 11:29:33 +08:00
|
|
|
|
<template #content> {{ getColumnTip(dt.name, item) }} </template>
|
2022-05-27 15:45:12 +08:00
|
|
|
|
{{ item }}
|
2022-03-03 11:29:33 +08:00
|
|
|
|
</el-tooltip>
|
|
|
|
|
|
</template>
|
2022-01-16 19:40:20 +08:00
|
|
|
|
</el-table-column>
|
|
|
|
|
|
</el-table>
|
2022-07-02 18:59:46 +08:00
|
|
|
|
<el-row type="flex" class="mt5" justify="center">
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<el-pagination small :total="dt.count" @current-change="handlePageChange(dt)"
|
|
|
|
|
|
layout="prev, pager, next, total, jumper" v-model:current-page="dt.pageNum"
|
|
|
|
|
|
:page-size="defalutLimit"></el-pagination>
|
2022-07-02 18:59:46 +08:00
|
|
|
|
</el-row>
|
2023-02-06 14:50:57 +08:00
|
|
|
|
<div style=" font-size: 12px; padding: 0 10px; color: #606266"><span>{{dt.sql}}</span></div>
|
2022-01-16 19:40:20 +08:00
|
|
|
|
</el-tab-pane>
|
|
|
|
|
|
</el-tabs>
|
2021-06-07 17:22:07 +08:00
|
|
|
|
</el-container>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
|
</el-row>
|
|
|
|
|
|
|
|
|
|
|
|
<el-dialog v-model="state.conditionDialog.visible" :title="state.conditionDialog.title" width="420px">
|
2022-07-26 18:32:45 +08:00
|
|
|
|
<el-row>
|
|
|
|
|
|
<el-col :span="5">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-select v-model="state.conditionDialog.condition">
|
2022-07-26 18:32:45 +08:00
|
|
|
|
<el-option label="=" value="="> </el-option>
|
|
|
|
|
|
<el-option label="LIKE" value="LIKE"> </el-option>
|
|
|
|
|
|
<el-option label=">" value=">"> </el-option>
|
|
|
|
|
|
<el-option label=">=" value=">="> </el-option>
|
|
|
|
|
|
<el-option label="<" value="<"> </el-option>
|
|
|
|
|
|
<el-option label="<=" value="<="> </el-option>
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col :span="19">
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-input v-model="state.conditionDialog.value" :placeholder="state.conditionDialog.placeholder" />
|
2022-07-26 18:32:45 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
|
</el-row>
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
|
<span class="dialog-footer">
|
|
|
|
|
|
<el-button @click="onCancelCondition">取消</el-button>
|
|
|
|
|
|
<el-button type="primary" @click="onConfirmCondition">确定</el-button>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-dialog>
|
2022-07-27 15:36:56 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
<el-dialog @close="state.genSqlDialog.visible = false" v-model="state.genSqlDialog.visible" title="SQL" width="1000px">
|
|
|
|
|
|
<el-input v-model="state.genSqlDialog.sql" type="textarea" rows="20" />
|
2022-07-27 15:36:56 +08:00
|
|
|
|
</el-dialog>
|
2021-06-07 17:22:07 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
<script lang="ts" setup>
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {computed, nextTick, onMounted, reactive, watch} from 'vue';
|
|
|
|
|
|
import {dbApi} from './api';
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {format as sqlFormatter} from 'sql-formatter';
|
|
|
|
|
|
import {isTrue, notBlank, notEmpty} from '@/common/assert';
|
|
|
|
|
|
import {ElMessage, ElMessageBox} from 'element-plus';
|
2021-11-25 14:34:15 +08:00
|
|
|
|
import config from '@/common/config';
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {getSession} from '@/common/utils/storage';
|
2022-02-14 14:58:25 +08:00
|
|
|
|
import SqlExecBox from './component/SqlExecBox';
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {dateStrFormat} from '@/common/utils/date.ts';
|
|
|
|
|
|
import {useStore} from '@/store/index.ts';
|
2022-11-01 22:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js?worker';
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {language as sqlLanguage} from 'monaco-editor/esm/vs/basic-languages/mysql/mysql.js';
|
2022-11-01 22:18:54 +08:00
|
|
|
|
import * as monaco from 'monaco-editor';
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import {editor, languages, Position} from 'monaco-editor';
|
2022-11-01 22:18:54 +08:00
|
|
|
|
|
2022-11-19 15:05:44 +08:00
|
|
|
|
// 主题仓库 https://github.com/brijeshb42/monaco-themes
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 主题例子 https://editor.bitwiser.in/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
import SolarizedLight from 'monaco-themes/themes/Solarized-light.json';
|
|
|
|
|
|
import InstanceTree from '@/views/ops/db/component/InstanceTree.vue';
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const store = useStore();
|
|
|
|
|
|
const token = getSession('token');
|
|
|
|
|
|
const tableMap = new Map();
|
|
|
|
|
|
const defalutLimit = 20
|
|
|
|
|
|
|
2022-11-05 15:13:40 +08:00
|
|
|
|
type TableMeta = {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 表名
|
2022-11-05 15:13:40 +08:00
|
|
|
|
tableName: string,
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 表注释
|
2022-11-05 15:13:40 +08:00
|
|
|
|
tableComment: string
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
2022-11-22 00:34:42 +08:00
|
|
|
|
/** 修改表字段所需数据 */
|
|
|
|
|
|
type UpdateFieldsMeta = {
|
|
|
|
|
|
// 主键值
|
|
|
|
|
|
primaryKey: string
|
|
|
|
|
|
// 主键名
|
|
|
|
|
|
primaryKeyName: string
|
|
|
|
|
|
// 主键类型
|
|
|
|
|
|
primaryKeyType: string
|
|
|
|
|
|
// 新值
|
|
|
|
|
|
fields: FieldsMeta[]
|
|
|
|
|
|
}
|
|
|
|
|
|
type FieldsMeta = {
|
|
|
|
|
|
// 字段所在div
|
2022-11-22 15:54:11 +08:00
|
|
|
|
div: HTMLElement
|
2022-11-22 00:34:42 +08:00
|
|
|
|
// 字段名
|
|
|
|
|
|
fieldName: string
|
2022-11-22 15:54:11 +08:00
|
|
|
|
// 字段所在的表格行数据
|
|
|
|
|
|
row: any
|
2022-11-22 00:34:42 +08:00
|
|
|
|
// 字段类型
|
|
|
|
|
|
fieldType: string
|
|
|
|
|
|
// 原值
|
|
|
|
|
|
oldValue: string
|
|
|
|
|
|
// 新值
|
|
|
|
|
|
newValue: string
|
|
|
|
|
|
}
|
2023-01-31 10:37:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用全局变量缓存实例对应的
|
|
|
|
|
|
const queryTabMonacoEditors = {}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const state = reactive({
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
tags: [],
|
|
|
|
|
|
dbs: [] as any, // 数据库实例列表
|
2023-01-31 10:37:40 +08:00
|
|
|
|
databaseList: {}, // 数据库实例拥有的数据库列表,1数据库实例 -> 多数据库
|
2022-10-29 20:08:15 +08:00
|
|
|
|
db: '', // 当前操作的数据库
|
|
|
|
|
|
dbType: '',
|
|
|
|
|
|
tables: [] as any,
|
|
|
|
|
|
dbId: null, // 当前选中操作的数据库实例
|
2023-01-31 10:37:40 +08:00
|
|
|
|
sqlName: {}, // 当前sql模板名
|
|
|
|
|
|
sqlNames: {}, // 所有sql模板名
|
|
|
|
|
|
sqlMap: {}, // 所有sql名对应的sql
|
2022-10-29 20:08:15 +08:00
|
|
|
|
activeName: 'Query',
|
2023-01-31 10:37:40 +08:00
|
|
|
|
activeNameMap: {},// 缓存活跃tab对应的dbId和db
|
2022-10-29 20:08:15 +08:00
|
|
|
|
nowTableName: '', // 当前表格数据操作的数据库表名,用于双击编辑表内容使用
|
|
|
|
|
|
dataTabs: {} as any, // 点击表信息后执行结果数据展示tabs
|
2023-01-31 10:37:40 +08:00
|
|
|
|
queryTabs: {} as any, // 查询tab
|
|
|
|
|
|
// queryTab: {
|
|
|
|
|
|
// id: '', // 唯一id
|
|
|
|
|
|
// sql: '',
|
|
|
|
|
|
// label: '查询',
|
|
|
|
|
|
// name: 'Query',
|
|
|
|
|
|
// // 点击执行按钮执行结果信息
|
|
|
|
|
|
// execRes: {
|
|
|
|
|
|
// total: 0,
|
|
|
|
|
|
// pageNum: 1,
|
|
|
|
|
|
// showPage: false,
|
|
|
|
|
|
// data: [],
|
|
|
|
|
|
// tableColumn: []
|
|
|
|
|
|
// },
|
|
|
|
|
|
// loading: false,
|
|
|
|
|
|
// nowTableName: '', //当前表格数据操作的数据库表名,用于双击编辑表内容使用
|
|
|
|
|
|
// selectionDatas: [],
|
|
|
|
|
|
// updatedFields: [] as UpdateFieldsMeta[]
|
|
|
|
|
|
// },
|
2022-10-29 20:08:15 +08:00
|
|
|
|
dataTabsTableHeight: 600,
|
|
|
|
|
|
params: {
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
pageSize: 100,
|
2022-11-05 15:13:40 +08:00
|
|
|
|
tagPath: null
|
2022-10-29 20:08:15 +08:00
|
|
|
|
},
|
|
|
|
|
|
conditionDialog: {
|
|
|
|
|
|
title: '',
|
|
|
|
|
|
placeholder: '',
|
|
|
|
|
|
columnRow: null,
|
|
|
|
|
|
dataTab: null,
|
|
|
|
|
|
visible: false,
|
|
|
|
|
|
condition: '=',
|
2022-11-05 15:13:40 +08:00
|
|
|
|
value: null
|
2022-10-29 20:08:15 +08:00
|
|
|
|
},
|
|
|
|
|
|
genSqlDialog: {
|
|
|
|
|
|
visible: false,
|
|
|
|
|
|
sql: '',
|
|
|
|
|
|
},
|
2022-11-01 22:18:54 +08:00
|
|
|
|
monacoOptions: {
|
2022-11-05 15:13:40 +08:00
|
|
|
|
editor: {} as editor.IStandaloneCodeEditor,
|
2022-11-01 22:18:54 +08:00
|
|
|
|
height: '',
|
2022-11-05 15:13:40 +08:00
|
|
|
|
dbTables: {},
|
2022-11-22 00:34:42 +08:00
|
|
|
|
},
|
2023-01-31 10:37:40 +08:00
|
|
|
|
updatedFields: {} as { [tableName: string]: UpdateFieldsMeta[] },// 各个tab表被修改的字段信息
|
|
|
|
|
|
instances:{
|
|
|
|
|
|
tags:{},
|
|
|
|
|
|
tree:{},
|
|
|
|
|
|
dbs:{},
|
|
|
|
|
|
tables:{},
|
|
|
|
|
|
sqls:{},
|
|
|
|
|
|
},
|
|
|
|
|
|
instanceMenuMaxHeight:'850px'
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-11-05 22:18:59 +08:00
|
|
|
|
// 获取布局配置信息
|
|
|
|
|
|
const getThemeConfig: any = computed(() => {
|
|
|
|
|
|
return store.state.themeConfig.themeConfig;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-10-28 17:42:23 +08:00
|
|
|
|
self.MonacoEnvironment = {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
getWorker() {
|
2022-10-28 17:42:23 +08:00
|
|
|
|
return new EditorWorker();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const initMonacoEditor = (queryTab: any) => {
|
|
|
|
|
|
let monacoTextarea = document.getElementById(queryTab.editorId) as HTMLElement
|
2022-10-28 17:42:23 +08:00
|
|
|
|
// options参数参考 https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneEditorConstructionOptions.html#language
|
2022-11-05 15:13:40 +08:00
|
|
|
|
// 初始化一些主题
|
|
|
|
|
|
monaco.editor.defineTheme('SolarizedLight', SolarizedLight);
|
2023-01-31 10:37:40 +08:00
|
|
|
|
queryTab.monacoEditor = monaco.editor.create(monacoTextarea, {
|
2022-10-28 17:42:23 +08:00
|
|
|
|
language: 'sql',
|
2022-11-05 22:18:59 +08:00
|
|
|
|
theme: getThemeConfig.value.editorTheme,
|
2022-11-01 22:18:54 +08:00
|
|
|
|
automaticLayout: true, //自适应宽高布局
|
2022-11-05 21:08:01 +08:00
|
|
|
|
folding: false,
|
2022-11-01 22:18:54 +08:00
|
|
|
|
roundedSelection: false, // 禁用选择文本背景的圆角
|
|
|
|
|
|
matchBrackets: 'near',
|
2022-11-05 15:13:40 +08:00
|
|
|
|
linkedEditing: true,
|
2022-11-01 22:18:54 +08:00
|
|
|
|
cursorBlinking: 'smooth',// 光标闪烁样式
|
|
|
|
|
|
mouseWheelZoom: true, // 在按住Ctrl键的同时使用鼠标滚轮时,在编辑器中缩放字体
|
|
|
|
|
|
overviewRulerBorder: false, // 不要滚动条的边框
|
|
|
|
|
|
tabSize: 2, // tab 缩进长度
|
2022-11-19 15:05:44 +08:00
|
|
|
|
// fontFamily: 'JetBrainsMono', // 字体 暂时不要设置,否则光标容易错位
|
2022-11-05 15:13:40 +08:00
|
|
|
|
fontWeight: 'bold',
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// letterSpacing: 1, 字符间距
|
|
|
|
|
|
// quickSuggestions:false, // 禁用代码提示
|
|
|
|
|
|
minimap: {
|
|
|
|
|
|
enabled: false, // 不要小地图
|
|
|
|
|
|
},
|
2022-10-28 17:42:23 +08:00
|
|
|
|
});
|
2023-01-31 10:37:40 +08:00
|
|
|
|
|
|
|
|
|
|
queryTabMonacoEditors[queryTab.name] = queryTab.monacoEditor
|
2022-10-28 17:42:23 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 注册快捷键:ctrl + R 运行选中的sql
|
|
|
|
|
|
queryTab.monacoEditor.addAction({
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// An unique identifier of the contributed action.
|
2023-01-31 10:37:40 +08:00
|
|
|
|
id: 'run-sql-action' + queryTab.dbId + queryTab.db,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// A label of the action that will be presented to the user.
|
|
|
|
|
|
label: '执行SQL',
|
|
|
|
|
|
// A precondition for this action.
|
2022-11-19 15:05:44 +08:00
|
|
|
|
precondition: undefined,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
|
2022-11-19 15:05:44 +08:00
|
|
|
|
keybindingContext: undefined,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
keybindings: [
|
|
|
|
|
|
// chord
|
2023-01-31 10:37:40 +08:00
|
|
|
|
monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyR, 0)
|
2022-11-07 21:57:51 +08:00
|
|
|
|
],
|
|
|
|
|
|
contextMenuGroupId: 'navigation',
|
|
|
|
|
|
contextMenuOrder: 1.5,
|
|
|
|
|
|
// Method that will be executed when the action is triggered.
|
|
|
|
|
|
// @param editor The editor instance is passed in as a convenience
|
|
|
|
|
|
run: async function () {
|
|
|
|
|
|
try {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await onRunSql(queryTab.dbId, queryTab.db);
|
2022-11-07 21:57:51 +08:00
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
e.message && ElMessage.error(e.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 注册快捷键:ctrl + shift + f 格式化sql
|
|
|
|
|
|
queryTab.monacoEditor.addAction({
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// An unique identifier of the contributed action.
|
2023-01-31 10:37:40 +08:00
|
|
|
|
id: 'format-sql-action' + queryTab.dbId + queryTab.db,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// A label of the action that will be presented to the user.
|
|
|
|
|
|
label: '格式化SQL',
|
|
|
|
|
|
// A precondition for this action.
|
2022-11-19 15:05:44 +08:00
|
|
|
|
precondition: undefined,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
|
2022-11-19 15:05:44 +08:00
|
|
|
|
keybindingContext: undefined,
|
2022-11-07 21:57:51 +08:00
|
|
|
|
keybindings: [
|
|
|
|
|
|
// chord
|
2023-01-31 10:37:40 +08:00
|
|
|
|
monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF, 0)
|
2022-11-07 21:57:51 +08:00
|
|
|
|
],
|
|
|
|
|
|
contextMenuGroupId: 'navigation',
|
|
|
|
|
|
contextMenuOrder: 2,
|
|
|
|
|
|
// Method that will be executed when the action is triggered.
|
|
|
|
|
|
// @param editor The editor instance is passed in as a convenience
|
|
|
|
|
|
run: async function () {
|
|
|
|
|
|
try {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await formatSql(queryTab.dbId, queryTab.db);
|
2022-11-07 21:57:51 +08:00
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
e.message && ElMessage.error(e.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 动态设置主题
|
|
|
|
|
|
// monaco.editor.setTheme('hc-black');
|
|
|
|
|
|
|
2022-10-28 17:42:23 +08:00
|
|
|
|
// 参考 https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example
|
2023-01-31 10:37:40 +08:00
|
|
|
|
self.completionItemProvider = self.completionItemProvider || monaco.languages.registerCompletionItemProvider('sql', {
|
2022-11-05 15:13:40 +08:00
|
|
|
|
triggerCharacters: ['.'],
|
2022-11-01 22:18:54 +08:00
|
|
|
|
provideCompletionItems: async (model: editor.ITextModel, position: Position): Promise<languages.CompletionList | null | undefined> => {
|
2022-10-28 17:42:23 +08:00
|
|
|
|
let word = model.getWordUntilPosition(position);
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let {dbId, db} = state.activeNameMap[state.activeName]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
const { lineNumber, column } = position
|
|
|
|
|
|
const { startColumn, endColumn } = word
|
2022-11-01 22:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
// 当前行文本
|
|
|
|
|
|
let lineContent = model.getLineContent(lineNumber);
|
|
|
|
|
|
// 注释行不需要代码提示
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (lineContent.startsWith('--')) {
|
|
|
|
|
|
return { suggestions: [] }
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
2022-11-05 15:13:40 +08:00
|
|
|
|
|
2022-10-28 17:42:23 +08:00
|
|
|
|
let range = {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
startLineNumber: lineNumber,
|
|
|
|
|
|
endLineNumber: lineNumber,
|
|
|
|
|
|
startColumn,
|
|
|
|
|
|
endColumn,
|
2022-10-28 17:42:23 +08:00
|
|
|
|
};
|
2022-11-05 15:13:40 +08:00
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 光标前文本
|
|
|
|
|
|
const textBeforePointer = model.getValueInRange({
|
|
|
|
|
|
startLineNumber: lineNumber,
|
|
|
|
|
|
startColumn: 0,
|
|
|
|
|
|
endLineNumber: lineNumber,
|
|
|
|
|
|
endColumn: column
|
|
|
|
|
|
})
|
|
|
|
|
|
const textBeforePointerMulti = model.getValueInRange({
|
|
|
|
|
|
startLineNumber: 1,
|
|
|
|
|
|
startColumn: 0,
|
|
|
|
|
|
endLineNumber: lineNumber,
|
|
|
|
|
|
endColumn: column
|
|
|
|
|
|
})
|
|
|
|
|
|
// 光标后文本
|
|
|
|
|
|
const textAfterPointerMulti = model.getValueInRange({
|
|
|
|
|
|
startLineNumber: lineNumber,
|
|
|
|
|
|
startColumn: column,
|
|
|
|
|
|
endLineNumber: model.getLineCount(),
|
|
|
|
|
|
endColumn: model.getLineMaxColumn(model.getLineCount())
|
|
|
|
|
|
})
|
|
|
|
|
|
// // const nextTokens = textAfterPointer.trim().split(/\s+/)
|
|
|
|
|
|
// // const nextToken = nextTokens[0].toLowerCase()
|
|
|
|
|
|
const tokens = textBeforePointer.trim().split(/\s+/)
|
|
|
|
|
|
const lastToken = tokens[tokens.length - 1].toLowerCase()
|
|
|
|
|
|
|
2022-11-05 15:13:40 +08:00
|
|
|
|
// console.log("光标前文本:=>" + textBeforePointerMulti)
|
2022-11-01 22:18:54 +08:00
|
|
|
|
|
2022-11-05 15:13:40 +08:00
|
|
|
|
// console.log("最后输入的:=>" + lastToken)
|
2022-11-01 22:18:54 +08:00
|
|
|
|
if (lastToken.endsWith('.')) {
|
|
|
|
|
|
// 如果是.触发代码提示,则进行【 库.表名联想 】 或 【 表别名.表字段联想 】
|
|
|
|
|
|
let str = lastToken.substring(0, lastToken.lastIndexOf('.'))
|
|
|
|
|
|
// 库.表名联想
|
2023-01-31 10:37:40 +08:00
|
|
|
|
if (state.instances.dbs[dbId].indexOf(str) > -1) {
|
|
|
|
|
|
let tables = await loadTableMetadata(dbId, str)
|
2022-11-01 22:18:54 +08:00
|
|
|
|
let suggestions: languages.CompletionItem[] = []
|
2022-11-05 15:13:40 +08:00
|
|
|
|
for (let item of tables) {
|
|
|
|
|
|
const { tableName, tableComment } = item
|
2022-11-01 22:18:54 +08:00
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: tableName + (tableComment ? ' - ' + tableComment : ''),
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'table'
|
|
|
|
|
|
},
|
2022-11-01 22:18:54 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.File,
|
|
|
|
|
|
insertText: tableName,
|
|
|
|
|
|
range
|
2022-11-05 15:13:40 +08:00
|
|
|
|
});
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
return { suggestions }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let sql = textBeforePointerMulti.split(';')[textBeforePointerMulti.split(';').length - 1] + textAfterPointerMulti.split(';')[0];
|
|
|
|
|
|
// 表别名.表字段联想
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let tableInfo = getTableByAlias(sql, db, str)
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (tableInfo.tableName) {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
let table = tableInfo.tableName
|
|
|
|
|
|
let db = tableInfo.dbName
|
|
|
|
|
|
// 取出表名并提示
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let dbs = state.monacoOptions.dbTables[dbId+db]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
let columns = dbs ? (dbs[table] || []) : [];
|
|
|
|
|
|
if ((!columns || columns.length === 0) && db) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.monacoOptions.dbTables[dbId+db] = await loadHintTables(dbId, db)
|
|
|
|
|
|
dbs = state.monacoOptions.dbTables[dbId+db]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
columns = dbs ? (dbs[table] || []) : [];
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
let suggestions: languages.CompletionItem[] = []
|
2022-11-05 15:13:40 +08:00
|
|
|
|
columns.forEach((a: string, index: any) => {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 字段数据格式 字段名 字段注释, 如: create_time [datetime][创建时间]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
const nameAndComment = a.split(" ")
|
|
|
|
|
|
const fieldName = nameAndComment[0]
|
2022-11-01 22:18:54 +08:00
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
|
|
|
|
|
label: a,
|
|
|
|
|
|
description: 'column'
|
|
|
|
|
|
},
|
2022-11-01 22:18:54 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Property,
|
2022-11-05 15:13:40 +08:00
|
|
|
|
detail: '', // 不显示detail, 否则选中时备注等会被遮挡
|
2022-11-19 15:05:44 +08:00
|
|
|
|
insertText: fieldName + ' ', // create_time
|
2022-11-05 15:13:40 +08:00
|
|
|
|
range,
|
2022-11-05 21:08:01 +08:00
|
|
|
|
sortText: 100 + index + '' // 使用表字段声明顺序排序,排序需为字符串类型
|
2022-11-01 22:18:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
return { suggestions }
|
|
|
|
|
|
}
|
2022-11-05 15:13:40 +08:00
|
|
|
|
return { suggestions: [] }
|
|
|
|
|
|
}
|
2022-10-28 17:42:23 +08:00
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 库名联想
|
|
|
|
|
|
|
|
|
|
|
|
let suggestions: languages.CompletionItem[] = []
|
|
|
|
|
|
// mysql关键字
|
2022-10-28 17:42:23 +08:00
|
|
|
|
sqlLanguage.keywords.forEach((item: any) => {
|
|
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: item,
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'keyword'
|
|
|
|
|
|
},
|
2022-10-28 17:42:23 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Keyword,
|
|
|
|
|
|
insertText: item,
|
|
|
|
|
|
range
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2022-11-19 15:05:44 +08:00
|
|
|
|
// 操作符
|
2022-10-28 17:42:23 +08:00
|
|
|
|
sqlLanguage.operators.forEach((item: any) => {
|
|
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: item,
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'opt'
|
|
|
|
|
|
},
|
2022-10-28 17:42:23 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Operator,
|
|
|
|
|
|
insertText: item,
|
|
|
|
|
|
range
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 内置函数
|
2022-10-28 17:42:23 +08:00
|
|
|
|
sqlLanguage.builtinFunctions.forEach((item: any) => {
|
|
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: item,
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'func'
|
|
|
|
|
|
},
|
2022-10-28 17:42:23 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Function,
|
|
|
|
|
|
insertText: item,
|
|
|
|
|
|
range
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 内置变量
|
|
|
|
|
|
sqlLanguage.builtinVariables.forEach((item: string) => {
|
2022-10-28 17:42:23 +08:00
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: item,
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'var'
|
|
|
|
|
|
},
|
2022-10-28 17:42:23 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Variable,
|
|
|
|
|
|
insertText: item,
|
|
|
|
|
|
range
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2022-11-01 22:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
// 库名提示
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.instances.dbs[dbId].forEach((a: string) => {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
|
|
|
|
|
label: a,
|
|
|
|
|
|
description: 'schema'
|
|
|
|
|
|
},
|
2022-11-01 22:18:54 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.Folder,
|
|
|
|
|
|
insertText: a,
|
|
|
|
|
|
range
|
2022-11-05 15:13:40 +08:00
|
|
|
|
});
|
2022-11-01 22:18:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 表名联想
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.instances.tables[dbId + db]?.forEach((tableMeta: TableMeta) => {
|
2022-11-05 15:13:40 +08:00
|
|
|
|
const { tableName, tableComment } = tableMeta
|
2022-11-01 22:18:54 +08:00
|
|
|
|
suggestions.push({
|
2022-11-09 15:34:43 +08:00
|
|
|
|
label: {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
label: tableName + ' - ' + tableComment,
|
2022-11-09 15:34:43 +08:00
|
|
|
|
description: 'table'
|
|
|
|
|
|
},
|
2022-11-01 22:18:54 +08:00
|
|
|
|
kind: monaco.languages.CompletionItemKind.File,
|
|
|
|
|
|
detail: tableComment,
|
2022-11-05 15:13:40 +08:00
|
|
|
|
insertText: tableName + ' ',
|
2022-11-01 22:18:54 +08:00
|
|
|
|
range
|
2022-11-05 15:13:40 +08:00
|
|
|
|
});
|
2022-11-01 22:18:54 +08:00
|
|
|
|
})
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2022-10-28 17:42:23 +08:00
|
|
|
|
// 默认提示
|
|
|
|
|
|
return {
|
|
|
|
|
|
suggestions: suggestions
|
2022-11-05 15:13:40 +08:00
|
|
|
|
};
|
2022-10-28 17:42:23 +08:00
|
|
|
|
},
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 根据别名获取sql里的表名
|
|
|
|
|
|
* @param sql sql
|
|
|
|
|
|
* @param db 默认数据库
|
|
|
|
|
|
* @param alias 别名
|
|
|
|
|
|
*/
|
2022-11-05 15:13:40 +08:00
|
|
|
|
const getTableByAlias = (sql: string, db: string, alias: string): { dbName: string, tableName: string } => {
|
|
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 表别名:表名
|
|
|
|
|
|
let result = {};
|
|
|
|
|
|
let defName = '';
|
|
|
|
|
|
let defResult = {};
|
|
|
|
|
|
// 正则匹配取出表名和表别名
|
|
|
|
|
|
// 测试sql
|
|
|
|
|
|
/*
|
2022-11-19 15:05:44 +08:00
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
`select * from database.Outvisit l
|
|
|
|
|
|
left join patient p on l.patid=p.patientid
|
|
|
|
|
|
join patstatic c on l.patid=c.patid inner join patphone ph on l.patid=ph.patid
|
|
|
|
|
|
where l.name='kevin' and exsits(select 1 from pharmacywestpas pw where p.outvisitid=l.outvisitid)
|
|
|
|
|
|
unit all
|
|
|
|
|
|
select * from invisit v where`.match(/(join|from)\s+(\w*-?\w*\.?\w+)\s*(as)?\s*(\w*)/gi)
|
|
|
|
|
|
*/
|
2022-11-09 15:34:43 +08:00
|
|
|
|
let match = sql.match(/(join|from)\n*\s+\n*(\w*-?\w*\.?\w+)\s*(as)?\s*(\w*)\n*/gi)
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (match && match.length > 0) {
|
|
|
|
|
|
match.forEach(a => {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 去掉前缀,取出
|
|
|
|
|
|
let t = a.substring(5, a.length)
|
2022-11-09 15:34:43 +08:00
|
|
|
|
.replaceAll(/\s+/g, ' ')
|
|
|
|
|
|
.replaceAll(/\s+as\s+/gi, ' ')
|
|
|
|
|
|
.replaceAll(/\r\n/g, ' ').trim()
|
2022-11-01 22:18:54 +08:00
|
|
|
|
.split(/\s+/);
|
|
|
|
|
|
let withDb = t[0].split('.');
|
|
|
|
|
|
// 表名是 db名.表名
|
|
|
|
|
|
let tName = withDb.length > 1 ? withDb[1] : withDb[0]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
let dbName = withDb.length > 1 ? withDb[0] : (db || '')
|
|
|
|
|
|
if (t.length == 2) {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 表别名:表名
|
2022-11-05 15:13:40 +08:00
|
|
|
|
result[t[1]] = { tableName: tName, dbName }
|
|
|
|
|
|
} else {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
// 只有表名无别名 取第一个无别名的表为默认表
|
|
|
|
|
|
!defName && (defResult = { tableName: tName, dbName: db })
|
2022-10-28 17:42:23 +08:00
|
|
|
|
}
|
2022-11-01 22:18:54 +08:00
|
|
|
|
})
|
2022-10-28 17:42:23 +08:00
|
|
|
|
}
|
2022-11-01 22:18:54 +08:00
|
|
|
|
return result[alias] || defResult
|
2022-10-28 17:42:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
onMounted(() => {
|
2022-11-21 19:49:50 +08:00
|
|
|
|
self.completionItemProvider?.dispose()
|
2022-10-29 20:08:15 +08:00
|
|
|
|
setHeight();
|
2023-01-31 10:37:40 +08:00
|
|
|
|
instManage.loadSelectScheme()
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 监听浏览器窗口大小变化,更新对应组件高度
|
2023-01-31 10:37:40 +08:00
|
|
|
|
window.onresize = () => setHeight();
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
2022-06-16 15:55:18 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
/**
|
2022-11-05 15:13:40 +08:00
|
|
|
|
* 设置editor高度和数据表高度
|
2022-10-29 20:08:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
const setHeight = () => {
|
|
|
|
|
|
// 默认300px
|
2022-11-01 22:18:54 +08:00
|
|
|
|
state.monacoOptions.height = window.innerHeight - 550 + 'px'
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.dataTabsTableHeight = window.innerHeight - 219;
|
2023-02-06 14:50:57 +08:00
|
|
|
|
state.instanceMenuMaxHeight = window.innerHeight - 140 + 'px';
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行sql
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onRunSql = async (dbId: any, db: string) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 没有选中的文本,则为全部文本
|
2022-11-01 22:18:54 +08:00
|
|
|
|
let sql = getSql() as string;
|
|
|
|
|
|
notBlank(sql && sql.trim(), '请选中需要执行的sql');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 去除字符串前的空格、换行等
|
|
|
|
|
|
sql = sql.replace(/(^\s*)/g, '');
|
|
|
|
|
|
let execRemark = '';
|
|
|
|
|
|
let canRun = true;
|
|
|
|
|
|
if (
|
|
|
|
|
|
sql.startsWith('update') ||
|
|
|
|
|
|
sql.startsWith('UPDATE') ||
|
|
|
|
|
|
sql.startsWith('INSERT') ||
|
|
|
|
|
|
sql.startsWith('insert') ||
|
|
|
|
|
|
sql.startsWith('DELETE') ||
|
|
|
|
|
|
sql.startsWith('delete')
|
|
|
|
|
|
) {
|
|
|
|
|
|
const res: any = await ElMessageBox.prompt('请输入备注', 'Tip', {
|
|
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
|
|
cancelButtonText: '取消',
|
|
|
|
|
|
inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
|
|
|
|
|
|
inputErrorMessage: '请输入执行该sql的备注信息',
|
|
|
|
|
|
});
|
|
|
|
|
|
execRemark = res.value;
|
|
|
|
|
|
if (!execRemark) {
|
|
|
|
|
|
canRun = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!canRun) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-06-16 15:55:18 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await doRunSql(dbId, db, sql, execRemark)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let key = state.activeName;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 即只有以该字符串开头的sql才可修改表数据内容
|
|
|
|
|
|
if (sql.startsWith('SELECT *') || sql.startsWith('select *') || sql.startsWith('SELECT\n *')) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].selectionDatas = [];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const tableName = sql.split(/from/i)[1];
|
|
|
|
|
|
if (tableName) {
|
|
|
|
|
|
const tn = tableName.trim().split(' ')[0];
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].nowTableName = tn;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
state.nowTableName = tn;
|
|
|
|
|
|
} else {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].nowTableName = '';
|
2022-10-29 20:08:15 +08:00
|
|
|
|
state.nowTableName = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].nowTableName = '';
|
2022-10-29 20:08:15 +08:00
|
|
|
|
state.nowTableName = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const doRunSql = async (dbId: any, db: string, sql: string, execRemark?: string) => {
|
|
|
|
|
|
let key = state.activeName;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
try {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].loading = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 执行新sql,还原分页信息
|
|
|
|
|
|
if(state.queryTabs[key].sql !== sql){
|
|
|
|
|
|
state.queryTabs[key].execRes.total = 0
|
|
|
|
|
|
state.queryTabs[key].execRes.pageNum = 1
|
|
|
|
|
|
state.queryTabs[key].sql = sql;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 干掉sql最后的分号
|
|
|
|
|
|
if(sql.endsWith(';')){
|
|
|
|
|
|
sql = sql.substring(0, sql.length -1)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 给sql添加limit (pageNum-1)*pageSize, pageSize
|
|
|
|
|
|
sql = sql.trim().toLowerCase();
|
|
|
|
|
|
let countSql = `select count(*) ct from (${sql}) a`
|
|
|
|
|
|
if (sql.startsWith('select') && sql.indexOf('limit') < 0){
|
|
|
|
|
|
state.queryTabs[key].execRes.showPage = true
|
|
|
|
|
|
switch (state.dbType) {
|
|
|
|
|
|
case 'mysql':
|
|
|
|
|
|
sql += ` limit ${(state.queryTabs[key].execRes.pageNum-1)*defalutLimit}, ${defalutLimit} `
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'postgres':
|
|
|
|
|
|
sql += ` OFFSET ${(state.queryTabs[key].execRes.pageNum-1)*defalutLimit} LIMIT ${defalutLimit} `
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
state.queryTabs[key].execRes.showPage = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const colAndData: any = await runSql(dbId, db, sql, execRemark);
|
2022-12-28 10:15:22 +08:00
|
|
|
|
if(!colAndData.res || colAndData.res.length===0){
|
|
|
|
|
|
ElMessage.warning('暂无数据')
|
|
|
|
|
|
}
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].execRes.data = colAndData.res;
|
|
|
|
|
|
if(colAndData.res && colAndData.res.length == defalutLimit){
|
|
|
|
|
|
const countRes = await runSql(dbId, db, countSql);
|
|
|
|
|
|
state.queryTabs[key].execRes.total = countRes.res[0].ct
|
|
|
|
|
|
}else{
|
|
|
|
|
|
state.queryTabs[key].execRes.total = (state.queryTabs[key].execRes.pageNum-1)*defalutLimit + colAndData.res.length
|
|
|
|
|
|
}
|
|
|
|
|
|
state.queryTabs[key].execRes.tableColumn = colAndData.colNames;
|
|
|
|
|
|
state.queryTabs[key].loading = false;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
cancelUpdateFields()
|
|
|
|
|
|
} catch (e: any) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[key].loading = false;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
}
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const exportData = () => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const dataList = state.queryTabs[state.activeName].execRes.data;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
isTrue(dataList.length > 0, '没有数据可导出');
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const tableColumn = state.queryTabs[state.activeName].execRes.tableColumn;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 二维数组
|
|
|
|
|
|
const cvsData = [tableColumn];
|
|
|
|
|
|
for (let data of dataList) {
|
|
|
|
|
|
// 数据值组成的一维数组
|
|
|
|
|
|
let dataValueArr: any = [];
|
|
|
|
|
|
for (let column of tableColumn) {
|
|
|
|
|
|
dataValueArr.push(data[column]);
|
|
|
|
|
|
}
|
|
|
|
|
|
cvsData.push(dataValueArr);
|
|
|
|
|
|
}
|
|
|
|
|
|
const csvString = cvsData.map((e) => e.join(',')).join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
// 导出
|
|
|
|
|
|
let link = document.createElement('a');
|
|
|
|
|
|
let exportContent = '\uFEFF';
|
|
|
|
|
|
let blob = new Blob([exportContent + csvString], {
|
|
|
|
|
|
type: 'text/plain;charset=utrf-8',
|
|
|
|
|
|
});
|
|
|
|
|
|
link.id = 'download-csv';
|
|
|
|
|
|
link.setAttribute('href', URL.createObjectURL(blob));
|
|
|
|
|
|
link.setAttribute('download', `查询数据导出-${dateStrFormat('yyyyMMddHHmmss', new Date())}.csv`);
|
|
|
|
|
|
document.body.appendChild(link);
|
|
|
|
|
|
link.click();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行sql str
|
|
|
|
|
|
*
|
2023-01-31 10:37:40 +08:00
|
|
|
|
* @param dbId 数据库实例id
|
|
|
|
|
|
* @param db 数据库schema
|
2022-10-29 20:08:15 +08:00
|
|
|
|
* @param sql 执行的sql
|
2022-11-22 00:34:42 +08:00
|
|
|
|
* @param remark sql备注
|
2022-10-29 20:08:15 +08:00
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const runSql = async (dbId: any, db: string, sql: string, remark: string = '') => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
return await dbApi.sqlExec.request({
|
2023-01-31 10:37:40 +08:00
|
|
|
|
id: dbId,
|
|
|
|
|
|
db: db,
|
2022-10-29 20:08:15 +08:00
|
|
|
|
sql: sql.trim(),
|
|
|
|
|
|
remark,
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const removeDataTab = (targetName: string) => {
|
|
|
|
|
|
let activeName = state.activeName;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 计算下一个tab
|
|
|
|
|
|
const dataTabNames = Object.keys(state.dataTabs);
|
|
|
|
|
|
let matched = false;
|
|
|
|
|
|
dataTabNames.forEach((name, index) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if (name === targetName) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const nextTab = dataTabNames[index + 1] || dataTabNames[index - 1];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if (nextTab) {
|
|
|
|
|
|
activeName = nextTab;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
matched = true
|
2021-11-25 14:34:15 +08:00
|
|
|
|
}
|
2023-01-31 10:37:40 +08:00
|
|
|
|
delete state.dataTabs[targetName];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-01-31 10:37:40 +08:00
|
|
|
|
|
|
|
|
|
|
if(!matched){
|
|
|
|
|
|
const queryTabNames = Object.keys(state.queryTabs);
|
|
|
|
|
|
queryTabNames.forEach((name: string, index: number) => {
|
|
|
|
|
|
if (name === targetName) {
|
|
|
|
|
|
const nextTab = queryTabNames[index + 1] || queryTabNames[index - 1];
|
|
|
|
|
|
if (nextTab) {
|
|
|
|
|
|
activeName = nextTab;
|
|
|
|
|
|
}
|
|
|
|
|
|
delete state.queryTabs[targetName];
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
state.activeName = activeName;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 数据tab点击
|
|
|
|
|
|
*/
|
|
|
|
|
|
const onDataTabClick = (tab: any) => {
|
|
|
|
|
|
const name = tab.props.name;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 表数据tab,赋值当前表名,用于在线修改表数据
|
|
|
|
|
|
if (!name.startsWith('查询')) {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
state.nowTableName = name;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2021-11-25 14:34:15 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const beforeUpload = (file: File) => {
|
|
|
|
|
|
ElMessage.success(`'${file.name}' 正在上传执行, 请关注结果通知`);
|
|
|
|
|
|
};
|
2021-12-20 20:42:52 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 执行sql成功
|
|
|
|
|
|
const execSqlFileSuccess = (res: any) => {
|
|
|
|
|
|
if (res.code !== 200) {
|
|
|
|
|
|
ElMessage.error(res.msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取sql文件上传执行url
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const getUploadSqlFileUrl = (dbId: any, db: string) => {
|
|
|
|
|
|
return `${config.baseApiUrl}/dbs/${dbId}/exec-sql-file?db=${db}`;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const flexColumnWidth = (str: any, tableData: any, flag = 'equal') => {
|
|
|
|
|
|
// str为该列的字段名(传字符串);tableData为该表格的数据源(传变量);
|
|
|
|
|
|
// flag为可选值,可不传该参数,传参时可选'max'或'equal',默认为'max'
|
|
|
|
|
|
// flag为'max'则设置列宽适配该列中最长的内容,flag为'equal'则设置列宽适配该列中第一行内容的长度。
|
|
|
|
|
|
str = str + '';
|
|
|
|
|
|
let columnContent = '';
|
|
|
|
|
|
if (!tableData || !tableData.length || tableData.length === 0 || tableData === undefined) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!str || !str.length || str.length === 0 || str === undefined) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (flag === 'equal') {
|
|
|
|
|
|
// 获取该列中第一个不为空的数据(内容)
|
|
|
|
|
|
for (let i = 0; i < tableData.length; i++) {
|
|
|
|
|
|
// 转为字符串后比较
|
|
|
|
|
|
if ((tableData[i][str] + '').length > 0) {
|
|
|
|
|
|
columnContent = tableData[i][str] + '';
|
|
|
|
|
|
break;
|
2022-01-19 15:10:17 +08:00
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 获取该列中最长的数据(内容)
|
|
|
|
|
|
let index = 0;
|
|
|
|
|
|
for (let i = 0; i < tableData.length; i++) {
|
|
|
|
|
|
if (tableData[i][str] === null) {
|
2022-01-20 18:01:11 +08:00
|
|
|
|
return;
|
2022-01-19 15:10:17 +08:00
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const now_temp = tableData[i][str] + '';
|
|
|
|
|
|
const max_temp = tableData[index][str] + '';
|
|
|
|
|
|
if (now_temp.length > max_temp.length) {
|
|
|
|
|
|
index = i;
|
2022-07-10 12:14:06 +08:00
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
columnContent = tableData[index][str] + '';
|
|
|
|
|
|
}
|
|
|
|
|
|
const contentWidth: number = getContentWidth(columnContent);
|
|
|
|
|
|
// 获取列名称的长度 加上排序图标长度
|
|
|
|
|
|
const columnWidth: number = getContentWidth(str) + 43;
|
|
|
|
|
|
const flexWidth: number = contentWidth > columnWidth ? contentWidth : columnWidth;
|
|
|
|
|
|
return flexWidth + 'px';
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取内容所需要占用的宽度
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getContentWidth = (content: any): number => {
|
|
|
|
|
|
// 以下分配的单位长度可根据实际需求进行调整
|
|
|
|
|
|
let flexWidth = 0;
|
|
|
|
|
|
for (const char of content) {
|
|
|
|
|
|
if (flexWidth > 500) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ((char >= '0' && char <= '9') || (char >= 'a' && char <= 'z')) {
|
|
|
|
|
|
// 如果是小写字母、数字字符,分配8个单位宽度
|
|
|
|
|
|
flexWidth += 8.5;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (char >= 'A' && char <= 'Z') {
|
|
|
|
|
|
flexWidth += 9;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (char >= '\u4e00' && char <= '\u9fa5') {
|
|
|
|
|
|
// 如果是中文字符,为字符分配16个单位宽度
|
|
|
|
|
|
flexWidth += 16;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 其他种类字符,为字符分配9个单位宽度
|
|
|
|
|
|
flexWidth += 8;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (flexWidth > 500) {
|
|
|
|
|
|
// 设置最大宽度
|
|
|
|
|
|
flexWidth = 500;
|
|
|
|
|
|
}
|
|
|
|
|
|
return flexWidth;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getColumnTip = (tableName: string, columnName: string) => {
|
|
|
|
|
|
// 优先从 table map中获取
|
|
|
|
|
|
let columns = getColumns4Map(tableName);
|
|
|
|
|
|
if (!columns) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
const column = columns.find((c: any) => c.columnName == columnName);
|
|
|
|
|
|
const comment = column.columnComment;
|
|
|
|
|
|
return `${column.columnType} ${comment ? ' | ' + comment : ''}`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取sql,如果有鼠标选中,则返回选中内容,否则返回输入框内所有内容
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getSql = () => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let monacoEditor = queryTabMonacoEditors[state.activeName]
|
2022-11-01 22:18:54 +08:00
|
|
|
|
let res = '' as string | undefined;
|
|
|
|
|
|
// 编辑器还没初始化
|
2022-11-19 15:05:44 +08:00
|
|
|
|
if (!monacoEditor?.getModel) {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 选择选中的sql
|
|
|
|
|
|
let selection = monacoEditor.getSelection()
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (selection) {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
res = monacoEditor.getModel()?.getValueInRange(selection)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 整个编辑器的sql
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (!res) {
|
2022-11-01 22:18:54 +08:00
|
|
|
|
return monacoEditor.getModel()?.getValue()
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
2022-11-01 22:18:54 +08:00
|
|
|
|
return res
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const loadSchemaMeta = async (dbId: any, db: string)=>{
|
|
|
|
|
|
// 加载数据库下所有表
|
|
|
|
|
|
state.instances.tables[dbId+db] = state.instances.tables[dbId+db] || await loadTableMetadata(dbId, db)
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 加载数据库下所有表字段信息
|
|
|
|
|
|
state.monacoOptions.dbTables[dbId+db] = state.monacoOptions.dbTables[dbId+db] || await loadHintTables(dbId, db)
|
|
|
|
|
|
}
|
2022-01-20 18:01:11 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const loadTableMetadata = async (dbId: any, db: string) => {
|
|
|
|
|
|
return await dbApi.tableMetadata.request({ id: dbId, db })
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const loadHintTables = async (dbId: any, db: string) => {
|
|
|
|
|
|
return await dbApi.hintTables.request({ id: dbId, db, })
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 选择表事件
|
|
|
|
|
|
const changeTable = async (tableName: string, execSelectSql: boolean = true) => {
|
|
|
|
|
|
if (tableName == '') {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!execSelectSql) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-02-07 17:49:48 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 执行sql,并新增tab
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.activeName = state.dbId + state.db + tableName;
|
|
|
|
|
|
state.nowTableName = state.activeName;
|
|
|
|
|
|
let tab = state.dataTabs[state.dbId + state.db +tableName];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 如果存在该表tab,则直接返回
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-01-20 18:01:11 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
tab = {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
label: '`' + state.db+ '`.'+ tableName,
|
|
|
|
|
|
key : state.dbId + state.db +tableName,
|
2022-10-29 20:08:15 +08:00
|
|
|
|
name: tableName,
|
|
|
|
|
|
datas: [],
|
|
|
|
|
|
columnNames: [],
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
count: 0,
|
2023-01-31 10:37:40 +08:00
|
|
|
|
dbId: state.dbId, // 数据库id跟着tab走
|
|
|
|
|
|
db: state.db,
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
tab.columnNames = await getColumnNames(tableName);
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.dataTabs[state.dbId + state.db +tableName] = tab;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await onRefresh(state.dbId, state.db, tableName);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取表的所有列信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getColumns = async (tableName: string) => {
|
|
|
|
|
|
// 优先从 table map中获取
|
|
|
|
|
|
let columns = getColumns4Map(tableName);
|
|
|
|
|
|
if (columns) {
|
|
|
|
|
|
return columns;
|
|
|
|
|
|
}
|
|
|
|
|
|
columns = await dbApi.columnMetadata.request({
|
|
|
|
|
|
id: state.dbId,
|
|
|
|
|
|
db: state.db,
|
|
|
|
|
|
tableName: tableName,
|
|
|
|
|
|
});
|
|
|
|
|
|
tableMap.set(tableName, columns);
|
|
|
|
|
|
return columns;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 从缓存map获取列信息
|
|
|
|
|
|
const getColumns4Map = (tableName: string) => {
|
|
|
|
|
|
return tableMap.get(tableName);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getColumnNames = async (tableName: string) => {
|
|
|
|
|
|
const columns = await getColumns(tableName);
|
|
|
|
|
|
return columns.map((t: any) => t.columnName);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 条件查询,点击列信息后显示输入对应的值
|
|
|
|
|
|
*/
|
|
|
|
|
|
const onConditionRowClick = (event: any, dataTab: any) => {
|
|
|
|
|
|
const row = event[0];
|
|
|
|
|
|
state.conditionDialog.title = `请输入 [${row.columnName}] 的值`;
|
|
|
|
|
|
state.conditionDialog.placeholder = `${row.columnType} ${row.columnComment}`;
|
|
|
|
|
|
state.conditionDialog.columnRow = row;
|
|
|
|
|
|
state.conditionDialog.dataTab = dataTab;
|
|
|
|
|
|
state.conditionDialog.visible = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 确认条件
|
|
|
|
|
|
const onConfirmCondition = () => {
|
|
|
|
|
|
const conditionDialog = state.conditionDialog;
|
|
|
|
|
|
const dataTab = state.conditionDialog.dataTab as any;
|
|
|
|
|
|
let condition = dataTab.condition;
|
|
|
|
|
|
if (condition) {
|
|
|
|
|
|
condition += ` AND `;
|
|
|
|
|
|
}
|
|
|
|
|
|
const row = conditionDialog.columnRow as any;
|
|
|
|
|
|
condition += `${row.columnName} ${conditionDialog.condition} `;
|
2022-11-22 00:34:42 +08:00
|
|
|
|
dataTab.condition = condition + wrapColumnValue(row.columnType, conditionDialog.value);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
onCancelCondition();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const onCancelCondition = () => {
|
|
|
|
|
|
state.conditionDialog.visible = false;
|
|
|
|
|
|
state.conditionDialog.title = ``;
|
|
|
|
|
|
state.conditionDialog.placeholder = ``;
|
|
|
|
|
|
state.conditionDialog.value = null;
|
|
|
|
|
|
state.conditionDialog.columnRow = null;
|
|
|
|
|
|
state.conditionDialog.dataTab = null;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onRefresh = async (dbId:any, db: string, tableName: string) => {
|
|
|
|
|
|
const dataTab = state.dataTabs[state.dbId + state.db + tableName];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 查询条件置空
|
|
|
|
|
|
dataTab.condition = '';
|
|
|
|
|
|
dataTab.pageNum = 1;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
setDataTabDatas(dataTab).then(() => {
|
|
|
|
|
|
cancelUpdateFields()
|
2022-11-22 15:54:11 +08:00
|
|
|
|
});
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-22 15:54:11 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 数据tab修改页数
|
|
|
|
|
|
*/
|
|
|
|
|
|
const handlePageChange = async (dataTab: any) => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await setDataTabDatas(dataTab);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据条件查询数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
const selectByCondition = async (tableName: string, condition: string) => {
|
|
|
|
|
|
notEmpty(condition, '条件不能为空');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const dataTab = state.dataTabs[state.dbId + state.db +tableName];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
dataTab.pageNum = 1;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await setDataTabDatas(dataTab);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置data tab的表数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
const setDataTabDatas = async (dataTab: any) => {
|
|
|
|
|
|
dataTab.loading = true;
|
|
|
|
|
|
try {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
dataTab.count = await getTableCount(dataTab.dbId, dataTab.db, dataTab.name, dataTab.condition);
|
|
|
|
|
|
let sql = getDefaultSelectSql(dataTab.name, dataTab.condition, dataTab.orderBy, dataTab.pageNum)
|
|
|
|
|
|
state.dataTabs[state.dbId + state.db + dataTab.name].sql = sql;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if (dataTab.count > 0) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const colAndData: any = await runSql(dataTab.dbId, dataTab.db, sql);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
dataTab.datas = colAndData.res;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dataTab.datas = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
dataTab.loading = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取表的统计数量
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const getTableCount = async (dbId: any, db: string, tableName: string, condition: string = '') => {
|
|
|
|
|
|
const countRes = await runSql(dbId, db, getDefaultCountSql(tableName, condition));
|
2022-10-29 20:08:15 +08:00
|
|
|
|
return countRes.res[0].count;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取默认查询语句
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '', pageNum: number = 1) => {
|
|
|
|
|
|
const baseSql = `SELECT * FROM ${tableName} ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''}`;
|
|
|
|
|
|
if (state.dbType == 'mysql') {
|
|
|
|
|
|
return `${baseSql} LIMIT ${(pageNum - 1) * defalutLimit}, ${defalutLimit};`;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (state.dbType == 'postgres') {
|
|
|
|
|
|
return `${baseSql} OFFSET ${(pageNum - 1) * defalutLimit} LIMIT ${defalutLimit};`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return baseSql;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取默认查询统计语句
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getDefaultCountSql = (tableName: string, where: string = '') => {
|
|
|
|
|
|
return `SELECT COUNT(*) count FROM ${tableName} ${where ? 'WHERE ' + where : ''}`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 提交事务,用于没有开启自动提交事务
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onCommit = (dbId: any, db: string) => {
|
|
|
|
|
|
runSql(dbId, db, 'COMMIT;');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
ElMessage.success('COMMIT success');
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 表排序字段变更
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onTableSortChange = async (dbId: any, db: string, sort: any, tableName: string) => {
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if (!state.nowTableName || !sort.prop) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const sortType = sort.order == 'descending' ? 'DESC' : 'ASC';
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.dataTabs[state.activeName].orderBy = `ORDER BY ${sort.prop} ${sortType}`;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await onRefresh(dbId, db, tableName);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const changeSqlTemplate = (dbId: any, db: string) => {
|
|
|
|
|
|
getUserSql(dbId, db, '');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
2022-11-05 15:13:40 +08:00
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取用户保存的sql模板内容
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const getUserSql = (dbId: any, db: string, sql: string) => {
|
|
|
|
|
|
notBlank(dbId, '请先选择数据库');
|
|
|
|
|
|
sql && setSqlEditorValue(dbId, db, sql) ||
|
|
|
|
|
|
dbApi.getSql.request({ id: dbId, type: 1, name: state.sqlName[dbId+db], db: db }).then((res: any) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if (res) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
setSqlEditorValue(dbId, db, res.sql);
|
|
|
|
|
|
state.sqlMap[dbId+db+state.sqlName[dbId+db]] = res.sql
|
2022-10-29 20:08:15 +08:00
|
|
|
|
} else {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
setSqlEditorValue(dbId, db, '');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const setSqlEditorValue = (dbId: any, db: string, value: string) => {
|
|
|
|
|
|
let monacoEditor = queryTabMonacoEditors[state.activeName]
|
|
|
|
|
|
monacoEditor?.getModel()?.setValue(value);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取用户保存的sql模板名称
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const getSqlNames = (dbId: any, db: string) => {
|
|
|
|
|
|
!state.sqlNames[dbId + db] &&
|
|
|
|
|
|
dbApi.getSqlNames.request({id: dbId, db: db,})
|
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
|
if (res && res.length > 0) {
|
|
|
|
|
|
state.sqlNames[dbId + db] = res.map((r: any) => r.name);
|
|
|
|
|
|
state.sqlName[dbId + db] = state.sqlNames[0];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
state.sqlNames[dbId + db] = ['default'] as any;
|
|
|
|
|
|
state.sqlName[dbId + db] = 'default';
|
|
|
|
|
|
}
|
|
|
|
|
|
getUserSql(dbId, db, '');
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
const saveSql = async (dbId: any, db: string) => {
|
|
|
|
|
|
let monacoEditor = queryTabMonacoEditors[state.activeName]
|
2022-11-01 22:18:54 +08:00
|
|
|
|
const sql = monacoEditor.getModel()?.getValue();
|
|
|
|
|
|
notBlank(sql, 'sql内容不能为空');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
notBlank(state.dbId, '请先选择数据库实例');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await dbApi.saveSql.request({ id: dbId, db: db, sql: sql, type: 1, name: state.sqlName[dbId+db] });
|
2022-10-29 20:08:15 +08:00
|
|
|
|
ElMessage.success('保存成功');
|
|
|
|
|
|
|
|
|
|
|
|
dbApi.getSqlNames
|
|
|
|
|
|
.request({
|
|
|
|
|
|
id: state.dbId,
|
|
|
|
|
|
db: state.db,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((res) => {
|
|
|
|
|
|
if (res) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.sqlNames[dbId+db] = res.map((r: any) => r.name);
|
2022-01-17 17:10:38 +08:00
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const deleteSql = async (dbId: any, db: string) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
try {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await ElMessageBox.confirm(`确定删除【${state.sqlName[dbId+db]}】该SQL模板?`, '提示', {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
|
|
cancelButtonText: '取消',
|
|
|
|
|
|
type: 'warning',
|
|
|
|
|
|
});
|
2023-01-31 10:37:40 +08:00
|
|
|
|
await dbApi.deleteDbSql.request({ id: dbId, db: db, name: state.sqlName[dbId+db]});
|
2022-10-29 20:08:15 +08:00
|
|
|
|
ElMessage.success('删除成功');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
getSqlNames(dbId, db);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
} catch (err) { }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const onDataSelectionChange = (datas: []) => {
|
|
|
|
|
|
if (isQueryTab()) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[state.activeName].selectionDatas = datas;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
state.dataTabs[state.activeName].selectionDatas = datas;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行删除数据事件
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onDeleteData = async (dbId:any, db: string, tableName: string) => {
|
|
|
|
|
|
const isQuery = isQueryTab();
|
|
|
|
|
|
const queryTab = state.queryTabs[state.activeName];
|
|
|
|
|
|
const deleteDatas = isQuery ? queryTab.selectionDatas : state.dataTabs[state.activeName].selectionDatas;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
isTrue(deleteDatas && deleteDatas.length > 0, '请先选择要删除的数据');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const primaryKey = await getColumn(tableName);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const primaryKeyColumnName = primaryKey.columnName;
|
2022-11-22 00:34:42 +08:00
|
|
|
|
const ids = deleteDatas.map((d: any) => `${wrapColumnValue(primaryKey.columnType, d[primaryKeyColumnName])}`).join(',');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const sql = `DELETE FROM ${tableName} WHERE ${primaryKeyColumnName} IN (${ids})`;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
promptExeSql(dbId, db, sql, null, () => {
|
|
|
|
|
|
if (!isQuery) {
|
|
|
|
|
|
onRefresh(dbId, db, tableName);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
} else {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
queryTab.execRes.data = queryTab.execRes.data.filter(
|
2022-10-29 20:08:15 +08:00
|
|
|
|
(d: any) => !(deleteDatas.findIndex((x: any) => x[primaryKeyColumnName] == d[primaryKeyColumnName]) != -1)
|
|
|
|
|
|
);
|
2023-01-31 10:37:40 +08:00
|
|
|
|
queryTab.selectionDatas = [];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const onGenerateInsertSql = async (dbId: any, db: string, tableName: string) => {
|
|
|
|
|
|
const datas = isQueryTab() ? state.queryTabs[state.activeName].selectionDatas : state.dataTabs[state.activeName].selectionDatas;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
isTrue(datas && datas.length > 0, '请先选择要生成insert语句的数据');
|
|
|
|
|
|
const columns: any = await getColumns(tableName);
|
|
|
|
|
|
|
|
|
|
|
|
const sqls = [];
|
|
|
|
|
|
for (let data of datas) {
|
|
|
|
|
|
let colNames = [];
|
|
|
|
|
|
let values = [];
|
|
|
|
|
|
for (let column of columns) {
|
|
|
|
|
|
const colName = column.columnName;
|
|
|
|
|
|
colNames.push(colName);
|
|
|
|
|
|
values.push(wrapValueByType(data[colName]));
|
|
|
|
|
|
}
|
|
|
|
|
|
sqls.push(`INSERT INTO ${tableName} (${colNames.join(', ')}) VALUES(${values.join(', ')})`);
|
|
|
|
|
|
}
|
|
|
|
|
|
state.genSqlDialog.sql = sqls.join(';\n') + ';';
|
|
|
|
|
|
state.genSqlDialog.visible = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const wrapValueByType = (val: any) => {
|
|
|
|
|
|
if (val == null) {
|
|
|
|
|
|
return 'NULL';
|
|
|
|
|
|
}
|
|
|
|
|
|
if (typeof val == 'number') {
|
|
|
|
|
|
return val;
|
|
|
|
|
|
}
|
|
|
|
|
|
return `'${val}'`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 是否为查询tab
|
|
|
|
|
|
*/
|
|
|
|
|
|
const isQueryTab = () => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
return state.activeName.startsWith('查询');
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 监听单元格点击事件
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const cellClick = (row: any, column: any, cell: any, event: Event, tabInfo: {dbId: number, db: string, tableName: string}) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const property = column.property;
|
|
|
|
|
|
// 如果当前操作的表名不存在 或者 当前列的property不存在(如多选框),则不允许修改当前单元格内容
|
|
|
|
|
|
if (!state.nowTableName || !property) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-11-22 17:15:08 +08:00
|
|
|
|
let div: HTMLElement = cell.children[0];
|
2022-12-13 11:03:15 +08:00
|
|
|
|
if (div && div.tagName === 'DIV') {
|
|
|
|
|
|
// 转为字符串比较,可能存在数字等
|
|
|
|
|
|
let text = (row[property] || row[property] == 0 ? row[property] : '') + '';
|
2022-10-29 20:08:15 +08:00
|
|
|
|
let input = document.createElement('input');
|
|
|
|
|
|
input.setAttribute('value', text);
|
|
|
|
|
|
// 将表格width也赋值于输入框,避免输入框长度超过表格长度
|
2022-11-01 22:18:54 +08:00
|
|
|
|
input.setAttribute('style', 'height:23px;text-align:center;border:none;' + div.getAttribute('style'));
|
2022-10-29 20:08:15 +08:00
|
|
|
|
cell.replaceChildren(input);
|
|
|
|
|
|
input.focus();
|
|
|
|
|
|
input.addEventListener('blur', async () => {
|
|
|
|
|
|
row[property] = input.value;
|
|
|
|
|
|
cell.replaceChildren(div);
|
|
|
|
|
|
if (input.value !== text) {
|
2022-11-22 00:34:42 +08:00
|
|
|
|
let currentUpdatedFields: UpdateFieldsMeta[]
|
2023-01-31 10:37:40 +08:00
|
|
|
|
if (isQueryTab()) {
|
|
|
|
|
|
currentUpdatedFields = state.queryTabs[state.activeName].updatedFields
|
2022-11-22 00:34:42 +08:00
|
|
|
|
} else {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
currentUpdatedFields = state.updatedFields[state.nowTableName];
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 主键
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const primaryKey = await getColumn(tabInfo.tableName);
|
2022-11-22 00:34:42 +08:00
|
|
|
|
const primaryKeyValue = row[primaryKey.columnName];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 更新字段列信息
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const updateColumn = await getColumn(tabInfo.tableName, property);
|
|
|
|
|
|
const newField = { div, row,
|
|
|
|
|
|
fieldName: column.rawColumnKey,
|
|
|
|
|
|
fieldType: updateColumn.columnType,
|
|
|
|
|
|
oldValue: text,
|
|
|
|
|
|
newValue: input.value
|
|
|
|
|
|
} as FieldsMeta;
|
2022-11-22 00:34:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 被修改的字段
|
2022-11-22 17:15:08 +08:00
|
|
|
|
const primaryKeyFields = currentUpdatedFields.filter((meta) => meta.primaryKey === primaryKeyValue)
|
2022-11-22 00:34:42 +08:00
|
|
|
|
let hasKey = false;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
if (primaryKeyFields.length <= 0) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
primaryKeyFields[0] = {
|
|
|
|
|
|
primaryKey: primaryKeyValue,
|
|
|
|
|
|
primaryKeyName: primaryKey.columnName,
|
|
|
|
|
|
primaryKeyType: primaryKey.columnType,
|
|
|
|
|
|
fields: [newField] }
|
2022-11-22 17:15:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
hasKey = true
|
|
|
|
|
|
let hasField = primaryKeyFields[0].fields.some(a => {
|
|
|
|
|
|
if (a.fieldName === newField.fieldName) {
|
|
|
|
|
|
a.newValue = newField.newValue
|
|
|
|
|
|
}
|
|
|
|
|
|
return a.fieldName === newField.fieldName
|
|
|
|
|
|
})
|
|
|
|
|
|
if (!hasField) {
|
|
|
|
|
|
primaryKeyFields[0].fields.push(newField)
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let fields = primaryKeyFields[0].fields
|
|
|
|
|
|
|
2022-11-22 17:15:08 +08:00
|
|
|
|
const fieldsParam = fields.filter((a) => {
|
|
|
|
|
|
if (a.fieldName === column.rawColumnKey) {
|
|
|
|
|
|
a.newValue = input.value
|
|
|
|
|
|
}
|
|
|
|
|
|
return a.fieldName === column.rawColumnKey
|
2022-11-22 00:34:42 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const field = fieldsParam.length > 0 && fieldsParam[0] || {} as FieldsMeta
|
2022-11-22 17:15:08 +08:00
|
|
|
|
if (field.oldValue === input.value) { // 新值=旧值
|
2022-11-22 00:34:42 +08:00
|
|
|
|
// 删除数据
|
|
|
|
|
|
div.classList.remove('update_field_active')
|
|
|
|
|
|
let delIndex: number[] = [];
|
2022-11-22 17:15:08 +08:00
|
|
|
|
currentUpdatedFields.forEach((a, i) => {
|
|
|
|
|
|
if (a.primaryKey === primaryKeyValue) {
|
|
|
|
|
|
a.fields = a.fields && a.fields.length > 0 ? a.fields.filter(f => f.fieldName !== column.rawColumnKey) : [];
|
|
|
|
|
|
a.fields.length <= 0 && delIndex.push(i)
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2022-11-22 17:15:08 +08:00
|
|
|
|
});
|
|
|
|
|
|
delIndex.forEach(i => delete currentUpdatedFields[i])
|
|
|
|
|
|
currentUpdatedFields = currentUpdatedFields.filter(a => a)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 新增数据
|
|
|
|
|
|
div.classList.add('update_field_active')
|
|
|
|
|
|
if (hasKey) {
|
|
|
|
|
|
currentUpdatedFields.forEach((value, index, array) => {
|
|
|
|
|
|
if (value.primaryKey === primaryKeyValue) {
|
|
|
|
|
|
array[index].fields = fields
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
currentUpdatedFields.push({
|
|
|
|
|
|
primaryKey: primaryKeyValue,
|
|
|
|
|
|
primaryKeyName: primaryKey.columnName,
|
|
|
|
|
|
primaryKeyType: primaryKey.columnType,
|
|
|
|
|
|
fields
|
|
|
|
|
|
})
|
2022-11-22 17:15:08 +08:00
|
|
|
|
}
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2023-01-31 10:37:40 +08:00
|
|
|
|
if (isQueryTab()) {
|
|
|
|
|
|
state.queryTabs[state.activeName].updatedFields = currentUpdatedFields
|
2022-11-22 00:34:42 +08:00
|
|
|
|
} else {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
state.updatedFields[state.nowTableName] = currentUpdatedFields
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2022-01-16 05:19:44 +08:00
|
|
|
|
}
|
2022-10-27 19:33:21 +08:00
|
|
|
|
});
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const submitUpdateFields = (dbId:any, db: string, tableName: string) => {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
let currentUpdatedFields: UpdateFieldsMeta[];
|
|
|
|
|
|
let isQuery = false;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
if (isQueryTab()) {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
isQuery = true;
|
2023-01-31 10:37:40 +08:00
|
|
|
|
currentUpdatedFields = state.queryTabs[state.activeName].updatedFields
|
2022-11-22 17:15:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
currentUpdatedFields = state.updatedFields[state.nowTableName]
|
|
|
|
|
|
}
|
|
|
|
|
|
if (currentUpdatedFields.length <= 0) {
|
|
|
|
|
|
return;
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2022-11-22 17:15:08 +08:00
|
|
|
|
let res = '';
|
|
|
|
|
|
let divs: HTMLElement[] = [];
|
|
|
|
|
|
currentUpdatedFields.forEach(a => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let sql = `UPDATE ${tableName} SET `;
|
2022-11-22 17:15:08 +08:00
|
|
|
|
let primaryKey = a.primaryKey;
|
|
|
|
|
|
let primaryKeyType = a.primaryKeyType;
|
|
|
|
|
|
let primaryKeyName = a.primaryKeyName;
|
|
|
|
|
|
a.fields.forEach(f => {
|
|
|
|
|
|
sql += ` ${f.fieldName} = ${wrapColumnValue(f.fieldType, f.newValue)},`
|
|
|
|
|
|
divs.push(f.div)
|
|
|
|
|
|
})
|
|
|
|
|
|
sql = sql.substring(0, sql.length - 1)
|
|
|
|
|
|
sql += ` WHERE ${primaryKeyName} = ${wrapColumnValue(primaryKeyType, primaryKey)} ;`
|
|
|
|
|
|
res += sql;
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
promptExeSql(dbId, db, res, () => { }, () => {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
currentUpdatedFields = [];
|
|
|
|
|
|
divs.forEach(a => {
|
|
|
|
|
|
a.classList.remove('update_field_active')
|
|
|
|
|
|
})
|
|
|
|
|
|
if (isQuery) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[state.activeName].updatedFields = []
|
|
|
|
|
|
doRunSql(dbId, db, state.queryTabs[state.activeName].sql)
|
2022-11-22 17:15:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
state.updatedFields[state.nowTableName] = []
|
2023-01-31 10:37:40 +08:00
|
|
|
|
onRefresh(dbId, db, tableName)
|
2022-11-22 17:15:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-11-22 00:34:42 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-22 15:54:11 +08:00
|
|
|
|
const cancelUpdateFields = () => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
if (isQueryTab()) {
|
|
|
|
|
|
state.queryTabs[state.activeName].updatedFields.forEach((a: any) => {
|
|
|
|
|
|
a.fields.forEach((b: any) => {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
b.div.classList.remove('update_field_active')
|
|
|
|
|
|
b.row[b.fieldName] = b.oldValue
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2023-01-31 10:37:40 +08:00
|
|
|
|
state.queryTabs[state.activeName].updatedFields = []
|
2022-11-22 17:15:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
state.updatedFields[state.nowTableName]?.forEach(a => {
|
|
|
|
|
|
a.fields.forEach(b => {
|
|
|
|
|
|
b.div.classList.remove('update_field_active')
|
|
|
|
|
|
b.row[b.fieldName] = b.oldValue
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
state.updatedFields[state.nowTableName] = []
|
|
|
|
|
|
}
|
2022-11-22 15:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 根据字段列名获取字段列信息。
|
2022-11-14 18:07:27 +08:00
|
|
|
|
* 若字段列名为空,则返回主键列,若无主键列返回第一个字段列信息(用于获取主键等)
|
2022-10-29 20:08:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
const getColumn = async (tableName: string, columnName: string = '') => {
|
|
|
|
|
|
const cols = await getColumns(tableName);
|
|
|
|
|
|
if (!columnName) {
|
2022-11-14 18:07:27 +08:00
|
|
|
|
const col = cols.find((c: any) => c.columnKey == 'PRI');
|
|
|
|
|
|
return col || cols[0];
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return cols.find((c: any) => c.columnName == columnName);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据字段信息包装字段值,如为字符串等则添加‘’
|
|
|
|
|
|
*/
|
2022-11-22 00:34:42 +08:00
|
|
|
|
const wrapColumnValue = (columnType: string, value: any) => {
|
|
|
|
|
|
if (isNumber(columnType)) {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
return `'${value}'`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断字段类型是否为数字类型
|
|
|
|
|
|
*/
|
|
|
|
|
|
const isNumber = (columnType: string) => {
|
|
|
|
|
|
return columnType.match(/int|double|float|nubmer|decimal/gi);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 弹框提示是否执行sql
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const promptExeSql = (dbId:any, db: string, sql: string, cancelFunc: any = null, successFunc: any = null) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
SqlExecBox({
|
2023-01-31 10:37:40 +08:00
|
|
|
|
sql, dbId, db,
|
2022-10-29 20:08:15 +08:00
|
|
|
|
runSuccessCallback: successFunc,
|
|
|
|
|
|
cancelCallback: cancelFunc,
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 添加新数据行
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const addRow = async (dbId:any, db: string, tableName: string) => {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
const columns = await getColumns(tableName);
|
|
|
|
|
|
// key: 字段名,value: 字段名提示
|
|
|
|
|
|
let obj: any = {};
|
|
|
|
|
|
columns.forEach((item: any) => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
obj[`${item.columnName}`] = `'${item.columnComment||''} ${item.columnName}[${item.columnType}]${item.nullable == 'YES' ? '' : '[not null]'}'`;
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
let columnNames = Object.keys(obj).join(',');
|
|
|
|
|
|
let values = Object.values(obj).join(',');
|
2023-01-31 10:37:40 +08:00
|
|
|
|
let sql = `INSERT INTO ${tableName} (${columnNames}) VALUES (${values});`;
|
|
|
|
|
|
promptExeSql(dbId, db, sql, null, () => {
|
|
|
|
|
|
onRefresh(dbId, db, tableName);
|
2022-10-29 20:08:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 格式化sql
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const formatSql = (dbId: any, db: string) => {
|
|
|
|
|
|
let monacoEditor = queryTabMonacoEditors[state.activeName]
|
2022-11-05 15:13:40 +08:00
|
|
|
|
let selection = monacoEditor.getSelection()
|
2022-11-21 19:49:50 +08:00
|
|
|
|
if (!selection) {
|
|
|
|
|
|
return;
|
2022-11-19 15:05:44 +08:00
|
|
|
|
}
|
2022-11-05 15:13:40 +08:00
|
|
|
|
let sql = monacoEditor.getModel()?.getValueInRange(selection)
|
|
|
|
|
|
// 有选中sql则格式化并替换选中sql, 否则格式化编辑器所有内容
|
|
|
|
|
|
if (sql) {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
replaceSelection(dbId, db, sqlFormatter(sql), selection)
|
2022-11-05 15:13:40 +08:00
|
|
|
|
return;
|
2022-11-01 22:18:54 +08:00
|
|
|
|
}
|
2022-11-05 15:13:40 +08:00
|
|
|
|
monacoEditor.getModel()?.setValue(sqlFormatter(monacoEditor.getValue()));
|
2022-10-29 20:08:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-05 15:13:40 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 替换选中的内容
|
|
|
|
|
|
*/
|
2023-01-31 10:37:40 +08:00
|
|
|
|
const replaceSelection = (dbId: any, db: string, str: string, selection: any) => {
|
|
|
|
|
|
let monacoEditor = queryTabMonacoEditors[state.activeName]
|
2022-11-19 15:05:44 +08:00
|
|
|
|
const model = monacoEditor.getModel();
|
2022-11-21 19:49:50 +08:00
|
|
|
|
if (!model) {
|
|
|
|
|
|
return;
|
2022-11-19 15:05:44 +08:00
|
|
|
|
}
|
2022-11-05 15:13:40 +08:00
|
|
|
|
if (!selection) {
|
2022-11-19 15:05:44 +08:00
|
|
|
|
model.setValue(str);
|
2022-11-05 15:13:40 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const { startLineNumber, endLineNumber, startColumn, endColumn } = selection
|
|
|
|
|
|
|
|
|
|
|
|
const textBeforeSelection = model.getValueInRange({
|
|
|
|
|
|
startLineNumber: 1,
|
|
|
|
|
|
startColumn: 0,
|
|
|
|
|
|
endLineNumber: startLineNumber,
|
|
|
|
|
|
endColumn: startColumn,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const textAfterSelection = model.getValueInRange({
|
|
|
|
|
|
startLineNumber: endLineNumber,
|
|
|
|
|
|
startColumn: endColumn,
|
|
|
|
|
|
endLineNumber: model.getLineCount(),
|
|
|
|
|
|
endColumn: model.getLineMaxColumn(model.getLineCount()),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
monacoEditor.setValue(textBeforeSelection + str + textAfterSelection)
|
|
|
|
|
|
monacoEditor.focus()
|
|
|
|
|
|
monacoEditor.setPosition({
|
|
|
|
|
|
lineNumber: startLineNumber,
|
|
|
|
|
|
column: 0,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 加载实例数据
|
|
|
|
|
|
const loadInstances = async () => {
|
|
|
|
|
|
const res = await dbApi.dbs.request({pageNum: 1, pageSize: 1000,})
|
|
|
|
|
|
if(!res.total) return
|
|
|
|
|
|
|
|
|
|
|
|
state.instances = {tags:{}, tree:{}, dbs:{}, tables:{}, sqls:{}} ; // 初始化变量
|
|
|
|
|
|
for (const db of res.list) {
|
|
|
|
|
|
let arr = state.instances.tree[db.tagId] || []
|
|
|
|
|
|
const {tagId, tagPath} = db
|
|
|
|
|
|
// tags
|
|
|
|
|
|
state.instances.tags[db.tagId]={tagId, tagPath}
|
|
|
|
|
|
|
|
|
|
|
|
// tree
|
|
|
|
|
|
arr.push(db)
|
|
|
|
|
|
state.instances.tree[db.tagId] = arr;
|
|
|
|
|
|
|
|
|
|
|
|
// dbs
|
|
|
|
|
|
state.instances.dbs[db.id] = db.database.split(' ')
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 加载实例对应的所有表名
|
2023-02-06 14:50:57 +08:00
|
|
|
|
const loadSchemaTables = async (inst: any, schema: string, fn: Function) => {
|
2023-01-31 10:37:40 +08:00
|
|
|
|
changeSchema(inst, schema)
|
|
|
|
|
|
let {id} = inst
|
|
|
|
|
|
let tables = state.instances.tables[id+schema];
|
|
|
|
|
|
if(!tables){
|
|
|
|
|
|
let tables = await dbApi.tableMetadata.request({ id, db: schema })
|
|
|
|
|
|
tables && tables.forEach((a:any)=>a.show=true)
|
|
|
|
|
|
state.instances.tables[id+schema] = tables;
|
|
|
|
|
|
}else {
|
|
|
|
|
|
tables.forEach((a:any)=>a.show=true)
|
|
|
|
|
|
}
|
2023-02-06 14:50:57 +08:00
|
|
|
|
fn()
|
2023-01-31 10:37:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 选择数据库实例
|
|
|
|
|
|
const changeInstance = (inst: any) => {
|
|
|
|
|
|
state.dbId = inst.id
|
|
|
|
|
|
state.dbType = inst.type
|
|
|
|
|
|
}
|
|
|
|
|
|
// 选择数据库
|
|
|
|
|
|
const changeSchema = (inst: any, schema: string) => {
|
|
|
|
|
|
changeInstance(inst)
|
|
|
|
|
|
state.db = schema
|
|
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 加载选中的表数据
|
|
|
|
|
|
const loadTableData = async (inst: any, schema: string, tableName: string) => {
|
|
|
|
|
|
changeSchema(inst, schema)
|
|
|
|
|
|
await changeTable(tableName)
|
2022-10-29 20:08:15 +08:00
|
|
|
|
}
|
2022-10-27 10:53:29 +08:00
|
|
|
|
|
2023-01-31 10:37:40 +08:00
|
|
|
|
// 新建查询panel
|
|
|
|
|
|
const addQueryTab = async () => {
|
|
|
|
|
|
let {dbId, db} = state
|
|
|
|
|
|
|
|
|
|
|
|
if(!db){
|
|
|
|
|
|
ElMessage.warning('请选择schema')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
const index = Object.keys(state.queryTabs).length;
|
|
|
|
|
|
const id = dbId + db + index;
|
|
|
|
|
|
const name = '查询-' + dbId + db + index
|
|
|
|
|
|
const queryTab = {
|
|
|
|
|
|
id: id,
|
|
|
|
|
|
sql: '',
|
|
|
|
|
|
label: '查询:' + db,
|
|
|
|
|
|
name: name,
|
|
|
|
|
|
// 点击执行按钮执行结果信息
|
|
|
|
|
|
execRes: {
|
|
|
|
|
|
total: 0,
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
showPage: false,
|
|
|
|
|
|
data: [],
|
|
|
|
|
|
tableColumn: []
|
|
|
|
|
|
},
|
|
|
|
|
|
loading: false,
|
|
|
|
|
|
nowTableName: '', //当前表格数据操作的数据库表名,用于双击编辑表内容使用
|
|
|
|
|
|
selectionDatas: [],
|
|
|
|
|
|
updatedFields: [] as UpdateFieldsMeta[],
|
|
|
|
|
|
editorId: 'MonacoTextarea-'+id,
|
|
|
|
|
|
dbId:dbId,
|
|
|
|
|
|
db:db
|
|
|
|
|
|
}
|
|
|
|
|
|
state.queryTabs[name]=queryTab
|
|
|
|
|
|
|
|
|
|
|
|
state.activeName = name;
|
|
|
|
|
|
state.activeNameMap[name] = {dbId, db}
|
|
|
|
|
|
|
|
|
|
|
|
getSqlNames(dbId, db)
|
|
|
|
|
|
|
|
|
|
|
|
await nextTick(()=>{
|
|
|
|
|
|
loadSchemaMeta(dbId, db)
|
|
|
|
|
|
setTimeout(()=>initMonacoEditor(queryTab), 500)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const instManage = {
|
|
|
|
|
|
loadSelectScheme: ()=>{
|
|
|
|
|
|
let {tagPath, dbId, db} = store.state.sqlExecInfo.dbOptInfo;
|
|
|
|
|
|
if(dbId){
|
|
|
|
|
|
state.dbId = dbId;
|
|
|
|
|
|
state.db = db;
|
|
|
|
|
|
addQueryTab()
|
|
|
|
|
|
// fixme 差展开菜单树至对应的db
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
watch(()=>store.state.sqlExecInfo.dbOptInfo, () => {
|
|
|
|
|
|
instManage.loadSelectScheme()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
2022-03-15 22:27:39 +08:00
|
|
|
|
<style lang="scss">
|
2022-11-22 00:34:42 +08:00
|
|
|
|
.sql-file-exec {
|
2022-11-22 17:15:08 +08:00
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
vertical-align: middle;
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
text-decoration: none;
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2022-11-22 17:15:08 +08:00
|
|
|
|
|
2022-04-10 10:41:14 +08:00
|
|
|
|
.sqlEditor {
|
|
|
|
|
|
font-size: 8pt;
|
2022-01-12 16:00:31 +08:00
|
|
|
|
font-weight: 600;
|
2022-04-10 10:41:14 +08:00
|
|
|
|
border: 1px solid #ccc;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
|
2022-11-01 22:18:54 +08:00
|
|
|
|
.editor-move-resize {
|
|
|
|
|
|
cursor: n-resize;
|
|
|
|
|
|
height: 3px;
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-16 19:40:20 +08:00
|
|
|
|
.el-tabs__header {
|
2022-01-19 15:10:17 +08:00
|
|
|
|
padding: 0 10px;
|
2022-01-16 19:40:20 +08:00
|
|
|
|
background-color: #fff;
|
|
|
|
|
|
}
|
2022-02-07 17:49:48 +08:00
|
|
|
|
|
|
|
|
|
|
#data-exec {
|
|
|
|
|
|
min-height: calc(100vh - 155px);
|
2023-01-31 10:37:40 +08:00
|
|
|
|
.el-tabs__header{
|
|
|
|
|
|
margin: 0 0 5px;
|
|
|
|
|
|
.el-tabs__item{
|
|
|
|
|
|
padding: 0 5px;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-02-07 17:49:48 +08:00
|
|
|
|
}
|
2022-11-22 17:15:08 +08:00
|
|
|
|
|
|
|
|
|
|
.update_field_active {
|
|
|
|
|
|
background-color: var(--el-color-success)
|
2022-11-22 00:34:42 +08:00
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
</style>
|