Skip to content

Commit 90c133b

Browse files
committed
Support SETOF data-type in PostgreSQL
Refs #120
1 parent 70ba255 commit 90c133b

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/cst/DataType.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type AllDataTypeNodes =
1515
export type DataType =
1616
| NamedDataType
1717
| ArrayDataType
18+
| SetofDataType
1819
| WithTimeZoneDataType
1920
| TableDataType;
2021

@@ -82,3 +83,9 @@ export interface TableDataType extends BaseNode {
8283
tableKw: Keyword<"TABLE">;
8384
columns: ParenExpr<ListExpr<ColumnDefinition>>;
8485
}
86+
87+
export interface SetofDataType extends BaseNode {
88+
type: "setof_data_type";
89+
setofKw: Keyword<"SETOF">;
90+
dataType: DataType;
91+
}

src/parser.pegjs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6993,17 +6993,23 @@ index_tablespace_clause
69936993
* ------------------------------------------------------------------------------------ *
69946994
*/
69956995
data_type
6996-
= &postgres head:named_data_type tail:(__ array_bounds)+ {
6997-
return loc(createArrayDataTypeChain(head, tail));
6996+
= setof_data_type
6997+
6998+
setof_data_type
6999+
= &postgres kw:(SETOF __) dataType:array_data_type {
7000+
return loc({ type: "setof_data_type", setofKw: read(kw), dataType })
69987001
}
6999-
/ &postgres dataType:named_data_type tz:(__ (WITHOUT / WITH) __ TIME __ ZONE) {
7000-
return loc({
7001-
type: "with_time_zone_data_type",
7002-
dataType,
7003-
withTimeZoneKw: read(tz),
7004-
});
7002+
/ array_data_type
7003+
7004+
array_data_type
7005+
= &postgres head:with_time_zone_data_type tail:(__ array_bounds)* {
7006+
if (tail.length > 0) {
7007+
return loc(createArrayDataTypeChain(head, tail));
7008+
} else {
7009+
return head;
7010+
}
70057011
}
7006-
/ named_data_type
7012+
/ !postgres with_time_zone_data_type
70077013

70087014
array_bounds
70097015
= "[" bounds:(__ empty __) "]" {
@@ -7013,6 +7019,16 @@ array_bounds
70137019
return loc({ type: "array_bounds", bounds: read(bounds) });
70147020
}
70157021

7022+
with_time_zone_data_type
7023+
= &postgres dataType:named_data_type tz:(__ (WITHOUT / WITH) __ TIME __ ZONE) {
7024+
return loc({
7025+
type: "with_time_zone_data_type",
7026+
dataType,
7027+
withTimeZoneKw: read(tz),
7028+
});
7029+
}
7030+
/ named_data_type
7031+
70167032
named_data_type
70177033
= kw:(type_name __) params:paren$list$literal {
70187034
return loc({ type: "named_data_type", name: read(kw), params });
@@ -9782,6 +9798,7 @@ SERVER = kw:"SERVER"i !ident_part { return loc(createK
97829798
SESSION = kw:"SESSION"i !ident_part { return loc(createKeyword(kw)); }
97839799
SESSION_USER = kw:"SESSION_USER"i !ident_part { return loc(createKeyword(kw)); }
97849800
SET = kw:"SET"i !ident_part { return loc(createKeyword(kw)); }
9801+
SETOF = kw:"SETOF"i !ident_part { return loc(createKeyword(kw)); }
97859802
SETS = kw:"SETS"i !ident_part { return loc(createKeyword(kw)); }
97869803
SHARE = kw:"SHARE"i !ident_part { return loc(createKeyword(kw)); }
97879804
SHARED = kw:"SHARED"i !ident_part { return loc(createKeyword(kw)); }

src/showNode/data_type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export const dataTypeMap: FullTransformMap<string, AllDataTypeNodes> = {
1414
struct_type_param: (node) =>
1515
show([node.name, node.dataType, node.constraints]),
1616
table_data_type: (node) => show([node.tableKw, node.columns]),
17+
setof_data_type: (node) => show([node.setofKw, node.dataType]),
1718
};

test/ddl/data_types.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ describe("data types", () => {
384384
testType("MY_CUSTOM_TYPE");
385385
});
386386

387+
it("supports SETOF types", () => {
388+
testType("SETOF INT");
389+
testType("SETOF VARCHAR (100)");
390+
});
391+
387392
it("supports schema-qualified types", () => {
388393
testType("public.my_type");
389394
});

0 commit comments

Comments
 (0)