-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstatement_splitter.plsql
More file actions
1512 lines (1357 loc) · 48.7 KB
/
statement_splitter.plsql
File metadata and controls
1512 lines (1357 loc) · 48.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
create or replace package statement_splitter is
--Copyright (C) 2015 Jon Heller. This program is licensed under the LGPLv3.
function split_by_semicolon(p_tokens in token_table) return token_table_table;
function split_by_sqlplus_delimiter(p_statements in clob, p_sqlplus_delimiter in varchar2 default '/') return clob_table;
function split_by_sqlplus_del_and_semi(p_statements in clob, p_sqlplus_delimiter in varchar2 default '/') return token_table_table;
/*
== Purpose ==
Split a string of SQL and PL/SQL statements into individual statements.
SPLIT_BY_SEMICOLON - Use semicolons for all terminators, even for PL/SQL
statements. This mode is useful for IDEs where a "/" in strings causes problems.
SPLIT_BY_SQLPLUS_DELIMITER - Uses a delimiter the way SQL*Plus does - it must
be on a line with only whitespace.
SPLIT_BY_SQLPLUS_DEL_AND_SEMI - Combines the above two.
== Example ==
SPLIT_BY_SEMICOLON:
select rownum, plsql_lexer.concatenate(column_value) statement
from table(
statement_splitter.split_by_semicolon(
plsql_lexer.lex('begin null; end;select * from test2;')
)
);
Results:
* ROWNUM STATEMENT
* ------ ---------
* 1 begin null; end;
* 2 select * from test2;
SPLIT_BY_SQLPLUS_DELIMITER:
select rownum, column_value statement
from table(
statement_splitter.split_by_sqlplus_delimiter(
'begin null; end;'||chr(10)||
'/'||chr(10)||
'select * from test2'
)
);
Results:
* ROWNUM STATEMENT
* ------ ---------
* 1 begin null; end;
* /
* 2 select * from test2;
*/
end;
/
create or replace package body statement_splitter is
C_TERMINATOR_SEMI constant number := 1;
C_TERMINATOR_PLSQL_DECLARATION constant number := 2;
C_TERMINATOR_PLSQL constant number := 3;
C_TERMINATOR_EOF constant number := 4;
--------------------------------------------------------------------------------
procedure add_statement_consume_tokens(
p_split_tokens in out nocopy token_table_table,
p_parse_tree in token_table,
p_terminator number,
p_parse_tree_index in out number,
p_command_name in varchar2
) is
/*
This is a recursive descent parser for PL/SQL.
This link has a good introduction to recursive descent parsers: https://www.cis.upenn.edu/~matuszek/General/recursive-descent-parsing.html)
The functions roughly follow the same order as the "Block" chapater in the 12c PL/SQL Langauge Reference:
http://docs.oracle.com/database/121/LNPLS/block.htm#LNPLS01303
The splitter only needs to know when the statement ends and does not consume
every token in a meaningful way, like a real parser would. For example,
there are many times when tokens can be skipped until the next semicolon.
If Oracle ever allows PLSQL_DECLARATIONS inside PL/SQL code this code will need to
be much more complicated.
*/
-------------------------------------------------------------------------------
--Globals
-------------------------------------------------------------------------------
--v_code clob := 'declare procedure p1 is begin null; end; begin null; end;select * from dual;';
--v_code clob := '<<asdf>>declare a number; procedure p1 is begin null; end; begin null; end;select * from dual;';
--Cursors can have lots of parentheses, and even an "IS" inside them.
--v_code clob := 'declare cursor c(a number default case when (((1 is null))) then 1 else 0 end) is select 1 a from dual; begin null; end;select * from dual;';
--v_code clob := '<<asdf>>begin null; end;';
--SELECT test.
--v_code clob := 'declare a number; select 1 into a from dual; end;';
type string_table is table of varchar2(32767);
type number_table is table of number;
g_debug_lines string_table := string_table();
g_ast_index number := 1;
v_abstract_syntax_tree token_table := token_table();
v_map_between_parse_and_ast number_table := number_table();
--Holds return value of optional functions.
g_optional boolean;
-------------------------------------------------------------------------------
--Forward declarations so that functions can be in the same order as the documentation.
-------------------------------------------------------------------------------
function anything_(p_value varchar2) return boolean;
function anything_before_begin return boolean;
function anything_in_parentheses return boolean;
function anything_up_to_may_include_(p_value varchar2) return boolean;
function anything_up_to_must_include_(p_value varchar2) return boolean;
function basic_loop_statement return boolean;
function body return boolean;
function case_statement return boolean;
function create_procedure return boolean;
function create_function return boolean;
function create_package return boolean;
function create_type_body return boolean;
function create_trigger return boolean;
function cursor_for_loop_statement return boolean;
function declare_section return boolean;
function exception_handler return boolean;
function expression_case_when_then return boolean;
function initialize_section return boolean;
function for_loop_statement return boolean;
function for_each_row return boolean;
function function_definition return boolean;
function if_statement return boolean;
function label return boolean;
function name return boolean;
function name_maybe_schema return boolean;
function nested_table_nt_column_of return boolean;
function p_end return boolean;
function plsql_block return boolean;
function procedure_definition return boolean;
function referencing_clause return boolean;
function statement_or_inline_pragma return boolean;
function trigger_edition_clause return boolean;
function trigger_ordering_clause return boolean;
function when_condition return boolean;
-------------------------------------------------------------------------------
--Helper functions
-------------------------------------------------------------------------------
procedure push(p_line varchar2) is
begin
g_debug_lines.extend;
g_debug_lines(g_debug_lines.count) := p_line;
end;
function pop(p_local_ast_before number default null, p_local_lines_before string_table default null) return boolean is
begin
if p_local_ast_before is null then
g_debug_lines.trim;
else
g_ast_index := p_local_ast_before;
g_debug_lines := p_local_lines_before;
end if;
return false;
end;
procedure pop is
begin
g_debug_lines.trim;
end;
procedure increment(p_increment number default 1) is begin
g_ast_index := g_ast_index + p_increment;
end;
function get_next_(p_value varchar2) return number is begin
for i in g_ast_index .. v_abstract_syntax_tree.count loop
if upper(v_abstract_syntax_tree(i).value) = p_value then
return i;
end if;
end loop;
return null;
end;
function current_value return clob is begin
return upper(v_abstract_syntax_tree(g_ast_index).value);
end;
procedure parse_error(p_syntax_type varchar2) is begin
raise_application_error(-20002, 'Fatal parse error in '||p_syntax_type||' around line #'||
v_abstract_syntax_tree(g_ast_index).line_number||', column #'||
v_abstract_syntax_tree(g_ast_index).column_number||' of the original string.');
end;
function next_value(p_increment number default 1) return clob is begin
begin
return upper(v_abstract_syntax_tree(g_ast_index+p_increment).value);
exception when subscript_beyond_count then
null;
end;
end;
function previous_value(p_decrement number) return clob is begin
begin
if g_ast_index - p_decrement <= 0 then
return null;
else
return upper(v_abstract_syntax_tree(g_ast_index - p_decrement).value);
end if;
exception when subscript_beyond_count then
null;
end;
end;
function current_type return varchar2 is begin
return v_abstract_syntax_tree(g_ast_index).type;
end;
function anything_(p_value varchar2) return boolean is begin
push(p_value);
if current_value = p_value then
increment;
return true;
else
return pop;
end if;
end;
function anything_up_to_may_include_(p_value varchar2) return boolean is begin
push('ANYTHING_UP_TO_MAY_INCLUDE_'||p_value);
begin
loop
if current_value = p_value then
increment;
return true;
end if;
increment;
end loop;
exception when subscript_beyond_count then return true;
end;
end;
function anything_up_to_must_include_(p_value varchar2) return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('ANYTHING_UP_TO_MUST_INCLUDE_'||p_value);
begin
loop
if current_value = p_value then
increment;
return true;
end if;
increment;
end loop;
exception when subscript_beyond_count then null;
end;
return pop(v_local_ast_before, v_local_lines_before);
end;
function anything_before_begin return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('ANYTHING_BUT_BEGIN');
begin
loop
if current_value = 'BEGIN' then
return true;
end if;
increment;
end loop;
exception when subscript_beyond_count then null;
end;
return pop(v_local_ast_before, v_local_lines_before);
end;
function anything_in_parentheses return boolean is v_paren_counter number; begin
push('ANYTHING_IN_PARENTHESES');
if anything_('(') then
v_paren_counter := 1;
while v_paren_counter >= 1 loop
if current_value = '(' then
v_paren_counter := v_paren_counter + 1;
elsif current_value = ')' then
v_paren_counter := v_paren_counter - 1;
end if;
increment;
end loop;
return true;
end if;
return pop;
end;
-------------------------------------------------------------------------------
--Production rules that consume tokens and return true or false if rule was found.
-------------------------------------------------------------------------------
function plsql_block return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('PLSQL_BLOCK');
g_optional := label;
if anything_('DECLARE') then
g_optional := declare_section;
if body then
return true;
else
return pop(v_local_ast_before, v_local_lines_before);
end if;
elsif body then
return true;
else
return pop(v_local_ast_before, v_local_lines_before);
end if;
end;
function label return boolean is begin
push('LABEL');
if current_value = '<<' then
loop
increment;
if current_value = '>>' then
increment;
return true;
end if;
end loop;
end if;
return pop;
end;
function declare_section return boolean is begin
push('DECLARE_SECTION');
if current_value in ('BEGIN', 'END') then
return pop;
else
loop
if current_value in ('BEGIN', 'END') then
return true;
end if;
--Of the items in ITEM_LIST_1 and ITEM_LIST_2, only
--these two require any special processing.
if procedure_definition then null;
elsif function_definition then null;
elsif anything_up_to_may_include_(';') then null;
end if;
end loop;
end if;
end;
function body return boolean is begin
push('BODY');
if anything_('BEGIN') then
g_optional := statement_or_inline_pragma;
while statement_or_inline_pragma loop null; end loop;
if anything_('EXCEPTION') then
while exception_handler loop null; end loop;
end if;
g_optional := p_end;
return true;
end if;
return pop;
end;
function initialize_section return boolean is begin
push('BODY');
if anything_('BEGIN') then
g_optional := statement_or_inline_pragma;
while statement_or_inline_pragma loop null; end loop;
if anything_('EXCEPTION') then
while exception_handler loop null; end loop;
end if;
return true;
end if;
return pop;
end;
function procedure_definition return boolean is begin
push('PROCEDURE_DEFINITION');
--Exclude CTE queries that create a table expression named "PROCEDURE".
if current_value = 'PROCEDURE' and next_value not in ('AS', '(') then
g_optional := anything_before_begin; --Don't need the header information.
return body;
end if;
return pop;
end;
function function_definition return boolean is begin
push('FUNCTION_DEFINITION');
--Exclude CTE queries that create a table expression named "FUNCTION".
if current_value = 'FUNCTION' and next_value not in ('AS', '(') then
g_optional := anything_before_begin; --Don't need the header information.
return body;
end if;
return pop;
end;
function name return boolean is begin
push('NAME');
if current_type = plsql_lexer.c_word then
increment;
return true;
end if;
return pop;
end;
function name_maybe_schema return boolean is begin
push('NAME_MAYBE_SCHEMA');
if name then
if anything_('.') then
g_optional := name;
end if;
return true;
end if;
return pop;
end;
function statement_or_inline_pragma return boolean is begin
push('STATEMENT_OR_INLINE_PRAGMA');
if label then return true;
--Types that might have more statements:
elsif basic_loop_statement then return true;
elsif case_statement then return true;
elsif for_loop_statement then return true;
elsif cursor_for_loop_statement then return true;
elsif if_statement then return true;
elsif plsql_block then return true;
--Anything else
elsif current_value not in ('EXCEPTION', 'END', 'ELSE', 'ELSIF') then
return anything_up_to_may_include_(';');
end if;
return pop;
end;
function p_end return boolean is begin
push('P_END');
if current_value = 'END' then
increment;
g_optional := name;
if current_type = ';' then
increment;
end if;
return true;
end if;
return pop;
end;
function exception_handler return boolean is begin
push('EXCEPTION_HANDLER');
if current_value = 'WHEN' then
g_optional := anything_up_to_must_include_('THEN');
while statement_or_inline_pragma loop null; end loop;
return true;
end if;
return pop;
end;
function basic_loop_statement return boolean is begin
push('BASIC_LOOP_STATEMENT');
if current_value = 'LOOP' then
increment;
while statement_or_inline_pragma loop null; end loop;
if current_value = 'END' then
increment;
if current_value = 'LOOP' then
increment;
g_optional := name;
if current_value = ';' then
increment;
return true;
end if;
end if;
end if;
parse_error('BASIC_LOOP_STATEMENT');
end if;
return pop;
end;
function for_loop_statement return boolean is begin
push('FOR_LOOP_STATEMENT');
if current_value = 'FOR' and get_next_('..') < get_next_(';') then
g_optional := anything_up_to_must_include_('LOOP');
while statement_or_inline_pragma loop null; end loop;
if current_value = 'END' then
increment;
if current_value = 'LOOP' then
increment;
g_optional := name;
if current_value = ';' then
increment;
return true;
end if;
end if;
end if;
parse_error('FOR_LOOP_STATEMENT');
else
return pop;
end if;
end;
function cursor_for_loop_statement return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CURSOR_FOR_LOOP_STATEMENT');
if current_value = 'FOR' then
increment;
if name then
if current_value = 'IN' then
increment;
g_optional := name;
if current_value = '(' then
g_optional := anything_in_parentheses;
if current_value = 'LOOP' then
increment;
while statement_or_inline_pragma loop null; end loop;
if current_value = 'END' then
increment;
if current_value = 'LOOP' then
increment;
g_optional := name;
if current_value = ';' then
increment;
return true;
end if;
end if;
end if;
end if;
end if;
end if;
end if;
parse_error('CURSOR_FOR_LOOP_STATEMENT');
else
return pop(v_local_ast_before, v_local_lines_before);
end if;
end;
procedure case_expression is begin
push('CASE_EXPRESSION');
loop
if anything_('CASE') then
case_expression;
return;
elsif anything_('END') then
return;
else
increment;
end if;
end loop;
pop;
end;
function expression_case_when_then return boolean is begin
push('EXPRESSION_CASE_WHEN_THEN');
loop
if current_value = 'CASE' then
case_expression;
elsif current_value = 'WHEN' or current_value = 'THEN' then
return true;
else
increment;
end if;
end loop;
return pop;
end;
function case_statement return boolean is begin
push('CASE_STATEMENT');
if anything_('CASE') then
--Searched case.
if current_value = 'WHEN' then
while anything_('WHEN') and expression_case_when_then and anything_('THEN') loop
while statement_or_inline_pragma loop null; end loop;
end loop;
if anything_('ELSE') then
while statement_or_inline_pragma loop null; end loop;
end if;
if anything_('END') and anything_('CASE') and (name or not name) and anything_(';') then
return true;
end if;
parse_error('SEARCHED_CASE_STATEMENT');
--Simple case.
else
if expression_case_when_then then
while anything_('WHEN') and expression_case_when_then and anything_('THEN') loop
while statement_or_inline_pragma loop null; end loop;
end loop;
if anything_('ELSE') then
while statement_or_inline_pragma loop null; end loop;
end if;
if anything_('END') and anything_('CASE') and (name or not name) and anything_(';') then
return true;
end if;
end if;
parse_error('SIMPLE_CASE_STATEMENT');
end if;
else
return pop;
end if;
end;
function if_statement return boolean is begin
push('IF_STATEMENT');
if anything_('IF') then
if expression_case_when_then and anything_('THEN') then
while statement_or_inline_pragma loop null; end loop;
while anything_('ELSIF') and expression_case_when_then and anything_('THEN') loop
while statement_or_inline_pragma loop null; end loop;
end loop;
if anything_('ELSE') then
while statement_or_inline_pragma loop null; end loop;
end if;
if anything_('END') and anything_('IF') and anything_(';') then
return true;
end if;
end if;
parse_error('IF_STATEMENT');
end if;
return pop;
end;
function create_or_replace_edition return boolean is begin
push('CREATE_OR_REPLACE_EDITION');
if anything_('CREATE') then
g_optional := anything_('OR');
g_optional := anything_('REPLACE');
g_optional := anything_('EDITIONABLE');
g_optional := anything_('NONEDITIONABLE');
return true;
end if;
return pop;
end;
function create_procedure return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CREATE_PROCEDURE');
if create_or_replace_edition and anything_('PROCEDURE') and name_maybe_schema then
g_optional := anything_in_parentheses;
if anything_up_to_must_include_('IS') or anything_up_to_must_include_('AS') then
if anything_('EXTERNAL') or anything_('LANGUAGE') then
g_optional := anything_up_to_may_include_(';');
return true;
elsif plsql_block then
return true;
end if;
end if;
parse_error('CREATE_PROCEDURE');
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function create_function return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CREATE_FUNCTION');
if create_or_replace_edition and anything_('FUNCTION') and name_maybe_schema then
--Consume everything between the function name and either AGGREGATE|PIPELINED USING
--or the last IS/AS.
--This is necessary to exclude some options that may include another IS, such as
--expressions in the PARALLEL_ENABLE_CLAUSE.
loop
if current_value in ('AGGREGATE', 'PIPELINED') and next_value = 'USING' then
--This one is simple, return true.
increment(2);
g_optional := anything_up_to_may_include_(';');
return true;
elsif current_value = '(' then
g_optional := anything_in_parentheses;
elsif current_value in ('IS', 'AS') then
increment;
exit;
else
increment;
end if;
end loop;
--There must have been an IS or AS to get here:
if anything_('EXTERNAL') then
g_optional := anything_up_to_may_include_(';');
return true;
elsif anything_('LANGUAGE') then
g_optional := anything_up_to_may_include_(';');
return true;
else
return plsql_block;
end if;
parse_error('CREATE_FUNCTION');
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function create_package return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CREATE_PACKAGE');
if create_or_replace_edition and anything_('PACKAGE') and name_maybe_schema then
g_optional := anything_in_parentheses;
if anything_up_to_must_include_('IS') or anything_up_to_must_include_('AS') then
loop
if anything_('END') then
g_optional := name;
g_optional := anything_(';');
return true;
else
g_optional := anything_up_to_may_include_(';');
end if;
end loop;
end if;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function create_package_body return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CREATE_PACKAGE_BODY');
if create_or_replace_edition and anything_('PACKAGE') and anything_('BODY') and name_maybe_schema then
if anything_('IS') or anything_('AS') then
g_optional := declare_section;
g_optional := initialize_section;
if anything_('END') then
g_optional := name;
g_optional := anything_(';');
return true;
end if;
end if;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function create_type_body return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('CREATE_TYPE_BODY');
if create_or_replace_edition and anything_('TYPE') and anything_('BODY') and name_maybe_schema then
g_optional := anything_in_parentheses;
if anything_up_to_must_include_('IS') or anything_up_to_must_include_('AS') then
loop
if anything_('END') and anything_(';') then
return true;
elsif current_value in ('MAP', 'ORDER', 'MEMBER') then
g_optional := anything_('MAP');
g_optional := anything_('ORDER');
g_optional := anything_('MEMBER');
if procedure_definition or function_definition then
null;
end if;
elsif current_value in ('FINAL', 'INSTANTIABLE', 'CONSTRUCTOR') then
g_optional := anything_('FINAL');
g_optional := anything_('INSTANTIABLE');
g_optional := anything_('CONSTRUCTOR');
g_optional := function_definition;
else
g_optional := anything_up_to_may_include_(';');
end if;
end loop;
end if;
parse_error('CREATE_TYPE_BODY');
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function dml_event_clause return boolean is
function update_of_column return boolean is begin
if anything_('UPDATE') then
if anything_('OF') and name then
while anything_(',') and name loop null; end loop;
return true;
else
return true;
end if;
end if;
return false;
end;
begin
push('DML_EVENT_CLAUSE');
if anything_('DELETE') or anything_('INSERT') or update_of_column then
while anything_('OR') and (anything_('DELETE') or anything_('INSERT') or update_of_column) loop null; end loop;
if anything_('ON') and name_maybe_schema then
return true;
end if;
parse_error('DML_EVENT_CLAUSE');
end if;
return pop;
end;
function referencing_clause return boolean is begin
push('REFERENCING_CLAUSE');
if anything_('REFERENCING') then
if anything_('OLD') or anything_('NEW') or anything_('PARENT') then
g_optional := anything_('AS');
g_optional := name;
while anything_('OLD') or anything_('NEW') or anything_('PARENT') loop
g_optional := anything_('AS');
g_optional := name;
end loop;
return true;
end if;
parse_error('REFERENCING_CLAUSE');
end if;
return pop;
end;
function for_each_row return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('FOR_EACH_ROW');
if anything_('FOR') and anything_('EACH') and anything_('ROW') then
return true;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function trigger_edition_clause return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('TRIGGER_EDITION_CLAUSE');
if anything_('FORWARD') or anything_('REVERSE') then
null;
end if;
if anything_('CROSSEDITION') then
return true;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function trigger_ordering_clause return boolean is begin
push('TRIGGER_ORDERING_CLAUSE');
if anything_('FOLLOWS') or anything_('PRECEDES') then
if name_maybe_schema then
while anything_(',') and name_maybe_schema loop null; end loop;
return true;
end if;
parse_error('TRIGGER_ORDERING_CLAUSE');
end if;
return pop;
end;
function when_condition return boolean is begin
push('WHEN_CONDITION');
if anything_('WHEN') then
if anything_in_parentheses then
return true;
end if;
parse_error('WHEN_CONDITION');
end if;
return pop;
end;
function trigger_body return boolean is begin
push('TRIGGER_BODY');
if anything_('CALL') then
g_optional := anything_up_to_may_include_(';');
return true;
elsif plsql_block then return true;
end if;
return pop;
end;
function delete_insert_update_or return boolean is begin
push('DELETE_INSERT_UPDATE_OR');
if anything_('DELETE') or anything_('INSERT') or anything_('UPDATE') then
while anything_('OR') and (anything_('DELETE') or anything_('INSERT') or anything_('UPDATE')) loop null; end loop;
return true;
end if;
return pop;
end;
function nested_table_nt_column_of return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('NESTED_TABLE_NT_COLUMN_OF');
if anything_('NESTED') and anything_('TABLE') and name and anything_('OF') then
return true;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function timing_point return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('TIMING_POINT');
if current_value = 'BEFORE' and next_value = 'STATEMENT' then
increment(2);
return true;
elsif current_value = 'BEFORE' and next_value = 'EACH' and next_value(2) = 'ROW' then
increment(3);
return true;
elsif current_value = 'AFTER' and next_value = 'STATEMENT' then
increment(2);
return true;
elsif current_value = 'AFTER' and next_value = 'EACH' and next_value(2) = 'ROW' then
increment(3);
return true;
elsif current_value = 'INSTEAD' and next_value = 'OF' and next_value(2) = 'EACH' and next_value(3) = 'ROW' then
increment(4);
return true;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function tps_body return boolean is begin
push('TPS_BODY');
if statement_or_inline_pragma then
while statement_or_inline_pragma loop null; end loop;
if anything_('EXCEPTION') then
while exception_handler loop null; end loop;
end if;
return true;
end if;
return pop;
end;
function timing_point_section return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('TIMING_POINT_SECTION');
if timing_point and anything_('IS') and anything_('BEGIN') and tps_body and anything_('END') and timing_point and anything_(';') then
return true;
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function compound_trigger_block return boolean is
--Similar to the regular DECLARE_SECTION but also stops at timing point keywords.
function declare_section return boolean is begin
push('DECLARE_SECTION');
if current_value in ('BEGIN', 'END', 'BEFORE', 'AFTER', 'INSTEAD') then
return pop;
else
loop
if current_value in ('BEGIN', 'END', 'BEFORE', 'AFTER', 'INSTEAD') then
return true;
end if;
--Of the items in ITEM_LIST_1 and ITEM_LIST_2, only
--these two require any special processing.
if procedure_definition then null;
elsif function_definition then null;
elsif anything_up_to_may_include_(';') then null;
end if;
end loop;
end if;
end;
begin
push('COMPOUND_TRIGGER_BLOCK');
if anything_('COMPOUND') and anything_('TRIGGER') then
g_optional := declare_section;
if timing_point_section then
while timing_point_section loop null; end loop;
if anything_('END') then
g_optional := name;
if anything_(';') then
return true;
end if;
end if;
end if;
parse_error('COMPOUND_TRIGGER_BLOCK');
end if;
return pop;
end;
function ddl_or_database_event return boolean is begin
push('DDL_OR_DATABASE_EVENT');
if current_value in
(
'ALTER', 'ANALYZE', 'AUDIT', 'COMMENT', 'CREATE', 'DROP', 'GRANT', 'NOAUDIT',
'RENAME', 'REVOKE', 'TRUNCATE', 'DDL', 'STARTUP', 'SHUTDOWN', 'DB_ROLE_CHANGE',
'SERVERERROR', 'LOGON', 'LOGOFF', 'SUSPEND', 'CLONE', 'UNPLUG'
) then
increment;
return true;
elsif current_value||' '|| next_value in
(
'ASSOCIATE STATISTICS', 'DISASSOCIATE STATISTICS', 'SET CONTAINER'
) then
increment(2);
return true;
end if;
return pop;
end;
function simple_dml_trigger return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('SIMPLE_DML_TRIGGER');
if (anything_('BEFORE') or anything_('AFTER')) and dml_event_clause then
g_optional := referencing_clause;
g_optional := for_each_row;
g_optional := trigger_edition_clause;
g_optional := trigger_ordering_clause;
if anything_('ENABLE') or anything_('DISABLE') then
null;
end if;
g_optional := when_condition;
if trigger_body then
return true;
end if;
parse_error('SIMPLE_DML_TRIGGER');
end if;
return pop(v_local_ast_before, v_local_lines_before);
end;
function instead_of_dml_trigger return boolean is v_local_ast_before number := g_ast_index; v_local_lines_before string_table := g_debug_lines; begin
push('INSTEAD_OF_DML_TRIGGER');
if anything_('INSTEAD') and anything_('OF') and delete_insert_update_or then
if anything_('ON') then
g_optional := nested_table_nt_column_of;
if name_maybe_schema then
g_optional := referencing_clause;
g_optional := for_each_row;
g_optional := trigger_edition_clause;
g_optional := trigger_ordering_clause;
if anything_('ENABLE') or anything_('DISABLE') then
null;
end if;