Skip to content

Commit

Permalink
Fix tabular formatting for CREATE TABLE
Browse files Browse the repository at this point in the history
Fixes #690

This fix is somewhat ugly. I really don't like it.
Frankly, I don't like this whole tabular style feature.
  • Loading branch information
nene committed Jan 22, 2024
1 parent bec158b commit ee56459
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 65 deletions.
3 changes: 3 additions & 0 deletions src/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ const processDialectFormatOptions = (
): ProcessedDialectFormatOptions => ({
alwaysDenseOperators: options.alwaysDenseOperators || [],
onelineClauses: Object.fromEntries(options.onelineClauses.map(name => [name, true])),
tabularOnelineClauses: Object.fromEntries(
(options.tabularOnelineClauses ?? options.onelineClauses).map(name => [name, true])
),
});
9 changes: 8 additions & 1 deletion src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ export interface DialectFormatOptions {
alwaysDenseOperators?: string[];
// List of clauses that should be formatted on a single line
onelineClauses: string[];
// List of clauses that should be formatted on a single line in tabular style
tabularOnelineClauses?: string[];
}

// Contains the same data as DialectFormatOptions,
// but optimized for faster and more conventient lookup.
export interface ProcessedDialectFormatOptions {
alwaysDenseOperators: string[];
onelineClauses: Record<string, boolean>;
tabularOnelineClauses: Record<string, boolean>;
}

/** Formats a generic SQL expression */
Expand Down Expand Up @@ -255,7 +258,11 @@ export default class ExpressionFormatter {
}

private isOnelineClause(node: ClauseNode): boolean {
return this.dialectCfg.onelineClauses[node.nameKw.text];
if (isTabularStyle(this.cfg)) {
return this.dialectCfg.tabularOnelineClauses[node.nameKw.text];
} else {
return this.dialectCfg.onelineClauses[node.nameKw.text];
}
}

private formatClauseInIndentedStyle(node: ClauseNode) {
Expand Down
12 changes: 8 additions & 4 deletions src/languages/bigquery/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ const reservedClauses = expandPhrases([
'REMOTE WITH CONNECTION',
]);

const onelineClauses = expandPhrases([
const standardOnelineClauses = expandPhrases([
'CREATE [OR REPLACE] [TEMP|TEMPORARY|SNAPSHOT|EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
'CREATE [OR REPLACE] [TEMP|TEMPORARY|SNAPSHOT|EXTERNAL] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [MATERIALIZED] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE',
Expand Down Expand Up @@ -160,7 +163,7 @@ export const bigquery: DialectOptions = {
name: 'bigquery',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...tabularOnelineClauses, ...standardOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand All @@ -187,7 +190,8 @@ export const bigquery: DialectOptions = {
postProcess,
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};

Expand Down
12 changes: 8 additions & 4 deletions src/languages/db2/db2.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ const reservedClauses = expandPhrases([
'INSERT',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [GLOBAL TEMPORARY | EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] VIEW',
// - update:
'UPDATE',
Expand Down Expand Up @@ -261,7 +264,7 @@ export const db2: DialectOptions = {
name: 'db2',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -298,6 +301,7 @@ export const db2: DialectOptions = {
],
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
10 changes: 6 additions & 4 deletions src/languages/db2i/db2i.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ const reservedClauses = expandPhrases([
'FOR SYSTEM NAME',
]);

const onelineClauses = expandPhrases([
const standardOnelineClauses = expandPhrases(['CREATE [OR REPLACE] TABLE']);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] TABLE',
'CREATE [OR REPLACE] [RECURSIVE] VIEW',
// - update:
'UPDATE',
Expand Down Expand Up @@ -157,7 +158,7 @@ export const db2i: DialectOptions = {
name: 'db2i',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand All @@ -177,6 +178,7 @@ export const db2i: DialectOptions = {
operators: ['**', '¬=', '¬>', '¬<', '!>', '!<', '||', '=>'],
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
12 changes: 8 additions & 4 deletions src/languages/hive/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ const reservedClauses = expandPhrases([
'[OVERWRITE] INTO TABLE',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE',
Expand Down Expand Up @@ -86,7 +89,7 @@ export const hive: DialectOptions = {
name: 'hive',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand All @@ -100,6 +103,7 @@ export const hive: DialectOptions = {
operators: ['%', '~', '^', '|', '&', '<=>', '==', '!', '||'],
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
12 changes: 8 additions & 4 deletions src/languages/mariadb/mariadb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ const reservedClauses = expandPhrases([
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE [LOW_PRIORITY] [IGNORE]',
Expand Down Expand Up @@ -269,7 +272,7 @@ export const mariadb: DialectOptions = {
name: 'mariadb',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -311,6 +314,7 @@ export const mariadb: DialectOptions = {
postProcess,
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
10 changes: 6 additions & 4 deletions src/languages/mysql/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const reservedClauses = expandPhrases([
'SET',
]);

const onelineClauses = expandPhrases([
const standardOnelineClauses = expandPhrases(['CREATE [TEMPORARY] TABLE [IF NOT EXISTS]']);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE [LOW_PRIORITY] [IGNORE]',
Expand Down Expand Up @@ -236,7 +237,7 @@ export const mysql: DialectOptions = {
name: 'mysql',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -280,6 +281,7 @@ export const mysql: DialectOptions = {
postProcess,
},
formatOptions: {
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
12 changes: 8 additions & 4 deletions src/languages/plsql/plsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ const reservedClauses = expandPhrases([
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [GLOBAL TEMPORARY | PRIVATE TEMPORARY | SHARDED | DUPLICATED | IMMUTABLE BLOCKCHAIN | BLOCKCHAIN | IMMUTABLE] TABLE',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [NO FORCE | FORCE] [EDITIONING | EDITIONABLE | EDITIONABLE EDITIONING | NONEDITIONABLE] VIEW',
'CREATE MATERIALIZED VIEW',
// - update:
Expand Down Expand Up @@ -85,7 +88,7 @@ export const plsql: DialectOptions = {
name: 'plsql',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -122,7 +125,8 @@ export const plsql: DialectOptions = {
},
formatOptions: {
alwaysDenseOperators: ['@'],
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};

Expand Down
12 changes: 8 additions & 4 deletions src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ const reservedClauses = expandPhrases([
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create
const standardOnelineClauses = expandPhrases([
'CREATE [GLOBAL | LOCAL] [TEMPORARY | TEMP | UNLOGGED] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create
'CREATE [OR REPLACE] [TEMP | TEMPORARY] [RECURSIVE] VIEW',
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
// - update:
Expand Down Expand Up @@ -256,7 +259,7 @@ export const postgresql: DialectOptions = {
name: 'postgresql',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -364,6 +367,7 @@ export const postgresql: DialectOptions = {
},
formatOptions: {
alwaysDenseOperators: ['::', ':'],
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
12 changes: 8 additions & 4 deletions src/languages/redshift/redshift.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ const reservedClauses = expandPhrases([
'SET',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [TEMPORARY | TEMP | LOCAL TEMPORARY | LOCAL TEMP] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE | MATERIALIZED] VIEW',
// - update:
'UPDATE',
Expand Down Expand Up @@ -143,7 +146,7 @@ export const redshift: DialectOptions = {
name: 'redshift',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -172,6 +175,7 @@ export const redshift: DialectOptions = {
},
formatOptions: {
alwaysDenseOperators: ['::'],
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
12 changes: 8 additions & 4 deletions src/languages/singlestoredb/singlestoredb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ const reservedClauses = expandPhrases([
'CREATE [OR REPLACE] [EXTERNAL] FUNCTION',
]);

const onelineClauses = expandPhrases([
// - create:
const standardOnelineClauses = expandPhrases([
'CREATE [ROWSTORE] [REFERENCE | TEMPORARY | GLOBAL TEMPORARY] TABLE [IF NOT EXISTS]',
]);

const tabularOnelineClauses = expandPhrases([
// - create:
'CREATE VIEW',
// - update:
'UPDATE',
Expand Down Expand Up @@ -238,7 +241,7 @@ export const singlestoredb: DialectOptions = {
name: 'singlestoredb',
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
Expand Down Expand Up @@ -280,6 +283,7 @@ export const singlestoredb: DialectOptions = {
},
formatOptions: {
alwaysDenseOperators: ['::', '::$', '::%'],
onelineClauses,
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
tabularOnelineClauses,
},
};
Loading

0 comments on commit ee56459

Please sign in to comment.