Skip to content

Commit 56998f9

Browse files
committed
fix:edit table execute sql
1 parent 43b3b56 commit 56998f9

11 files changed

Lines changed: 82 additions & 22 deletions

File tree

chat2db-client/src/blocks/DatabaseTableEditor/ColumnList/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
325325
if (editStatus !== EditColumnOperationType.Add) {
326326
editStatus = EditColumnOperationType.Modify;
327327
}
328+
if (name === 'columnType') {
329+
// 根据当前字段类型,设置编辑配置
330+
databaseSupportField.columnTypes.forEach((i) => {
331+
if (i.typeName === value) {
332+
setEditingConfig({
333+
...editingConfig!,
334+
...i,
335+
});
336+
}
337+
});
338+
}
328339
return {
329340
...item,
330341
[name]: value,

chat2db-client/src/blocks/DatabaseTableEditor/IndexList/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const createInitialData = (): IIndexItem => {
3939
columnList: [],
4040
name: '',
4141
type: null,
42-
columns: null,
4342
comment: null,
4443
editStatus: EditColumnOperationType.Add,
4544
};

chat2db-client/src/blocks/DatabaseTableEditor/index.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@
6363
z-index: 1;
6464
opacity: 0;
6565
}
66+
67+
.monacoEditor {
68+
height: 400px;
69+
}

chat2db-client/src/blocks/DatabaseTableEditor/index.tsx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { memo, useRef, useState, createContext, useEffect, useMemo } from 'react';
2-
import { Button } from 'antd';
2+
import { Button, Modal, message } from 'antd';
33
import styles from './index.less';
44
import classnames from 'classnames';
55
import IndexList, { IIndexListRef } from './IndexList';
66
import ColumnList, { IColumnListRef } from './ColumnList';
77
import BaseInfo, { IBaseInfoRef } from './BaseInfo';
8-
import sqlService, { IModifyTableSqlParams } from '@/service/sql';
8+
import sqlService, { IModifyTableSqlParams, IExecuteSqlParams } from '@/service/sql';
9+
import MonacoEditor from '@/components/Console/MonacoEditor';
910
import { IEditTableInfo } from '@/typings';
1011
import i18n from '@/i18n';
1112
import lodash from 'lodash';
@@ -36,6 +37,7 @@ export default memo((props: IProps) => {
3637
const { databaseName, dataSourceId, tableName, schemaName } = props;
3738
const [tableDetails, setTableDetails] = useState<IEditTableInfo>({} as any);
3839
const [oldTableDetails, setOldTableDetails] = useState<IEditTableInfo>({} as any);
40+
const [viewSqlModal, setViewSqlModal] = useState<string | false>(false);
3941
const baseInfoRef = useRef<IBaseInfoRef>(null);
4042
const columnListRef = useRef<IColumnListRef>(null);
4143
const indexListRef = useRef<IIndexListRef>(null);
@@ -114,14 +116,27 @@ export default memo((props: IProps) => {
114116
params.tableName = tableName;
115117
params.oldTable = oldTableDetails;
116118
}
117-
console.log(newTable);
118-
console.log(params.oldTable);
119119
sqlService.getModifyTableSql(params).then((res) => {
120-
console.log(res);
120+
setViewSqlModal(res?.[0].sql);
121121
});
122122
}
123123
}
124124

125+
const executeSql = () => {
126+
const executeSQLParams: IExecuteSqlParams = {
127+
sql: viewSqlModal || '',
128+
dataSourceId,
129+
databaseName,
130+
schemaName,
131+
};
132+
133+
// 获取当前SQL的查询结果
134+
sqlService.executeSql(executeSQLParams).then((res) => {
135+
message.success('修改成功');
136+
setViewSqlModal(false);
137+
});
138+
};
139+
125140
return (
126141
<Context.Provider
127142
value={{
@@ -163,6 +178,27 @@ export default memo((props: IProps) => {
163178
})}
164179
</div>
165180
</div>
181+
<Modal
182+
title="sql预览"
183+
open={!!viewSqlModal}
184+
onCancel={() => {
185+
setViewSqlModal(false);
186+
}}
187+
okText="执行"
188+
onOk={executeSql}
189+
width="60vw"
190+
maskClosable={false}
191+
>
192+
<div className={styles.monacoEditor}>
193+
<MonacoEditor
194+
id="view_sql"
195+
appendValue={{
196+
text: viewSqlModal,
197+
range: 'reset',
198+
}}
199+
/>
200+
</div>
201+
</Modal>
166202
</Context.Provider>
167203
);
168204
});

chat2db-client/src/blocks/SQLExecute/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import sql from '@/service/sql';
1616
import { isNumber } from 'lodash';
1717
import { ExportSizeEnum, ExportTypeEnum } from '@/typings/resultTable';
1818
import { downloadFile } from '@/utils/common';
19-
import { useUpdateEffect } from '@/hooks/useUpdateEffect'
19+
import { useUpdateEffect } from '@/hooks/useUpdateEffect';
2020
interface IProps {
2121
className?: string;
2222
isActive: boolean;
@@ -41,7 +41,7 @@ const defaultResultConfig: IResultConfig = {
4141
hasNextPage: true,
4242
};
4343

44-
const SQLExecute = memo<IProps>(function (props) {
44+
const SQLExecute = memo<IProps>((props) => {
4545
const { data, workspaceModel, aiModel, isActive, dispatch } = props;
4646
const draggableRef = useRef<any>();
4747
const [appendValue, setAppendValue] = useState<IAppendValue>();
@@ -71,7 +71,7 @@ const SQLExecute = memo<IProps>(function (props) {
7171

7272
useUpdateEffect(() => {
7373
setAppendValue({ text: data.initDDL });
74-
}, [data.initDDL])
74+
}, [data.initDDL]);
7575

7676
/**
7777
* 执行SQL

chat2db-client/src/components/Console/MonacoEditor/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
148148
useEffect(() => {
149149
if (options?.theme) {
150150
monaco.editor.setTheme(options.theme);
151-
return
151+
return;
152152
}
153153
monaco.editor.setTheme(appTheme.backgroundColor);
154154
}, [appTheme.backgroundColor, options?.theme]);
@@ -327,7 +327,12 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
327327
case 'select':
328328
const selection = editor.getSelection();
329329
if (selection) {
330-
newRange = new monaco.Range(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
330+
newRange = new monaco.Range(
331+
selection.startLineNumber,
332+
selection.startColumn,
333+
selection.endLineNumber,
334+
selection.endColumn,
335+
);
331336
}
332337
break;
333338
// 在末尾添加内容

chat2db-client/src/components/Iconfont/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ if (__ENV__ === 'local') {
99
/* 在线链接服务仅供平台体验和调试使用,平台不承诺服务的稳定性,企业客户需下载字体包自行发布使用并做好备份。 */
1010
@font-face {
1111
font-family: 'iconfont'; /* Project id 3633546 */
12-
src: url('//at.alicdn.com/t/c/font_3633546_xr1m2ndzj4j.woff2?t=1695475278282') format('woff2'),
13-
url('//at.alicdn.com/t/c/font_3633546_xr1m2ndzj4j.woff?t=1695475278282') format('woff'),
14-
url('//at.alicdn.com/t/c/font_3633546_xr1m2ndzj4j.ttf?t=1695475278282') format('truetype');
12+
src: url('//at.alicdn.com/t/c/font_3633546_85ed0z3ydn4.woff2?t=1695519893270') format('woff2'),
13+
url('//at.alicdn.com/t/c/font_3633546_85ed0z3ydn4.woff?t=1695519893270') format('woff'),
14+
url('//at.alicdn.com/t/c/font_3633546_85ed0z3ydn4.ttf?t=1695519893270') format('truetype');
1515
}
1616
`;
1717
const style = document.createElement('style');

chat2db-client/src/pages/main/workspace/components/TableList/index.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
}
8282
}
8383

84+
.importTableOperationItem {
85+
.operationIcon {
86+
font-size: 16px;
87+
}
88+
}
89+
8490
:global {
8591
.ant-dropdown-menu-submenu-title {
8692
display: flex;

chat2db-client/src/pages/main/workspace/components/TableList/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const TableList = dvaModel((props: any) => {
6565
{
6666
label: (
6767
<div className={styles.operationItem}>
68-
<Iconfont className={styles.operationIcon} code="&#xe6b6;" />
68+
<Iconfont className={styles.operationIcon} code="&#xe792;" />
6969
<div className={styles.operationTitle}>{i18n('editTable.button.createTable')}</div>
7070
</div>
7171
),
@@ -93,7 +93,7 @@ const TableList = dvaModel((props: any) => {
9393
children: [
9494
{
9595
label: (
96-
<div className={styles.operationItem}>
96+
<div className={classnames(styles.operationItem, styles.importTableOperationItem)}>
9797
<Iconfont className={styles.operationIcon} code="&#xe7ba;" />
9898
<div className={styles.operationTitle}>{i18n('common.button.exportWord')}</div>
9999
</div>
@@ -105,7 +105,7 @@ const TableList = dvaModel((props: any) => {
105105
},
106106
{
107107
label: (
108-
<div className={styles.operationItem}>
108+
<div className={classnames(styles.operationItem, styles.importTableOperationItem)}>
109109
<Iconfont className={styles.operationIcon} code="&#xe7b7;" />
110110
<div className={styles.operationTitle}>{i18n('common.button.exportExcel')}</div>
111111
</div>
@@ -117,7 +117,7 @@ const TableList = dvaModel((props: any) => {
117117
},
118118
{
119119
label: (
120-
<div className={styles.operationItem}>
120+
<div className={classnames(styles.operationItem, styles.importTableOperationItem)}>
121121
<Iconfont className={styles.operationIcon} code="&#xe87d;" />
122122
<div className={styles.operationTitle}>{i18n('common.button.exportHtml')}</div>
123123
</div>
@@ -129,7 +129,7 @@ const TableList = dvaModel((props: any) => {
129129
},
130130
{
131131
label: (
132-
<div className={styles.operationItem}>
132+
<div className={classnames(styles.operationItem, styles.importTableOperationItem)}>
133133
<Iconfont className={styles.operationIcon} code="&#xe7b8;" />
134134
<div className={styles.operationTitle}>{i18n('common.button.exportMarkdown')}</div>
135135
</div>
@@ -141,7 +141,7 @@ const TableList = dvaModel((props: any) => {
141141
},
142142
{
143143
label: (
144-
<div className={styles.operationItem}>
144+
<div className={classnames(styles.operationItem, styles.importTableOperationItem)}>
145145
<Iconfont className={styles.operationIcon} code="&#xe67a;" />
146146
<div className={styles.operationTitle}>{i18n('common.button.exportPdf')}</div>
147147
</div>

chat2db-client/src/service/sql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export interface IModifyTableSqlParams {
205205
}
206206

207207
/** 获取修改表的sql */
208-
const getModifyTableSql = createRequest<IModifyTableSqlParams, string>('/api/rdb/table/modify/sql', { method: 'post' });
208+
const getModifyTableSql = createRequest<IModifyTableSqlParams, {sql:string}[]>('/api/rdb/table/modify/sql', { method: 'post' });
209209

210210
export default {
211211
getModifyTableSql,

0 commit comments

Comments
 (0)