Skip to content

Commit

Permalink
Format PostgreSQL CREATE TABLE clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 17, 2024
1 parent 4c62fe1 commit 4bba717
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/syntax/create_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export const createTableMap: Partial<CstToDocMap<AllCreateTableNodes>> = {
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<CreateTableStmt> = (print, node) => {
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/dialects/postgresql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AllPostgresqlNodes } from "sql-parser-cst";
import { CstToDocMap } from "../../CstToDocMap";

export const postgresqlMap: Partial<CstToDocMap<AllPostgresqlNodes>> = {
export const postgresqlMap: CstToDocMap<AllPostgresqlNodes> = {
postgresql_operator_expr: (print) => print(["operatorKw", "expr"]),
postgresql_operator: (print) => print("operator"),
postgresql_operator_class: (print) => print("name"),
};
15 changes: 15 additions & 0 deletions test/ddl/create_table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4bba717

Please sign in to comment.