Skip to content

Commit 4df22c3

Browse files
authored
Merge branch 'chat2db:dev' into dev
2 parents eea4f48 + 2f4f027 commit 4df22c3

58 files changed

Lines changed: 3181 additions & 269 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

chat2db-client/.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"umijs",
7979
"USERANDPASSWORD",
8080
"uuidv",
81+
"VARCHAR",
8182
"VIEWCOLUMN",
8283
"wireframe"
8384
],

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
348348
if (editStatus !== EditColumnOperationType.Add) {
349349
editStatus = EditColumnOperationType.Modify;
350350
}
351+
const editingDataItem = {
352+
...item,
353+
[name]: value,
354+
editStatus,
355+
};
356+
351357
if (name === 'columnType') {
352358
// 根据当前字段类型,设置编辑配置
353359
databaseSupportField.columnTypes.forEach((i) => {
@@ -358,15 +364,19 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
358364
});
359365
}
360366
});
367+
// 特殊处理VARCHAR的默认长度 为255
368+
if (value === 'VARCHAR' && editingDataItem.columnSize === null) {
369+
editingDataItem.columnSize = 255;
370+
form.setFieldsValue({
371+
columnSize: 255,
372+
});
373+
}
361374
}
362-
return {
363-
...item,
364-
[name]: value,
365-
editStatus,
366-
};
375+
return editingDataItem;
367376
}
368377
return item;
369378
});
379+
console.log(newData);
370380
setDataSource(newData);
371381
};
372382

@@ -417,7 +427,7 @@ const ColumnList = forwardRef((props: IProps, ref: ForwardedRef<IColumnListRef>)
417427

418428
const renderOtherInfoForm = () => {
419429
const labelCol = {
420-
style: { width: 90 },
430+
style: { width: 100 },
421431
};
422432

423433
return (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import '../../../styles/var.less';
2+
3+
.realTimeSQL {
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { memo } from 'react';
2+
import styles from './index.less';
3+
import classnames from 'classnames';
4+
5+
interface IProps {
6+
className?: string;
7+
}
8+
9+
export default memo<IProps>((props) => {
10+
const { className } = props;
11+
return (
12+
<div className={classnames(styles.realTimeSQL, className)}>
13+
<div className={styles.title}>实时 SQL</div>
14+
<div className={styles.content}>实时 SQL</div>
15+
</div>
16+
);
17+
});

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,91 @@
7979
opacity: 0;
8080
}
8181

82-
.monacoEditor {
82+
.monacoEditorModal {
83+
display: flex;
8384
height: 400px;
85+
border: 1px solid var(--color-border);
86+
border-radius: 4px;
87+
88+
.monacoEditorHeader {
89+
height: 38px;
90+
padding: 0px 10px;
91+
display: flex;
92+
justify-content: space-between;
93+
align-items: center;
94+
border-bottom: 1px solid var(--color-border);
95+
.formatButton {
96+
border-radius: 3px;
97+
background-color: var(--color-fill-quaternary);
98+
height: 24px;
99+
padding: 0px 7px;
100+
cursor: pointer;
101+
i {
102+
margin-right: 6px;
103+
}
104+
&:hover {
105+
color: var(--color-primary);
106+
}
107+
}
108+
.executeButton {
109+
height: 24px;
110+
line-height: 24px;
111+
display: flex;
112+
justify-content: center;
113+
align-items: center;
114+
i {
115+
margin-right: 4px;
116+
}
117+
}
118+
}
119+
.monacoEditorContent {
120+
width: 0px;
121+
flex: 1;
122+
display: flex;
123+
flex-direction: column;
124+
padding: 0px -6px;
125+
border-right: 1px solid var(--color-border);
126+
.monacoEditor {
127+
flex: 1;
128+
margin: 0px 6px;
129+
}
130+
}
131+
.result {
132+
width: 0px;
133+
flex: 1;
134+
display: flex;
135+
flex-direction: column;
136+
align-items: center;
137+
justify-content: center;
138+
.resultHeader {
139+
width: 100%;
140+
line-height: 38px;
141+
text-align: center;
142+
border-bottom: 1px solid var(--color-border);
143+
}
144+
.resultContent {
145+
flex: 1;
146+
padding: 0px 10px;
147+
overflow-y: auto;
148+
.errorTitle {
149+
display: flex;
150+
align-items: center;
151+
padding: 4px 10px;
152+
// background-color: var(--color-bg-subtle);
153+
border-radius: 8px;
154+
margin-top: 10px;
155+
i {
156+
color: var(--color-error);
157+
margin-right: 4px;
158+
}
159+
}
160+
161+
.errorMessage {
162+
margin: 10px 0px;
163+
padding: 4px 10px;
164+
background-color: var(--color-bg-subtle);
165+
border-radius: 8px;
166+
}
167+
}
168+
}
84169
}

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

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ import IndexList, { IIndexListRef } from './IndexList';
66
import ColumnList, { IColumnListRef } from './ColumnList';
77
import BaseInfo, { IBaseInfoRef } from './BaseInfo';
88
import sqlService, { IModifyTableSqlParams, IExecuteSqlParams } from '@/service/sql';
9-
import MonacoEditor from '@/components/Console/MonacoEditor';
10-
import { IEditTableInfo } from '@/typings';
9+
import MonacoEditor, { IExportRefFunction } from '@/components/Console/MonacoEditor';
10+
import { IEditTableInfo, IWorkspaceTab } from '@/typings';
11+
import { DatabaseTypeCode } from '@/constants';
1112
import i18n from '@/i18n';
1213
import lodash from 'lodash';
14+
import Iconfont from '@/components/Iconfont';
15+
import { formatSql } from '@/utils';
1316

1417
interface IProps {
1518
dataSourceId: number;
1619
databaseName: string;
1720
schemaName: string | undefined;
1821
tableName?: string;
22+
databaseType: DatabaseTypeCode;
23+
changeTabDetails: (data: IWorkspaceTab) => void;
24+
tabDetails: IWorkspaceTab;
1925
}
2026

2127
interface ITabItem {
@@ -35,14 +41,17 @@ interface IContext extends IProps {
3541
export const Context = createContext<IContext>({} as any);
3642

3743
export default memo((props: IProps) => {
38-
const { databaseName, dataSourceId, tableName, schemaName } = props;
44+
const { databaseName, dataSourceId, tableName, schemaName, changeTabDetails, tabDetails, databaseType } = props;
3945
const [tableDetails, setTableDetails] = useState<IEditTableInfo>({} as any);
4046
const [oldTableDetails, setOldTableDetails] = useState<IEditTableInfo>({} as any);
41-
const [viewSqlModal, setViewSqlModal] = useState<string | false>(false);
42-
const [newTableName, setNewTableName] = useState<string | null | undefined>(tableName);
47+
const [viewSqlModal, setViewSqlModal] = useState<boolean>(false);
4348
const baseInfoRef = useRef<IBaseInfoRef>(null);
4449
const columnListRef = useRef<IColumnListRef>(null);
4550
const indexListRef = useRef<IIndexListRef>(null);
51+
const monacoEditorRef = useRef<IExportRefFunction>(null);
52+
const [executeSqlResult, setExecuteSqlResult] = useState<string | null>(null);
53+
const [executeLoading, setExecuteLoading] = useState<boolean>(false);
54+
const [appendValue, setAppendValue] = useState<string>('');
4655
const tabList = useMemo(() => {
4756
return [
4857
{
@@ -71,18 +80,24 @@ export default memo((props: IProps) => {
7180
setCurrentTab(item);
7281
}
7382

83+
useEffect(() => {
84+
if (!viewSqlModal) {
85+
setExecuteSqlResult(null);
86+
}
87+
}, [viewSqlModal]);
88+
7489
useEffect(() => {
7590
if (tableName) {
76-
getTableDetails();
91+
getTableDetails({});
7792
}
7893
}, []);
7994

80-
const getTableDetails = () => {
81-
if (!newTableName) return;
95+
const getTableDetails = ({ tableNameProps }: { tableNameProps?: string }) => {
96+
if (!tableName) return;
8297
const params = {
8398
databaseName,
8499
dataSourceId,
85-
tableName: newTableName,
100+
tableName: tableNameProps || tableName,
86101
schemaName,
87102
refresh: true,
88103
};
@@ -110,29 +125,71 @@ export default memo((props: IProps) => {
110125
newTable,
111126
};
112127

113-
if (newTableName) {
128+
if (tableName) {
114129
// params.tableName = tableName;
115130
params.oldTable = oldTableDetails;
116131
}
117132
sqlService.getModifyTableSql(params).then((res) => {
118-
setNewTableName(newTable.name);
119-
setViewSqlModal(res?.[0].sql);
133+
setViewSqlModal(true);
134+
setAppendValue(res?.[0].sql);
120135
});
121136
}
122137
}
123138

124139
const executeSql = () => {
125140
const executeSQLParams: IExecuteSqlParams = {
126-
sql: viewSqlModal || '',
141+
sql: monacoEditorRef.current?.getAllContent() || '',
127142
dataSourceId,
128143
databaseName,
129144
schemaName,
130145
};
146+
setExecuteLoading(true);
147+
sqlService
148+
.executeDDL(executeSQLParams)
149+
.then((res) => {
150+
if (res.success) {
151+
setViewSqlModal(false);
152+
message.success(i18n('common.text.successfulExecution'));
153+
const newTableName = baseInfoRef.current?.getBaseInfo().name;
154+
getTableDetails({ tableNameProps: newTableName });
155+
if (!tableName) {
156+
changeTabDetails({
157+
...tabDetails,
158+
title: `edit-${newTableName}`,
159+
uniqueData: {
160+
...(tabDetails.uniqueData || {}),
161+
tableName: newTableName,
162+
},
163+
});
164+
}
165+
} else {
166+
setExecuteSqlResult(res.message);
167+
}
168+
})
169+
.finally(() => {
170+
setExecuteLoading(false);
171+
});
172+
};
131173

132-
sqlService.executeSql(executeSQLParams).then(() => {
133-
message.success(i18n('common.text.successfulExecution'));
134-
getTableDetails();
135-
setViewSqlModal(false);
174+
//
175+
const renderMonacoEditor = useMemo(() => {
176+
return (
177+
<MonacoEditor
178+
className={styles.monacoEditor}
179+
id="view_sql"
180+
ref={monacoEditorRef}
181+
appendValue={{
182+
text: appendValue,
183+
range: 'reset',
184+
}}
185+
/>
186+
);
187+
}, [appendValue]);
188+
189+
const handleFormatSql = () => {
190+
const sql = monacoEditorRef.current?.getAllContent() || '';
191+
formatSql(sql, databaseType).then((res) => {
192+
setAppendValue(res);
136193
});
137194
};
138195

@@ -183,19 +240,32 @@ export default memo((props: IProps) => {
183240
onCancel={() => {
184241
setViewSqlModal(false);
185242
}}
186-
okText="执行"
187-
onOk={executeSql}
188243
width="60vw"
189244
maskClosable={false}
245+
footer={false}
190246
>
191-
<div className={styles.monacoEditor}>
192-
<MonacoEditor
193-
id="view_sql"
194-
appendValue={{
195-
text: viewSqlModal,
196-
range: 'reset',
197-
}}
198-
/>
247+
<div className={styles.monacoEditorModal}>
248+
<div className={styles.monacoEditorContent}>
249+
<div className={styles.monacoEditorHeader}>
250+
<div className={styles.formatButton} onClick={handleFormatSql}>
251+
<Iconfont code="&#xe64f;" />
252+
{i18n('common.button.format')}
253+
</div>
254+
<Button className={styles.executeButton} type="primary" onClick={executeSql} loading={executeLoading}>
255+
<Iconfont code="&#xe656;" />
256+
{i18n('common.button.execute')}
257+
</Button>
258+
</div>
259+
{renderMonacoEditor}
260+
</div>
261+
{executeSqlResult && (
262+
<div className={styles.result}>
263+
<div className={styles.resultHeader}>{i18n('common.text.errorMessage')}</div>
264+
<div className={styles.resultContent}>
265+
<div className={styles.errorMessage}>{executeSqlResult}</div>
266+
</div>
267+
</div>
268+
)}
199269
</div>
200270
</Modal>
201271
</Context.Provider>

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_m8g5g53rp.woff2?t=1695636919643') format('woff2'),
13-
url('//at.alicdn.com/t/c/font_3633546_m8g5g53rp.woff?t=1695636919643') format('woff'),
14-
url('//at.alicdn.com/t/c/font_3633546_m8g5g53rp.ttf?t=1695636919643') format('truetype');
12+
src: url('//at.alicdn.com/t/c/font_3633546_rlj1v1fcjnk.woff2?t=1695716733221') format('woff2'),
13+
url('//at.alicdn.com/t/c/font_3633546_rlj1v1fcjnk.woff?t=1695716733221') format('woff'),
14+
url('//at.alicdn.com/t/c/font_3633546_rlj1v1fcjnk.ttf?t=1695716733221') format('truetype');
1515
}
1616
`;
1717
const style = document.createElement('style');

chat2db-client/src/i18n/en-us/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ export default {
8686
'common.tips.createSuccess': 'Create Successfully',
8787
'common.text.action': 'Action',
8888
'common.button.add': 'Add',
89+
'common.text.errorMessage': 'Error Message',
8990

9091
};

chat2db-client/src/i18n/zh-cn/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ export default {
8484
'common.tips.createSuccess': '创建成功',
8585
'common.text.action': '操作',
8686
'common.button.add': '添加',
87+
'common.text.errorMessage': '错误信息',
8788

8889
};

0 commit comments

Comments
 (0)