-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlSqlParser.g4
More file actions
2697 lines (2232 loc) · 55.7 KB
/
PlSqlParser.g4
File metadata and controls
2697 lines (2232 loc) · 55.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Oracle(c) PL/SQL 11g Parser
*
* Copyright (c) 2009-2011 Alexandre Porcelli <alexandre.porcelli@gmail.com>
* Copyright (c) 2015-2017 Ivan Kochurkin (KvanTTT, kvanttt@gmail.com, Positive Technologies).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
parser grammar PlSqlParser;
@header {
package plsql_parser;
}
options { tokenVocab=PlSqlLexer; }
swallow_to_semi
: ~';'+
;
compilation_unit
: unit_statement* EOF
;
sql_script
: ((unit_statement | sql_plus_command) SEMICOLON?)* EOF
;
unit_statement
: alter_function
| alter_package
| alter_procedure
| alter_sequence
| alter_trigger
| alter_type
| alter_table
| create_function_body
| create_procedure_body
| create_package
| create_package_body
// | create_index //TODO
| create_table
// | create_view //TODO
// | create_directory //TODO
// | create_materialized_view //TODO
| create_sequence
| create_trigger
| create_type
| create_synonym
| drop_function
| drop_package
| drop_procedure
| drop_sequence
| drop_trigger
| drop_type
| data_manipulation_language_statements
| drop_table
| comment_on_column
| comment_on_table
| anonymous_block
;
// $<DDL -> SQL Statements for Stored PL/SQL Units
// $<Function DDLs
drop_function
: DROP FUNCTION function_name ';'
;
alter_function
: ALTER FUNCTION function_name COMPILE DEBUG? compiler_parameters_clause* (REUSE SETTINGS)? ';'
;
create_function_body
: CREATE (OR REPLACE)? FUNCTION function_name ('(' parameter (',' parameter)* ')')?
RETURN type_spec (invoker_rights_clause|parallel_enable_clause|result_cache_clause|DETERMINISTIC)*
((PIPELINED? (IS | AS) (DECLARE? declare_spec* body | call_spec)) | (PIPELINED | AGGREGATE) USING implementation_type_name) ';'
;
// $<Creation Function - Specific Clauses
parallel_enable_clause
: PARALLEL_ENABLE partition_by_clause?
;
partition_by_clause
: '(' PARTITION expression BY (ANY | (HASH | RANGE) '(' column_name (',' column_name)* ')')streaming_clause? ')'
;
result_cache_clause
: RESULT_CACHE relies_on_part?
;
relies_on_part
: RELIES_ON '(' tableview_name (',' tableview_name)* ')'
;
streaming_clause
: (ORDER | CLUSTER) expression BY '(' column_name (',' column_name)* ')'
;
// $<Package DDLs
drop_package
: DROP PACKAGE BODY? (schema_object_name '.')? package_name ';'
;
alter_package
: ALTER PACKAGE package_name COMPILE DEBUG? (PACKAGE | BODY | SPECIFICATION)? compiler_parameters_clause* (REUSE SETTINGS)? ';'
;
create_package
: CREATE (OR REPLACE)? PACKAGE (schema_object_name '.')? package_name invoker_rights_clause? (IS | AS) package_obj_spec* END package_name? ';'
;
create_package_body
: CREATE (OR REPLACE)? PACKAGE BODY (schema_object_name '.')? package_name (IS | AS) package_obj_body* (BEGIN seq_of_statements | END package_name?) ';'
;
// $<Create Package - Specific Clauses
package_obj_spec
: variable_declaration
| subtype_declaration
| cursor_declaration
| exception_declaration
| pragma_declaration
| type_declaration
| procedure_spec
| function_spec
;
procedure_spec
: PROCEDURE identifier ('(' parameter ( ',' parameter )* ')')? ';'
;
function_spec
: FUNCTION identifier ('(' parameter ( ',' parameter)* ')')? RETURN type_spec (DETERMINISTIC)? (RESULT_CACHE)? ';'
;
package_obj_body
: variable_declaration
| subtype_declaration
| cursor_declaration
| exception_declaration
| type_declaration
| procedure_body
| function_body
| procedure_spec
| function_spec
;
// $<Procedure DDLs
drop_procedure
: DROP PROCEDURE procedure_name ';'
;
alter_procedure
: ALTER PROCEDURE procedure_name COMPILE DEBUG? compiler_parameters_clause* (REUSE SETTINGS)? ';'
;
function_body
: FUNCTION identifier ('(' parameter (',' parameter)* ')')?
RETURN type_spec (invoker_rights_clause|parallel_enable_clause|result_cache_clause|DETERMINISTIC)*
((PIPELINED? (IS | AS) (DECLARE? declare_spec* body | call_spec)) | (PIPELINED | AGGREGATE) USING implementation_type_name) ';'
;
procedure_body
: PROCEDURE identifier ('(' parameter (',' parameter)* ')')?
(IS | AS)
(DECLARE? declare_spec* body | call_spec | EXTERNAL) ';'
;
create_procedure_body
: CREATE (OR REPLACE)? PROCEDURE procedure_name ('(' parameter (',' parameter)* ')')?
invoker_rights_clause? (IS | AS)
(DECLARE? declare_spec* body | call_spec | EXTERNAL) ';'
;
// $>
// $<Trigger DDLs
drop_trigger
: DROP TRIGGER trigger_name ';'
;
alter_trigger
: ALTER TRIGGER tn1=trigger_name
((ENABLE | DISABLE) | RENAME TO tn2=trigger_name | COMPILE DEBUG? compiler_parameters_clause* (REUSE SETTINGS)?) ';'
;
create_trigger
: CREATE ( OR REPLACE )? TRIGGER trigger_name
(simple_dml_trigger | compound_dml_trigger | non_dml_trigger)
trigger_follows_clause? (ENABLE | DISABLE)? trigger_when_clause? trigger_body ';'
;
trigger_follows_clause
: FOLLOWS trigger_name (',' trigger_name)*
;
trigger_when_clause
: WHEN '(' condition ')'
;
// $<Create Trigger- Specific Clauses
simple_dml_trigger
: (BEFORE | AFTER | INSTEAD OF) dml_event_clause referencing_clause? for_each_row?
;
for_each_row
: FOR EACH ROW
;
compound_dml_trigger
: FOR dml_event_clause referencing_clause?
;
non_dml_trigger
: (BEFORE | AFTER) non_dml_event (OR non_dml_event)* ON (DATABASE | (schema_name '.')? SCHEMA)
;
trigger_body
: COMPOUND TRIGGER
| CALL identifier
| trigger_block
;
routine_clause
: routine_name function_argument?
;
compound_trigger_block
: COMPOUND TRIGGER declare_spec* timing_point_section+ END trigger_name
;
timing_point_section
: bk=BEFORE STATEMENT IS trigger_block BEFORE STATEMENT ';'
| bk=BEFORE EACH ROW IS trigger_block BEFORE EACH ROW ';'
| ak=AFTER STATEMENT IS trigger_block AFTER STATEMENT ';'
| ak=AFTER EACH ROW IS trigger_block AFTER EACH ROW ';'
;
non_dml_event
: ALTER
| ANALYZE
| ASSOCIATE STATISTICS
| AUDIT
| COMMENT
| CREATE
| DISASSOCIATE STATISTICS
| DROP
| GRANT
| NOAUDIT
| RENAME
| REVOKE
| TRUNCATE
| DDL
| STARTUP
| SHUTDOWN
| DB_ROLE_CHANGE
| LOGON
| LOGOFF
| SERVERERROR
| SUSPEND
| DATABASE
| SCHEMA
| FOLLOWS
;
dml_event_clause
: dml_event_element (OR dml_event_element)* ON dml_event_nested_clause? tableview_name
;
dml_event_element
: (DELETE|INSERT|UPDATE) (OF column_name (',' column_name)*)?
;
dml_event_nested_clause
: NESTED TABLE tableview_name OF
;
referencing_clause
: REFERENCING referencing_element+
;
referencing_element
: (NEW | OLD | PARENT) column_alias
;
// $>
// $>
// $<Type DDLs
drop_type
: DROP TYPE BODY? type_name (FORCE | VALIDATE)? ';'
;
alter_type
: ALTER TYPE type_name
(compile_type_clause
| replace_type_clause
//TODO | {input.LT(2).getText().equalsIgnoreCase("attribute")}? alter_attribute_definition
| alter_method_spec
| alter_collection_clauses
| modifier_clause
) dependent_handling_clause? ';'
;
// $<Alter Type - Specific Clauses
compile_type_clause
: COMPILE DEBUG? (SPECIFICATION | BODY)? compiler_parameters_clause* (REUSE SETTINGS)?
;
replace_type_clause
: REPLACE invoker_rights_clause? AS OBJECT '(' object_member_spec (',' object_member_spec)* ')'
;
alter_method_spec
: alter_method_element (',' alter_method_element)*
;
alter_method_element
: (ADD|DROP) (map_order_function_spec | subprogram_spec)
;
alter_attribute_definition
: (ADD | MODIFY | DROP) ATTRIBUTE (attribute_definition | '(' attribute_definition (',' attribute_definition)* ')')
;
attribute_definition
: attribute_name type_spec?
;
alter_collection_clauses
: MODIFY (LIMIT expression | ELEMENT TYPE type_spec)
;
dependent_handling_clause
: INVALIDATE
| CASCADE (CONVERT TO SUBSTITUTABLE | NOT? INCLUDING TABLE DATA)? dependent_exceptions_part?
;
dependent_exceptions_part
: FORCE? EXCEPTIONS INTO tableview_name
;
create_type
: CREATE (OR REPLACE)? TYPE (type_definition | type_body) ';'
;
// $<Create Type - Specific Clauses
type_definition
: type_name (OID CHAR_STRING)? object_type_def?
;
object_type_def
: invoker_rights_clause? (object_as_part | object_under_part) sqlj_object_type?
('(' object_member_spec (',' object_member_spec)* ')')? modifier_clause*
;
object_as_part
: (IS | AS) (OBJECT | varray_type_def | nested_table_type_def)
;
object_under_part
: UNDER type_spec
;
nested_table_type_def
: TABLE OF type_spec (NOT NULL)?
;
sqlj_object_type
: EXTERNAL NAME expression LANGUAGE JAVA USING (SQLDATA|CUSTOMDATUM|ORADATA)
;
type_body
: BODY type_name (IS | AS) (type_body_elements)+ END
;
type_body_elements
: map_order_func_declaration
| subprog_decl_in_type
;
map_order_func_declaration
: (MAP | ORDER) MEMBER func_decl_in_type
;
subprog_decl_in_type
: (MEMBER | STATIC) (proc_decl_in_type | func_decl_in_type | constructor_declaration)
;
proc_decl_in_type
: PROCEDURE procedure_name '(' type_elements_parameter (',' type_elements_parameter)* ')'
(IS | AS) (call_spec | DECLARE? declare_spec* body ';')
;
func_decl_in_type
: FUNCTION function_name ('(' type_elements_parameter (',' type_elements_parameter)* ')')?
RETURN type_spec (IS | AS) (call_spec | DECLARE? declare_spec* body ';')
;
constructor_declaration
: FINAL? INSTANTIABLE? CONSTRUCTOR FUNCTION type_spec
('(' (SELF IN OUT type_spec ',') type_elements_parameter (',' type_elements_parameter)* ')')?
RETURN SELF AS RESULT (IS | AS) (call_spec | DECLARE? declare_spec* body ';')
;
// $>
// $<Common Type Clauses
modifier_clause
: NOT? (INSTANTIABLE | FINAL | OVERRIDING)
;
object_member_spec
: identifier type_spec sqlj_object_type_attr?
| element_spec
;
sqlj_object_type_attr
: EXTERNAL NAME expression
;
element_spec
: modifier_clause? element_spec_options+ (',' pragma_clause)?
;
element_spec_options
: subprogram_spec
| constructor_spec
| map_order_function_spec
;
subprogram_spec
: (MEMBER|STATIC) (type_procedure_spec|type_function_spec)
;
type_procedure_spec
: PROCEDURE procedure_name '(' type_elements_parameter (',' type_elements_parameter)* ')' ((IS | AS) call_spec)?
;
type_function_spec
: FUNCTION function_name ('(' type_elements_parameter (',' type_elements_parameter)* ')')?
RETURN (type_spec | SELF AS RESULT) ((IS | AS) call_spec | EXTERNAL VARIABLE? NAME expression)?
;
constructor_spec
: FINAL? INSTANTIABLE? CONSTRUCTOR FUNCTION type_spec ('(' (SELF IN OUT type_spec ',') type_elements_parameter (',' type_elements_parameter)* ')')? RETURN SELF AS RESULT ((IS | AS) call_spec)?
;
map_order_function_spec
: (MAP | ORDER) MEMBER type_function_spec
;
pragma_clause
: PRAGMA RESTRICT_REFERENCES '(' pragma_elements (',' pragma_elements)* ')'
;
pragma_elements
: identifier
| DEFAULT
;
type_elements_parameter
: parameter_name type_spec
;
// $<Sequence DDLs
drop_sequence
: DROP SEQUENCE sequence_name ';'
;
alter_sequence
: ALTER SEQUENCE sequence_name sequence_spec+ ';'
;
create_sequence
: CREATE SEQUENCE sequence_name (sequence_start_clause | sequence_spec)* ';'
;
// $<Common Sequence
sequence_spec
: INCREMENT BY UNSIGNED_INTEGER
| MAXVALUE UNSIGNED_INTEGER
| NOMAXVALUE
| MINVALUE UNSIGNED_INTEGER
| NOMINVALUE
| CYCLE
| NOCYCLE
| CACHE UNSIGNED_INTEGER
| NOCACHE
| ORDER
| NOORDER
;
sequence_start_clause
: START WITH UNSIGNED_INTEGER
;
// $<Table DDL Clauses
create_table
: CREATE TABLE tableview_name LEFT_PAREN column_name datatype (COMMA column_name datatype)* RIGHT_PAREN
;
drop_table
: DROP TABLE tableview_name
;
comment_on_column
: COMMENT ON COLUMN tableview_name PERIOD column_name IS quoted_string
;
// $<Synonym DDL Clauses
create_synonym
// Synonym's schema cannot be specified for public synonyms
: CREATE (OR REPLACE)? PUBLIC SYNONYM synonym_name FOR (schema_name PERIOD)? schema_object_name (AT_SIGN link_name)?
| CREATE (OR REPLACE)? SYNONYM (schema_name PERIOD)? synonym_name FOR (schema_name PERIOD)? schema_object_name (AT_SIGN link_name)?
;
comment_on_table
: COMMENT ON TABLE tableview_name IS quoted_string
;
alter_table
: ALTER TABLE tableview_name add_constraint
// TODO | drop_constraint
;
add_constraint
: ADD (CONSTRAINT constraint_name)? (primary_key_clause | foreign_key_clause | unique_key_clause)
// TODO | implement check_constraint_clause, but need separate rule boolean_expression
;
foreign_key_clause
: FOREIGN KEY LEFT_PAREN column_name (COMMA column_name)* RIGHT_PAREN references_clause on_delete_clause?
;
references_clause
: REFERENCES tableview_name LEFT_PAREN column_name (COMMA column_name)* RIGHT_PAREN
;
on_delete_clause
: ON DELETE (CASCADE | SET NULL)
;
unique_key_clause
: UNIQUE LEFT_PAREN column_name (COMMA column_name)* RIGHT_PAREN
// TODO implement USING INDEX clause
;
primary_key_clause
: PRIMARY KEY LEFT_PAREN column_name (COMMA column_name)* RIGHT_PAREN
// TODO implement USING INDEX clause
;
// $<Anonymous PL/SQL code block
anonymous_block
: BEGIN seq_of_statements END SEMICOLON
;
// $<Common DDL Clauses
invoker_rights_clause
: AUTHID (CURRENT_USER|DEFINER)
;
compiler_parameters_clause
: identifier '=' expression
;
call_spec
: LANGUAGE (java_spec | c_spec)
;
// $<Call Spec - Specific Clauses
java_spec
: JAVA NAME CHAR_STRING
;
c_spec
: C_LETTER (NAME CHAR_STRING)? LIBRARY identifier c_agent_in_clause? (WITH CONTEXT)? c_parameters_clause?
;
c_agent_in_clause
: AGENT IN '(' expression (',' expression)* ')'
;
c_parameters_clause
: PARAMETERS '(' (expression (',' expression)* | '.' '.' '.') ')'
;
// $>
parameter
: parameter_name (IN | OUT | INOUT | NOCOPY)* type_spec? default_value_part?
;
default_value_part
: (ASSIGN_OP | DEFAULT) expression
;
// $<PL/SQL Elements Declarations
declare_spec
: variable_declaration
| subtype_declaration
| cursor_declaration
| exception_declaration
| pragma_declaration
| type_declaration
| procedure_spec
| function_spec
| procedure_body
| function_body
;
//incorporates constant_declaration
variable_declaration
: identifier CONSTANT? type_spec (NOT NULL)? default_value_part? ';'
;
subtype_declaration
: SUBTYPE identifier IS type_spec (RANGE expression '..' expression)? (NOT NULL)? ';'
;
//cursor_declaration incorportates curscursor_body and cursor_spec
cursor_declaration
: CURSOR identifier ('(' parameter_spec (',' parameter_spec)* ')' )? (RETURN type_spec)? (IS select_statement)? ';'
;
parameter_spec
: parameter_name (IN? type_spec)? default_value_part?
;
exception_declaration
: identifier EXCEPTION ';'
;
pragma_declaration
: PRAGMA (SERIALLY_REUSABLE
| AUTONOMOUS_TRANSACTION
| EXCEPTION_INIT '(' exception_name ',' numeric_negative ')'
| INLINE '(' id1=identifier ',' expression ')'
| RESTRICT_REFERENCES '(' (identifier | DEFAULT) (',' identifier)+ ')') ';'
;
// $<Record Declaration - Specific Clauses
//incorporates ref_cursor_type_definition
record_type_def
: RECORD '(' field_spec (',' field_spec)* ')'
;
field_spec
: column_name type_spec? (NOT NULL)? default_value_part?
;
ref_cursor_type_def
: REF CURSOR (RETURN type_spec)?
;
// $>
type_declaration
: TYPE identifier IS (table_type_def | varray_type_def | record_type_def | ref_cursor_type_def) ';'
;
table_type_def
: TABLE OF type_spec table_indexed_by_part? (NOT NULL)?
;
table_indexed_by_part
: (idx1=INDEXED | idx2=INDEX) BY type_spec
;
varray_type_def
: (VARRAY | VARYING ARRAY) '(' expression ')' OF type_spec (NOT NULL)?
;
// $>
// $<PL/SQL Statements
seq_of_statements
: (statement (';' | EOF) | label_declaration)+
;
label_declaration
: ltp1= '<' '<' label_name '>' '>'
;
statement
: CREATE swallow_to_semi
| ALTER swallow_to_semi
| GRANT ALL? swallow_to_semi
| TRUNCATE swallow_to_semi
| body
| block
| assignment_statement
| continue_statement
| exit_statement
| goto_statement
| if_statement
| loop_statement
| forall_statement
| null_statement
| raise_statement
| return_statement
| case_statement/*[true]*/
| sql_statement
| function_call
| pipe_row_statement
;
assignment_statement
: (general_element | bind_variable) ASSIGN_OP expression
;
continue_statement
: CONTINUE label_name? (WHEN condition)?
;
exit_statement
: EXIT label_name? (WHEN condition)?
;
goto_statement
: GOTO label_name
;
if_statement
: IF condition THEN seq_of_statements elsif_part* else_part? END IF
;
elsif_part
: ELSIF condition THEN seq_of_statements
;
else_part
: ELSE seq_of_statements
;
loop_statement
: label_name? (WHILE condition | FOR cursor_loop_param)? LOOP seq_of_statements END LOOP label_name?
;
// $<Loop - Specific Clause
cursor_loop_param
: index_name IN REVERSE? lower_bound range='..' upper_bound
| record_name IN (cursor_name expression_list? | '(' select_statement ')')
;
// $>
forall_statement
: FORALL index_name IN bounds_clause sql_statement (SAVE EXCEPTIONS)?
;
bounds_clause
: lower_bound '..' upper_bound
| INDICES OF collection_name between_bound?
| VALUES OF index_name
;
between_bound
: BETWEEN lower_bound AND upper_bound
;
lower_bound
: concatenation
;
upper_bound
: concatenation
;
null_statement
: NULL
;
raise_statement
: RAISE exception_name?
;
return_statement
: RETURN expression?
;
function_call
: CALL? routine_name function_argument?
;
pipe_row_statement
: PIPE ROW '(' expression ')';
body
: BEGIN seq_of_statements (EXCEPTION exception_handler+)? END label_name?
;
// $<Body - Specific Clause
exception_handler
: WHEN exception_name (OR exception_name)* THEN seq_of_statements
;
// $>
trigger_block
: (DECLARE? declare_spec+)? body
;
block
: DECLARE? declare_spec+ body
;
// $>
// $<SQL PL/SQL Statements
sql_statement
: execute_immediate
| data_manipulation_language_statements
| cursor_manipulation_statements
| transaction_control_statements
;
execute_immediate
: EXECUTE IMMEDIATE expression (into_clause using_clause? | using_clause dynamic_returning_clause? | dynamic_returning_clause)?
;
// $<Execute Immediate - Specific Clause
dynamic_returning_clause
: (RETURNING | RETURN) into_clause
;
// $>
// $<DML SQL PL/SQL Statements
data_manipulation_language_statements
: merge_statement
| lock_table_statement
| select_statement
| update_statement
| delete_statement
| insert_statement
| explain_statement
;
// $>
// $<Cursor Manipulation SQL PL/SQL Statements
cursor_manipulation_statements
: close_statement
| open_statement
| fetch_statement
| open_for_statement
;
close_statement
: CLOSE cursor_name
;
open_statement
: OPEN cursor_name expression_list?
;
fetch_statement
: FETCH cursor_name (it1=INTO variable_name (',' variable_name )* | BULK COLLECT INTO variable_name (',' variable_name )*)
;
open_for_statement
: OPEN variable_name FOR (select_statement | expression) using_clause?
;
// $>
// $<Transaction Control SQL PL/SQL Statements
transaction_control_statements
: set_transaction_command
| set_constraint_command
| commit_statement
| rollback_statement
| savepoint_statement
;
set_transaction_command
: SET TRANSACTION
(READ (ONLY | WRITE) | ISOLATION LEVEL (SERIALIZABLE | READ COMMITTED) | USE ROLLBACK SEGMENT rollback_segment_name)?
(NAME quoted_string)?
;
set_constraint_command
: SET (CONSTRAINT | CONSTRAINTS) (ALL | constraint_name (',' constraint_name)*) (IMMEDIATE | DEFERRED)
;
commit_statement
: COMMIT WORK?
(COMMENT expression | FORCE (CORRUPT_XID expression| CORRUPT_XID_ALL | expression (',' expression)?))?
write_clause?
;
write_clause
: WRITE (WAIT|NOWAIT)? (IMMEDIATE|BATCH)?
;
rollback_statement
: ROLLBACK WORK? (TO SAVEPOINT? savepoint_name | FORCE quoted_string)?
;
savepoint_statement
: SAVEPOINT savepoint_name
;
// Dml
/* TODO
//SHOULD BE OVERRIDEN!
compilation_unit
: seq_of_statements* EOF
;
//SHOULD BE OVERRIDEN!
seq_of_statements
: select_statement
| update_statement
| delete_statement
| insert_statement
| lock_table_statement
| merge_statement
| explain_statement
// | case_statement[true]
;
*/
explain_statement
: EXPLAIN PLAN (SET STATEMENT_ID '=' quoted_string)? (INTO tableview_name)?
FOR (select_statement | update_statement | delete_statement | insert_statement | merge_statement)
;
select_statement
: subquery_factoring_clause? subquery (for_update_clause | order_by_clause)*
;
// $<Select - Specific Clauses
subquery_factoring_clause
: WITH factoring_element (',' factoring_element)*
;
factoring_element
: query_name ('(' column_name (',' column_name)* ')')? AS '(' subquery order_by_clause? ')'
search_clause? cycle_clause?
;
search_clause
: SEARCH (DEPTH | BREADTH) FIRST BY column_name ASC? DESC? (NULLS FIRST)? (NULLS LAST)?
(',' column_name ASC? DESC? (NULLS FIRST)? (NULLS LAST)?)* SET column_name
;
cycle_clause
: CYCLE column_name (',' column_name)* SET column_name TO expression DEFAULT expression
;
subquery
: subquery_basic_elements subquery_operation_part*
;