-
Notifications
You must be signed in to change notification settings - Fork 14
/
install_all_packages.sql
3452 lines (3268 loc) · 143 KB
/
install_all_packages.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
/**********************************************
**
** Author: Pavel Glebov
** Date: 11-2020
** Version: 3.27
**
** This all in one install script contains headrs and bodies of 5 packages
**
** IR_TO_XML.sql
** AS_ZIP.sql
** IR_TO_XSLX.sql
** IR_TO_MSEXCEL.sql
** APEXIR_XLSX_PKG.sql (mock)
**
**********************************************/
set define off;
CREATE OR REPLACE PACKAGE "AS_ZIP"
is
/**********************************************
**
** Author: Anton Scheffer
** Date: 25-01-2012
** Website: http://technology.amis.nl/blog
**
** Changelog:
** Date: 29-04-2012
** fixed bug for large uncompressed files, thanks Morten Braten
** Date: 21-03-2012
** Take CRC32, compressed length and uncompressed length from
** Central file header instead of Local file header
** Date: 17-02-2012
** Added more support for non-ascii filenames
** Date: 25-01-2012
** Added MIT-license
** Some minor improvements
**
******************************************************************************
******************************************************************************
Copyright (C) 2010,2011 by Anton Scheffer
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.
******************************************************************************
******************************************** */
type file_list is table of clob;
--
function file2blob
( p_dir varchar2
, p_file_name varchar2
)
return blob;
--
function get_file_list
( p_dir varchar2
, p_zip_file varchar2
, p_encoding varchar2 := null
)
return file_list;
--
function get_file_list
( p_zipped_blob blob
, p_encoding varchar2 := null
)
return file_list;
--
function get_file
( p_dir varchar2
, p_zip_file varchar2
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob;
--
function get_file
( p_zipped_blob blob
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob;
--
procedure add1file
( p_zipped_blob in out blob
, p_name varchar2
, p_content blob
);
--
procedure finish_zip( p_zipped_blob in out blob );
--
procedure save_zip
( p_zipped_blob blob
, p_dir varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.zip'
);
--
/*
declare
g_zipped_blob blob;
begin
as_zip.add1file( g_zipped_blob, 'test4.txt', null ); -- a empty file
as_zip.add1file( g_zipped_blob, 'dir1/test1.txt', utl_raw.cast_to_raw( q'<A file with some more text, stored in a subfolder which isn't added>' ) );
as_zip.add1file( g_zipped_blob, 'test1234.txt', utl_raw.cast_to_raw( 'A small file' ) );
as_zip.add1file( g_zipped_blob, 'dir2/', null ); -- a folder
as_zip.add1file( g_zipped_blob, 'dir3/', null ); -- a folder
as_zip.add1file( g_zipped_blob, 'dir3/test2.txt', utl_raw.cast_to_raw( 'A small filein a previous created folder' ) );
as_zip.finish_zip( g_zipped_blob );
as_zip.save_zip( g_zipped_blob, 'MY_DIR', 'my.zip' );
dbms_lob.freetemporary( g_zipped_blob );
end;
--
declare
zip_files as_zip.file_list;
begin
zip_files := as_zip.get_file_list( 'MY_DIR', 'my.zip' );
for i in zip_files.first() .. zip_files.last
loop
dbms_output.put_line( zip_files( i ) );
dbms_output.put_line( utl_raw.cast_to_varchar2( as_zip.get_file( 'MY_DIR', 'my.zip', zip_files( i ) ) ) );
end loop;
end;
*/
end;
/
CREATE OR REPLACE PACKAGE BODY "AS_ZIP"
is
--
c_LOCAL_FILE_HEADER constant raw(4) := hextoraw( '504B0304' ); -- Local file header signature
c_END_OF_CENTRAL_DIRECTORY constant raw(4) := hextoraw( '504B0506' ); -- End of central directory signature
--
function blob2num( p_blob blob, p_len integer, p_pos integer )
return number
is
begin
return utl_raw.cast_to_binary_integer( dbms_lob.substr( p_blob, p_len, p_pos ), utl_raw.little_endian );
end;
--
function raw2varchar2( p_raw raw, p_encoding varchar2 )
return varchar2
is
begin
return coalesce( utl_i18n.raw_to_char( p_raw, p_encoding )
, utl_i18n.raw_to_char( p_raw, utl_i18n.map_charset( p_encoding, utl_i18n.GENERIC_CONTEXT, utl_i18n.IANA_TO_ORACLE ) )
);
end;
--
function little_endian( p_big number, p_bytes pls_integer := 4 )
return raw
is
begin
return utl_raw.substr( utl_raw.cast_from_binary_integer( p_big, utl_raw.little_endian ), 1, p_bytes );
end;
--
function file2blob
( p_dir varchar2
, p_file_name varchar2
)
return blob
is
file_lob bfile;
file_blob blob;
begin
file_lob := bfilename( p_dir, p_file_name );
dbms_lob.open( file_lob, dbms_lob.file_readonly );
dbms_lob.createtemporary( file_blob, true );
dbms_lob.loadfromfile( file_blob, file_lob, dbms_lob.lobmaxsize );
dbms_lob.close( file_lob );
return file_blob;
exception
when others then
if dbms_lob.isopen( file_lob ) = 1
then
dbms_lob.close( file_lob );
end if;
if dbms_lob.istemporary( file_blob ) = 1
then
dbms_lob.freetemporary( file_blob );
end if;
raise;
end;
--
function get_file_list
( p_zipped_blob blob
, p_encoding varchar2 := null
)
return file_list
is
t_ind integer;
t_hd_ind integer;
t_rv file_list;
t_encoding varchar2(32767);
begin
t_ind := dbms_lob.getlength( p_zipped_blob ) - 21;
loop
exit when t_ind < 1 or dbms_lob.substr( p_zipped_blob, 4, t_ind ) = c_END_OF_CENTRAL_DIRECTORY;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := blob2num( p_zipped_blob, 4, t_ind + 16 ) + 1;
t_rv := file_list();
t_rv.extend( blob2num( p_zipped_blob, 2, t_ind + 10 ) );
for i in 1 .. blob2num( p_zipped_blob, 2, t_ind + 8 )
loop
if p_encoding is null
then
if utl_raw.bit_and( dbms_lob.substr( p_zipped_blob, 1, t_hd_ind + 9 ), hextoraw( '08' ) ) = hextoraw( '08' )
then
t_encoding := 'AL32UTF8'; -- utf8
else
t_encoding := 'US8PC437'; -- IBM codepage 437
end if;
else
t_encoding := p_encoding;
end if;
t_rv( i ) := raw2varchar2
( dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_hd_ind + 28 )
, t_hd_ind + 46
)
, t_encoding
);
t_hd_ind := t_hd_ind + 46
+ blob2num( p_zipped_blob, 2, t_hd_ind + 28 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 30 ) -- Extra field length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 32 ); -- File comment length
end loop;
--
return t_rv;
end;
--
function get_file_list
( p_dir varchar2
, p_zip_file varchar2
, p_encoding varchar2 := null
)
return file_list
is
begin
return get_file_list( file2blob( p_dir, p_zip_file ), p_encoding );
end;
--
function get_file
( p_zipped_blob blob
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob
is
t_tmp blob;
t_ind integer;
t_hd_ind integer;
t_fl_ind integer;
t_encoding varchar2(32767);
t_len integer;
begin
t_ind := dbms_lob.getlength( p_zipped_blob ) - 21;
loop
exit when t_ind < 1 or dbms_lob.substr( p_zipped_blob, 4, t_ind ) = c_END_OF_CENTRAL_DIRECTORY;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := blob2num( p_zipped_blob, 4, t_ind + 16 ) + 1;
for i in 1 .. blob2num( p_zipped_blob, 2, t_ind + 8 )
loop
if p_encoding is null
then
if utl_raw.bit_and( dbms_lob.substr( p_zipped_blob, 1, t_hd_ind + 9 ), hextoraw( '08' ) ) = hextoraw( '08' )
then
t_encoding := 'AL32UTF8'; -- utf8
else
t_encoding := 'US8PC437'; -- IBM codepage 437
end if;
else
t_encoding := p_encoding;
end if;
if p_file_name = raw2varchar2
( dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_hd_ind + 28 )
, t_hd_ind + 46
)
, t_encoding
)
then
t_len := blob2num( p_zipped_blob, 4, t_hd_ind + 24 ); -- uncompressed length
if t_len = 0
then
if substr( p_file_name, -1 ) in ( '/', '\' )
then -- directory/folder
return null;
else -- empty file
return empty_blob();
end if;
end if;
--
if dbms_lob.substr( p_zipped_blob, 2, t_hd_ind + 10 ) = hextoraw( '0800' ) -- deflate
then
t_fl_ind := blob2num( p_zipped_blob, 4, t_hd_ind + 42 );
t_tmp := hextoraw( '1F8B0800000000000003' ); -- gzip header
dbms_lob.copy( t_tmp
, p_zipped_blob
, blob2num( p_zipped_blob, 4, t_hd_ind + 20 )
, 11
, t_fl_ind + 31
+ blob2num( p_zipped_blob, 2, t_fl_ind + 27 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_fl_ind + 29 ) -- Extra field length
);
dbms_lob.append( t_tmp, utl_raw.concat( dbms_lob.substr( p_zipped_blob, 4, t_hd_ind + 16 ) -- CRC32
, little_endian( t_len ) -- uncompressed length
)
);
return utl_compress.lz_uncompress( t_tmp );
end if;
--
if dbms_lob.substr( p_zipped_blob, 2, t_hd_ind + 10 ) = hextoraw( '0000' ) -- The file is stored (no compression)
then
t_fl_ind := blob2num( p_zipped_blob, 4, t_hd_ind + 42 );
dbms_lob.createtemporary( t_tmp, true );
dbms_lob.copy( t_tmp
, p_zipped_blob
, t_len
, 1
, t_fl_ind + 31
+ blob2num( p_zipped_blob, 2, t_fl_ind + 27 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_fl_ind + 29 ) -- Extra field length
);
return t_tmp;
end if;
end if;
t_hd_ind := t_hd_ind + 46
+ blob2num( p_zipped_blob, 2, t_hd_ind + 28 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 30 ) -- Extra field length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 32 ); -- File comment length
end loop;
--
return null;
end;
--
function get_file
( p_dir varchar2
, p_zip_file varchar2
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob
is
begin
return get_file( file2blob( p_dir, p_zip_file ), p_file_name, p_encoding );
end;
--
procedure add1file
( p_zipped_blob in out blob
, p_name varchar2
, p_content blob
)
is
t_now date;
t_blob blob;
t_len integer;
t_clen integer;
t_crc32 raw(4) := hextoraw( '00000000' );
t_compressed boolean := false;
t_name raw(32767);
begin
t_now := sysdate;
t_len := nvl( dbms_lob.getlength( p_content ), 0 );
if t_len > 0
then
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob ) - 18;
t_compressed := t_clen < t_len;
t_crc32 := dbms_lob.substr( t_blob, 4, t_clen + 11 );
end if;
if not t_compressed
then
t_clen := t_len;
t_blob := p_content;
end if;
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob, true );
end if;
t_name := utl_i18n.string_to_raw( p_name, 'AL32UTF8' );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( c_LOCAL_FILE_HEADER -- Local file header signature
, hextoraw( '1400' ) -- version 2.0
, case when t_name = utl_i18n.string_to_raw( p_name, 'US8PC437' )
then hextoraw( '0000' ) -- no General purpose bits
else hextoraw( '0008' ) -- set Language encoding flag (EFS)
end
, case when t_compressed
then hextoraw( '0800' ) -- deflate
else hextoraw( '0000' ) -- stored
end
, little_endian( to_number( to_char( t_now, 'ss' ) ) / 2
+ to_number( to_char( t_now, 'mi' ) ) * 32
+ to_number( to_char( t_now, 'hh24' ) ) * 2048
, 2
) -- File last modification time
, little_endian( to_number( to_char( t_now, 'dd' ) )
+ to_number( to_char( t_now, 'mm' ) ) * 32
+ ( to_number( to_char( t_now, 'yyyy' ) ) - 1980 ) * 512
, 2
) -- File last modification date
, t_crc32 -- CRC-32
, little_endian( t_clen ) -- compressed size
, little_endian( t_len ) -- uncompressed size
, little_endian( utl_raw.length( t_name ), 2 ) -- File name length
, hextoraw( '0000' ) -- Extra field length
, t_name -- File name
)
);
if t_compressed
then
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 11 ); -- compressed content
elsif t_clen > 0
then
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 1 ); -- content
end if;
if dbms_lob.istemporary( t_blob ) = 1
then
dbms_lob.freetemporary( t_blob );
end if;
end;
--
procedure finish_zip( p_zipped_blob in out blob )
is
t_cnt pls_integer := 0;
t_offs integer;
t_offs_dir_header integer;
t_offs_end_header integer;
t_comment raw(32767) := utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
begin
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := 1;
while dbms_lob.substr( p_zipped_blob, utl_raw.length( c_LOCAL_FILE_HEADER ), t_offs ) = c_LOCAL_FILE_HEADER
loop
t_cnt := t_cnt + 1;
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0102' ) -- Central directory file header signature
, hextoraw( '1400' ) -- version 2.0
, dbms_lob.substr( p_zipped_blob, 26, t_offs + 4 )
, hextoraw( '0000' ) -- File comment length
, hextoraw( '0000' ) -- Disk number where file starts
, hextoraw( '0000' ) -- Internal file attributes =>
-- 0000 binary file
-- 0100 (ascii)text file
, case
when dbms_lob.substr( p_zipped_blob
, 1
, t_offs + 30 + blob2num( p_zipped_blob, 2, t_offs + 26 ) - 1
) in ( hextoraw( '2F' ) -- /
, hextoraw( '5C' ) -- \
)
then hextoraw( '10000000' ) -- a directory/folder
else hextoraw( '2000B681' ) -- a file
end -- External file attributes
, little_endian( t_offs - 1 ) -- Relative offset of local file header
, dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_offs + 26 )
, t_offs + 30
) -- File name
)
);
t_offs := t_offs + 30 + blob2num( p_zipped_blob, 4, t_offs + 18 ) -- compressed size
+ blob2num( p_zipped_blob, 2, t_offs + 26 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_offs + 28 ); -- Extra field length
end loop;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( c_END_OF_CENTRAL_DIRECTORY -- End of central directory signature
, hextoraw( '0000' ) -- Number of this disk
, hextoraw( '0000' ) -- Disk where central directory starts
, little_endian( t_cnt, 2 ) -- Number of central directory records on this disk
, little_endian( t_cnt, 2 ) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header ) -- Size of central directory
, little_endian( t_offs_dir_header ) -- Offset of start of central directory, relative to start of archive
, little_endian( nvl( utl_raw.length( t_comment ), 0 ), 2 ) -- ZIP file comment length
, t_comment
)
);
end;
--
procedure save_zip
( p_zipped_blob blob
, p_dir varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.zip'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir, p_filename, 'wb' );
for i in 0 .. trunc( ( dbms_lob.getlength( p_zipped_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh, dbms_lob.substr( p_zipped_blob, t_len, i * t_len + 1 ) );
end loop;
utl_file.fclose( t_fh );
end;
--
end;
/
CREATE OR REPLACE PACKAGE ir_to_xlsx
AUTHID current_user
IS
PROCEDURE download_excel(p_app_id IN NUMBER,
p_page_id IN NUMBER,
p_region_id IN NUMBER,
p_col_length IN VARCHAR2 DEFAULT NULL,
p_max_rows IN NUMBER,
p_autofilter IN CHAR DEFAULT 'Y',
p_export_links IN CHAR DEFAULT 'N',
p_custom_width IN VARCHAR2
);
FUNCTION convert_date_format(p_format IN VARCHAR2)
RETURN VARCHAR2;
FUNCTION convert_number_format(p_format IN VARCHAR2)
RETURN VARCHAR2;
FUNCTION convert_date_format(p_datatype IN VARCHAR2,p_format IN VARCHAR2)
RETURN VARCHAR2;
FUNCTION convert_date_format_js(p_datatype IN VARCHAR2, p_format IN VARCHAR2)
RETURN VARCHAR2;
FUNCTION get_max_rows (p_app_id IN NUMBER,
p_page_id IN NUMBER,
p_region_id IN NUMBER)
RETURN NUMBER;
FUNCTION get_highlight_in_cond_sql(p_condition_expression IN apex_application_page_ir_cond.condition_expression%TYPE,
p_condition_sql IN apex_application_page_ir_cond.condition_sql%TYPE,
p_condition_column_name IN apex_application_page_ir_cond.condition_column_name%TYPE)
RETURN VARCHAR2;
/*
-- format test cases
select ir_to_xlsx.convert_date_format('dd.mm.yyyy hh24:mi:ss'),to_char(sysdate,'dd.mm.yyyy hh24:mi:ss') from dual
union
select ir_to_xlsx.convert_date_format('dd.mm.yyyy hh12:mi:ss'),to_char(sysdate,'dd.mm.yyyy hh12:mi:ss') from dual
union
select ir_to_xlsx.convert_date_format('day-mon-yyyy'),to_char(sysdate,'day-mon-yyyy') from dual
union
select ir_to_xlsx.convert_date_format('month'),to_char(sysdate,'month') from dual
union
select ir_to_xlsx.convert_date_format('RR-MON-DD'),to_char(sysdate,'RR-MON-DD') from dual
union
select ir_to_xlsx.convert_number_format('FML999G999G999G999G990D0099'),to_char(123456789/451,'FML999G999G999G999G990D0099') from dual
union
select ir_to_xlsx.convert_date_format('DD-MON-YYYY HH:MIPM'),to_char(sysdate,'DD-MON-YYYY HH:MIPM') from dual
union
select ir_to_xlsx.convert_date_format('fmDay, fmDD fmMonth, YYYY'),to_char(sysdate,'fmDay, fmDD fmMonth, YYYY') from dual
*/
END;
/
create or replace PACKAGE BODY IR_TO_XLSX
as
-- general: displayed / not displayed columns - can be sow/hidden by user
-- hidden columns - column with type "hidden"
CURSOR cur_highlight(
p_report_id in APEX_APPLICATION_PAGE_IR_RPT.REPORT_ID%TYPE,
p_delimetered_column_list IN VARCHAR2
)
IS
SELECT rez.* ,
rownum cond_number,
'HIGHLIGHT_'||rownum cond_name
FROM (
SELECT report_id,
CASE
WHEN condition_operator in ('not in', 'in') THEN
ir_to_xlsx.get_highlight_in_cond_sql(condition_expression,condition_sql,condition_column_name)
ELSE
REPLACE(REPLACE(REPLACE(REPLACE(condition_sql,'#APXWS_EXPR#',''''||condition_expression||''''),'#APXWS_EXPR2#',''''||condition_expression2||''''),'#APXWS_HL_ID#','1'),'#APXWS_CC_EXPR#','"'||condition_column_name||'"')
END condition_sql,
condition_column_name,
condition_enabled,
highlight_row_color,
highlight_row_font_color,
highlight_cell_color,
highlight_cell_font_color
FROM apex_application_page_ir_cond
WHERE condition_type = 'Highlight'
AND report_id = p_report_id
AND instr(':'||p_delimetered_column_list||':',':'||condition_column_name||':') > 0
AND condition_enabled = 'Yes'
ORDER BY --rows highlights first
nvl2(highlight_row_color,1,0) DESC,
nvl2(highlight_row_font_color,1,0) DESC,
highlight_sequence
) rez;
SUBTYPE t_color IS VARCHAR2(7);
SUBTYPE t_style_string IS VARCHAR2(300);
SUBTYPE t_format_mask IS VARCHAR2(100);
SUBTYPE t_font IS VARCHAR2(50);
SUBTYPE t_large_varchar2 IS VARCHAR2(32767);
SUBTYPE largevarchar2 IS VARCHAR2(32767);
SUBTYPE columntype IS VARCHAR2(15);
SUBTYPE t_formatmask IS VARCHAR2(100);
TYPE t_date_table IS TABLE OF DATE INDEX BY BINARY_INTEGER;
TYPE t_number_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
TYPE t_styles IS TABLE OF BINARY_INTEGER INDEX BY t_style_string;
TYPE t_fill_list IS TABLE OF BINARY_INTEGER INDEX BY t_color;
TYPE t_font_list IS TABLE OF BINARY_INTEGER INDEX BY t_font;
TYPE t_format_mask_list IS TABLE OF BINARY_INTEGER INDEX BY t_format_mask;
TYPE t_col_names IS TABLE OF apex_application_page_ir_col.report_label%TYPE INDEX BY apex_application_page_ir_col.column_alias%TYPE;
TYPE t_col_format_mask IS TABLE OF apex_application_page_ir_comp.computation_format_mask%TYPE INDEX BY apex_application_page_ir_col.column_alias%TYPE;
TYPE t_header_alignment IS TABLE OF apex_application_page_ir_col.heading_alignment%TYPE INDEX BY apex_application_page_ir_col.column_alias%TYPE;
TYPE t_column_alignment IS TABLE OF apex_application_page_ir_col.column_alignment%TYPE INDEX BY apex_application_page_ir_col.column_alias%TYPE;
TYPE t_column_data_types IS TABLE OF apex_application_page_ir_col.column_type%TYPE INDEX BY BINARY_INTEGER;
TYPE t_highlight IS TABLE OF cur_highlight%rowtype INDEX BY BINARY_INTEGER;
TYPE t_column_links IS TABLE OF apex_application_page_ir_col.column_link%TYPE INDEX BY apex_application_page_ir_col.column_alias%TYPE;
TYPE t_all_columns IS TABLE OF BINARY_INTEGER INDEX BY VARCHAR2(32767);
TYPE ir_report IS RECORD
(
report apex_ir.t_report,
ir_data APEX_APPLICATION_PAGE_IR_RPT%ROWTYPE,
displayed_columns APEX_APPLICATION_GLOBAL.VC_ARR2,
break_on APEX_APPLICATION_GLOBAL.VC_ARR2,
break_really_on APEX_APPLICATION_GLOBAL.VC_ARR2, -- "break on" except hidden columns
sum_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
avg_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
max_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
min_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
median_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
count_columns_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
count_distnt_col_on_break APEX_APPLICATION_GLOBAL.VC_ARR2,
skipped_columns BINARY_INTEGER default 0, -- when scpecial columns like apxws_row_pk is used
start_with BINARY_INTEGER default 0, -- position of first displayed column in query
end_with BINARY_INTEGER default 0, -- position of last displayed column in query
agg_cols_cnt BINARY_INTEGER default 0,
sql_query_columns_cnt BINARY_INTEGER default 0, -- columns count in SQL query
column_header_labels t_col_names, -- column names in report header
col_format_mask t_col_format_mask, -- format like $3849,56
row_highlight t_highlight,
col_highlight t_highlight,
header_alignment t_header_alignment,
column_alignment t_column_alignment,
column_data_types t_column_data_types,
column_link t_column_links,
desc_tab DBMS_SQL.DESC_TAB2, -- information about columns from final sql query
all_columns t_all_columns -- list of all columns from final sql query
);
TYPE t_cell_data IS RECORD
(
VALUE VARCHAR2(100),
text largevarchar2,
datatype VARCHAR2(50)
);
-- EXCEPTIONS
format_error EXCEPTION;
PRAGMA EXCEPTION_INIT(format_error, -01830);
date_format_error EXCEPTION;
PRAGMA EXCEPTION_INIT(date_format_error, -01821);
conversion_error EXCEPTION;
PRAGMA EXCEPTION_INIT(conversion_error,-06502);
l_report ir_report;
-- these plSQL tables are used ONLY to identify that unique entity
-- was already used,not to keep the XML content.
g_styles t_styles;
g_fills t_fill_list;
g_fonts t_font_list;
g_number_formats t_format_mask_list;
-- the unique XML content is saved in these text variables
g_fonts_xml t_large_varchar2;
g_back_xml t_large_varchar2;
v_styles_xml CLOB;
v_format_mask_xml CLOB;
-- CONSTANT
STRING_HEIGHT CONSTANT NUMBER DEFAULT 14.4;
WIDTH_COEFFICIENT CONSTANT NUMBER DEFAULT 6;
LOG_PREFIX VARCHAR2(9) DEFAULT 'GPV_XLSX:';
BACK_COLOR CONSTANT t_color DEFAULT '#C6E0B4';
-- Excel-template should have some pre-defined
-- fonts, format masks, fills, styles etc. to format column headers.
-- They are hardcoded in constants below (BOLD_FONT,UNDERLINE_FONT etc.).
-- One should keep this entities by adding new one.
-- Constatns below define an amount of this hardcoded entities
FONTS_CNT CONSTANT BINARY_INTEGER DEFAULT 3;
FORMAT_MASK_CNT CONSTANT BINARY_INTEGER DEFAULT 1;
DEFAULT_FILL_CNT CONSTANT BINARY_INTEGER DEFAULT 2;
DEFAULT_STYLES_CNT CONSTANT BINARY_INTEGER DEFAULT 1;
FORMAT_MASK_START_WITH CONSTANT BINARY_INTEGER DEFAULT 164;
------------------------------------------------------------------------------
t_sheet_rels CLOB DEFAULT '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>
</Relationships>';
t_workbook CLOB DEFAULT '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4506"/>
<workbookPr filterPrivacy="1" defaultThemeVersion="124226"/>
<bookViews>
<workbookView xWindow="120" yWindow="120" windowWidth="24780" windowHeight="12150"/>
</bookViews>
<sheets>
<sheet name="Sheet1" sheetId="1" r:id="rId1"/>
</sheets>
<definedNames><definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">Sheet1!$A$1:$H$1</definedName></definedNames>
<calcPr calcId="125725"/>
<fileRecoveryPr repairLoad="1"/>
</workbook>';
t_style_template CLOB DEFAULT '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
#FORMAT_MASKS#
#FONTS#
#FILLS#
<borders count="2">
<border>
<left/>
<right/>
<top/>
<bottom/>
<diagonal/>
</border>
<border>
<left/>
<right/>
<top style="thin">
<color indexed="64"/>
</top>
<bottom/>
<diagonal/>
</border>
</borders>
<cellStyleXfs count="2">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />
<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyNumberFormat="0" applyFill="0" applyBorder="0" applyAlignment="0" applyProtection="0" />
</cellStyleXfs>
#STYLES#
<cellStyles count="2">
<cellStyle name="Link" xfId="1" builtinId="8" />
<cellStyle name="Standard" xfId="0" builtinId="0" />
</cellStyles>
<dxfs count="0"/>
<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>
<extLst>
<ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">
<x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>
</ext>
</extLst>
</styleSheet>';
DEFAULT_FONT CONSTANT VARCHAR2(200) DEFAULT '
<font>
<sz val="11" />
<color theme="1" />
<name val="Calibri" />
<family val="2" />
<scheme val="minor" />
</font>';
BOLD_FONT CONSTANT VARCHAR2(200) DEFAULT '
<font>
<b />
<sz val="11" />
<color theme="1" />
<name val="Calibri" />
<family val="2" />
<scheme val="minor" />
</font>';
UNDERLINE_FONT CONSTANT VARCHAR2(200) DEFAULT '
<font>
<u />
<sz val="11" />
<color theme="10" />
<name val="Calibri" />
<family val="2" />
<scheme val="minor" />
</font>';
DEFAULT_FILL CONSTANT VARCHAR2(200) DEFAULT '
<fill>
<patternFill patternType="none" />
</fill>
<fill>
<patternFill patternType="gray125" />
</fill>';
DEFAULT_STYLE CONSTANT VARCHAR2(200) DEFAULT '
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>';
AGGREGATE_STYLE CONSTANT VARCHAR2(250) DEFAULT '
<xf numFmtId="#FMTID#" borderId="1" fillId="0" fontId="1" xfId="0" applyAlignment="1" applyFont="1" applyBorder="1">
<alignment wrapText="1" horizontal="right" vertical="top"/>
</xf>';
HEADER_STYLE CONSTANT VARCHAR2(200) DEFAULT '
<xf numFmtId="0" borderId="0" #FILL# fontId="1" xfId="0" applyAlignment="1" applyFont="1" >
<alignment wrapText="0" horizontal="#ALIGN#"/>
</xf>';
FORMAT_MASK CONSTANT VARCHAR2(100) DEFAULT '
<numFmt numFmtId="'||FORMAT_MASK_START_WITH||'" formatCode="dd.mm.yyyy"/>';
------------------------------------------------------------------------------
/**
* http://mk-commi.blogspot.co.at/2014/11/concatenating-varchar2-values-into-clob.html
* Procedure concatenates a VARCHAR2 to a CLOB.
* It uses another VARCHAR2 as a buffer until it reaches 32767 characters.
* Then it flushes the current buffer to the CLOB and resets the buffer using
* the actual VARCHAR2 to add.
* Your final call needs to be done setting p_eof to TRUE in order to
* flush everything to the CLOB.
*
* @param p_clob The CLOB buffer.
* @param p_vc_buffer The intermediate VARCHAR2 buffer. (must be VARCHAR2(32767))
* @param p_vc_addition The VARCHAR2 value you want to append.
* @param p_eof Indicates if complete buffer should be flushed to CLOB.
*/
PROCEDURE add(
p_clob IN OUT NOCOPY CLOB
,p_vc_buffer IN OUT NOCOPY VARCHAR2
,p_vc_addition IN VARCHAR2
,p_eof IN BOOLEAN DEFAULT false
) AS
BEGIN
-- Standard Flow
IF nvl(lengthb(p_vc_buffer), 0) + nvl(lengthb(p_vc_addition), 0) < (32767/2) THEN
p_vc_buffer := p_vc_buffer || convert(p_vc_addition,'utf8');
ELSE
IF p_clob IS NULL THEN
dbms_lob.createtemporary(p_clob, true);
END IF;
dbms_lob.writeappend(p_clob, length(p_vc_buffer), p_vc_buffer);
p_vc_buffer := convert(p_vc_addition,'utf8');
END IF;
-- Full Flush requested
IF p_eof THEN
IF p_clob IS NULL THEN
p_clob := p_vc_buffer;
ELSE
dbms_lob.writeappend(p_clob, length(p_vc_buffer), p_vc_buffer);
END IF;
p_vc_buffer := NULL;
END IF;
END add;
------------------------------------------------------------------------------
PROCEDURE log(p_message IN VARCHAR2)
IS
BEGIN
apex_debug_message.log_message(p_message => log_prefix||substr(p_message,1,32758),
p_enabled => false,
p_level => 4);
END log;
------------------------------------------------------------------------------
-- convert date format string from ORACLE-style to MS Excel style
FUNCTION convert_date_format(p_format IN VARCHAR2)
RETURN VARCHAR2
IS
v_str VARCHAR2(100);
BEGIN
v_str := apex_plugin_util.replace_substitutions(p_format);
v_str := upper(v_str);
--date
v_str := replace(v_str,'DAY','DDDD');
v_str := replace(v_str,'MONTH','MMMM');
v_str := replace(v_str,'MON','MMM');
v_str := replace(v_str,'R','Y');
v_str := replace(v_str,'FM','');
v_str := replace(v_str,'PM',' AM/PM');
--time
v_str := replace(v_str,'MI','mm');
v_str := replace(v_str,'SS','ss');
v_str := replace(v_str,'HH24','hh');
v_str := regexp_replace(v_str,'HH12([^ ]+)','h\1 AM/PM');
v_str := replace(v_str,'AM\/PM',' AM/PM');
RETURN v_str;
END convert_date_format;
------------------------------------------------------------------------------
-- convert number format string from ORACLE-style to MS Excel style
FUNCTION convert_number_format(p_format IN VARCHAR2)
RETURN VARCHAR2
IS
v_str VARCHAR2(100);
BEGIN
v_str := apex_plugin_util.replace_substitutions(p_format);
v_str := upper(v_str);
--number
v_str := replace(v_str,'9','#');
v_str := replace(v_str,'D','.');
v_str := replace(v_str,'G',',');
v_str := replace(v_str,'FM','');
-- plus/minus sign
IF instr(v_str,'S') > 0 THEN
v_str := replace(v_str,'S','');
v_str := '+'||v_str||';-'||v_str;
END IF;
--currency
v_str := replace(v_str,'L',convert('"'||rtrim(TO_CHAR(0,'FML0'),'0')||'"','UTF8'));
v_str := regexp_substr(v_str,'.G?[^G]+$');
RETURN v_str;
END convert_number_format;
------------------------------------------------------------------------------
-- get current date/time format used in DB session for given date or time type
-- this format is used if date/time format in APEX is not defined
FUNCTION get_current_format(p_data_type IN BINARY_INTEGER)
RETURN VARCHAR2
IS
v_format t_formatmask;
v_parameter VARCHAR2(50);
BEGIN
IF p_data_type IN (dbms_types.typecode_timestamp_tz,181) THEN
v_parameter := 'NLS_TIMESTAMP_TZ_FORMAT';
ELSIF p_data_type IN (dbms_types.typecode_timestamp_ltz,231) THEN
v_parameter := 'NLS_TIMESTAMP_TZ_FORMAT';
ELSIF p_data_type IN (dbms_types.typecode_timestamp,180) THEN
v_parameter := 'NLS_TIMESTAMP_FORMAT';
ELSIF p_data_type = dbms_types.typecode_date THEN
v_parameter := 'NLS_DATE_FORMAT';
ELSE
RETURN 'dd.mm.yyyy';
END IF;
SELECT value
INTO v_format
FROM v$nls_parameters
WHERE parameter = v_parameter;
RETURN v_format;
END get_current_format;
------------------------------------------------------------------------------
-- convert date format from ORACLE-style to MS Excel Style
-- for given date/time datatype
FUNCTION convert_date_format(
p_datatype IN VARCHAR2,
p_format IN VARCHAR2
)
RETURN VARCHAR2
IS
v_str VARCHAR2(100);
v_24h_format BOOLEAN DEFAULT true;
BEGIN
IF p_format IS NULL THEN
IF p_datatype = 'TIMESTAMP_TZ' THEN
v_str := get_current_format(dbms_types.typecode_timestamp_tz);
ELSIF p_datatype = 'TIMESTAMP_LTZ' THEN
v_str := get_current_format(dbms_types.typecode_timestamp_ltz);
ELSIF p_datatype = 'TIMESTAMP' THEN
v_str := get_current_format(dbms_types.typecode_timestamp);
ELSIF p_datatype = 'DATE' THEN
v_str := get_current_format(dbms_types.typecode_date);
ELSE
v_str := get_current_format('');
END IF;
ELSE
v_str := p_format;