Skip to content

Commit 311b97f

Browse files
committed
feat: Added the function of importing SQL messages
1 parent ce51196 commit 311b97f

8 files changed

Lines changed: 112 additions & 11 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import configService from '@/service/config';
2121
// import NewEditor from './NewMonacoEditor';
2222
import styles from './index.less';
2323
import indexedDB from '@/indexedDB';
24+
import ImportBlock from '../ImportBlock';
2425

2526
enum IPromptType {
2627
NL_2_SQL = 'NL_2_SQL',
@@ -410,6 +411,8 @@ function Console(props: IProps) {
410411
setPopularizeModal(true);
411412
};
412413

414+
const handleImportSQL = () => {};
415+
413416
/**
414417
* 格式化sql
415418
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from 'react';
2+
import { Modal, Upload, Button } from 'antd';
3+
import { UploadOutlined } from '@ant-design/icons';
4+
import i18n from '@/i18n';
5+
6+
interface IImportBlockProps {
7+
children: React.ReactNode;
8+
title?: string;
9+
accept: string;
10+
maxCount?: number;
11+
onConfirm?: (fileList: Array<File> | File) => Promise<boolean>;
12+
}
13+
14+
function ImportBlock(props: IImportBlockProps) {
15+
const { title, children, accept, maxCount = 1 } = props;
16+
17+
const [open, setOpen] = React.useState(false);
18+
const [selectedFile, setSelectedFile] = React.useState<Array<File> | null>(null);
19+
20+
const onClose = () => {
21+
setOpen(false);
22+
};
23+
24+
const onOk = async () => {
25+
if (!selectedFile) return;
26+
27+
if (props.onConfirm) {
28+
const success = await props.onConfirm(maxCount === 1 ? selectedFile?.[0] : selectedFile);
29+
if (success) {
30+
setOpen(false);
31+
}
32+
}
33+
};
34+
35+
const handleBeforeUpload = (file: File, FileList: File[]) => {
36+
setSelectedFile(FileList);
37+
return false; // 停止自动上传
38+
};
39+
40+
return (
41+
<div
42+
onClick={() => {
43+
setOpen(true);
44+
}}
45+
>
46+
{children}
47+
48+
<Modal title={title} open={open} onCancel={onClose} onOk={onOk}>
49+
<Upload
50+
name="file"
51+
accept={accept}
52+
beforeUpload={handleBeforeUpload}
53+
maxCount={maxCount}
54+
fileList={selectedFile ? [...selectedFile] : null}
55+
>
56+
<Button icon={<UploadOutlined />}>{i18n('common.text.selectFile')}</Button>
57+
</Upload>
58+
</Modal>
59+
</div>
60+
);
61+
}
62+
63+
export default ImportBlock;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect, useState } from 'react';
22
import { Modal, Upload, Button, message } from 'antd';
33
import { UploadOutlined } from '@ant-design/icons';
4+
import i18n from '@/i18n';
45

56
const uploadFileType = {
67
ncx: {
@@ -77,10 +78,9 @@ const ImportConnection: React.FC<IImportConnectionProps> = ({ open, onClose, onC
7778
accept=".ncx,.dbp"
7879
beforeUpload={handleBeforeUpload}
7980
maxCount={1}
80-
8181
fileList={selectedFile ? [selectedFile] : []}
8282
>
83-
<Button icon={<UploadOutlined />}>Select File</Button>
83+
<Button icon={<UploadOutlined />}>{i18n('common.text.selectFile')}</Button>
8484
</Upload>
8585
</Modal>
8686
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
'common.button.open': 'Open',
2626
'common.button.refresh': 'Refresh',
2727
'common.button.execute': 'Run',
28+
"common.button.import": 'Import SQL',
2829
'common.button.format': 'Format',
2930
'common.message.successfulConfig': 'Successful configuration',
3031
'common.text.successful': 'successful',
@@ -89,5 +90,5 @@ export default {
8990
'common.text.errorMessage': 'Error Message',
9091
'common.button.cancelRequest': 'Cancel Request',
9192
'common.button.executionError': 'Execution Error',
92-
93+
'common.text.selectFile' : 'Select File'
9394
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
'common.button.open': '打开',
2626
'common.button.refresh': '刷新',
2727
'common.button.execute': '执行',
28+
"common.button.import": '导入SQL',
2829
'common.button.format': '格式化',
2930
'common.message.successfulConfig': '配置成功',
3031
'common.text.successful': '成功',
@@ -87,5 +88,5 @@ export default {
8788
'common.text.errorMessage': '错误信息',
8889
'common.button.cancelRequest': '取消请求',
8990
'common.button.executionError': '执行错误',
90-
91+
'common.text.selectFile' : '选择文件'
9192
};

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import { approximateTreeNode } from '@/utils';
1414
import { useUpdateEffect } from '@/hooks/useUpdateEffect';
1515
import { v4 as uuidV4 } from 'uuid';
1616
import { IPagingData, ITreeNode } from '@/typings';
17-
import styles from './index.less';
1817
import { ExportTypeEnum } from '@/typings/resultTable';
1918
import historyService from '@/service/history';
2019
import { debounce } from 'lodash';
2120
import { dataSourceFormConfigs } from '@/components/ConnectionEdit/config/dataSource';
21+
import styles from './index.less';
22+
import ImportBlock from '@/components/ImportBlock';
2223

2324
interface IOption {
2425
value: TreeNodeType;
@@ -87,6 +88,36 @@ const TableList = dvaModel((props: any) => {
8788
addConsole();
8889
},
8990
},
91+
{
92+
label: (
93+
<ImportBlock
94+
title={i18n('common.button.import')}
95+
accept={'.sql'}
96+
onConfirm={async (file) => {
97+
if (Array.isArray(file)) return Promise.resolve(false);
98+
99+
const reader = new FileReader();
100+
101+
reader.onload = function (event) {
102+
const sqlContent = event.target?.result ?? '';
103+
addConsole(sqlContent);
104+
};
105+
106+
reader.readAsText(file);
107+
return Promise.resolve(true);
108+
}}
109+
>
110+
<div className={styles.operationItem}>
111+
<Iconfont className={styles.operationIcon} code="&#xe66c;" />
112+
<div className={styles.operationTitle}>{i18n('common.button.import')}</div>
113+
</div>
114+
</ImportBlock>
115+
),
116+
key: 'importSQL',
117+
onClick: () => {
118+
// addConsole();
119+
},
120+
},
90121
{
91122
label: (
92123
<div className={styles.operationItem}>
@@ -238,11 +269,11 @@ const TableList = dvaModel((props: any) => {
238269
};
239270
}, [treeBoxRef.current, leftModuleTitleRef.current]);
240271

241-
const addConsole = () => {
272+
const addConsole = (ddl?: string) => {
242273
const { dataSourceId, databaseName, schemaName, databaseType } = curWorkspaceParams;
243274
const params = {
244275
name: `new console`,
245-
ddl: '',
276+
ddl: ddl || '',
246277
dataSourceId: dataSourceId!,
247278
databaseName: databaseName!,
248279
schemaName: schemaName!,
@@ -326,7 +357,7 @@ const TableList = dvaModel((props: any) => {
326357
setCurType(selectedOptions[0]);
327358
}
328359

329-
const handelChangePagination = (pageNo: number) => {
360+
const handleChangePagination = (pageNo: number) => {
330361
setPagingData({
331362
...pagingData,
332363
pageNo,
@@ -405,16 +436,18 @@ const TableList = dvaModel((props: any) => {
405436
</div>
406437
)}
407438
</div>
439+
408440
<div ref={treeBoxRef} className={styles.treeBox}>
409441
<LoadingContent isLoading={tableLoading}>
410442
<Tree initialData={searchedTableList || curList} />
411443
</LoadingContent>
412444
</div>
445+
413446
{pagingData?.total > 100 && !searchKey && (
414447
<div className={styles.paging}>
415448
<div className={styles.paginationBox}>
416449
<Pagination
417-
onChange={handelChangePagination}
450+
onChange={handleChangePagination}
418451
current={pagingData?.pageNo}
419452
pageSize={pagingData?.pageSize}
420453
simple

chat2db-client/src/utils/IntelliSense/field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const registerIntelliSenseField = (tableList: string[], dataSourceId, databaseNa
4848
intelliSenseField.dispose();
4949
fieldList = {};
5050
intelliSenseField = monaco.languages.registerCompletionItemProvider('sql', {
51-
triggerCharacters: [' ', '.', '('],
51+
triggerCharacters: [' ', ',','.', '('],
5252
provideCompletionItems: async (model, position) => {
5353
// 获取到当前行文本
5454
const textUntilPosition = model.getValueInRange({

chat2db-client/src/utils/IntelliSense/table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const registerIntelliSenseTable = (
5555

5656
intelliSenseTable.dispose();
5757
intelliSenseTable = monaco.languages.registerCompletionItemProvider('sql', {
58-
triggerCharacters: [' '],
58+
triggerCharacters: [' ', ],
5959
provideCompletionItems: (model, position) => {
6060
const lineContentUntilPosition = model.getValueInRange({
6161
startLineNumber: position.lineNumber,

0 commit comments

Comments
 (0)