Skip to content

Commit 9668508

Browse files
committed
perf: Optimize sql formate
1 parent 53aaef3 commit 9668508

5 files changed

Lines changed: 84 additions & 62 deletions

File tree

chat2db-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"node-machine-id": "^1.1.12",
4545
"react-monaco-editor": "^0.54.0",
4646
"react-sortablejs": "^6.1.4",
47-
"sql-formatter": "^12.2.1",
47+
"sql-formatter": "^13.0.4",
4848
"styled-components": "^6.0.1",
4949
"umi": "^4.0.87",
5050
"umi-request": "^1.4.0",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import aiServer from '@/service/ai';
1010
import { v4 as uuidv4 } from 'uuid';
1111
import { DatabaseTypeCode, ConsoleStatus } from '@/constants';
1212
import Iconfont from '../Iconfont';
13-
import { IAiConfig, ITreeNode } from '@/typings';
13+
import { IAiConfig } from '@/typings';
1414
import { IAIState } from '@/models/ai';
1515
import Popularize from '@/components/Popularize';
16-
import { formatSql, getCookie } from '@/utils';
16+
import { getCookie } from '@/utils';
17+
import { formatSql } from '@/utils/sql';
1718
import { chatErrorForKey, chatErrorToLogin } from '@/constants/chat';
1819
import { AIType } from '@/typings/ai';
1920
import i18n from '@/i18n';
@@ -203,13 +204,12 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
203204
return;
204205
}
205206

206-
if(!props.tables || props.tables.length === 0) {
207+
if (!props.tables || props.tables.length === 0) {
207208
setTableNameList([]);
208209
setSelectedTables([]);
209-
return
210+
return;
210211
}
211212

212-
213213
const { dataSourceId, databaseName, schemaName } = props.executeParams;
214214
sqlService
215215
.getAllTableList({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Iconfont from '@/components/Iconfont';
55
import MonacoEditor, { IExportRefFunction } from '@/components/Console/MonacoEditor';
66
import i18n from '@/i18n';
77
import { Button } from 'antd';
8-
import { formatSql } from '@/utils';
8+
import { formatSql } from '@/utils/sql';
99
import sqlService, { IExecuteSqlParams } from '@/service/sql';
1010
import { DatabaseTypeCode } from '@/constants';
1111

chat2db-client/src/utils/index.ts

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { ThemeType, DatabaseTypeCode } from '@/constants';
1+
import { ThemeType } from '@/constants';
22
import { ITreeNode } from '@/typings';
3-
// import clipboardCopy from 'copy-to-clipboard';
43
import lodash from 'lodash';
5-
import sqlServer from '@/service/sql';
6-
import { format } from 'sql-formatter';
74

85
export function getOsTheme() {
96
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
@@ -212,37 +209,12 @@ export function osNow(): {
212209
};
213210
}
214211

215-
// 格式化sql
216-
export function formatSql(sql: string, dbType: DatabaseTypeCode) {
217-
return new Promise((r: (sql: string) => void) => {
218-
let formatRes = '';
219-
// try {
220-
// formatRes = format(sql || '');
221-
// // formatRes = '';
222-
// } catch {}
223-
// // 如果格式化失败,直接返回原始sql
224-
if (!formatRes) {
225-
sqlServer
226-
.sqlFormat({
227-
sql,
228-
dbType,
229-
})
230-
.then((res) => {
231-
formatRes = res;
232-
r(formatRes);
233-
});
234-
} else {
235-
r(formatRes);
236-
}
237-
});
238-
}
239-
240212
// 桌面端用hash模式,web端用history模式,路由跳转
241213
export function navigate(path: string) {
242214
if (__ENV__ === 'desktop') {
243-
window.location.replace(`#${path}`)
215+
window.location.replace(`#${path}`);
244216
} else {
245-
window.location.replace(path)
217+
window.location.replace(path);
246218
}
247219
}
248220

@@ -259,29 +231,28 @@ export function getCookie(name: string) {
259231
export function compareVersion(version1: string, version2: string) {
260232
const version1Arr = version1.split('.');
261233
const version2Arr = version2.split('.');
262-
const v1 = Number(version1Arr.join('')) ;
263-
const v2 = Number(version2Arr.join(''));
264-
if (v1 > v2) {
265-
return 1;
266-
} else if (v1 < v2) {
267-
return -1;
268-
}
234+
const v1 = Number(version1Arr.join(''));
235+
const v2 = Number(version2Arr.join(''));
236+
if (v1 > v2) {
237+
return 1;
238+
} else if (v1 < v2) {
239+
return -1;
240+
}
269241
return 0;
270242
}
271243

272244
// 把剪切板的内容转成二维数组
273-
export function clipboardToArray(text:string):Array<Array<string | null>> {
274-
if(!text){
275-
return [[]]
245+
export function clipboardToArray(text: string): Array<Array<string | null>> {
246+
if (!text) {
247+
return [[]];
276248
}
277-
try{
278-
const rows = text.split('\n')
279-
const array2D = rows.map(row => row.split('\t'))
280-
return array2D
281-
}
282-
catch{
283-
console.log('copy error')
284-
return [[]]
249+
try {
250+
const rows = text.split('\n');
251+
const array2D = rows.map((row) => row.split('\t'));
252+
return array2D;
253+
} catch {
254+
console.log('copy error');
255+
return [[]];
285256
}
286257
}
287258

@@ -292,12 +263,11 @@ export function copy(message: string) {
292263
}
293264

294265
// 二维数组复制
295-
export function tableCopy(array2D:Array<Array<string | null>>) {
296-
try{
297-
const text = array2D.map(row => row.join('\t')).join('\n')
266+
export function tableCopy(array2D: Array<Array<string | null>>) {
267+
try {
268+
const text = array2D.map((row) => row.join('\t')).join('\n');
298269
navigator.clipboard.writeText(text);
299-
}
300-
catch{
301-
console.log('copy error')
270+
} catch {
271+
console.log('copy error');
302272
}
303273
}

chat2db-client/src/utils/sql.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { DatabaseTypeCode } from '@/constants';
2+
import sqlServer from '@/service/sql';
3+
import { format } from 'sql-formatter';
4+
5+
/**
6+
* 格式化sql
7+
*/
8+
export function formatSql(sql: string, dbType: DatabaseTypeCode) {
9+
const arr = [
10+
'bigquery',
11+
'db2',
12+
'hive',
13+
'mariadb',
14+
'mysql',
15+
'n1ql',
16+
'plsql',
17+
'postgresql',
18+
'redshift',
19+
'spark',
20+
'sqlite',
21+
'sql',
22+
'trino',
23+
'transactsql',
24+
'singlestoredb',
25+
'snowflake',
26+
];
27+
const language = arr.includes(dbType.toLowerCase()) ? dbType.toLowerCase() : 'sql';
28+
return new Promise((r: (sql: string) => void) => {
29+
let formatRes = '';
30+
// debugger;
31+
try {
32+
formatRes = format(sql || '', { language });
33+
// formatRes = '';
34+
} catch (e) {
35+
console.log('Frontend format sql error', e);
36+
}
37+
// // 如果格式化失败,直接返回原始sql
38+
if (!formatRes) {
39+
sqlServer
40+
.sqlFormat({
41+
sql,
42+
dbType,
43+
})
44+
.then((res) => {
45+
formatRes = res;
46+
r(formatRes);
47+
});
48+
} else {
49+
r(formatRes);
50+
}
51+
});
52+
}

0 commit comments

Comments
 (0)