Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ea10d0

Browse files
committedMar 9, 2025·
parse domain enums
1 parent 12895f0 commit 3ea10d0

File tree

4 files changed

+121
-13
lines changed

4 files changed

+121
-13
lines changed
 

‎src/pegjs/oracle.pegjs

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,75 @@ start
145145

146146
stmt
147147
= create_table_stmt
148+
/ create_domain_stmt
148149
/ drop_table_stmt
149150
/ alter_table_stmt
150151

152+
create_domain_stmt
153+
= create_single_column_domain
154+
// / create_multi_column_domain
155+
// / create_flexible_domain
156+
157+
create_single_column_domain
158+
= operation:KW_CREATE _
159+
usecase:KW_USECASE? _
160+
object:KW_DOMAIN _
161+
if_not_exists:if_not_exists? _
162+
name:schema_object _
163+
KW_AS _ as:(data_type / enum) _ SEMI_COLON {
164+
return {
165+
operation,
166+
usecase,
167+
object,
168+
if_not_exists,
169+
name,
170+
as,
171+
};
172+
}
173+
174+
enum
175+
= object:KW_ENUM _
176+
LPAR _ enum_list:enum_list _ RPAR {
177+
return {
178+
object,
179+
enum_list
180+
};
181+
}
182+
183+
enum_list
184+
= x:enum_item_list _ xs:(COMMA _ e:enum_item_list { return e; } )* {
185+
return [x, ...xs];
186+
}
187+
188+
enum_item_list
189+
= name:identifier_name _
190+
enum_alias_list:enum_alias_list? _
191+
value:(EQ _ v:literal { return v; })? {
192+
return {
193+
name,
194+
enum_alias_list,
195+
value,
196+
};
197+
}
198+
199+
enum_alias_list
200+
= name:identifier_name _ enum_alias_list:enum_item_list {
201+
return {
202+
name,
203+
enum_alias_list,
204+
};
205+
}
206+
207+
if_not_exists
208+
= KW_IF _ KW_NOT _ KW_EXISTS {
209+
return 'if not exists';
210+
}
211+
151212
create_table_stmt
152213
= operation:KW_CREATE _
153214
object:KW_TABLE _
154215
type:table_type? _
155-
name:schema_table _
216+
name:schema_object _
156217
sharing:table_sharing_clause?
157218
table:(relational_table / object_table / XMLType_table)
158219
memoptimize_for:table_memoptimize_clauses? _
@@ -202,7 +263,7 @@ table_memoptimize_clause
202263
}
203264

204265
table_parent_clause
205-
= KW_PARENT _ table:schema_table {
266+
= KW_PARENT _ table:schema_object {
206267
return table;
207268
}
208269

@@ -365,7 +426,7 @@ clustering_column_group
365426
}
366427

367428
clusering_join
368-
= table:schema_table _ joins:clusering_join_stmts {
429+
= table:schema_object _ joins:clusering_join_stmts {
369430
return { table, joins };
370431
}
371432

@@ -376,19 +437,19 @@ clusering_join_stmts
376437
}
377438

378439
clusering_join_stmt
379-
= KW_JOIN _ join_table:schema_table _ KW_ON _ LPAR _ condition:equijoin_condition _ RPAR {
440+
= KW_JOIN _ join_table:schema_object _ KW_ON _ LPAR _ condition:equijoin_condition _ RPAR {
380441
return { join_table, condition };
381442
}
382443

383444
equijoin_condition
384-
= left_table:schema_table _ EQ _ right_table:schema_table {
445+
= left_table:schema_object _ EQ _ right_table:schema_object {
385446
return { left_table, right_table };
386447
}
387448

388449
// TODO: replace rest of the instances with this rule
389-
schema_table
390-
= schema:(s:identifier_name _ DOT { return s; })? _ table:identifier_name {
391-
return { schema, table };
450+
schema_object
451+
= schema:(s:identifier_name _ DOT { return s; })? _ name:identifier_name {
452+
return { schema, name };
392453
}
393454

394455
table_partitioning_clauses
@@ -2012,7 +2073,7 @@ XMLType_table = ""
20122073
drop_table_stmt
20132074
= operation:KW_DROP _
20142075
object:KW_TABLE _
2015-
name:schema_table _
2076+
name:schema_object _
20162077
cascade_constraints:(KW_CASCADE _ KW_CONSTRAINTS { return 'cascade constraints'; })? _
20172078
purge:KW_PURGE? _ SEMI_COLON {
20182079
return {
@@ -2027,7 +2088,7 @@ drop_table_stmt
20272088
alter_table_stmt
20282089
= opertaion:KW_ALTER _
20292090
object:KW_TABLE _
2030-
name:schema_table _
2091+
name:schema_object _
20312092
memoptimize_read:memoptimize_read_clause? _
20322093
memoptimize_write:memoptimize_write_clause? _
20332094
body:alter_table_stmt_body?
@@ -2788,6 +2849,11 @@ KW_BEFORE = 'before'i !ident_start { return '
27882849
KW_BEGINNING = 'beginning'i !ident_start { return 'beginning'; }
27892850
KW_OPAQUE = 'opaque'i !ident_start { return 'opaque'; }
27902851
KW_UNPACKED = 'unpacked'i !ident_start { return 'unpacked'; }
2852+
KW_USECASE = 'usecase'i !ident_start { return 'usecase'; }
2853+
KW_DOMAIN = 'domain'i !ident_start { return 'domain'; }
2854+
KW_EXISTS = 'exists'i !ident_start { return 'exists'; }
2855+
KW_IF = 'if'i !ident_start { return 'if'; }
2856+
KW_ENUM = 'enum'i !ident_start { return 'enum'; }
27912857
27922858
KW_VARYING = 'varying'i !ident_start { return 'varying'; }
27932859
KW_VARCHAR = 'varchar'i !ident_start { return 'varchar'; }

‎test/statements/create-domain.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { Parser } = require("../../");
2+
3+
const parser = new Parser();
4+
5+
describe("create domain statement", () => {
6+
it("create domain sizes as enum (small, medium, large);", () => {
7+
const sql = "create domain sizes as enum (small, medium, large);";
8+
const ast = parser.parse(sql);
9+
10+
const expected = {
11+
operation: "create",
12+
usecase: null,
13+
object: "domain",
14+
if_not_exists: null,
15+
name: {
16+
schema: null,
17+
name: "sizes",
18+
},
19+
as: {
20+
object: "enum",
21+
enum_list: [
22+
{
23+
name: "small",
24+
enum_alias_list: null,
25+
value: null,
26+
},
27+
{
28+
name: "medium",
29+
enum_alias_list: null,
30+
value: null,
31+
},
32+
{
33+
name: "large",
34+
enum_alias_list: null,
35+
value: null,
36+
},
37+
],
38+
},
39+
};
40+
expect(ast[0]).toMatchObject(expected);
41+
});
42+
});

‎test/statements/create-table.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("create table statement", () => {
88
const ast = parser.parse(sql);
99
const expectedName = {
1010
schema: "some_db",
11-
table: "users",
11+
name: "users",
1212
};
1313
expect(ast[0].name).toMatchObject(expectedName);
1414
});
@@ -57,7 +57,7 @@ describe("create table statement", () => {
5757
const sql = "create table users(col integer) parent some_other_db.person;";
5858
const ast = parser.parse(sql);
5959
const expectedParent = {
60-
table: "person",
60+
name: "person",
6161
schema: "some_other_db",
6262
};
6363
expect(ast[0].parent).toMatchObject(expectedParent);

‎test/statements/drop-table.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("drop table statement", () => {
99
const expected = {
1010
name: {
1111
schema: "some_db",
12-
table: "users",
12+
name: "users",
1313
},
1414
purge: "purge",
1515
object: "table",

0 commit comments

Comments
 (0)
Please sign in to comment.