Skip to content

Commit a0a070c

Browse files
committed
sql格式化
1 parent 0f50ce7 commit a0a070c

6 files changed

Lines changed: 79 additions & 41 deletions

File tree

chat2db-client/.vscode/settings.json

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,53 @@
1919
},
2020
"git.mergeEditor": false,
2121
"cSpell.words": [
22-
"AZUREAI",
23-
"Appstore",
24-
"CLICKHOUSE",
25-
"Cascader",
26-
"Consolas",
27-
"DBAI",
28-
"Dmaven",
29-
"Iconfont",
30-
"JDBC",
31-
"KEYPAIR",
32-
"KINGBASE",
33-
"MARIADB",
34-
"Mddhhmmss",
35-
"Menlo",
36-
"OCEANBASE",
37-
"OPENAI",
38-
"POSTGRESQL",
39-
"RESTAI",
40-
"SQLSERVER",
41-
"Sercurity",
42-
"USERANDPASSWORD",
43-
"VIEWCOLUMN",
4422
"ahooks",
4523
"antd",
24+
"Appstore",
25+
"AZUREAI",
4626
"bgcolor",
27+
"Cascader",
4728
"chatgpt",
29+
"CLICKHOUSE",
30+
"Consolas",
4831
"datas",
4932
"datasource",
33+
"DBAI",
5034
"dbhub",
35+
"Dmaven",
5136
"echarts",
5237
"favicons",
5338
"findstr",
5439
"gtag",
40+
"Iconfont",
5541
"indexs",
42+
"JDBC",
43+
"KEYPAIR",
44+
"KINGBASE",
5645
"lsof",
46+
"MARIADB",
47+
"Mddhhmmss",
48+
"Menlo",
5749
"netstat",
50+
"OCEANBASE",
51+
"OPENAI",
5852
"pgsql",
5953
"pnpm",
54+
"POSTGRESQL",
6055
"remaininguses",
56+
"RESTAI",
6157
"scrollbar",
58+
"Sercurity",
6259
"sortablejs",
60+
"SQLSERVER",
61+
"Tigger",
6362
"ueabe",
6463
"ueabf",
6564
"ueac",
6665
"umijs",
66+
"USERANDPASSWORD",
6767
"uuidv",
68+
"VIEWCOLUMN",
6869
"wireframe"
6970
],
7071
"typescript.tsdk": "/Users/wangjiaqi/Desktop/Chat2DB/chat2db-client/node_modules/typescript/lib"

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
149149
if (options?.theme) {
150150
monaco.editor.setTheme(options.theme);
151151
return
152-
}
152+
}
153153
monaco.editor.setTheme(appTheme.backgroundColor);
154154
}, [appTheme.backgroundColor, options?.theme]);
155155

@@ -315,12 +315,22 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
315315
return;
316316
}
317317
switch (range) {
318+
// 覆盖所有内容
318319
case 'cover':
319320
newRange = model.getFullModelRange();
320321
break;
322+
// 在开头添加内容
321323
case 'front':
322324
newRange = new monaco.Range(1, 1, 1, 1);
323325
break;
326+
// 格式化选中区域的sql
327+
case 'select':
328+
const selection = editor.getSelection();
329+
if (selection) {
330+
newRange = new monaco.Range(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
331+
}
332+
break;
333+
// 在末尾添加内容
324334
case 'end':
325335
const lastLine = editor.getModel().getLineCount();
326336
const lastLineLength = editor.getModel().getLineMaxColumn(lastLine);

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import connectToEventSource from '@/utils/eventSource';
55
import { Button, Spin, message, Drawer, Modal } from 'antd';
66
import ChatInput from './ChatInput';
77
import Editor, { IEditorOptions, IExportRefFunction, IRangeType } from './MonacoEditor';
8-
import { format } from 'sql-formatter';
98
import historyServer from '@/service/history';
109
import aiServer from '@/service/ai';
1110
import { v4 as uuidv4 } from 'uuid';
@@ -14,7 +13,7 @@ import Iconfont from '../Iconfont';
1413
import { IAiConfig, ITreeNode } from '@/typings';
1514
import { IAIState } from '@/models/ai';
1615
import Popularize from '@/components/Popularize';
17-
import { handleLocalStorageSavedConsole, readLocalStorageSavedConsoleText } from '@/utils';
16+
import { handleLocalStorageSavedConsole, readLocalStorageSavedConsoleText, formatSql } from '@/utils';
1817
import { chatErrorForKey, chatErrorToLogin } from '@/constants/chat';
1918
import { AiSqlSourceType } from '@/typings/ai';
2019
import i18n from '@/i18n';
@@ -162,7 +161,7 @@ function Console(props: IProps) {
162161
function timingAutoSave() {
163162
timerRef.current = setInterval(() => {
164163
handleLocalStorageSavedConsole(executeParams.consoleId!, 'save', editorRef?.current?.getAllContent());
165-
}, 5000);
164+
}, 500);
166165
}
167166

168167
const tableListName = useMemo(() => {
@@ -213,7 +212,7 @@ function Console(props: IProps) {
213212
}
214213
}, 3000);
215214
}
216-
}catch (e) {
215+
} catch (e) {
217216
setIsLoading(false);
218217
}
219218

@@ -471,17 +470,11 @@ function Console(props: IProps) {
471470
<Button
472471
type="text"
473472
onClick={() => {
474-
let formatRes = '';
475-
try {
476-
const contextTmp = editorRef?.current?.getAllContent();
477-
formatRes = format(contextTmp || '');
478-
}
479-
catch { }
480-
if (formatRes) {
481-
editorRef?.current?.setValue(formatRes, 'cover');
482-
} else {
483-
message.error(i18n('common.tips.formatError'))
484-
}
473+
// 格式化sql
474+
const sql = editorRef?.current?.getCurrentSelectContent() || ''
475+
formatSql(sql, executeParams.type!).then((res) => {
476+
editorRef?.current?.setValue(res, 'select');
477+
});
485478
}}
486479
>
487480
{i18n('common.button.format')}

chat2db-client/src/service/sql.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,16 @@ const getProcedureDetail = createRequest<{
171171
databaseName: string;
172172
schemaName?: string;
173173
procedureName: string
174-
}, {procedureBody: string}>('/api/rdb/procedure/detail', { method: 'get' });
174+
}, { procedureBody: string }>('/api/rdb/procedure/detail', { method: 'get' });
175+
176+
/** 格式化sql */
177+
const sqlFormat = createRequest<{
178+
sql: string;
179+
dbType: DatabaseTypeCode;
180+
}, string>('/api/sql/format', { method: 'get' });
175181

176182
export default {
183+
sqlFormat,
177184
getTriggerDetail,
178185
getProcedureDetail,
179186
getFunctionDetail,

chat2db-client/src/theme/background/darkDimmed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const antdPrimaryColor: IAntdPrimaryColor = {
1212
colorPrimary: '#3c8618',
1313
},
1414
[PrimaryColorType.Golden_Purple]: {
15-
colorPrimary: '#7688c9',
15+
colorPrimary: '#8276c9',
1616
},
1717
[PrimaryColorType.Polar_Blue]: {
1818
colorPrimary: '#1677ff',

chat2db-client/src/utils/index.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { ThemeType, OSType } from '@/constants';
1+
import { ThemeType, OSType, DatabaseTypeCode } from '@/constants';
22
import { ITreeNode } from '@/typings';
33
import clipboardCopy from 'copy-to-clipboard';
44
import lodash from 'lodash';
5+
import sqlServer from '@/service/sql';
6+
import { format } from 'sql-formatter';
57

68
export function getOsTheme() {
79
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
@@ -230,3 +232,28 @@ export function OSnow(): {
230232
}
231233
}
232234

235+
// 格式化sql
236+
export function formatSql(sql: string, dbType: DatabaseTypeCode) {
237+
return new Promise((r: (sql: string) => void, j) => {
238+
let formatRes = '';
239+
try {
240+
formatRes = format(sql || '');
241+
}
242+
catch {
243+
244+
}
245+
// 如果格式化失败,直接返回原始sql
246+
if (!formatRes) {
247+
sqlServer.sqlFormat({
248+
sql,
249+
dbType,
250+
}).then((res) => {
251+
formatRes = res;
252+
r(formatRes);
253+
})
254+
} else {
255+
r(formatRes);
256+
}
257+
})
258+
}
259+

0 commit comments

Comments
 (0)