Skip to content

Commit fa2c41a

Browse files
committed
Support SEQUENCE NAME
Fixes #107
1 parent 2422847 commit fa2c41a

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

src/cst/Sequence.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export type SequenceOption =
4343
| SequenceOptionCache
4444
| SequenceOptionCycle
4545
| SequenceOptionNoCycle
46-
| SequenceOptionOwnedBy;
46+
| SequenceOptionOwnedBy
47+
| SequenceOptionSequenceName;
4748

4849
export interface SequenceOptionAsType extends BaseNode {
4950
type: "sequence_option_as_type";
@@ -116,6 +117,12 @@ export interface SequenceOptionOwnedBy extends BaseNode {
116117
owner: EntityName;
117118
}
118119

120+
export interface SequenceOptionSequenceName extends BaseNode {
121+
type: "sequence_option_sequence_name";
122+
sequenceNameKw: [Keyword<"SEQUENCE">, Keyword<"NAME">];
123+
name: EntityName;
124+
}
125+
119126
export interface AlterSequenceStmt extends BaseNode {
120127
type: "alter_sequence_stmt";
121128
alterKw: Keyword<"ALTER">;

src/parser.pegjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,6 +4199,7 @@ sequence_option
41994199
/ sequence_option_cycle
42004200
/ sequence_option_no_cycle
42014201
/ sequence_option_owned_by
4202+
/ sequence_option_sequence_name
42024203

42034204
sequence_option_as_type
42044205
= kw:(AS __) type:data_type {
@@ -4277,6 +4278,11 @@ sequence_option_owned_by
42774278
return loc({ type: "sequence_option_owned_by", ownedByKw: read(kw), owner });
42784279
}
42794280

4281+
sequence_option_sequence_name
4282+
= kw:(SEQUENCE __ NAME __) name:entity_name {
4283+
return loc({ type: "sequence_option_sequence_name", sequenceNameKw: read(kw), name });
4284+
}
4285+
42804286
alter_sequence_stmt
42814287
= kw:(ALTER __)
42824288
sequenceKw:(SEQUENCE __)
@@ -9047,6 +9053,7 @@ MODE = kw:"MODE"i !ident_part { return loc(createK
90479053
MODULUS = kw:"MODULUS"i !ident_part { return loc(createKeyword(kw)); }
90489054
MONDAY = kw:"MONDAY"i !ident_part { return loc(createKeyword(kw)); }
90499055
MONTH = kw:"MONTH"i !ident_part { return loc(createKeyword(kw)); }
9056+
NAME = kw:"NAME"i !ident_part { return loc(createKeyword(kw)); }
90509057
NATIVE = kw:"NATIVE"i !ident_part { return loc(createKeyword(kw)); }
90519058
NATURAL = kw:"NATURAL"i !ident_part { return loc(createKeyword(kw)); }
90529059
NCHAR = kw:"NCHAR"i !ident_part { return loc(createKeyword(kw)); }

src/showNode/sequence.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const sequenceMap: FullTransformMap<string, AllSequenceNodes> = {
2929
sequence_option_cycle: (node) => show(node.cycleKw),
3030
sequence_option_no_cycle: (node) => show(node.noCycleKw),
3131
sequence_option_owned_by: (node) => show([node.ownedByKw, node.owner]),
32+
sequence_option_sequence_name: (node) =>
33+
show([node.sequenceNameKw, node.name]),
3234

3335
// ALTER SEQUENCE
3436
alter_sequence_stmt: (node) =>

test/ddl/alter_table.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ describe("alter table", () => {
234234
"ALTER COLUMN foo ADD GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 5)"
235235
);
236236
});
237+
// SEQUENCE NAME is not among the general sequence options
238+
it("supports ADD GENERATED .. AS IDENTITY (SEQUENCE NAME ...)", () => {
239+
testAlterWc(
240+
"ALTER COLUMN foo ADD GENERATED ALWAYS AS IDENTITY (SEQUENCE NAME my_seq START WITH 10)"
241+
);
242+
});
237243

238244
describe("alter identity column", () => {
239245
it("supports SET GENERATED", () => {

test/ddl/constraints_column.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ describe("column constraints", () => {
183183
// For all sequence options, see test/ddl/sequence.test.ts
184184
testColConstWc("GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 5)");
185185
});
186+
187+
// SEQUENCE NAME is not among the general sequence options
188+
it("GENERATED .. AS IDENTITY (SEQUENCE NAME ...)", () => {
189+
testColConstWc("GENERATED ALWAYS AS IDENTITY (START WITH 10 SEQUENCE NAME schm.my_seq)");
190+
});
186191
});
187192
});
188193

test/ddl/sequence.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe("sequence", () => {
1212
CACHE: ["CACHE 10", "CACHE -1"], // Negative makes no sense, but Postgres allows it
1313
CYCLE: ["CYCLE", "NO CYCLE"],
1414
"OWNED BY": ["OWNED BY blah", "OWNED BY foo.bar", "OWNED BY NONE"],
15+
// Not testing SEQUENCE NAME here (it's not supported in CREATE SEQUENCE and ALTER SEQUENCE)
16+
// only inside ALTER TABLE / CREATE TABLE
1517
};
1618

1719
describe("CREATE SEQUENCE", () => {

0 commit comments

Comments
 (0)