Skip to content

Commit ab37d8f

Browse files
committed
Format all COMMENT ON syntax
1 parent c993b57 commit ab37d8f

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

src/syntax/cast.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AllCastNodes } from "sql-parser-cst";
2+
import { group } from "../print_utils";
3+
import { CstToDocMap } from "../CstToDocMap";
4+
5+
export const castMap: CstToDocMap<AllCastNodes> = {
6+
cast_definition: (print) => group(print.spaced(["from", "asKw", "to"])),
7+
};

src/syntax/comment.ts

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,95 @@ import { AllCommentNodes } from "sql-parser-cst";
22
import { group, indent, line } from "../print_utils";
33
import { CstToDocMap } from "../CstToDocMap";
44

5-
export const commentMap: Partial<CstToDocMap<AllCommentNodes>> = {
5+
export const commentMap: CstToDocMap<AllCommentNodes> = {
66
comment_stmt: (print) =>
77
group([
88
print.spaced(["commentKw", "onKw", "target", "isKw"]),
99
indent([line, print("message")]),
1010
]),
1111

12+
comment_target_aggregate: (print) =>
13+
group([print.spaced(["aggregateKw", "name"]), print(["params"])]),
1214
comment_target_function: (print) =>
1315
group([print.spaced(["functionKw", "name"]), print(["params"])]),
14-
comment_target_table: (print) => group(print.spaced(["tableKw", "name"])),
16+
comment_target_procedure: (print) =>
17+
group([print.spaced(["procedureKw", "name"]), print(["params"])]),
18+
comment_target_routine: (print) =>
19+
group([print.spaced(["routineKw", "name"]), print(["params"])]),
20+
21+
comment_target_access_method: (print) =>
22+
group(print.spaced(["accessMethodKw", "name"])),
23+
comment_target_cast: (print) => group(print.spaced(["castKw", "args"])),
24+
comment_target_collation: (print) =>
25+
group(print.spaced(["collationKw", "name"])),
26+
comment_target_column: (print) => group(print.spaced(["columnKw", "name"])),
1527
comment_target_table_constraint: (print) =>
1628
group(print.spaced(["constraintKw", "name", "onKw", "tableName"])),
1729
comment_target_domain_constraint: (print) =>
1830
group(
1931
print.spaced(["constraintKw", "name", "onKw", "domainKw", "domainName"]),
2032
),
33+
comment_target_conversion: (print) =>
34+
group(print.spaced(["conversionKw", "name"])),
35+
comment_target_database: (print) =>
36+
group(print.spaced(["databaseKw", "name"])),
37+
comment_target_domain: (print) => group(print.spaced(["domainKw", "name"])),
38+
comment_target_extension: (print) =>
39+
group(print.spaced(["extensionKw", "name"])),
40+
comment_target_event_trigger: (print) =>
41+
group(print.spaced(["eventTriggerKw", "name"])),
42+
comment_target_foreign_data_wrapper: (print) =>
43+
group(print.spaced(["foreignDataWrapperKw", "name"])),
44+
comment_target_foreign_table: (print) =>
45+
group(print.spaced(["foreignTableKw", "name"])),
46+
comment_target_index: (print) => group(print.spaced(["indexKw", "name"])),
47+
comment_target_language: (print) =>
48+
group(print.spaced(["languageKw", "name"])),
49+
comment_target_large_object: (print) =>
50+
group(print.spaced(["largeObjectKw", "oid"])),
51+
comment_target_materialized_view: (print) =>
52+
group(print.spaced(["materializedViewKw", "name"])),
53+
comment_target_operator_class: (print) =>
54+
group(print.spaced(["operatorClassKw", "name", "usingKw", "methodName"])),
55+
comment_target_operator_family: (print) =>
56+
group(print.spaced(["operatorFamilyKw", "name", "usingKw", "methodName"])),
57+
comment_target_policy: (print) =>
58+
group(print.spaced(["policyKw", "name", "onKw", "tableName"])),
59+
comment_target_publication: (print) =>
60+
group(print.spaced(["publicationKw", "name"])),
61+
comment_target_role: (print) => group(print.spaced(["roleKw", "name"])),
62+
comment_target_rule: (print) =>
63+
group(print.spaced(["ruleKw", "name", "onKw", "tableName"])),
64+
comment_target_schema: (print) => group(print.spaced(["schemaKw", "name"])),
65+
comment_target_sequence: (print) =>
66+
group(print.spaced(["sequenceKw", "name"])),
67+
comment_target_server: (print) => group(print.spaced(["serverKw", "name"])),
68+
comment_target_statistics: (print) =>
69+
group(print.spaced(["statisticsKw", "name"])),
70+
comment_target_subscription: (print) =>
71+
group(print.spaced(["subscriptionKw", "name"])),
72+
comment_target_table: (print) => group(print.spaced(["tableKw", "name"])),
73+
comment_target_tablespace: (print) =>
74+
group(print.spaced(["tablespaceKw", "name"])),
75+
comment_target_text_search_configuration: (print) =>
76+
group(print.spaced(["textSearchConfigurationKw", "name"])),
77+
comment_target_text_search_dictionary: (print) =>
78+
group(print.spaced(["textSearchDictionaryKw", "name"])),
79+
comment_target_text_search_parser: (print) =>
80+
group(print.spaced(["textSearchParserKw", "name"])),
81+
comment_target_text_search_template: (print) =>
82+
group(print.spaced(["textSearchTemplateKw", "name"])),
83+
comment_target_transform: (print) =>
84+
group(
85+
print.spaced([
86+
"transformForKw",
87+
"typeName",
88+
"languageKw",
89+
"languageName",
90+
]),
91+
),
92+
comment_target_trigger: (print) =>
93+
group(print.spaced(["triggerKw", "name", "onKw", "tableName"])),
94+
comment_target_type: (print) => group(print.spaced(["typeKw", "name"])),
95+
comment_target_view: (print) => group(print.spaced(["viewKw", "name"])),
2196
};

src/syntax/transformMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { alterActionMap } from "./alter_action";
55
import { alterTableMap } from "./alter_table";
66
import { analyzeMap } from "./analyze";
77
import { baseMap } from "./base";
8+
import { castMap } from "./cast";
89
import { commentMap } from "./comment";
910
import { constraintMap } from "./constraint";
1011
import { createTableMap } from "./create_table";
@@ -53,6 +54,7 @@ export const transformMap: Partial<CstToDocMap<Node>> = {
5354
...alterTableMap,
5455
...analyzeMap,
5556
...baseMap,
57+
...castMap,
5658
...commentMap,
5759
...constraintMap,
5860
...createTableMap,

test/postgresql/comment.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,61 @@ describe("comment", () => {
3333
'This is a really nice comment here.'
3434
`);
3535
});
36+
37+
[
38+
"ACCESS METHOD foo",
39+
"AGGREGATE foo(bar INT)",
40+
"CAST (TEXT AS INT)",
41+
"COLLATION foo",
42+
"COLUMN my_table.my_column",
43+
"CONSTRAINT constraint_name ON table_name",
44+
"CONSTRAINT constraint_name ON DOMAIN domain_name",
45+
"CONVERSION foo",
46+
"DATABASE my_database",
47+
"DOMAIN my_domain",
48+
"EXTENSION my_extension",
49+
"EVENT TRIGGER my_event_trigger",
50+
"FOREIGN DATA WRAPPER my_foreign_data_wrapper",
51+
"FOREIGN TABLE my_foreign_table",
52+
"FUNCTION my_function",
53+
"FUNCTION my_function(INT, TEXT)",
54+
// "FUNCTION my_function()", // Not supported by parser
55+
"INDEX my_index",
56+
"LANGUAGE mylang",
57+
"PROCEDURAL LANGUAGE plpgsql",
58+
"LARGE OBJECT 12345",
59+
"MATERIALIZED VIEW my_materialized_view",
60+
// "OPERATOR + (INT, INT)", // Not supported by parser
61+
"OPERATOR CLASS my_operator_class USING btree",
62+
"OPERATOR FAMILY my_operator_family USING btree",
63+
"POLICY my_policy ON my_table",
64+
"PROCEDURE my_procedure",
65+
"PROCEDURE my_procedure(INT)",
66+
"PUBLICATION my_publication",
67+
"ROLE my_role",
68+
"ROUTINE my_routine",
69+
"ROUTINE my_routine(INT)",
70+
"RULE my_rule ON my_table",
71+
"SCHEMA my_schema",
72+
"SEQUENCE my_sequence",
73+
"SERVER my_server",
74+
"STATISTICS my_statistics",
75+
"SUBSCRIPTION my_subscription",
76+
"TABLE my_table",
77+
"TABLESPACE my_tablespace",
78+
"TEXT SEARCH CONFIGURATION my_text_search_configuration",
79+
"TEXT SEARCH DICTIONARY my_text_search_dictionary",
80+
"TEXT SEARCH PARSER my_text_search_parser",
81+
"TEXT SEARCH TEMPLATE my_text_search_template",
82+
"TRANSFORM FOR INTEGER LANGUAGE my_lang",
83+
"TRIGGER my_trigger ON my_table",
84+
"TYPE MY_TYPE",
85+
"VIEW my_view",
86+
].forEach((target) => {
87+
it(`formats ${target}`, async () => {
88+
await testPostgresql(dedent`
89+
COMMENT ON ${target} IS 'Blah'
90+
`);
91+
});
92+
});
3693
});

0 commit comments

Comments
 (0)