-
Notifications
You must be signed in to change notification settings - Fork 8
/
p_stack.9.sql
1068 lines (1024 loc) · 44.8 KB
/
p_stack.9.sql
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 p_stack authid current_user is
-- Returns a call stack. The call of this procedure is included and is at the first line.
-- tDepth: which line of stack to show. Lesser numbers are most recent calls. Numeration starts from 1.
-- The call of this procedure has depth = 1.
-- If tDepth is null then the whole stack is returned as a string where lines are delimited by "new line" symbol.
-- Output format: LINE || ': ' || OWNER || '.' || PROGRAM TYPE || [ ' ' || PROGRAM NAME ]? || [ '.' || SUBPROGRAM TYPE || ' ' || SUBPROGRAM NAME ]*
-- LINE is a source code line number as returned by dbms_utility.format_call_stack.
-- OWNER is an owner of program unit being called, or parsing schema name for anonymous blocks.
-- If the parsing schema name could not be retrieved then OWNER equals to current schema name.
-- PROGRAM TYPE is one of ANONYMOUS BLOCK, PACKAGE, PACKAGE BODY, TYPE BODY, TRIGGER, PROCEDURE, FUNCTION.
-- It's the type of outermost program unit.
-- PROGRAM NAME is the name of program unit being called as returned by dbms_utility.format_call_stack.
-- It's absent for anonymous blocks.
-- It's double quoted if the source code contains its name in double quotes.
-- SUBPROGRAM TYPE is one of PROCEDURE or FUNCTION.
-- It's the type of inner subprogram.
-- SUBPROGRAM NAME is the name of inner subprogram.
-- If there are several inner units then all of them are separated by dots.
function getCallStack( tDepth in number default null ) return varchar2;
-- Returns the stack information of a current program unit.
-- See output format description of function getCallStack.
function whoAmI return varchar2;
-- Returns the stack information of a program unit which has called currently executing code.
-- See output format description of function getCallStack.
function whoCalledMe return varchar2;
-- Returns one stack line at a given depth.
-- See output format description of function getCallStack.
-- tCallStack: information returned by getCallStack.
-- tDepth: number of requested line. If tDepth is out of bounds then null is returned.
function getCallStackLine( tCallStack in varchar2, tDepth in number ) return varchar2;
-- Returns current stack depth including the call of this function.
-- tCallStack: information returned by getCallStack. Can be omitted.
-- See: utl_call_stack.dynamic_depth
function getDynamicDepth( tCallStack in varchar2 default '' ) return number;
-- Returns a depth of a current program unit.
-- Stored procedures, functions, packages and their bodies, type bodies, triggers and anonymous blocks have a depth equal to 0.
-- Inner procedures and functions have depth equal to 1 + depth of parent program unit.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.lexical_depth
function getLexicalDepth( tCallStack in varchar2, tDepth in number default null ) return number;
-- Returns a source line number as returned by dbms_utility.format_call_stack.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.unit_line
function getUnitLine( tCallStack in varchar2, tDepth in number default null ) return number;
-- Returns owner of a requested program unit, or a parsing schema for an anonymous block.
-- If the parsing schema name could not be retrieved then OWNER equals to current schema name.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.owner
function getOwner( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns name of a requested stored procedure. Empty string for anonymous block.
-- The name returned won't contain double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getProgram( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns a type of a requested stored procedure. One of:
-- ANONYMOUS BLOCK, PROCEDURE, FUNCTION, TRIGGER, PACKAGE, PACKAGE BODY, TYPE BODY.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.unit_type
function getProgramType( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns a name of a requested innermost subprogram.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogram are called inside the function.
-- "inner_function" would be returned (without double quotes).
-- If getCallStack and getSubprogram are called inside package body then empty string is returned.
-- The name returned never contains double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.subprogram
function getSubprogram( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns a type of requested innermost subprogram.
-- See example in getSubprogram describing the concept of innermost subprogram.
-- Value returned is one of PROCEDURE or FUNCTION.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getSubprogramType( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns a table of names of requested subprograms.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprograms are called inside the function.
-- An array [ "inner", "inner_function" ] would be returned (without double quotes).
-- If getCallStack and getSubprograms are called inside package body then an empty table is returned.
-- The names returned never contain double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.subprogram
function getSubprograms( tCallStack in varchar2, tDepth in number default null ) return str_table;
-- Returns a table of types of requested subprograms.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogramsTypes are called inside the function.
-- An array [ "PROCEDURE", "FUNCTION" ] would be returned (without double quotes).
-- If getCallStack and getSubprogramsTypes are called inside package body then an empty table is returned.
-- Each value in the returned array is one of PROCEDURE or FUNCTION.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getSubprogramsTypes( tCallStack in varchar2, tDepth in number default null ) return str_table;
-- Returns the hierarchy of names separated by dot from outermost to innermost program unit.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogram are called inside the function.
-- "outer.inner.inner_function" would be returned (without double quotes).
-- If one of program units in this hierarchy has double quotes in its name, they would be preserved.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.concatenate_subprogram
function getConcatenatedSubprograms( tCallStack in varchar2, tDepth in number default null ) return varchar2;
-- Returns an error stack as a string.
-- tDepth: number of requested stack line. Lesser numbers are most recent errors. Numeration starts from 1.
-- If omitted then the full stack is concatenated via newline character.
-- The full stack output equals to dbms_utility.format_call_stack.
-- See: dbms_utility.format_call_stack
function getErrorStack( tDepth in number default null ) return varchar2;
-- Returns current error stack depth.
-- tErrorStack: information returned by getErrorStack. Can be omitted.
-- See: utl_call_stack.error_depth
function getErrorDepth( tErrorStack in varchar2 default '' ) return number;
-- Returns error code at a given depth.
-- tErrorStack: information returned by getErrorStack.
-- tDepth: number of requested stack line if tErrorStack = getErrorStack, null otherwise.
-- See: utl_call_stack.error_number
function getErrorCode( tErrorStack in varchar2, tDepth in number default null ) return number;
-- Returns error message at a given depth.
-- tErrorStack: information returned by getErrorStack.
-- tDepth: number of requested stack line if tErrorStack = getErrorStack, null otherwise.
-- See: utl_call_stack.error_msg
function getErrorMessage( tErrorStack in varchar2, tDepth in number default null ) return varchar2;
end;
/
create or replace package body p_stack is
CHAR_BACKSPACE constant char := chr( 8 );
CHAR_TAB constant char := chr( 9 );
CHAR_NEW_LINE constant char := chr( 10 );
CHAR_CARRIAGE_RETURN constant char := chr( 13 );
UNKNOWN_OWNER constant varchar2( 4 ) := 'NULL';
PACKAGE_NAME constant varchar2( 7 ) := 'P_STACK';
cursor getSource( tOwner in varchar2, tName in varchar2, tType in varchar2, tLine in number ) is
select TEXT, LINE
from ALL_SOURCE
where OWNER = tOwner
and NAME = tName
and TYPE = tType
and LINE <= tLine
order by LINE;
-- Retrieves info from one line of dbms_utility.format_call_stack output
-- tCallPositionsLine: one line of dbms_utility.format_call_stack output
-- tHandle: CHILD_ADDRESS of source code in V$SQL view
-- tLine: line number in source code
-- tType: program type (see available values in getProgramType description)
-- tOwner: schema of stored procedure
-- tName: name of stored procedure
-- Private function.
procedure parseCallStackLine( tCallPositionsLine in varchar2, tHandle out raw, tLine out number, tType out varchar2, tOwner out varchar2, tName out varchar2 ) is
pos pls_integer;
tCallLine varchar2( 255 ) := tCallPositionsLine;
begin
pos := instr( tCallLine, ' ' );
if pos > 0 then
tHandle := hextoraw( replace( substr( tCallLine, 1, pos - 1 ), '0x', '' ) );
tCallLine := ltrim( substr( tCallLine, pos ) );
else
tHandle := null;
end if;
pos := instr( tCallLine, ' ' );
if pos > 0 then
tLine := to_number( substr( tCallLine, 1, pos - 1 ) );
tCallLine := ltrim( substr( tCallLine, pos ) );
else
tLine := null;
end if;
tType := case substr( tCallLine, 1, 3 )
when 'pro' then 'PROCEDURE'
when 'fun' then 'FUNCTION'
when 'tri' then 'TRIGGER'
when 'typ' then 'TYPE BODY'
when 'pac' then case when tCallLine like 'package body%' then 'PACKAGE BODY' else 'PACKAGE' end
else 'ANONYMOUS BLOCK'
end;
tCallLine := substr( tCallLine, length( tType ) + 2 );
pos := instr( tCallLine, '.' );
if pos > 0 then
tOwner := substr( tCallLine, 1, pos - 1 );
tName := substr( tCallLine, pos + 1 );
else
tOwner := '';
tName := '';
end if;
end;
-- Remove subsequences inside double quotes (quotes themselves are removed as well)
-- Private function.
function cutQuotes( str in varchar2 ) return varchar2 is
ret varchar2( 4000 ) := str;
pos_from pls_integer;
pos_to pls_integer;
begin
loop
pos_from := instr( ret, '"' );
if pos_from is null or pos_from = 0 then
return ret;
end if;
pos_to := instr( ret, '"', pos_from + 1 );
if pos_to is null or pos_to = 0 then
return ret;
end if;
ret := substr( ret, 1, pos_from - 1 ) || substr( ret, pos_to + 1 );
end loop;
end;
-- Returns a call stack. The call of this procedure is included and is at the first line.
-- tDepth: which line of stack to show. Lesser numbers are most recent calls. Numeration starts from 1.
-- The call of this procedure has depth = 1.
-- If tDepth is null then the whole stack is returned as a string where lines are delimited by "new line" symbol.
-- Output format: LINE || ': ' || OWNER || '.' || PROGRAM TYPE || [ ' ' || PROGRAM NAME ]? || [ '.' || SUBPROGRAM TYPE || ' ' || SUBPROGRAM NAME ]*
-- LINE is a source code line number as returned by dbms_utility.format_call_stack.
-- OWNER is an owner of program unit being called, or parsing schema name for anonymous blocks.
-- If the parsing schema name could not be retrieved then OWNER equals to current schema name.
-- PROGRAM TYPE is one of ANONYMOUS BLOCK, PACKAGE, PACKAGE BODY, TYPE BODY, TRIGGER, PROCEDURE, FUNCTION.
-- It's the type of outermost program unit.
-- PROGRAM NAME is the name of program unit being called as returned by dbms_utility.format_call_stack.
-- It's absent for anonymous blocks.
-- It's double quoted if the source code contains its name in double quotes.
-- SUBPROGRAM TYPE is one of PROCEDURE or FUNCTION.
-- It's the type of inner subprogram.
-- SUBPROGRAM NAME is the name of inner subprogram.
-- If there are several inner units then all of them are separated by dots.
function getCallStack( tDepth in number default null ) return varchar2 is
tCallPositions varchar2( 4000 );
tCallPositionsLine varchar2( 255 );
tReached pls_integer;
tHandle raw( 16 );
tHashValue number;
tOwnerId number;
tOwner varchar2( 255 );
tName varchar2( 255 );
tLine number;
tCurrentLine number;
tType varchar2( 255 );
tAnonymousBlock pls_integer;
tSqlFullText clob;
s varchar2( 4000 );
t varchar2( 4000 );
c char;
pos pls_integer;
len pls_integer;
tCommented pls_integer;
tIdentifier pls_integer;
tIdentifierName varchar2( 30 );
tIdentifiers dbms_sql.varchar2_table;
tMaxIdentifier pls_integer;
tQuoteDelimiter char;
tString pls_integer;
tToken varchar2( 4000 );
tTokenType pls_integer;
tPreviousTokenType pls_integer;
tTokensQueue str_table;
tCallStack str_table;
tLast pls_integer;
tLookForDefinition pls_integer;
tPrevToken varchar2( 4000 );
tPrevPrevToken varchar2( 4000 );
tCallStackLine varchar2( 4000 );
ret varchar2( 4000 );
begin
tCallPositions := dbms_utility.format_call_stack;
tReached := 0;
loop
pos := instr( tCallPositions, CHAR_NEW_LINE );
exit when pos is null or pos = 0;
tCallPositionsLine := substr( tCallPositions, 1, pos - 1 );
tCallPositions := substr( tCallPositions, pos + 1 );
if tReached = 0 or tDepth != tReached then
goto NEXT_CALL_POSITION;
end if;
parseCallStackLine( tCallPositionsLine, tHandle, tLine, tType, tOwner, tName );
tAnonymousBlock := case when tType = 'ANONYMOUS BLOCK' then 1 else 0 end;
tCommented := 0;
tIdentifier := 0;
tString := 0;
tMaxIdentifier := 0;
tQuoteDelimiter := null;
tLookForDefinition := 0;
tIdentifiers.delete;
tTokensQueue := str_table( '', '', '' );
if tAnonymousBlock = 0 then
if getSource%isopen then
close getSource;
end if;
open getSource( tOwner, tName, tType, tLine );
tCallStack := str_table( '' );
else
if tHandle is null then
tSqlFullText := null;
else
begin
select ADDRESS, HASH_VALUE, PARSING_SCHEMA_ID
into tHandle, tHashValue, tOwnerId
from V$SQL
where CHILD_ADDRESS = tHandle;
execute immediate
'select replace( replace( XMLAgg( XMLElement( "e", SQL_TEXT ) order by PIECE ).getClobVal(), ''</e>'' ), ''<e>'' ) as SQL_FULL_TEXT
from V$SQLTEXT_WITH_NEWLINES
where ADDRESS = :tHandle
and HASH_VALUE = :tHashValue'
into tSqlFullText
using tHandle, tHashValue;
tSqlFullText := dbms_xmlgen.convert( tSqlFullText, 1 );
begin
select USERNAME
into tOwner
from ALL_USERS
where USER_ID = tOwnerId;
exception
when NO_DATA_FOUND then
null;
end;
exception
when NO_DATA_FOUND then
tSqlFullText := null;
end;
end if;
tCurrentLine := 0;
tCallStack := str_table( '', 'ANON' );
end if;
loop
if tAnonymousBlock = 0 then
fetch getSource into s, tCurrentLine;
exit when getSource%notfound;
else
exit when tCurrentLine = tLine;
pos := instr( tSqlFullText, CHAR_NEW_LINE );
if pos > 0 then
s := substr( tSqlFullText, 1, pos - 1 );
tSqlFullText := substr( tSqlFullText, pos + 1 );
elsif pos = 0 then
s := tSqlFullText;
tSqlFullText := null;
else
exit;
end if;
tCurrentLine := tCurrentLine + 1;
end if;
t := '';
len := length( s );
if len is null then
goto NEXT_LINE;
end if;
pos := 1;
loop
c := substr( s, pos, 1 );
if tCommented = 1 then
case c
when '*' then
if substr( s, pos + 1, 1 ) = '/' then
pos := pos + 1;
tCommented := 0;
end if;
else
null;
end case;
goto NEXT_CHAR;
end if;
if tIdentifier = 1 then
case c
when '"' then
tIdentifier := 0;
tMaxIdentifier := tMaxIdentifier + 1;
while upper( s ) like '%ID' || tMaxIdentifier || '%' loop
tMaxIdentifier := tMaxIdentifier + 1;
end loop;
tIdentifiers( tMaxIdentifier ) := tIdentifierName;
t := t || 'ID' || tMaxIdentifier || ' ';
tIdentifierName := '';
else
tIdentifierName := tIdentifierName || c;
end case;
goto NEXT_CHAR;
end if;
if tString = 1 then
if tQuoteDelimiter is null then
if c = '''' then
if substr( s, pos + 1, 1 ) = '''' then
pos := pos + 1;
else
tString := 0;
end if;
end if;
elsif c = tQuoteDelimiter then
if substr( s, pos + 1, 1 ) = '''' then
tString := 0;
tQuoteDelimiter := null;
pos := pos + 1;
end if;
end if;
goto NEXT_CHAR;
end if;
c := upper( c );
case c
when '/' then
if substr( s, pos + 1, 1 ) = '*' then
pos := pos + 1;
tCommented := 1;
c := ' ';
end if;
when '-' then
if substr( s, pos + 1, 1 ) = '-' then
goto NEXT_LINE;
end if;
when '"' then
tIdentifier := 1;
c := ' ';
when 'N' then
case upper( substr( s, pos + 1, 1 ) )
when 'Q' then
if substr( s, pos + 2, 1 ) = '''' then
tQuoteDelimiter := substr( s, pos + 3, 1 );
tQuoteDelimiter := case tQuoteDelimiter when '(' then ')' when '[' then ']' when '<' then '>' when '{' then '}' else tQuoteDelimiter end;
tString := 1;
pos := pos + 3;
t := t || ' STRING ';
goto NEXT_CHAR;
end if;
when '''' then
tString := 1;
pos := pos + 1;
t := t || ' STRING ';
goto NEXT_CHAR;
else
null;
end case;
when 'Q' then
if substr( s, pos + 1, 1 ) = '''' then
tQuoteDelimiter := substr( s, pos + 2, 1 );
tQuoteDelimiter := case tQuoteDelimiter when '(' then ')' when '[' then ']' when '<' then '>' when '{' then '}' else tQuoteDelimiter end;
tString := 1;
pos := pos + 2;
t := t || ' STRING ';
goto NEXT_CHAR;
end if;
when '''' then
tString := 1;
t := t || ' STRING ';
goto NEXT_CHAR;
else
null;
end case;
t := t || c;
<<NEXT_CHAR>>
pos := pos + 1;
exit when pos > len;
end loop;
<<NEXT_LINE>>
s := t;
len := length( s );
if len is not null then
tToken := '';
pos := 1;
tPreviousTokenType := 3;
loop
if pos > len then
tTokenType := 3;
else
c := substr( s, pos, 1 );
tTokenType := case
when c = '$'
or c = '_'
or c = '#'
or c between '0' and '9'
or c between 'A' and 'Z'
then 1
when c = ';'
then 2
when c = ' '
or c = CHAR_BACKSPACE
or c = CHAR_TAB
or c = CHAR_NEW_LINE
or c = CHAR_CARRIAGE_RETURN
then 3
else 4
end;
end if;
if tTokenType != tPreviousTokenType then
if tPreviousTokenType != 3 then
tTokensQueue.delete( tTokensQueue.first );
tTokensQueue.extend;
tLast := tTokensQueue.last;
tTokensQueue( tLast ) := tToken;
exit when tCurrentLine = tLine and tToken = PACKAGE_NAME;
tPrevToken := tTokensQueue( tLast - 1 );
tPrevPrevToken := tTokensQueue( tLast - 2 );
if tToken = 'CASE' then
if tPrevToken in ( '>>', ';', 'BEGIN', 'LOOP' )
or tPrevToken in ( 'ELSE', 'THEN' ) and tCallStack( tCallStack.count ) in ( 'IF', 'CASEBLOCK' ) then
tCallStack.extend;
tCallStack( tCallStack.count ) := 'CASEBLOCK';
elsif tPrevToken is null or tPrevToken != 'END' then
tCallStack.extend;
tCallStack( tCallStack.count ) := 'CASEEXPR';
end if;
elsif tToken = 'LOOP' then
if tPrevToken is null or tPrevToken != 'END' then
tCallStack.extend;
tCallStack( tCallStack.count ) := 'LOOP';
end if;
elsif tToken = 'IF' then
if tPrevToken in ( '>>', 'THEN', 'ELSE', ';', 'BEGIN', 'LOOP' ) then
tCallStack.extend;
tCallStack( tCallStack.count ) := 'IF';
end if;
elsif tToken = 'BEGIN' then
if tPrevToken in ( '>>', 'THEN', 'ELSE', ';', 'IS', 'AS', 'DECLARE', 'BEGIN', 'LOOP' ) then
if tCallStack( tCallStack.count ) like '-%' then
tCallStack( tCallStack.count ) := '+' || substr( tCallStack( tCallStack.count ), 2 );
else
tCallStack.extend;
tCallStack( tCallStack.count ) := 'BEGIN';
end if;
end if;
elsif tToken = ';' then
if tLookForDefinition = 1 then
tCallStack.trim;
tLookForDefinition := 0;
elsif tPrevPrevToken = 'END' and tPrevToken in ( 'IF', 'LOOP', 'CASE' ) then
case tPrevToken
when 'IF' then
if tCallStack( tCallStack.count ) = 'IF' then
tCallStack.trim;
end if;
when 'LOOP' then
if tCallStack( tCallStack.count ) = 'LOOP' then
tCallStack.trim;
end if;
else
if tCallStack( tCallStack.count ) = 'CASEBLOCK' then
tCallStack.trim;
end if;
end case;
elsif tPrevPrevToken = 'END' or tPrevToken = 'END' then
if tCallStack( tCallStack.count ) like '+%' then
tCallStack.trim;
elsif tPrevToken = 'END' then
if tCallStack( tCallStack.count ) in ( 'BEGIN', 'CASEEXPR' ) then
tCallStack.trim;
end if;
end if;
end if;
elsif tPrevToken = 'END' then
if tCallStack( tCallStack.count ) = 'CASEEXPR' then
tCallStack.trim;
end if;
elsif tPrevToken in ( 'TRIGGER', 'FUNCTION', 'PROCEDURE' )
or tPrevToken = 'PACKAGE' and tToken != 'BODY'
or tPrevToken = 'BODY' and tPrevPrevToken in ( 'PACKAGE', 'TYPE' ) then
if tPrevToken in ( 'FUNCTION', 'PROCEDURE' ) then
tLookForDefinition := 1;
end if;
tCallStack.extend;
tCallStack( tCallStack.count ) := case
when tPrevToken = 'TRIGGER' then '-TRI'
when tPrevToken = 'FUNCTION' then '-FUN'
when tPrevToken = 'PROCEDURE' then '-PRO'
when tPrevToken = 'PACKAGE' then '-PAC'
when tPrevPrevToken = 'PACKAGE' then '-PCB'
else '-TYP'
end || tToken;
end if;
if tLookForDefinition = 1 and tPrevToken in ( 'IS', 'AS' ) and tToken not in ( 'NOT', 'NULL', 'LANGUAGE' ) then
tLookForDefinition := 0;
end if;
end if;
tToken := '';
end if;
tToken := tToken || c;
tPreviousTokenType := tTokenType;
<<NEXT_TOKEN_CHAR>>
pos := pos + 1;
exit when pos > len + 1;
end loop;
end if;
end loop;
if tAnonymousBlock = 0 then
close getSource;
end if;
tCallStackLine := '';
for i in 2 .. tCallStack.count loop
tToken := case substr( tCallStack( i ), 2, 3 )
when 'TRI' then 'TRIGGER '
when 'FUN' then 'FUNCTION '
when 'PRO' then 'PROCEDURE '
when 'PAC' then 'PACKAGE '
when 'PCB' then 'PACKAGE BODY '
when 'TYP' then 'TYPE BODY '
when 'NON' then 'ANONYMOUS BLOCK'
end;
if tToken is not null then
if tCallStackLine is not null then
tCallStackLine := tCallStackLine || '.';
end if;
tCallStackLine := tCallStackLine || tToken;
tToken := substr( tCallStack( i ), 5 );
if tToken like 'ID%' and tIdentifiers.exists( to_number( substr( tToken, 3 ) ) ) then
tToken := '"' || tIdentifiers( to_number( substr( tToken, 3 ) ) ) || '"';
end if;
tCallStackLine := tCallStackLine || tToken;
end if;
end loop;
if ret is not null then
ret := ret || CHAR_NEW_LINE;
end if;
if tOwner is null then
tOwner := user; -- UNKNOWN_OWNER;
end if;
ret := ret || tLine || ': ' || tOwner || '.' || tCallStackLine;
exit when tDepth = tReached;
<<NEXT_CALL_POSITION>>
if tReached > 0 then
tReached := tReached + 1;
elsif tCallPositionsLine like '%handle%number%name%' then
tReached := 1;
end if;
end loop;
return ret;
end;
-- Returns the stack information of a current program unit.
-- See output format description of function getCallStack.
function whoAmI return varchar2 is
begin
return getCallStack( 3 );
end;
-- Returns the stack information of a program unit which has called currently executing code.
-- See output format description of function getCallStack.
function whoCalledMe return varchar2 is
begin
return getCallStack( 4 );
end;
-- Returns one stack line at a given depth.
-- See output format description of function getCallStack.
-- tCallStack: information returned by getCallStack.
-- tDepth: number of requested line. If tDepth is out of bounds then null is returned.
function getCallStackLine( tCallStack in varchar2, tDepth in number ) return varchar2 is
pos pls_integer;
depth pls_integer;
tCallLine varchar2( 4000 ) := tCallStack;
begin
if tDepth is null or tDepth <= 0 or tDepth > 4000 then
return '';
end if;
depth := 1;
loop
pos := instr( tCallLine, CHAR_NEW_LINE );
if pos is null or pos = 0 then
return case when tDepth = depth then tCallLine end;
elsif tDepth = depth then
return substr( tCallLine, 1, pos - 1 );
elsif tDepth < depth then
return '';
else
tCallLine := substr( tCallLine, pos + 1 );
depth := depth + 1;
end if;
end loop;
end;
-- Returns current stack depth including the call of this function.
-- tCallStack: information returned by getCallStack. Can be omitted.
-- See: utl_call_stack.dynamic_depth
function getDynamicDepth( tCallStack in varchar2 default '' ) return number is
tCallPositions varchar2( 4000 );
begin
if tCallStack is null then
tCallPositions := dbms_utility.format_call_stack;
return greatest( nvl( length( tCallPositions ) - length( replace( tCallPositions, CHAR_NEW_LINE ) ) - 3, 0 ), 0 );
else
return greatest( nvl( length( tCallStack ) - length( replace( tCallStack, CHAR_NEW_LINE ) ) + 1, 0 ), 0 );
end if;
end;
-- Returns a depth of a current program unit.
-- Stored procedures, functions, packages and their bodies, type bodies, triggers and anonymous blocks have a depth equal to 0.
-- Inner procedures and functions have depth equal to 1 + depth of parent program unit.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.lexical_depth
function getLexicalDepth( tCallStack in varchar2, tDepth in number default null ) return number is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
begin
tCallLine := cutQuotes( tCallLine );
return greatest( nvl( length( tCallLine ) - length( replace( tCallLine, '.' ) ) - 1, 0 ), 0 );
end;
-- Returns a source line number as returned by dbms_utility.format_call_stack.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.unit_line
function getUnitLine( tCallStack in varchar2, tDepth in number default null ) return number is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
pos := instr( tCallLine, ':' );
if pos is null or pos = 0 then
return null;
end if;
return to_number( substr( tCallLine, 1, pos - 1 ) );
end;
-- Returns owner of a requested program unit, or a parsing schema for an anonymous block.
-- If the parsing schema name could not be retrieved then OWNER equals to current schema name.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.owner
function getOwner( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
pos := instr( tCallLine, ':' );
if pos > 0 then
tCallLine := substr( tCallLine, pos + 2 );
end if;
pos := instr( tCallLine, '.' );
if pos > 0 then
return nullif( substr( tCallLine, 1, pos - 1 ), UNKNOWN_OWNER );
end if;
return '';
end;
-- Returns name of a requested stored procedure. Empty string for anonymous block.
-- The name returned won't contain double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getProgram( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
posTo pls_integer;
begin
pos := instr( tCallLine, '.' );
if pos > 0 then
tCallLine := substr( tCallLine, pos + 1 );
end if;
posTo := nvl( instr( tCallLine, '.' ), 0 );
if posTo = 0 then
posTo := length( tCallLine ) + 1;
end if;
pos := nvl( instr( tCallLine, '"' ), 0 );
if posTo > pos and pos > 0 then
posTo := nvl( instr( tCallLine, '"', pos + 1 ), posTo );
elsif tCallLine like 'ANON%' then
pos := length( 'ANONYMOUS BLOCK' );
elsif tCallLine like 'PROC%' then
pos := length( 'PROCEDURE ' );
elsif tCallLine like 'FUNC%' then
pos := length( 'FUNCTION ' );
elsif tCallLine like 'TRIG%' then
pos := length( 'TRIGGER ' );
elsif tCallLine like 'TYPE%' then
pos := length( 'TYPE BODY ' );
elsif tCallLine like 'PACKAGE B%' then
pos := length( 'PACKAGE BODY ' );
else
pos := length( 'PACKAGE ' );
end if;
return substr( tCallLine, pos + 1, posTo - pos - 1 );
end;
-- Returns a type of a requested stored procedure. One of:
-- ANONYMOUS BLOCK, PROCEDURE, FUNCTION, TRIGGER, PACKAGE, PACKAGE BODY, TYPE BODY.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.unit_type
function getProgramType( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
pos := instr( tCallLine, '.' );
if pos > 0 then
tCallLine := substr( tCallLine, pos + 1 );
end if;
if tCallLine like 'ANON%' then
return 'ANONYMOUS BLOCK';
elsif tCallLine like 'PROC%' then
return 'PROCEDURE';
elsif tCallLine like 'FUNC%' then
return 'FUNCTION';
elsif tCallLine like 'TRIG%' then
return 'TRIGGER';
elsif tCallLine like 'TYPE%' then
return 'TYPE BODY';
elsif tCallLine like 'PACKAGE B%' then
return 'PACKAGE BODY';
else
return 'PACKAGE';
end if;
end;
-- Returns a name of a requested innermost subprogram.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogram are called inside the function.
-- "inner_function" would be returned (without double quotes).
-- If getCallStack and getSubprogram are called inside package body then empty string is returned.
-- The name returned never contains double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.subprogram
function getSubprogram( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
if getLexicalDepth( tCallLine ) = 0 then
return '';
end if;
if tCallLine like '%"' then
pos := instr( tCallLine, '"', -2 );
return substr( tCallLine, pos + 1, length( tCallLine ) - pos - 1 );
elsif tCallLine like '%.ANONYMOUS BLOCK' then
return '';
else
pos := instr( tCallLine, ' ', -1 );
return substr( tCallLine, pos + 1 );
end if;
end;
-- Returns a type of requested innermost subprogram.
-- See example in getSubprogram describing the concept of innermost subprogram.
-- Value returned is one of PROCEDURE or FUNCTION.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getSubprogramType( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
if tCallLine like '%"' then
pos := instr( tCallLine, '"', -2 ) - 2;
elsif tCallLine like '%.ANONYMOUS BLOCK' then
pos := length( tCallLine );
else
pos := instr( tCallLine, ' ', -1 ) - 1;
end if;
tCallLine := substr( tCallLine, 1, pos );
pos := instr( tCallLine, '.', -1 );
return substr( tCallLine, pos + 1 );
end;
-- Returns a table of names of requested subprograms.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprograms are called inside the function.
-- An array [ "inner", "inner_function" ] would be returned (without double quotes).
-- If getCallStack and getSubprograms are called inside package body then an empty table is returned.
-- The names returned never contain double quotes.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.subprogram
function getSubprograms( tCallStack in varchar2, tDepth in number default null ) return str_table is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
dqpos pls_integer;
tLexicalDepth pls_integer;
tSubprogram varchar2( 4000 );
ret str_table := str_table();
begin
if getLexicalDepth( tCallLine ) = 0 then
return ret;
end if;
tLexicalDepth := 0;
loop
dqpos := instr( tCallLine, '"' );
pos := instr( tCallLine, '.' );
if dqpos > 0 and dqpos < pos then
dqpos := instr( tCallLine, '"', dqpos + 1 );
while pos > 0 and pos < dqpos loop
pos := instr( tCallLine, '.', pos + 1 );
end loop;
end if;
if tLexicalDepth >= 2 then
if pos > 0 then
tSubprogram := substr( tCallLine, 1, pos - 1 );
else
tSubprogram := tCallLine;
end if;
if tSubprogram like 'FUNCTION %' then
tSubprogram := substr( tSubprogram, 10 );
elsif tSubprogram like 'PROCEDURE %' then
tSubprogram := substr( tSubprogram, 11 );
end if;
if tSubprogram like '%"%' then
tSubprogram := translate( tSubprogram, ' "', ' ' );
end if;
ret.extend;
ret( ret.count ) := tSubprogram;
end if;
if pos > 0 then
tCallLine := substr( tCallLine, pos + 1 );
tLexicalDepth := tLexicalDepth + 1;
else
exit;
end if;
end loop;
return ret;
end;
-- Returns a table of types of requested subprograms.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogramsTypes are called inside the function.
-- An array [ "PROCEDURE", "FUNCTION" ] would be returned (without double quotes).
-- If getCallStack and getSubprogramsTypes are called inside package body then an empty table is returned.
-- Each value in the returned array is one of PROCEDURE or FUNCTION.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
function getSubprogramsTypes( tCallStack in varchar2, tDepth in number default null ) return str_table is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
dqpos pls_integer;
tLexicalDepth pls_integer;
tSubprogram varchar2( 4000 );
ret str_table := str_table();
begin
if getLexicalDepth( tCallLine ) = 0 then
return ret;
end if;
tLexicalDepth := 0;
loop
dqpos := instr( tCallLine, '"' );
pos := instr( tCallLine, '.' );
if dqpos > 0 and dqpos < pos then
dqpos := instr( tCallLine, '"', dqpos + 1 );
while pos > 0 and pos < dqpos loop
pos := instr( tCallLine, '.', pos + 1 );
end loop;
end if;
if tLexicalDepth >= 2 then
if pos > 0 then
tSubprogram := substr( tCallLine, 1, pos - 1 );
else
tSubprogram := tCallLine;
end if;
ret.extend;
if tSubprogram like 'FUNCTION %' then
ret( ret.count ) := 'FUNCTION';
elsif tSubprogram like 'PROCEDURE %' then
ret( ret.count ) := 'PROCEDURE';
end if;
end if;
if pos > 0 then
tCallLine := substr( tCallLine, pos + 1 );
tLexicalDepth := tLexicalDepth + 1;
else
exit;
end if;
end loop;
return ret;
end;
-- Returns the hierarchy of names separated by dot from outermost to innermost program unit.
-- Example: package "outer" contains procedure "inner" which contains function "inner_function".
-- getCallStack and subsequent getSubprogram are called inside the function.
-- "outer.inner.inner_function" would be returned (without double quotes).
-- If one of program units in this hierarchy has double quotes in its name, they would be preserved.
-- tCallStack: information returned by getCallStack if tDepth is set or information returned by getCallStackLine.
-- tDepth: number of requested line if tCallStack = getCallStack, null otherwise.
-- See: utl_call_stack.concatenate_subprogram
function getConcatenatedSubprograms( tCallStack in varchar2, tDepth in number default null ) return varchar2 is
tCallLine varchar2( 4000 ) := case when tDepth is null then tCallStack else getCallStackLine( tCallStack, tDepth ) end;
pos pls_integer;
begin
pos := instr( tCallLine, '.' );
tCallLine := substr( tCallLine, pos );
tCallLine := replace( tCallLine, '.PACKAGE BODY ', '.' );
tCallLine := replace( tCallLine, '.PACKAGE ', '.' );
tCallLine := replace( tCallLine, '.TYPE BODY ', '.' );
tCallLine := replace( tCallLine, '.TRIGGER ', '.' );
tCallLine := replace( tCallLine, '.PROCEDURE ', '.' );
tCallLine := replace( tCallLine, '.FUNCTION ', '.' );
return substr( tCallLine, 2 );
end;
-- Returns an error stack as a string.