-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_gen__package.sql
1281 lines (1129 loc) · 48.4 KB
/
pkg_gen__package.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
-- encoding Windows-1251
-- https://github.com/ValeriyTyutyunnik/PL-SQL-PackageGenerator
create or replace package pkg_gen as
/*
MIT License
Copyright (c) 2020 Valeriy Tyutyunnik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
-- define the prefix and suffix that should be part of the parameter/variable name
param_prefix varchar2(5 char) := 'p_';
param_suffix varchar2(5 char);
var_prefix varchar2(5 char) := 'l_';
var_suffix varchar2(5 char);
-- if true returns null when no_data_found exception raises at select functions otherwise raises an application error
use_ndf_ret_null boolean := true;
-- if true uses %type attribute for variable and parameter declarations
use_type_attr boolean := false;
-- set false for any program types whose code should not be generated
print_ins_proc boolean := true;
print_ins_func boolean := true;
print_del_proc boolean := true;
print_common_upd_proc boolean := true;
print_upd_proc boolean := true;
print_exist_func boolean := true;
print_select_funcs boolean := true;
/**
* Set documentation format you need or leave it empty to leave code without documentation.
* Availablable documentation snippets:
* {author} - substitutes the value of a variable doc_author
* {name} - programm name
* {params} - all programm parameters line by line with str_before_param_tag data at begin of line
* and str_after_param_tag at the end of it
* {cs1} .. {cs5} - custom snippets
*/
pkg_doc varchar2(1000 char);
proc_doc varchar2(1000 char);
func_doc varchar2(1000 char);
str_before_param_tag varchar2(100 char);
str_after_param_tag varchar2(100 char);
doc_author varchar2(100 char);
custom_snippet_1 varchar2(1000 char);
custom_snippet_2 varchar2(1000 char);
custom_snippet_3 varchar2(1000 char);
custom_snippet_4 varchar2(1000 char);
custom_snippet_5 varchar2(1000 char);
/**
* The main procedure in which the internal data is initialized and some settings are reset.
* The procedure must be called first.
* @param p_table_name - Name of the table with which the generated code will work.
* The table must be physically created.
* @param p_owner - table owner (schema)
* @param p_pk_column - Primary key of the table. if not specified, then the data will be taken
* from the system information about the primary key of the table.
* If physically the primary key does not exist,
* be sure to indicate in this parameter the column that acts as the primary key.
* In other cases, code generation does not work.
* @param p_pkg_name - The name of the package that will be installed for the generated code.
* If not specified then "%table_name%_pkg" will be used.
* @param p_name_chr_limit - Number of characters that limits the length of the variable/parameter name.
* Default 30. From version 12.2 of Oracle Database, the maximum size of
* most identifiers has been increased from 30 to 128 bytes.
* Minimum size is 10.
*/
procedure init(p_table_name varchar2,
p_owner varchar2 default null,
p_pk_column varchar2 default null,
p_pkg_name varchar2 default null,
p_name_chr_limit integer default null);
/**
* Add column to the exclusion list to prevent
* the generation of a select function code.
* @param p_col_name - column name
*/
procedure add_exclude_select_col(p_col_name varchar2);
/**
* Add column to the exclusion list to prevent
* the generation of a update procedure code.
* @param p_col_name - column name
*/
procedure add_exclude_update_col(p_col_name varchar2);
/**
* Generates into output package code with all functions and procedures
*/
procedure generate;
/**
* Generates a procedure that inserts data into a table.
* As parameters, all columns of the table are used except virtual ones.
* For nullable columns, the parameters are set to the default null.
* @return - generated code
*/
function gen_ins_proc
return varchar2;
/**
* Generates a function that inserts data into a table and returns PK value of created row.
* As parameters, all columns of the table are used except virtual ones.
* For nullable columns, the parameters are set to the default null.
* @return - generated code
*/
function gen_ins_func
return varchar2;
/**
* Generates a procedure that delete row from the table by PK value.
* @return - generated code
*/
function gen_del_proc
return varchar2;
/**
* Generates a procedure that update all columns of a table except virtual and PK ones by PK value.
* All parameters are default null except PK.
* A null value does not overwrite non-null data in a column.
* @return - generated code
*/
function gen_common_upd_proc
return varchar2;
/**
* Generates a procedure that update one column of a table by PK value.
* @param p_col_indx - index in the column collection for which the procedure is being created
* @return - generated code
*/
function gen_upd_proc(p_col_indx integer)
return varchar2;
/**
* Generates a function that checks if record exist in a table by PK and returns boolean.
* @return - generated code
*/
function gen_exist_func
return varchar2;
/**
* Generates a function that returns one column value of a table by PK.
* @param p_col_indx - index in the column collection for which the function is being created
* @return - generated code
*/
function gen_sel_func(p_col_indx integer)
return varchar2;
end pkg_gen;
/
create or replace package body pkg_gen as
lf constant varchar2(1 char) := chr(10);
lf2 constant varchar2(2 char) := lf || lf;
type col_rec is record (column_name all_tab_cols.column_name%type,
data_type all_tab_cols.data_type%type,
data_precision all_tab_cols.data_precision%type,
data_scale all_tab_cols.data_scale%type,
char_length all_tab_cols.char_length%type,
char_used all_tab_cols.char_used%type,
virtual_column all_tab_cols.virtual_column%type,
nullable all_tab_cols.nullable%type);
type cols_tbl is table of col_rec index by pls_integer;
type params_tbl is table of varchar2(200) index by pls_integer;
type varchar_tbl is table of varchar2(200);
-- this collections and variables will reset whenever procedure "init" executed
g_select_excluded_cols varchar_tbl := varchar_tbl();
g_update_excluded_cols varchar_tbl := varchar_tbl();
g_table_name all_tables.table_name%type;
g_alias varchar2(1);
g_pk_column_rec col_rec;
g_columns_tbl cols_tbl;
g_pkg_name varchar2(128);
g_chr_limit integer := 30;
g_initialized boolean := false;
/**
* Return declaration data type for variable/parameter by system information.
* @param p_data_type - datatype of the column
* @param p_data_precision - decimal precision for number and float datatypes
* @param p_data_scale - digits to the right of the decimal point in a number
* @param p_char_length - length of the column for char datatypes
* @param p_char_used - indicates that the column uses BYTE length semantics (B) or CHAR length semantics (C)
(for char datatypes)
* @param p_add_precision - if false then returns only data type name without precision or length
*/
function get_data_type(p_data_type varchar2,
p_data_precision number,
p_data_scale number,
p_char_length number default null,
p_char_used varchar2 default null,
p_add_precision boolean default true)
return varchar2
is
l_result varchar2(100);
begin
if p_data_type = 'NUMBER' and p_data_precision is null and p_data_scale = 0 then
l_result := 'integer';
else
l_result := p_data_type;
if p_add_precision then
if p_data_precision is not null and p_data_scale is not null then
l_result := l_result || '(' || p_data_precision || ', ' || p_data_scale || ')';
elsif nvl(p_char_length, 0) > 0 then
l_result := l_result || '(' || p_char_length;
if p_char_used = 'C' then
l_result := l_result || ' char';
end if;
l_result := l_result || ')';
end if;
end if;
end if;
return l_result;
end get_data_type;
/**
* Return declaration type for parameter depending on the use_type_attr setting.
* @param p_column_name - name of the column
* @param p_data_type - column data_type
*/
function get_declaration_type(p_column_name varchar2,
p_data_type varchar2)
return varchar2
is
l_result varchar2(200);
begin
if use_type_attr then
l_result := g_table_name || '.' || p_column_name || '%type';
else
l_result := p_data_type;
end if;
return l_result;
end get_declaration_type;
/**
* Replaces snippets at documentation template.
* @param p_doc - documetation template
* @param p_name - name of the documented object
*/
function format_doc(p_doc varchar2,
p_name varchar2)
return varchar2
is
l_result varchar2(10000);
begin
if p_doc is not null then
l_result := replace(p_doc, '{author}', doc_author);
l_result := replace(l_result, '{name}', p_name);
l_result := replace(l_result, '{cs1}', custom_snippet_1);
l_result := replace(l_result, '{cs2}', custom_snippet_2);
l_result := replace(l_result, '{cs3}', custom_snippet_3);
l_result := replace(l_result, '{cs4}', custom_snippet_4);
l_result := replace(l_result, '{cs5}', custom_snippet_5);
end if;
return l_result;
exception
when others then
raise_application_error(-20001, dbms_utility.format_error_stack);
end format_doc;
/**
* Replaces snippets at documentation template.
* @override
* @param p_doc - documetation template
* @param p_name - name of the documented object
* @param p_params - list of params than should replace {params} snippets
*/
function format_doc(p_doc varchar2,
p_name varchar2,
p_params params_tbl)
return varchar2
is
l_result varchar2(10000);
l_tmp varchar2(4000);
begin
if p_doc is not null then
l_result := format_doc(p_doc, p_name);
if instr(l_result, '{params}') > 0 then
for i in 1 .. p_params.count loop
if i > 1 then
l_tmp := l_tmp || lf;
end if;
l_tmp := l_tmp || str_before_param_tag || p_params(i) || str_after_param_tag;
end loop;
l_result := replace(l_result, '{params}', l_tmp);
end if;
end if;
return l_result;
exception
when others then
raise_application_error(-20001, dbms_utility.format_error_stack);
end format_doc;
/**
* Return the param name that should be used in code.
* Name consists of prefix, middle part and suffix.
* Prefix and suffix is set by param_prefix and param_suffix variables and could be null.
* Parameter length is limited by setting in procedure init.name_chr_limit.
* @param p_param_middle_part - main part of parameter name
*/
function get_param_name(p_param_middle_part varchar2)
return varchar2
is
begin
return lower(substr(param_prefix || p_param_middle_part || param_suffix, 1, g_chr_limit));
end get_param_name;
/**
* Return the param name that should be used in code.
* Name consists of prefix, middle part and suffix.
* Prefix and suffix is set by var_prefix and var_suffix variables and could be null.
* Parameter length is limited by setting in procedure init.name_chr_limit.
* @param p_var_middle_part - main part of variable name
*/
function get_var_name(p_var_middle_part varchar2)
return varchar2
is
begin
return lower(substr(var_prefix || p_var_middle_part || var_suffix, 1, g_chr_limit));
end get_var_name;
/**
* Add column to the exclusion list to prevent
* the generation of a select function code.
* @param p_col_name - column name
*/
procedure add_exclude_select_col(p_col_name varchar2)
is
begin
if p_col_name is not null and not lower(p_col_name) member of g_select_excluded_cols then
g_select_excluded_cols.extend;
g_select_excluded_cols(g_select_excluded_cols.last) := lower(p_col_name);
end if;
end add_exclude_select_col;
/**
* Add column to the exclusion list to prevent
* the generation of a update procedure code.
* @param p_col_name - column name
*/
procedure add_exclude_update_col(p_col_name varchar2)
is
begin
if p_col_name is not null and not lower(p_col_name) member of g_update_excluded_cols then
g_update_excluded_cols.extend;
g_update_excluded_cols(g_update_excluded_cols.last) := lower(p_col_name);
end if;
end add_exclude_update_col;
/**
* The main procedure in which the internal data is initialized and some settings are reset.
* The procedure must be called first.
* @param p_table_name - Name of the table with which the generated code will work.
* The table must be physically created.
* @param p_owner - table owner (schema)
* @param p_pk_column - Primary key of the table. if not specified, then the data will be taken
* from the system information about the primary key of the table.
* If physically the primary key does not exist,
* be sure to indicate in this parameter the column that acts as the primary key.
* In other cases, code generation does not work.
* @param p_pkg_name - The name of the package that will be installed for the generated code.
* If not specified then "%table_name%_pkg" will be used.
* @param p_name_chr_limit - Number of characters that limits the length of the variable/parameter name.
* Default 30. From version 12.2 of Oracle Database, the maximum size of
* most identifiers has been increased from 30 to 128 bytes.
* Minimum size is 10.
*/
procedure init(p_table_name varchar2,
p_owner varchar2 default null,
p_pk_column varchar2 default null,
p_pkg_name varchar2 default null,
p_name_chr_limit integer default null)
is
l_table_name all_tables.table_name%type;
l_owner all_constraints.owner%type;
begin
g_initialized := false;
g_chr_limit := 30;
if p_table_name is null then
raise_application_error(-20001, 'empty table name!');
end if;
l_owner := coalesce(upper(p_owner), user);
l_table_name := upper(p_table_name);
g_table_name := lower(p_table_name);
g_alias := substr(g_table_name, 1, 1);
g_select_excluded_cols := varchar_tbl();
g_update_excluded_cols := varchar_tbl();
if p_name_chr_limit is not null then
if p_name_chr_limit between 10 and 128 then
g_chr_limit := p_name_chr_limit;
else
raise_application_error(-20002, 'chr_limit must be between 10 and 128');
end if;
end if;
if p_pkg_name is not null then
if length(p_pkg_name) <= 128 then
g_pkg_name := lower(p_pkg_name);
else
raise_application_error(-20003, 'p_pkg_name is too long');
end if;
else
g_pkg_name := substr(g_table_name, 1, g_chr_limit);
if length(g_pkg_name) + 4 <= g_chr_limit then
g_pkg_name := g_pkg_name||'_pkg';
end if;
end if;
begin
if p_pk_column is null then
select lower(tc.column_name),
lower(tc.data_type),
tc.data_precision,
tc.data_scale,
tc.char_length,
tc.char_used,
tc.virtual_column,
tc.nullable
into g_pk_column_rec
from all_constraints c,
all_cons_columns cc,
all_tab_cols tc
where c.owner = l_owner
and c.table_name = l_table_name
and c.constraint_type = 'P'
and c.constraint_name = cc.constraint_name
and c.owner = cc.owner
and c.table_name = cc.table_name
and cc.column_name = tc.column_name
and cc.owner = tc.owner
and cc.table_name = tc.table_name;
else
select lower(tc.column_name),
lower(tc.data_type),
tc.data_precision,
tc.data_scale,
tc.char_length,
tc.char_used,
tc.virtual_column,
tc.nullable
into g_pk_column_rec
from all_tab_cols tc
where tc.column_name = upper(p_pk_column)
and tc.owner = l_owner
and tc.table_name = l_table_name;
end if;
exception
when no_data_found then
raise_application_error(-20004, 'Primary key column not found or table not exists!');
end;
for rec in (select lower(tc.column_name) as column_name,
lower(tc.data_type) as data_type,
tc.data_precision,
tc.data_scale,
tc.char_length,
tc.char_used,
tc.virtual_column,
tc.nullable
from all_tab_cols tc
where tc.owner = l_owner
and tc.table_name = l_table_name
and lower(tc.column_name) != g_pk_column_rec.column_name
and tc.hidden_column = 'NO'
order by tc.column_id)
loop
g_columns_tbl(g_columns_tbl.count+1).column_name := rec.column_name;
g_columns_tbl(g_columns_tbl.count).data_type := rec.data_type;
g_columns_tbl(g_columns_tbl.count).data_precision := rec.data_precision;
g_columns_tbl(g_columns_tbl.count).data_scale := rec.data_scale;
g_columns_tbl(g_columns_tbl.count).char_length := rec.char_length;
g_columns_tbl(g_columns_tbl.count).char_used := rec.char_used;
g_columns_tbl(g_columns_tbl.count).virtual_column := rec.virtual_column;
g_columns_tbl(g_columns_tbl.count).nullable := rec.nullable;
end loop;
g_initialized := true;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end init;
/**
* Generates a procedure that inserts data into a table.
* As parameters, all columns of the table are used except virtual ones.
* For nullable columns, the parameters are set to the default null.
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_ins_proc(p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_line_len number;
l_first_col_set boolean := false;
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
l_name := 'ins_'||substr(g_table_name, 1, g_chr_limit-4);
p_body := ' procedure ' || l_name || '(';
l_line_len := length(p_body);
if g_pk_column_rec.virtual_column <> 'YES' then
l_params_tab(l_params_tab.count+1) := get_param_name(g_pk_column_rec.column_name);
p_body := p_body || l_params_tab(l_params_tab.count) || ' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type);
l_first_col_set := true;
end if;
for i in 1 .. g_columns_tbl.count loop
continue when g_columns_tbl(i).virtual_column = 'YES';
l_params_tab(l_params_tab.count+1) := get_param_name(g_columns_tbl(i).column_name);
if l_first_col_set then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || l_params_tab(l_params_tab.count) || ' '
|| get_declaration_type(g_columns_tbl(i).column_name, g_columns_tbl(i).data_type)
|| case
when g_columns_tbl(i).nullable = 'Y'
then ' default null'
else null
end;
l_first_col_set := true;
end loop;
if l_params_tab.count = 0 then
p_body := null;
p_spec := null;
return;
end if;
p_body := p_body || ')';
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' begin' || lf2
|| ' insert' || lf
|| ' into ' || g_table_name || ' (';
l_line_len := 13 + length(g_table_name);
l_first_col_set := false;
if g_pk_column_rec.virtual_column <> 'YES' then
p_body := p_body || g_pk_column_rec.column_name;
l_first_col_set := true;
end if;
for i in 1 .. g_columns_tbl.count loop
continue when g_columns_tbl(i).virtual_column = 'YES';
if l_first_col_set then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || g_columns_tbl(i).column_name;
l_first_col_set := true;
end loop;
p_body := p_body || ')' || lf || ' values (';
l_line_len := 14;
for i in 1 .. l_params_tab.count loop
if i > 1 then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || l_params_tab(i);
end loop;
p_body := p_body || ');' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if proc_doc is not null then
p_body := format_doc(proc_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(proc_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_ins_proc;
/**
* Generates a function that inserts data into a table and returns PK value of created row.
* As parameters, all columns of the table are used except virtual ones.
* For nullable columns, the parameters are set to the default null.
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_ins_func(p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_res_var_name varchar2(128) := get_var_name('result');
l_line_len number;
l_first_col_set boolean := false;
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
l_name := 'ins_'||substr(g_table_name, 1, g_chr_limit-4);
p_body := ' function ' || l_name || '(';
l_line_len := length(p_body);
if g_pk_column_rec.virtual_column <> 'YES' then
l_params_tab(l_params_tab.count+1) := get_param_name(g_pk_column_rec.column_name);
p_body := p_body || l_params_tab(l_params_tab.count) || ' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type);
l_first_col_set := true;
end if;
for i in 1 .. g_columns_tbl.count loop
continue when g_columns_tbl(i).virtual_column = 'YES';
if l_first_col_set then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
l_params_tab(l_params_tab.count+1) := get_param_name(g_columns_tbl(i).column_name);
p_body := p_body || l_params_tab(l_params_tab.count) || ' '
|| get_declaration_type(g_columns_tbl(i).column_name, g_columns_tbl(i).data_type)
|| case
when g_columns_tbl(i).nullable = 'Y'
then ' default null'
else null
end;
l_first_col_set := true;
end loop;
if l_params_tab.count = 0 then
p_body := null;
p_spec := null;
return;
end if;
p_body := p_body || ')' || lf || ' return '
|| get_declaration_type(g_pk_column_rec.column_name,
get_data_type(g_pk_column_rec.data_type,
g_pk_column_rec.data_precision,
g_pk_column_rec.data_scale,
p_add_precision => false));
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' ' || l_res_var_name || ' '
|| get_declaration_type(g_pk_column_rec.column_name,
get_data_type(g_pk_column_rec.data_type,
g_pk_column_rec.data_precision,
g_pk_column_rec.data_scale,
g_pk_column_rec.char_length,
g_pk_column_rec.char_used))
|| ';' || lf || ' begin' || lf2
|| ' insert ' || lf
|| ' into ' || g_table_name || ' (';
l_line_len := 13 + length(g_table_name);
l_first_col_set := false;
if g_pk_column_rec.virtual_column <> 'YES' then
p_body := p_body || g_pk_column_rec.column_name;
l_first_col_set := true;
end if;
for i in 1 .. g_columns_tbl.count loop
continue when g_columns_tbl(i).virtual_column = 'YES';
if l_first_col_set then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || g_columns_tbl(i).column_name;
l_first_col_set := true;
end loop;
p_body := p_body || ')' || lf || ' values (';
l_line_len := 14;
for i in 1 .. l_params_tab.count loop
if i > 1 then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || l_params_tab(i);
end loop;
p_body := p_body || ')' || lf
|| ' returning ' || g_pk_column_rec.column_name || ' into ' || l_res_var_name || ';' || lf2
|| ' return ' || l_res_var_name || ';' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if func_doc is not null then
p_body := format_doc(func_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(func_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_ins_func;
/**
* Generates a procedure that delete row from the table by PK value.
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_del_proc(p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
l_params_tab(1) := get_param_name(g_pk_column_rec.column_name);
l_name := 'del_'||substr(g_table_name, 1, g_chr_limit-4);
p_body := ' procedure ' || l_name || '('
|| l_params_tab(1) || ' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type)
|| ')';
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' begin' || lf2
|| ' delete from ' || g_table_name || ' ' || g_alias || lf
|| ' where ' || g_alias || '.' || g_pk_column_rec.column_name
|| ' = ' || l_params_tab(1) || ';' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if proc_doc is not null then
p_body := format_doc(proc_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(proc_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_del_proc;
/**
* Generates a procedure that update all columns of a table except virtual and PK ones by PK value.
* All parameters are default null except PK.
* A null value does not overwrite non-null data in a column.
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_common_upd_proc(p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_line_len number;
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
l_name := 'upd_'||substr(g_table_name, 1, g_chr_limit-4);
p_body := ' procedure ' || l_name || '(';
l_line_len := length(p_body);
l_params_tab(1) := get_param_name(g_pk_column_rec.column_name);
p_body := p_body || l_params_tab(1) || ' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type);
for i in 1 .. g_columns_tbl.count loop
continue when g_columns_tbl(i).virtual_column = 'YES';
l_params_tab(l_params_tab.count+1) := get_param_name(g_columns_tbl(i).column_name);
p_body := p_body || ',' || lf || rpad(' ', l_line_len)
|| l_params_tab(l_params_tab.count) || ' '
|| get_declaration_type(g_columns_tbl(i).column_name, g_columns_tbl(i).data_type)
|| ' default null';
end loop;
p_body := p_body || ')';
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' begin' || lf2
|| ' update ' || g_table_name || ' ' || g_alias || lf
|| ' set ';
l_line_len := 11;
for i in 2 .. l_params_tab.count loop
if i > 2 then
p_body := p_body || ',' || lf || rpad(' ', l_line_len);
end if;
p_body := p_body || g_alias || '.' || g_columns_tbl(i-1).column_name
|| ' = nvl(' || l_params_tab(i) || ', '
|| g_alias || '.' || g_columns_tbl(i-1).column_name || ')';
end loop;
p_body := p_body || lf || ' where ' || g_alias || '.' || g_pk_column_rec.column_name
|| ' = ' || l_params_tab(1) || ';' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if proc_doc is not null then
p_body := format_doc(proc_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(proc_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_common_upd_proc;
/**
* Generates a procedure that update one column of a table by PK value.
* @param p_col_indx - index in the column collection for which the procedure is being created
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_upd_proc(p_col_indx in integer,
p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_line_len number;
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
if g_columns_tbl(p_col_indx).virtual_column = 'YES' then
raise_application_error(-20005, 'Imposible to update virtual column');
end if;
l_name := 'upd_'||substr(g_columns_tbl(p_col_indx).column_name, 1, g_chr_limit-4);
p_body := ' procedure ' || l_name || '(';
l_line_len := length(p_body);
l_params_tab(1) := get_param_name(g_pk_column_rec.column_name);
l_params_tab(2) := get_param_name(g_columns_tbl(p_col_indx).column_name);
p_body := p_body || l_params_tab(1) || ' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type)
|| ',' || lf || rpad(' ', l_line_len)
|| l_params_tab(2) || ' '
|| get_declaration_type(g_columns_tbl(p_col_indx).column_name, g_columns_tbl(p_col_indx).data_type)
|| ')';
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' begin' || lf2
|| ' update ' || g_table_name || ' ' || g_alias || lf
|| ' set ' || g_alias || '.' || g_columns_tbl(p_col_indx).column_name
|| ' = ' || l_params_tab(2) || lf
|| ' where ' || g_alias || '.' || g_pk_column_rec.column_name
|| ' = ' || l_params_tab(1) || ';' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if proc_doc is not null then
p_body := format_doc(proc_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(proc_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_upd_proc;
/**
* Generates a function that checks if record exist in a table by PK and returns boolean
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_exist_func(p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_res_var_name varchar2(128) := get_var_name('result');
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;
l_name := 'exists_'||substr(g_table_name, 1, g_chr_limit-7);
p_body := ' function ' || l_name || '(';
l_params_tab(1) := get_param_name(g_pk_column_rec.column_name);
p_body := p_body || l_params_tab(1) ||' '
|| get_declaration_type(g_pk_column_rec.column_name, g_pk_column_rec.data_type)
|| ')' || lf || ' return boolean';
p_spec := p_body || ';' || lf2;
p_body := p_body || lf || ' is' || lf || ' ' || l_res_var_name || ' integer;' || lf
|| ' begin' || lf2
|| ' select case' || lf
|| ' when exists (select 1' || lf
|| ' from ' || g_table_name || ' ' || g_alias ||lf
|| ' where ' || g_alias || '.'
|| g_pk_column_rec.column_name || ' = ' || l_params_tab(1) || ')' || lf
|| ' then 1' || lf
|| ' else 0' || lf
|| ' end' || lf
|| ' into ' || l_res_var_name || lf
|| ' from dual;' || lf2
|| ' return ' || l_res_var_name || ' = 1;' || lf2
|| ' exception' || lf
|| ' when others then' || lf
|| ' raise_application_error(-20001, dbms_utility.format_error_stack);' || lf
|| ' end ' || l_name || ';' || lf2;
p_body := lower(p_body);
p_spec := lower(p_spec);
if func_doc is not null then
p_body := format_doc(func_doc, l_name, l_params_tab) || lf || p_body;
p_spec := format_doc(func_doc, l_name, l_params_tab) || lf || p_spec;
end if;
exception
when others then
raise_application_error(-20000, dbms_utility.format_error_stack);
end gen_exist_func;
/**
* Generates a function that returns one column value of a table by PK.
* @param p_col_indx - index in the column collection for which the function is being created
* @param p_spec - generated code for package spec
* @param p_body - generated code for package body
*/
procedure gen_sel_func(p_col_indx in integer,
p_spec out varchar2,
p_body out varchar2)
is
l_params_tab params_tbl;
l_name varchar2(128);
l_res_var_name varchar2(128) := get_var_name('result');
begin
if not g_initialized then
raise_application_error(-20001, 'Not initialized');
end if;