forked from OtterMind/Chat2DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.ts
More file actions
52 lines (51 loc) · 1.08 KB
/
Copy pathsql.ts
File metadata and controls
52 lines (51 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { DatabaseTypeCode } from '@/constants';
import sqlServer from '@/service/sql';
import { format } from 'sql-formatter';
/**
* 格式化sql
*/
export function formatSql(sql: string, dbType: DatabaseTypeCode) {
const arr = [
'bigquery',
'db2',
'hive',
'mariadb',
'mysql',
'n1ql',
'plsql',
'postgresql',
'redshift',
'spark',
'sqlite',
'sql',
'trino',
'transactsql',
'singlestoredb',
'snowflake',
];
const language = arr.includes(dbType.toLowerCase()) ? dbType.toLowerCase() : 'sql';
return new Promise((r: (sql: string) => void) => {
let formatRes = '';
// debugger;
try {
formatRes = format(sql || '', { language });
// formatRes = '';
} catch (e) {
console.log('Frontend format sql error', e);
}
// // 如果格式化失败,直接返回原始sql
if (!formatRes) {
sqlServer
.sqlFormat({
sql,
dbType,
})
.then((res) => {
formatRes = res;
r(formatRes);
});
} else {
r(formatRes);
}
});
}