From 8b5359c1cecc68654cd4b82bdaa2fdb74acb1bb9 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Sat, 5 Apr 2025 20:02:06 +0400 Subject: [PATCH] add create index on table --- src/pegjs/oracle.pegjs | 50 ++++++++++++++++++++++++++++ test/statements/create-index.test.js | 41 +++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 test/statements/create-index.test.js diff --git a/src/pegjs/oracle.pegjs b/src/pegjs/oracle.pegjs index 6792135..89accde 100644 --- a/src/pegjs/oracle.pegjs +++ b/src/pegjs/oracle.pegjs @@ -153,6 +153,50 @@ stmt / create_sequence_stmt / alter_sequence_stmt / drop_sequence_stmt + / create_index_stmt + +// TODO: missing index_ilm_clause, cluster_index_clause, bitmap_join_index_clause +create_index_stmt + = operation:KW_CREATE _ + type:(KW_UNIQUE / KW_BITMAP / KW_MULTIVALUE)? _ + object:KW_INDEX _ + if_not_exists:if_not_exists? _ + name:schema_object _ KW_ON _ + target:table_index_clause _ + usable:(KW_USABLE / KW_UNUSABLE)? _ + invalidation:(x:(KW_DEFERRED / KW_IMMEDIATE) _ KW_INVALIDATION { return x; })? _ + SEMI_COLON { + return { + operation, + type, + object, + if_not_exists, + name, + target, + usable, + invalidation, + }; + } + +// TODO: missing index_properties +table_index_clause + = name:schema_object _ + t_alias:identifier_name? _ + LPAR _ columns:table_index_columns _ RPAR { + return { name, t_alias, columns, object: 'table' }; + } + +table_index_columns + = x:table_index_column _ + xs:(COMMA _ c:table_index_column { return c; })* { + return [x, ...xs]; + } + +table_index_column + = name:identifier_name _ + order:(KW_ASC / KW_DESC)? { + return { name, order }; + } drop_sequence_stmt = operation:KW_DROP _ @@ -2992,6 +3036,12 @@ KW_NOSCALE = 'noscale'i !ident_start { return ' KW_EXTEND = 'extend'i !ident_start { return 'extend'; } KW_NOEXTEND = 'noextend'i !ident_start { return 'noextend'; } KW_NOSHARED = 'noshared'i !ident_start { return 'noshared'; } +KW_BITMAP = 'bitmap'i !ident_start { return 'bitmap'; } +KW_MULTIVALUE = 'multivalue'i !ident_start { return 'multivalue'; } +KW_ASC = 'asc'i !ident_start { return 'asc'; } +KW_DESC = 'desc'i !ident_start { return 'desc'; } +KW_USABLE = 'usable'i !ident_start { return 'usable'; } +KW_INVALIDATION = 'invalidation'i !ident_start { return 'invalidation'; } KW_VARYING = 'varying'i !ident_start { return 'varying'; } KW_VARCHAR = 'varchar'i !ident_start { return 'varchar'; } diff --git a/test/statements/create-index.test.js b/test/statements/create-index.test.js new file mode 100644 index 0000000..dd6dee4 --- /dev/null +++ b/test/statements/create-index.test.js @@ -0,0 +1,41 @@ +const { Parser } = require("../../"); + +const parser = new Parser(); + +describe("create index statement", () => { + it("create unique index some_index on some_table (col1, col2);", () => { + const sql = "create unique index some_index on some_table (col1, col2);"; + const ast = parser.parse(sql); + const expected = { + operation: "create", + type: "unique", + object: "index", + if_not_exists: null, + name: { + schema: null, + name: "some_index", + }, + target: { + name: { + schema: null, + name: "some_table", + }, + object: "table", + t_alias: null, + columns: [ + { + name: "col1", + order: null, + }, + { + name: "col2", + order: null, + }, + ], + }, + usable: null, + invalidation: null, + }; + expect(ast[0]).toMatchObject(expected); + }); +});