-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathsqlFormatter.ts
More file actions
102 lines (89 loc) · 2.85 KB
/
sqlFormatter.ts
File metadata and controls
102 lines (89 loc) · 2.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as allDialects from './allDialects.js';
import { FormatOptions } from './FormatOptions.js';
import { createDialect, DialectOptions } from './dialect.js';
import Formatter from './formatter/Formatter.js';
import { ConfigError, validateConfig } from './validateConfig.js';
const dialectNameMap: Record<keyof typeof allDialects | 'tsql', keyof typeof allDialects> = {
bigquery: 'bigquery',
db2: 'db2',
db2i: 'db2i',
duckdb: 'duckdb',
hive: 'hive',
mariadb: 'mariadb',
mysql: 'mysql',
n1ql: 'n1ql',
plsql: 'plsql',
postgresql: 'postgresql',
redshift: 'redshift',
spark: 'spark',
sqlite: 'sqlite',
sql: 'sql',
tidb: 'tidb',
trino: 'trino',
transactsql: 'transactsql',
tsql: 'transactsql', // alias for transactsq
singlestoredb: 'singlestoredb',
snowflake: 'snowflake',
};
export const supportedDialects = Object.keys(dialectNameMap);
export type SqlLanguage = keyof typeof dialectNameMap;
export type FormatOptionsWithLanguage = Partial<FormatOptions> & {
language?: SqlLanguage;
};
export type FormatOptionsWithDialect = Partial<FormatOptions> & {
dialect: DialectOptions;
};
const defaultOptions: FormatOptions = {
tabWidth: 2,
useTabs: false,
keywordCase: 'preserve',
identifierCase: 'preserve',
dataTypeCase: 'preserve',
functionCase: 'preserve',
indentStyle: 'standard',
logicalOperatorNewline: 'before',
commaPosition: 'trailing',
expressionWidth: 50,
linesBetweenQueries: 1,
denseOperators: false,
newlineBeforeSemicolon: false,
};
/**
* Format whitespace in a query to make it easier to read.
*
* @param {string} query - input SQL query string
* @param {FormatOptionsWithLanguage} cfg Configuration options (see docs in README)
* @return {string} formatted query
*/
export const format = (query: string, cfg: FormatOptionsWithLanguage = {}): string => {
if (typeof cfg.language === 'string' && !supportedDialects.includes(cfg.language)) {
throw new ConfigError(`Unsupported SQL dialect: ${cfg.language}`);
}
const canonicalDialectName = dialectNameMap[cfg.language || 'sql'];
return formatDialect(query, {
...cfg,
dialect: allDialects[canonicalDialectName],
});
};
/**
* Like the above format(), but language parameter is mandatory
* and must be a Dialect object instead of a string.
*
* @param {string} query - input SQL query string
* @param {FormatOptionsWithDialect} cfg Configuration options (see docs in README)
* @return {string} formatted query
*/
export const formatDialect = (
query: string,
{ dialect, ...cfg }: FormatOptionsWithDialect
): string => {
if (typeof query !== 'string') {
throw new Error('Invalid query argument. Expected string, instead got ' + typeof query);
}
const options = validateConfig({
...defaultOptions,
...cfg,
});
return new Formatter(createDialect(dialect), options).format(query);
};
export type FormatFn = typeof format;