Skip to content

Commit

Permalink
Change how CREATE VIEW is formatted
Browse files Browse the repository at this point in the history
Similarly to the recent CREATE TABLE change
  • Loading branch information
nene committed Dec 7, 2023
1 parent 3f62841 commit f5acece
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/languages/bigquery/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const reservedClauses = expandPhrases([
'MERGE [INTO]',
'WHEN [NOT] MATCHED [BY SOURCE | BY TARGET] [THEN]',
'UPDATE SET',
// Data definition, https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
'CREATE [OR REPLACE] [MATERIALIZED] VIEW [IF NOT EXISTS]',

'CLUSTER BY',
'FOR SYSTEM_TIME AS OF', // CREATE SNAPSHOT TABLE
Expand All @@ -42,7 +40,9 @@ const reservedClauses = expandPhrases([

const onelineClauses = 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',
// - delete:
Expand Down
3 changes: 1 addition & 2 deletions src/languages/db2/db2.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ const reservedClauses = expandPhrases([
'WHEN [NOT] MATCHED [THEN]',
'UPDATE SET',
'INSERT',
// Data definition
'CREATE [OR REPLACE] VIEW',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | EXTERNAL] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] VIEW',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 1 addition & 2 deletions src/languages/db2i/db2i.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ const reservedClauses = expandPhrases([
'INSERT',
// Data definition - table
'FOR SYSTEM NAME',
// Data definition - view
'CREATE [OR REPLACE] [RECURSIVE] VIEW',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] TABLE',
'CREATE [OR REPLACE] [RECURSIVE] VIEW',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 1 addition & 2 deletions src/languages/hive/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ const reservedClauses = expandPhrases([
// https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-Loadingfilesintotables
'LOAD DATA [LOCAL] INPATH',
'[OVERWRITE] INTO TABLE',
// Data definition
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS]',
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 1 addition & 2 deletions src/languages/mariadb/mariadb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ const reservedClauses = expandPhrases([
'ON DUPLICATE KEY UPDATE',
// - update:
'SET',
// Data definition
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE [LOW_PRIORITY] [IGNORE]',
// - delete:
Expand Down
3 changes: 1 addition & 2 deletions src/languages/mysql/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ const reservedClauses = expandPhrases([
'ON DUPLICATE KEY UPDATE',
// - update:
'SET',
// Data definition
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
]);

const onelineClauses = 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]',
// - delete:
Expand Down
5 changes: 2 additions & 3 deletions src/languages/plsql/plsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ const reservedClauses = expandPhrases([
'MERGE [INTO]',
'WHEN [NOT] MATCHED [THEN]',
'UPDATE SET',
// Data definition
'CREATE [OR REPLACE] [NO FORCE | FORCE] [EDITIONING | EDITIONABLE | EDITIONABLE EDITIONING | NONEDITIONABLE] VIEW',
'CREATE MATERIALIZED VIEW',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | PRIVATE TEMPORARY | SHARDED | DUPLICATED | IMMUTABLE BLOCKCHAIN | BLOCKCHAIN | IMMUTABLE] TABLE',
'CREATE [OR REPLACE] [NO FORCE | FORCE] [EDITIONING | EDITIONABLE | EDITIONABLE EDITIONING | NONEDITIONABLE] VIEW',
'CREATE MATERIALIZED VIEW',
// - update:
'UPDATE [ONLY]',
// - delete:
Expand Down
5 changes: 2 additions & 3 deletions src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
'CREATE [OR REPLACE] [TEMP | TEMPORARY] [RECURSIVE] VIEW',
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create
'CREATE [GLOBAL | LOCAL] [TEMPORARY | TEMP | UNLOGGED] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [TEMP | TEMPORARY] [RECURSIVE] VIEW',
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE [ONLY]',
'WHERE CURRENT OF',
Expand Down
3 changes: 1 addition & 2 deletions src/languages/redshift/redshift.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
'CREATE [OR REPLACE | MATERIALIZED] VIEW',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY | TEMP | LOCAL TEMPORARY | LOCAL TEMP] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE | MATERIALIZED] VIEW',
// - update:
'UPDATE',
// - delete:
Expand Down
2 changes: 1 addition & 1 deletion src/languages/singlestoredb/singlestoredb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const reservedClauses = expandPhrases([
// - update:
'SET',
// Data definition
'CREATE VIEW',
'CREATE [OR REPLACE] [TEMPORARY] PROCEDURE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [EXTERNAL] FUNCTION',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [ROWSTORE] [REFERENCE | TEMPORARY | GLOBAL TEMPORARY] TABLE [IF NOT EXISTS]',
'CREATE VIEW',
// - update:
'UPDATE',
// - delete:
Expand Down
6 changes: 2 additions & 4 deletions src/languages/snowflake/snowflake.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
// - view
'CREATE [OR REPLACE] [SECURE] [RECURSIVE] VIEW [IF NOT EXISTS]',
// - create/drop/merge table

'CLUSTER BY',
'[WITH] {MASKING POLICY | TAG | ROW ACCESS POLICY}',
'COPY GRANTS',
Expand All @@ -43,6 +40,7 @@ const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [VOLATILE] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [LOCAL | GLOBAL] {TEMP|TEMPORARY} TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [SECURE] [RECURSIVE] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 1 addition & 2 deletions src/languages/spark/spark.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ const reservedClauses = expandPhrases([
// https://spark.apache.org/docs/latest/sql-ref-syntax-dml-load.html
'LOAD DATA [LOCAL] INPATH',
'[OVERWRITE] INTO TABLE',
// Data definition
'CREATE [OR REPLACE] [GLOBAL TEMPORARY | TEMPORARY] VIEW [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [EXTERNAL] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [GLOBAL TEMPORARY | TEMPORARY] VIEW [IF NOT EXISTS]',
// - drop table:
'DROP TABLE [IF EXISTS]',
// - alter table:
Expand Down
3 changes: 1 addition & 2 deletions src/languages/sql/sql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
'CREATE [RECURSIVE] VIEW',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | LOCAL TEMPORARY] TABLE',
'CREATE [RECURSIVE] VIEW',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 1 addition & 2 deletions src/languages/sqlite/sqlite.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
'CREATE [TEMPORARY | TEMP] VIEW [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY | TEMP] TABLE [IF NOT EXISTS]',
'CREATE [TEMPORARY | TEMP] VIEW [IF NOT EXISTS]',
// - update:
'UPDATE [OR ABORT | OR FAIL | OR IGNORE | OR REPLACE | OR ROLLBACK]',
// - insert:
Expand Down
2 changes: 1 addition & 1 deletion src/languages/transactsql/transactsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ const reservedClauses = expandPhrases([
'WHEN [NOT] MATCHED [BY TARGET | BY SOURCE] [THEN]',
'UPDATE SET',
// Data definition
'CREATE [OR ALTER] [MATERIALIZED] VIEW',
'CREATE [OR ALTER] {PROC | PROCEDURE}',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE TABLE',
'CREATE [OR ALTER] [MATERIALIZED] VIEW',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 1 addition & 2 deletions src/languages/trino/trino.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const reservedClauses = expandPhrases([
'VALUES',
// - update:
'SET',
// Data definition
'CREATE [OR REPLACE] [MATERIALIZED] VIEW',
// MATCH_RECOGNIZE
'MATCH_RECOGNIZE',
'MEASURES',
Expand All @@ -41,6 +39,7 @@ const reservedClauses = expandPhrases([
const onelineClauses = expandPhrases([
// - create:
'CREATE TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [MATERIALIZED] VIEW',
// - update:
'UPDATE',
// - delete:
Expand Down
15 changes: 5 additions & 10 deletions test/features/createView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default function supportsCreateView(
) {
it('formats CREATE VIEW', () => {
expect(format('CREATE VIEW my_view AS SELECT id, fname, lname FROM tbl;')).toBe(dedent`
CREATE VIEW
my_view AS
CREATE VIEW my_view AS
SELECT
id,
fname,
Expand All @@ -27,8 +26,7 @@ export default function supportsCreateView(

it('formats CREATE VIEW with columns', () => {
expect(format('CREATE VIEW my_view (id, fname, lname) AS SELECT * FROM tbl;')).toBe(dedent`
CREATE VIEW
my_view (id, fname, lname) AS
CREATE VIEW my_view (id, fname, lname) AS
SELECT
*
FROM
Expand All @@ -39,8 +37,7 @@ export default function supportsCreateView(
if (orReplace) {
it('formats CREATE OR REPLACE VIEW', () => {
expect(format('CREATE OR REPLACE VIEW v1 AS SELECT 42;')).toBe(dedent`
CREATE OR REPLACE VIEW
v1 AS
CREATE OR REPLACE VIEW v1 AS
SELECT
42;
`);
Expand All @@ -50,8 +47,7 @@ export default function supportsCreateView(
if (materialized) {
it('formats CREATE MATERIALIZED VIEW', () => {
expect(format('CREATE MATERIALIZED VIEW mat_view AS SELECT 42;')).toBe(dedent`
CREATE MATERIALIZED VIEW
mat_view AS
CREATE MATERIALIZED VIEW mat_view AS
SELECT
42;
`);
Expand All @@ -61,8 +57,7 @@ export default function supportsCreateView(
if (ifNotExists) {
it('formats short CREATE VIEW IF NOT EXISTS', () => {
expect(format('CREATE VIEW IF NOT EXISTS my_view AS SELECT 42;')).toBe(dedent`
CREATE VIEW IF NOT EXISTS
my_view AS
CREATE VIEW IF NOT EXISTS my_view AS
SELECT
42;
`);
Expand Down

0 comments on commit f5acece

Please sign in to comment.