Skip to content

Commit c84159c

Browse files
committed
Upgrade types formatting to new CST structure
1 parent 3d733c3 commit c84159c

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

src/node_utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export const isListExpr = is("list_expr");
2626
export const isCreateFunctionStmt = is("create_function_stmt");
2727
export const isLanguageClause = is("language_clause");
2828
export const isDynamicallyLoadedFunction = is("dynamically_loaded_function");
29+
export const isDataTypeName = is("data_type_name");

src/syntax/base.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { AllColumns, Empty, Keyword, Node } from "sql-parser-cst";
22
import { CstToDocMap } from "../CstToDocMap";
3-
import { AstPath } from "prettier";
43

54
export const baseMap: CstToDocMap<Keyword | Empty | AllColumns> = {
65
/** cst-ignore: name */
76
keyword: (print, node, path, options) => {
8-
// Treat PostgreSQL "WITH TIME ZONE" as part of data type, not keyword,
9-
// so skip keyword case conversion for it
10-
if ((path as AstPath<Node>).parent?.type === "with_time_zone_data_type") {
11-
return path.node.text;
12-
}
137
switch (options.sqlKeywordCase) {
148
case "preserve":
159
return path.node.text;

src/syntax/data_type.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
import { AllDataTypeNodes, NamedDataType } from "sql-parser-cst";
1+
import { AllDataTypeNodes, DataType } from "sql-parser-cst";
22
import { isArray } from "../utils";
33
import { CstToDocMap } from "../CstToDocMap";
4-
import { group, indent, softline } from "../print_utils";
4+
import { group, indent, join, softline } from "../print_utils";
5+
import { isDataTypeName } from "../node_utils";
56

67
export const dataTypeMap: CstToDocMap<AllDataTypeNodes> = {
8+
data_type_name: (print) => print.spaced("name"),
79
// print single-word types as `TYPE(10)` and multi-word types as `MY TYPE (10)`
8-
named_data_type: (print, node) =>
9-
(isMultiWordTypeName(node) ? print.spaced : print)(["name", "params"]),
10-
data_type_identifier: (print) => print.spaced("name"),
10+
modified_data_type: (print, node) =>
11+
(isMultiWordTypeName(node.dataType) ? print.spaced : print)([
12+
"dataType",
13+
"modifiers",
14+
]),
15+
1116
setof_data_type: (print) => print.spaced(["setofKw", "dataType"]),
1217
array_data_type: (print) => print(["dataType", "bounds"]),
1318
array_bounds: (print) => ["[", print("bounds"), "]"],
14-
with_time_zone_data_type: (print) =>
15-
print.spaced(["dataType", "withTimeZoneKw"]),
19+
time_data_type: (print, node) =>
20+
group(
21+
join(" ", [
22+
print(["timeKw", "precision"]),
23+
...(node.timeZoneKw ? [print.spaced("timeZoneKw")] : []),
24+
]),
25+
),
26+
interval_data_type: (print) =>
27+
print.spaced(["intervalKw", "fieldsKw", "precision"]),
28+
parametric_data_type: (print) => print(["typeKw", "params"]),
1629
generic_type_params: (print) =>
1730
group(["<", indent([softline, print("params")]), softline, ">"]),
1831
array_type_param: (print) => print.spaced(["dataType", "constraints"]),
@@ -21,9 +34,6 @@ export const dataTypeMap: CstToDocMap<AllDataTypeNodes> = {
2134
table_data_type: (print) => print.spaced(["tableKw", "columns"]),
2235
};
2336

24-
function isMultiWordTypeName(node: NamedDataType): boolean {
25-
return (
26-
isArray(node.name) ||
27-
(node.name.type === "data_type_identifier" && isArray(node.name.name))
28-
);
37+
function isMultiWordTypeName(node: DataType): boolean {
38+
return isDataTypeName(node) && isArray(node.name) && node.name.length > 1;
2939
}

test/ddl/create_table.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe("create table", () => {
316316
`);
317317
});
318318

319-
it(`formats TIME ZONE modifier on TIME data types`, async () => {
319+
it(`formats TIME/TIMESTAMP data types`, async () => {
320320
await testPostgresql(dedent`
321321
CREATE TABLE client (
322322
from_date TIME WITH TIME ZONE,
@@ -325,6 +325,14 @@ describe("create table", () => {
325325
`);
326326
});
327327

328+
it(`formats INTERVAL data types`, async () => {
329+
await testPostgresql(dedent`
330+
CREATE TABLE client (
331+
foo INTERVAL DAY TO SECOND (2)
332+
)
333+
`);
334+
});
335+
328336
it(`formats SQLite table options`, async () => {
329337
await test(dedent`
330338
CREATE TABLE foo (

test/options/keywordCase.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ describe("sqlKeywordCase option", () => {
6161
},
6262
);
6363

64-
it(`sqlKeywordCase: "upper" does not effect PostgreSQL data types`, async () => {
64+
it(`sqlKeywordCase: "upper" effects PostgreSQL builtin data types`, async () => {
6565
expect(
6666
await pretty(
6767
`CREATE TABLE foo (
6868
col1 int, col2 character varying (255),
6969
col3 my_schema.custom_type,
70-
col4 timestamp with time zone
70+
col4 timestamp with time zone,
71+
col5 uuid
7172
)`,
7273
{
7374
dialect: "postgresql",
@@ -76,10 +77,11 @@ describe("sqlKeywordCase option", () => {
7677
),
7778
).toBe(dedent`
7879
CREATE TABLE foo (
79-
col1 int,
80-
col2 character varying (255),
80+
col1 INT,
81+
col2 CHARACTER VARYING (255),
8182
col3 my_schema.custom_type,
82-
col4 timestamp with time zone
83+
col4 TIMESTAMP WITH TIME ZONE,
84+
col5 uuid
8385
)
8486
`);
8587
});

0 commit comments

Comments
 (0)