From 4bba7179ab5d47f127177b1a45ae7b50bdbae3c6 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 17 Jan 2024 21:17:06 +0200 Subject: [PATCH] Format PostgreSQL CREATE TABLE clauses --- src/syntax/create_table.ts | 12 ++++++++++++ src/syntax/dialects/postgresql.ts | 3 ++- test/ddl/create_table.test.ts | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/syntax/create_table.ts b/src/syntax/create_table.ts index e2e872d..24a66c0 100644 --- a/src/syntax/create_table.ts +++ b/src/syntax/create_table.ts @@ -35,6 +35,18 @@ export const createTableMap: Partial> = { with_partition_columns_clause: (print) => print.spaced(["withPartitionColumnsKw", "columns"]), create_table_using_clause: (print) => print.spaced(["usingKw", "module"]), + create_table_inherits_clause: (print) => + print.spaced(["inheritsKw", "tables"]), + create_table_partition_by_clause: (print) => + print.spaced(["partitionByKw", "strategyKw", "columns"]), + using_access_method_clause: (print) => print.spaced(["usingKw", "method"]), + create_table_tablespace_clause: (print) => + print.spaced(["tablespaceKw", "name"]), + with_storage_parameters_clause: (print) => + print.spaced(["withKw", "options"]), + create_table_without_oids_clause: (print) => print.spaced("withoutOidsKw"), + create_table_on_commit_clause: (print) => + print.spaced(["onCommitKw", "actionKw"]), }; const printClauses: ToDocFn = (print, node) => { diff --git a/src/syntax/dialects/postgresql.ts b/src/syntax/dialects/postgresql.ts index 16f3dd2..cbb5608 100644 --- a/src/syntax/dialects/postgresql.ts +++ b/src/syntax/dialects/postgresql.ts @@ -1,7 +1,8 @@ import { AllPostgresqlNodes } from "sql-parser-cst"; import { CstToDocMap } from "../../CstToDocMap"; -export const postgresqlMap: Partial> = { +export const postgresqlMap: CstToDocMap = { postgresql_operator_expr: (print) => print(["operatorKw", "expr"]), postgresql_operator: (print) => print("operator"), + postgresql_operator_class: (print) => print("name"), }; diff --git a/test/ddl/create_table.test.ts b/test/ddl/create_table.test.ts index 8d00d9e..b85a3d8 100644 --- a/test/ddl/create_table.test.ts +++ b/test/ddl/create_table.test.ts @@ -281,6 +281,21 @@ describe("create table", () => { `); }); + it("formats additional PostgeSQL CREATE TABLE clauses", async () => { + await testPostgresql(dedent` + CREATE TABLE client ( + id INT + ) + INHERITS (parent_table1, parent_table2) + PARTITION BY LIST (id, name my_opclass) + USING "SP-GiST" + TABLESPACE pg_default + WITH (fillfactor = 70, autovacuum_enabled) + WITHOUT OIDS + ON COMMIT DELETE ROWS + `); + }); + it(`formats CREATE TABLE AS`, async () => { await test(dedent` CREATE TABLE foo AS