-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtePLSQL.pkb
executable file
·1695 lines (1459 loc) · 57.8 KB
/
tePLSQL.pkb
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 BODY teplsql
AS
/** A TYPE used to define a request for a template */
type t_include_parameters is record ( template_name TE_TEMPLATES.NAME%TYPE
, object_name varchar2(64)
, object_type varchar2(64)
, schema varchar2(64)
, indent int );
null_include_parameters t_include_parameters;
-- various system default values
g_max_includes_default constant int := 50;
g_globbing_mode_default constant t_template_variable_value := g_globbing_mode_off;
g_globbing_separator_default constant t_template_variable_value := chr(10);
g_render_mode_default constant t_template_variable_value := g_render_mode_normal;
g_indent_string_default constant t_template_variable_value := ' ';
-- various system options
g_max_includes int := g_max_includes_default;
g_globbing_mode t_template_variable_value := g_globbing_mode_default;
g_globbing_separator t_template_variable_value := g_globbing_separator_default;
g_render_mode t_template_variable_value := g_render_mode_default;
g_indent_string t_template_variable_value := g_indent_string_default;
-- run time global variables
g_buffer CLOB;
g_indention_level int := 1;
g_max_indention_level constant int := 20;
g_build_block varchar2(25) := 'main';
-- indention globals
type t_indentable_clob is varray(20) of clob;
g_buffer2 t_indentable_clob := new t_indentable_clob();
type t_tab_set is table of int index by pls_integer;
g_tab_set t_tab_set;
-- exceptions for flow control
only_hierarch_tags_complete exception;
only_fetch_complete exception;
/**
* Resets all of the system options to default values
*
* @param p_vars This is the Associative Array that contains system options
*/
procedure reset_system_defaults
as
begin
g_max_includes := g_max_includes_default;
g_globbing_mode := g_globbing_mode_default;
g_globbing_separator := g_globbing_separator_default;
g_render_mode := g_render_mode_default;
g_indent_string := g_indent_string_default;
end reset_system_defaults;
/**
* Decodes the properties of the "<%@ include() %>" directive.
*
* @param p_str Input string found between parentheses of an "<%@ include() %>" directive
* @return A record type containing the individual components
*/
FUNCTION decode_include_parameters (p_str IN VARCHAR2)
RETURN t_include_parameters
IS
TYPE array_t IS TABLE OF te_templates.name%TYPE;
l_string_tt array_t;
l_ret t_include_parameters := null_include_parameters;
BEGIN
IF LENGTH (p_str) > 0
THEN
SELECT REGEXP_REPLACE (REGEXP_SUBSTR (p_str
, '[^,]+'
, 1
, LEVEL), '\s', '')
text
BULK COLLECT
INTO l_string_tt
FROM DUAL
CONNECT BY REGEXP_SUBSTR (p_str
, '[^,]+'
, 1
, LEVEL) IS NOT NULL;
--populate variables
IF l_string_tt.EXISTS (1)
THEN
l_ret.template_name := l_string_tt (1);
END IF;
IF l_string_tt.EXISTS (2)
THEN
l_ret.object_name := l_string_tt (2);
END IF;
IF l_string_tt.EXISTS (3)
THEN
l_ret.object_type := l_string_tt (3);
END IF;
IF l_string_tt.EXISTS (4)
THEN
l_ret.schema := l_string_tt (4);
END IF;
IF l_string_tt.EXISTS (5)
THEN
l_ret.indent := l_string_tt (5);
END IF;
END IF;
RETURN l_ret;
END decode_include_parameters;
/**
* Process all parameters that adjust the tePLSQL engine
*
* @param p_vars the template's arguments and engine properties.
*/
PROCEDURE process_engine_parameters( p_vars IN t_assoc_array DEFAULT null_assoc_array)
IS
l_key t_template_variable_name;
l_value t_template_variable_value;
invalid_parameter_value EXCEPTION;
BEGIN
l_key := p_vars.first;
WHILE( l_key is not null )
LOOP
l_value := p_vars(l_key);
CASE l_key
WHEN g_set_max_includes THEN
-- test that the value is a number and the number is >= 1
IF NOT regexp_like( l_value, '^[[:digit:]]+$')
THEN
raise invalid_parameter_value;
END IF;
g_max_includes := to_number( l_value );
IF g_max_includes < 1 or g_max_includes IS NULL
THEN
raise invalid_parameter_value;
END IF;
WHEN g_set_globbing_separator THEN
g_globbing_separator := l_value;
WHEN g_set_globbing_mode THEN
-- assert the value is valid
IF l_value not in (g_globbing_mode_off, g_globbing_mode_on, g_globbing_mode_regexp,g_globbing_mode_like)
THEN
raise invalid_parameter_value;
END IF;
g_globbing_mode := l_value;
WHEN g_set_render_mode THEN
IF l_value in ( g_render_mode_hierarch_tags_only, g_render_mode_fetch_only )
THEN
g_render_mode := l_value;
ELSE
g_render_mode := g_render_mode_default;
end if;
WHEN g_set_indention_string THEN
g_indent_string := l_value;
ELSE
NULL;
END CASE;
l_key := p_vars.next( l_key );
END LOOP;
EXCEPTION
WHEN invalid_parameter_value THEN
raise_application_error( -20010, 'Parameter "' || l_key || '" has an invalid value of "' || l_value || '"');
END process_engine_parameters;
PROCEDURE output_clob (p_clob IN CLOB)
IS
v_offset PLS_INTEGER := 1;
v_new_line PLS_INTEGER;
/**
* Since 10gR2 Oracle increase the limit of DBMS_OUTPUT to 32767
*/
$if DBMS_DB_VERSION.ver_le_10_1 $then
v_chunk_size PLS_INTEGER := 255;
$else
v_chunk_size PLS_INTEGER := 32767;
$end
BEGIN
DBMS_OUTPUT.enable (1000000);
LOOP
EXIT WHEN v_offset > DBMS_LOB.getlength (p_clob);
DBMS_OUTPUT.put (DBMS_LOB.SUBSTR (p_clob, v_chunk_size, v_offset));
v_offset := v_offset + v_chunk_size;
END LOOP;
-- flush, inserts a new line at the end
DBMS_OUTPUT.new_line;
END output_clob;
/**
* Receives the template directive key-value data separated by commas
* and assign this key-values to the associative array
*
* @param p_directive the key-value data template directive
* @param p_vars the associative array
*/
PROCEDURE set_template_directive (p_directive IN CLOB, p_vars IN OUT NOCOPY t_assoc_array)
AS
l_key VARCHAR2 (256);
l_value VARCHAR2 (256);
l_directive VARCHAR2 (32767);
BEGIN
l_directive := REGEXP_REPLACE (p_directive, '\s', '');
FOR c1 IN ( SELECT REGEXP_REPLACE (REGEXP_SUBSTR (l_directive
, '[^,]+'
, 1
, LEVEL), '\s', '')
text
FROM DUAL
CONNECT BY REGEXP_SUBSTR (l_directive
, '[^,]+'
, 1
, LEVEL) IS NOT NULL)
LOOP
l_key := SUBSTR (c1.text, 1, INSTR (c1.text, '=') - 1);
l_value := SUBSTR (c1.text, INSTR (c1.text, '=') + 1);
p_vars ('template_' || l_key) := l_value;
if l_key = 'build'
then
g_build_block := regexp_replace( l_value, '[\(\)]', '' );
end if;
END LOOP;
END set_template_directive;
/**
* Retrieves template from TE_TEMPLATES that was defined by the input.
*
* @param p_inc Properties used to define which code to extract
* @return Returns the code representing the requested template name or EMBPTY_CLO() if not found.
*/
FUNCTION get_template_by_table (p_inc t_include_parameters)
RETURN CLOB
AS
l_template CLOB;
l_regexp varchar2(32767);
BEGIN
CASE g_globbing_mode
WHEN g_globbing_mode_off THEN
SELECT t.template
INTO l_template
FROM te_templates t
WHERE UPPER (t.name) = UPPER (p_inc.template_name);
WHEN g_globbing_mode_like THEN
FOR curr IN (
SELECT t.template
FROM te_templates t
WHERE UPPER (t.name) like UPPER (p_inc.template_name)
ORDER BY t.name
)
LOOP
IF l_template IS NULL
THEN
l_template := curr.template;
ELSE
l_template := l_template || g_globbing_separator || curr.template;
END IF;
END LOOP;
WHEN g_globbing_mode_regexp THEN
FOR curr IN (
SELECT t.template
FROM te_templates t
WHERE regexp_like(t.name, p_inc.template_name)
ORDER BY t.name
)
LOOP
IF l_template IS NULL
THEN
l_template := curr.template;
ELSE
l_template := l_template || g_globbing_separator || curr.template;
END IF;
END LOOP;
WHEN g_globbing_mode_on THEN
l_regexp := regexp_replace( p_inc.template_name, '([.(){}\])', '\\\1');
l_regexp := '^' || regexp_replace( l_regexp, '\*', '[^.]+' ) || '$';
FOR curr IN (
SELECT t.template
FROM te_templates t
WHERE regexp_like(t.name, l_regexp)
ORDER BY t.name
)
LOOP
IF l_template IS NULL
THEN
l_template := curr.template;
ELSE
l_template := l_template || g_globbing_separator || curr.template;
END IF;
END LOOP;
ELSE
raise no_data_found;
END CASE;
IF l_template IS NULL
THEN
raise no_data_found;
END IF;
RETURN l_template;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_template := EMPTY_CLOB ();
RETURN l_template;
END get_template_by_table;
/**
* Retrieves code from DB Object defined by the input.
*
* @param p_inc Properties used to define which code to extract
* @return Returns the code reperesnting the requested template
*/
FUNCTION get_template_by_obj (p_inc t_include_parameters)
RETURN CLOB
IS
l_result CLOB;
l_object_ddl CLOB;
l_template CLOB;
l_tmp CLOB;
i PLS_INTEGER := 1;
l_found PLS_INTEGER := 0;
BEGIN
--Search the template in other Oracle Object
--Get package source DDL
l_object_ddl :=
DBMS_METADATA.get_ddl (NVL (UPPER (p_inc.object_type), 'PACKAGE')
, UPPER (p_inc.object_name)
, UPPER (p_inc.schema));
--If p_template_name is null get all templates from the object
--else get only this template.
IF p_inc.template_name IS NOT NULL
THEN
LOOP
l_tmp :=
REGEXP_SUBSTR (l_object_ddl
, '<%@ template([^%>].*?)%>'
, 1
, i
, 'n');
l_found := INSTR (l_tmp, 'name=' || p_inc.template_name);
EXIT WHEN LENGTH (l_tmp) = 0 OR l_found <> 0;
i := i + 1;
END LOOP;
ELSE
l_found := 0;
END IF;
-- i has the occurrence of the substr where the template is
l_tmp := NULL;
LOOP
--Get Template from the object
$IF DBMS_DB_VERSION.ver_le_10
$THEN
l_tmp :=
REGEXP_REPLACE (REGEXP_REPLACE (REGEXP_SUBSTR (l_object_ddl
, '\$if[[:blank:]]+false[[:blank:]]+\$then'
|| CHR (10)
|| '([^\$end].*?)\$end'
, 1
, i
, 'n')
, '\$if[[:blank:]]+false[[:blank:]]+\$then\s*' || CHR (10)
, ''
, 1
, 1)
, '\$end'
, ''
, 1
, INSTR ('$end', 1, -1));
$ELSE
l_tmp :=
REGEXP_SUBSTR (l_object_ddl
, '\$if[[:blank:]]+false[[:blank:]]+\$then\s*' || CHR (10) || '([^\$end].*?)\$end'
, 1
, i
, 'n'
, 1);
$END
l_template := l_template || l_tmp;
EXIT WHEN LENGTH (l_tmp) = 0 OR l_found <> 0;
i := i + 1;
END LOOP;
RETURN l_template;
END get_template_by_obj;
/*
* merges template into TE_TEMPLATES or a GTT/PTT
*/
procedure set_template( name in te_templates.name%type
,template in te_templates.template%type
,description in te_templates.description%type default null
,use_gtt in varchar2 default null )
as
begin
case use_gtt
when 'GTT' then
raise no_data_found;
when 'PTT' then
raise no_data_found;
else
merge into te_templates a
using (select set_template.template as template
,set_template.name as name
,set_template.description as description
from dual) b
on (a.name = b.name)
when matched then update
set a.template=b.template, a.description = nvl(b.description,a.description)
when not matched then insert ( name, template )
values (b.name, b.template);
end case;
end set_template;
/**
* Retrieves the template based on the input.
* This code is responsible for deciding where the template is stored.
*
* @param p_inc Properties used to define which code to extract
* @return Returns the code reperesnting the requested template
*/
FUNCTION get_template (p_inc t_include_parameters)
RETURN CLOB
IS
l_inc t_include_parameters := null_include_parameters;
l_template CLOB;
BEGIN
--Force Defaults
l_inc.template_name := p_inc.template_name;
l_inc.object_name := NVL (p_inc.object_name, 'TE_TEMPLATES');
l_inc.object_type := NVL (p_inc.object_type, 'PACKAGE');
l_inc.schema := p_inc.schema;
-- Decision tree for deciding which method to use to retrieve the code
IF l_inc.template_name IS NOT NULL AND l_inc.object_name = 'TE_TEMPLATES'
THEN
l_template := get_template_by_table (l_inc);
ELSE
l_template := get_template_by_obj (l_inc);
END IF;
RETURN l_template;
END get_template;
/**
* Receives the name of the object, usually a package,
* which contains an embedded template and return the template.
*
* This has been refactored with "get_template()"
*
* @param p_template_name the name of the template
* @param p_object_name the name of the object (usually the name of the package)
* @param p_object_type the type of the object (PACKAGE, PROCEDURE, FUNCTION...)
* @param p_schema the schema of the object
* @return the template.
*/
FUNCTION include (p_template_name IN VARCHAR2 DEFAULT NULL
, p_object_name IN VARCHAR2 DEFAULT 'TE_TEMPLATES'
, p_object_type IN VARCHAR2 DEFAULT 'PACKAGE'
, p_schema IN VARCHAR2 DEFAULT NULL )
RETURN CLOB
AS
l_result CLOB;
l_object_ddl CLOB;
l_template CLOB;
l_tmp CLOB;
i PLS_INTEGER := 1;
l_found PLS_INTEGER := 0;
l_object_name VARCHAR2 (64);
l_template_name VARCHAR2 (64);
l_object_type VARCHAR2 (64);
l_schema VARCHAR2 (64);
BEGIN
--Force Defaults
l_template_name := p_template_name;
l_object_name := NVL(p_object_name,'TE_TEMPLATES');
l_object_type := NVL(p_object_type,'PACKAGE');
l_schema := p_schema;
--Search for the template in the table TE_TEMPLATES
IF l_template_name IS NOT NULL
AND l_object_name = 'TE_TEMPLATES'
THEN
BEGIN
SELECT template
INTO l_template
FROM te_templates
WHERE UPPER(name) = UPPER (l_template_name);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_template := EMPTY_CLOB();
END;
RETURN l_template;
ELSE
--Search the template in other Oracle Object
--Get package source DDL
l_object_ddl :=
DBMS_METADATA.get_ddl (NVL (UPPER (l_object_type), 'PACKAGE'), UPPER (l_object_name), UPPER (l_schema));
--If p_template_name is null get all templates from the object
--else get only this template.
IF l_template_name IS NOT NULL
THEN
LOOP
l_tmp :=
REGEXP_SUBSTR (l_object_ddl
, '<%@ template([^%>].*?)%>'
, 1
, i
, 'n');
l_found := INSTR (l_tmp, 'name=' || l_template_name);
EXIT WHEN LENGTH (l_tmp) = 0 OR l_found <> 0;
i := i + 1;
END LOOP;
ELSE
l_found := 0;
END IF;
-- i has the occurrence of the substr where the template is
l_tmp := NULL;
LOOP
--Get Template from the object
$IF DBMS_DB_VERSION.ver_le_10
$THEN
l_tmp :=
REGEXP_REPLACE (REGEXP_REPLACE (REGEXP_SUBSTR (l_object_ddl
, '\$if[[:blank:]]+false[[:blank:]]+\$then' || CHR (10) || '([^\$end].*?)\$end'
, 1
, i
, 'n')
, '\$if[[:blank:]]+false[[:blank:]]+\$then\s*' || CHR (10)
, ''
, 1
, 1)
, '\$end'
, ''
, 1
, INSTR ('$end', 1, -1));
$ELSE
l_tmp :=
REGEXP_SUBSTR (l_object_ddl
, '\$if[[:blank:]]+false[[:blank:]]+\$then\s*' || CHR (10) || '([^\$end].*?)\$end'
, 1
, i
, 'n'
, 1);
$END
l_template := l_template || l_tmp;
EXIT WHEN LENGTH (l_tmp) = 0 OR l_found <> 0;
i := i + 1;
END LOOP;
RETURN l_template;
END IF;
END include;
/**
* Bind associative array variables in the template
*
* @param p_template the template
* @param p_vars the associative array
*/
PROCEDURE bind_vars (p_template IN OUT NOCOPY CLOB, p_vars IN t_assoc_array)
AS
l_key VARCHAR2 (256);
l_pkey VARCHAR2 (256);
l_value te_templates.name%type;
l_render_all_tags boolean := true;
procedure replace_vars( i_key in varchar2, i_value in varchar2 )
as
begin
p_template := REPLACE (p_template, '${' || i_key || '}', TO_CLOB ( i_value ));
end;
BEGIN
if g_render_mode = g_render_mode_fetch_only
then
raise only_fetch_complete;
end if;
IF p_vars.COUNT () <> 0
THEN
l_key := p_vars.FIRST;
LOOP
EXIT WHEN l_key IS NULL;
case
when l_key in ( 'object_name', 'name' ) then
-- always replace certian vars
replace_vars( l_key, p_vars( l_key ) );
when l_key = 'this' then
-- special handling of ${this} and ${super} vars
replace_vars( l_key, p_vars(l_key) );
-- render up to 10 parents deep
l_value := p_vars( 'this' );
l_pkey := 'super';
for i in 1 .. 10
loop
l_value := rtrim( regexp_substr( l_value, '^.+\.' ), '.' );
replace_vars( l_pkey, l_value );
l_pkey := l_pkey || '.super';
end loop;
when g_render_mode not in ( g_render_mode_hierarch_tags_only )
then
-- replace other vars based on G_RENDER_MODE
replace_vars( l_key, p_vars( l_key ) );
else
null;
end case;
l_key := p_vars.NEXT (l_key);
END LOOP;
END IF;
if g_render_mode in ( g_render_mode_hierarch_tags_only )
then
raise only_hierarch_tags_complete;
end if;
END bind_vars;
/**
* Parse template marks
*
* @param p_template the template
* @param p_vars the associative array
*/
PROCEDURE parse (p_template IN CLOB, p_vars IN t_assoc_array DEFAULT null_assoc_array)
AS
l_open_count PLS_INTEGER;
l_close_count PLS_INTEGER;
l_template_name VARCHAR2(300);
BEGIN
$if dbms_db_version.ver_le_10 $then
/**
* ATTENTION, these instructions are very slow and penalize template processing time.
* If performance is critical to your system, you should disable the parser only for BD <= 10g
*/
l_open_count :=
NVL (LENGTH (REGEXP_REPLACE (p_template
, '(<)%|.'
, '\1'
, 1
, 0
, 'n')), 0);
l_close_count :=
NVL (LENGTH (REGEXP_REPLACE (p_template
, '(%)>|.'
, '\1'
, 1
, 0
, 'n')), 0);
$else
l_open_count := regexp_count (p_template, '<\%');
l_close_count := regexp_count (p_template, '\%>');
$end
IF l_open_count <> l_close_count
THEN
IF p_vars.exists('template_name')
THEN
l_template_name := ' ' || p_vars('template_name');
END IF;
raise_application_error (-20001
, '##Parser Exception processing the template'||l_template_name
|| '. One or more tags (<% %>) are not closed: '
|| l_open_count
|| ' <> '
|| l_close_count
|| CHR (10));
END IF;
END parse;
/**
* Interprets the received template and convert it into executable plsql
*
* @param p_template the template
* @param p_vars the associative array
*/
PROCEDURE interpret (p_template IN OUT NOCOPY CLOB, p_vars IN t_assoc_array DEFAULT null_assoc_array)
AS
l_vars t_assoc_array := p_vars;
l_declare CLOB;
l_tmp CLOB;
i PLS_INTEGER := 0;
BEGIN
--Template directive
$if dbms_db_version.ver_le_10 $then
l_tmp :=
REPLACE (REPLACE (REGEXP_SUBSTR (p_template
, '<%@ template([^%>].*?)\s*%>'
, 1
, 1
, 'n'), '<%@ template', ''), '%>', '');
$else
l_tmp :=
REGEXP_SUBSTR (p_template
, '<%@ template([^%>].*?)\s*%>'
, 1
, 1
, 'n'
, 1);
$end
--Set template directive variables into var associative array
set_template_directive (l_tmp, l_vars);
if g_render_mode = g_render_mode_fetch_only
then
--Delete all template directives
p_template :=
REGEXP_REPLACE (p_template
, '<%@ template([^%>].*?)\s*%>[[:blank:]]*\s$?'
, ''
, 1
, 0
, 'n');
raise only_fetch_complete;
end if;
--Bind the variables into template
bind_vars (p_template, l_vars);
--Null all variables not binded
p_template := REGEXP_REPLACE (p_template, '\$\{\S*\}', '');
--Parse <% %> tags
parse (p_template, l_vars);
--Dos to Unix
p_template :=
REGEXP_REPLACE (p_template
, CHR(13)||CHR(10)
, CHR(10)
, 1,0,'nm');
--Delete all template directives
p_template :=
REGEXP_REPLACE (p_template
, '<%@ template([^%>].*?)\s*%>[[:blank:]]*\s$?'
, ''
, 1
, 0
, 'n');
--Escaped chars except \\n
p_template :=
REGEXP_REPLACE (p_template
, '\\\\([^n])'
, ']'');tePLSQL.p(q''[\1]'');tePLSQL.p(q''['
, 1
, 0
, 'n');
--New lines.
p_template :=
REGEXP_REPLACE (p_template
, '(\\\\n)'
, CHR (10) --|| ']'');tePLSQL.p(q''['
, 1
, 0
, 'n');
--Delete the line breaks for lines ending in %>[blanks]CHR(10)
p_template :=
REGEXP_REPLACE (p_template
, '(%>[[:blank:]]*?' || CHR (10) || ')'
, '%>'
, 1
, 0
, '');
--Delete new lines with !\n
p_template :=
REGEXP_REPLACE (p_template
, '([[:blank:]]*\!\\n[[:blank:]]*' || CHR (10) || '?[[:blank:]]*)'
, ''
, 1
, 0
, 'm');
-- Delete all blanks before <% in the beginning of each line
p_template :=
REGEXP_REPLACE (p_template
, '(^[[:blank:]]*<%)'
, '<%'
, 1
, 0
, 'm');
--Merge all declaration blocks into a single block
l_tmp := NULL;
LOOP
i := i + 1;
$if dbms_db_version.ver_le_10 $then
l_tmp :=
REPLACE (REPLACE (REGEXP_SUBSTR (p_template
, '<%!([^%>].*?)%>'
, 1
, i
, 'n'), '<%!', ''), '%>', '');
$else
l_tmp :=
REGEXP_SUBSTR (p_template
, '<%!([^%>].*?)%>'
, 1
, i
, 'n'
, 1);
$end
l_declare := l_declare || l_tmp;
EXIT WHEN LENGTH (l_tmp) = 0;
END LOOP;
--Delete declaration blocks from template
p_template :=
REGEXP_REPLACE (p_template
, '<%!([^%>].*?)%>'
, ''
, 1
, 0
, 'n');
--Expresison directive
p_template :=
REGEXP_REPLACE (p_template
, '<%=([^%>].*?)%>'
, ']'');tePLSQL.p(\1);tePLSQL.p(q''['
, 1
, 0
, 'n');
--Code blocks directive
--p_template :=
-- REGEXP_REPLACE (p_template
-- , '<%([^%>].*?)%>'
-- , ']''); \1 tePLSQL.p(q''['
-- , 1
-- , 0
-- , 'n');
p_template := 'DECLARE ' || l_declare || ' BEGIN tePLSQL.p(q''[' || p_template || ']''); END;';
END interpret;
/**
* Search for include directives, includes and evaluates the specified templates.
* Nested include are allowed
*
* @param p_template the template
* @param p_vars the associative array
*/
PROCEDURE get_includes (p_template IN OUT NOCOPY CLOB, p_vars IN t_assoc_array DEFAULT null_assoc_array )
AS
l_tmp CLOB;
l_result CLOB;
l_str_tmp VARCHAR2 (32767);
l_inc t_include_parameters;
TYPE array_t IS TABLE OF VARCHAR2 (64);
l_strig_tt array_t;
l_object_name VARCHAR2 (64);
l_template_name VARCHAR2 (64);
l_object_type VARCHAR2 (64);
l_schema VARCHAR2 (64);
l_start PLS_INTEGER := 0;
l_end PLS_INTEGER := 0;
l_number_includes PLS_INTEGER := 0;
BEGIN
/*
--Pseudocode
while there includes
do
find include directive
extract parameters (decode_include_parameters)
get include (get_template)
interpret template
concatenate result template into p_template
done
*/
WHILE REGEXP_INSTR (p_template, '<%@ include\((.*?)\)\s*%>') <> 0
LOOP
--Init
l_str_tmp := NULL;
l_object_name := NULL;
l_template_name := NULL;
l_object_type := NULL;
l_schema := NULL;
l_tmp := NULL;
l_start := 0;
l_end := 0;
--get include directive
$if dbms_db_version.ver_le_10 $then
l_str_tmp :=
REGEXP_REPLACE (REGEXP_REPLACE (REGEXP_SUBSTR (p_template
, '<%@ include\((.*?)\)\s*%>'
, 1
, 1
, 'n'),'<%@ include\(',''),'\)\s*%>','');
$else
l_str_tmp :=
REGEXP_SUBSTR (p_template
, '<%@ include\((.*?)\)\s*%>'
, 1
, 1
, 'n'
, 1);
$end
IF LENGTH (l_str_tmp) > 0
THEN
--Bind the variables into include() parameters
bind_vars (l_str_tmp, p_vars);
-- translate the string
l_inc := decode_include_parameters(l_str_tmp);
--get included template
l_tmp := get_template( l_inc );
--Interpret the template
interpret (l_tmp, p_vars);
l_tmp := ']'');'
|| case when l_inc.indent > 0 then chr(10) || 'teplsql.begin_indent;' || chr(10) end
|| l_tmp
|| case when l_inc.indent > 0 then chr(10) || 'teplsql.end_indent;' || chr(10) end
||' tePLSQL.p(q''[';
--Start and End of the expression
l_start :=
REGEXP_INSTR (p_template
, '<%@ include\((.*?)\)\s*%>'
, 1
, 1
, 0
, 'n');
l_end :=
REGEXP_INSTR (p_template
, '<%@ include\((.*?)\)\s*%>'
, 1
, 1
, 1
, 'n');
--concatenate result template into first template
IF (NVL (l_start, 0) > 0)
THEN