forked from OraOpenSource/oos-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oos_utils_install.sql
3253 lines (2943 loc) · 153 KB
/
oos_utils_install.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
-- DO NOT MODIFY THIS FILE. IT IS AUTO GENERATED
prompt *** OOS_UTILS ***
prompt *** Prereqs OOS_UTILS ***
-- This script will ensure that current user has the appropriate privileges
whenever sqlerror exit
set serveroutput on
-- SESSION PRIVILEGES (#34)
declare
type t_sess_privs is table of pls_integer index by varchar2(50);
l_sess_privs t_sess_privs;
l_req_privs t_sess_privs;
l_priv varchar2(50);
l_dummy pls_integer;
l_priv_error boolean := false;
begin
l_req_privs('CREATE SESSION') := 1;
l_req_privs('CREATE TABLE') := 1;
l_req_privs('CREATE PROCEDURE') := 1;
for c1 in (select privilege from session_privs) loop
l_sess_privs(c1.privilege) := 1;
end loop; --c1
dbms_output.put_line('_____________________________________________________________________________');
l_priv := l_req_privs.first;
loop
exit when l_priv is null;
begin
l_dummy := l_sess_privs(l_priv);
exception when no_data_found then
dbms_output.put_line('Error, the current schema is missing the following privilege: '||l_priv);
l_priv_error := true;
end;
l_priv := l_req_privs.next(l_priv);
end loop;
if not l_priv_error then
dbms_output.put_line('User has all required privileges, installation will continue.');
end if;
dbms_output.put_line('_____________________________________________________________________________');
if l_priv_error then
raise_application_error (-20000, 'One or more required privileges are missing.');
end if;
-- Check that user is NOT oos_util (#35)
if upper(user) = 'OOS_UTIL' then
raise_application_error(-20001, 'Can not install in user OOS_UTIL due to naming conflicts. Chose another user');
end if;
end;
/
whenever sqlerror continue
prompt *** Installing OOS_UTILS ***
prompt *** TABLES ***
prompt oos_util_values
declare
l_count pls_integer;
l_table_name user_tables.table_name%type;
l_sql varchar2(4000);
begin
l_table_name := lower('oos_util_values');
select count(1)
into l_count
from user_tables
where 1=1
and table_name = upper(l_table_name);
if l_count = 0 then
l_sql := 'create table %table_name% (
cat varchar2(255) not null,
name varchar2(255) not null,
value varchar2(255) not null,
constraint %table_name%_uk1 unique (cat,name)
)';
l_sql := replace(l_sql, '%table_name%', l_table_name);
end if;
if l_sql is not null then
execute immediate l_sql;
end if;
end;
/
prompt *** PACKAGES ***
prompt oos_util
create or replace package oos_util
as
-- CONSTANTS
/**
* @constant gc_date_format default date format
* @constant gc_timestamp_format default timestamp format
* @constant gc_timestamp_tz_format default timestamp (with TZ) format
*/
gc_date_format constant varchar2(255) := 'DD-MON-YYYY HH24:MI:SS';
gc_timestamp_format constant varchar2(255) := gc_date_format || ':FF';
gc_timestamp_tz_format constant varchar2(255) := gc_timestamp_format || ' TZR';
-- OOS Util Val Cats
gc_vals_cat_mime_type constant oos_util_values.cat%type := 'mime-type';
gc_version constant varchar2(10) := '1.0.0';
procedure assert(
p_condition in boolean,
p_msg in varchar2);
procedure sleep(
p_seconds in simple_integer);
end oos_util;
/
create or replace package body oos_util
as
-- CONSTANTS
gc_assert_error_number constant pls_integer := -20000;
-- ******** PRIVATE ********
/**
* Internal logging procedure.
* Requires Logger to be installed only while developing.
* -- TODO mdsouza: conditional compilation notes
*
*
* @author Martin D'Souza
* @created 17-Aug-2015
* @param p_message Item to log
* @param p_scope Logger scope
*/
procedure log(
p_text in varchar2,
p_scope in varchar2)
as
begin
$if $$oos_util_debug $then
logger.log(p_text, p_scope);
$else
null;
$end
end log;
-- ******** PUBLIC ********
/**
* Validates assertion.
* Will raise an application error if assertion is false
*
* @example
*
* oos_util.assert(1=2, 'this assertion did not pass');
*
* -- Results in
*
* Error starting at line : 1 in command -
* exec oos_util.assert(1=2, 'this assertion did not pass')
* Error report -
* ORA-06550: line 1, column 7:
* PLS-00306: wrong number or types of arguments in call to 'ASSERT'
* ORA-06550: line 1, column 7:
* PL/SQL: Statement ignored
* 06550. 00000 - "line %s, column %s:\n%s"
* *Cause: Usually a PL/SQL compilation error.
* *Action:
* @issue #19
*
* @author Martin D'Souza
* @created 05-Sep-2015
* @param p_condition Boolean condition to validate
* @param p_msg Message to include in application error if p_condition fails
*/
procedure assert(
p_condition in boolean,
p_msg in varchar2)
as
begin
if not p_condition or p_condition is null then
raise_application_error(gc_assert_error_number, p_msg);
end if;
end assert;
/**
* Sleep procedure for n seconds
*
* Notes:
* - It is recommended that you use Oracle's lock procedures: http://psoug.org/reference/sleep.html
* - In instances where you do not have access use this sleep method instead
* - This implementation may tie up CPU so only use for development purposes
* - This is a custom implementation of sleep and as a result the times are not 100% accurate
* - If calling in SQLDeveloper may get "IO Error: Socket read timed out". This is a JDBC driver setting, not a bug in this code.
*
* @issue #13
*
* @author Martin Giffy D'Souza
* @created 31-Dec-2015
* @param p_seconds Number of seconds to sleep for
*/
procedure sleep(
p_seconds in simple_integer)
as
l_now timestamp := systimestamp;
l_end_time timestamp;
begin
l_end_time := l_now + numtodsinterval (p_seconds, 'second');
-- Note: Can't use systimestamp in loop since it doesn't seem to calculate a new timestamp each iteration.
while(l_end_time > l_now) loop
l_now := systimestamp;
end loop;
end sleep;
end oos_util;
/
prompt oos_util_apex
create or replace package oos_util_apex
as
function is_developer
return boolean;
function is_developer_yn
return varchar2;
function is_session_valid(
p_session_id in apex_workspace_sessions.apex_session_id%type)
return boolean;
function is_session_valid_yn(
p_session_id in apex_workspace_sessions.apex_session_id%type)
return varchar2;
procedure create_session(
p_app_id in apex_applications.application_id%type,
p_user_name in apex_workspace_sessions.user_name%type,
p_page_id in apex_application_pages.page_id%type default null,
p_session_id in apex_workspace_sessions.apex_session_id%type default null);
procedure join_session(
p_session_id in apex_workspace_sessions.apex_session_id%type,
p_app_id in apex_applications.application_id%type default null);
procedure trim_page_items(
p_page_id in apex_application_pages.page_id%type default apex_application.g_flow_step_id);
function is_page_item_rendered(
p_item_name in apex_application_page_items.item_name%type)
return boolean;
end oos_util_apex;
/
create or replace package body oos_util_apex
as
/**
* Returns true/false if APEX developer is enable
* Supports both APEX 4 and 5
*
* Can be used in APEX to declaratively determine if in development mode
*
* @issue 25
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @return true/false
*/
function is_developer
return boolean
as
begin
if coalesce(apex_application.g_edit_cookie_session_id, v('APP_BUILDER_SESSION')) is null then
return false;
else
return true;
end if;
end is_developer;
/**
* Returns Y/N if APEX developer is enable
* See `is_developer` for details
*
* @issue #25
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @return Y or N
*/
function is_developer_yn
return varchar2
as
$if dbms_db_version.version >= 12 $then
pragma udf;
$end
begin
if is_developer then
return 'Y';
else
return 'N';
end if;
end is_developer_yn;
/**
* Checks if APEX session is still active/valid
*
* @issue #9
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @param p_session_id APEX session ID
* @return true/false
*/
function is_session_valid(
p_session_id in apex_workspace_sessions.apex_session_id%type)
return boolean
as
l_count pls_integer;
begin
oos_util.assert(p_session_id is not null, 'p_session_id must contain value');
select count(1)
into l_count
from apex_workspace_sessions aws
where 1=1
and aws.apex_session_id = p_session_id
and sysdate <= aws.session_idle_timeout_on
and sysdate <= aws.session_life_timeout_on;
if l_count = 0 then
return false;
else
return true;
end if;
end is_session_valid;
/**
* Checks if session is still active
*
* @issue 9
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @param p_session_id APEX session ID
* @return Y/N
*/
function is_session_valid_yn(
p_session_id in apex_workspace_sessions.apex_session_id%type)
return varchar2
as
$if dbms_db_version.version >= 12 $then
pragma udf;
$end
begin
if is_session_valid(p_session_id => p_session_id) then
return 'Y';
else
return 'N';
end if;
end is_session_valid_yn;
/**
* Creates a new APEX session.
* Useful when testing APEX functionality in PL/SQL or using apex_mail etc
*
* Can only create one per Oracle session. To connect to a different APEX session, reconnect the Oracle session
*
*
* Notes:
* - Content taken from:
* - http://www.talkapex.com/2012/08/how-to-create-apex-session-in-plsql.html
* - http://apextips.blogspot.com.au/2014/10/debugging-parameterised-views-outside.html
*
*
* @issue #7
* @issue #49 ensure page and user exist
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @param p_app_id
* @param p_user_name
* @param p_page_id Page to try and register for post login. Recommended to leave null
* @param p_session_id Session to re-join. Recommended leave null
*/
procedure create_session(
p_app_id in apex_applications.application_id%type,
p_user_name in apex_workspace_sessions.user_name%type,
p_page_id in apex_application_pages.page_id%type default null,
p_session_id in apex_workspace_sessions.apex_session_id%type default null)
as
l_workspace_id apex_applications.workspace_id%TYPE;
l_cgivar_name sys.owa.vc_arr;
l_cgivar_val sys.owa.vc_arr;
l_page_id apex_application_pages.page_id%type := p_page_id;
l_home_link apex_applications.home_link%type;
l_url_arr apex_application_global.vc_arr2;
l_count pls_integer;
begin
sys.htp.init;
l_cgivar_name(1) := 'REQUEST_PROTOCOL';
l_cgivar_val(1) := 'HTTP';
sys.owa.init_cgi_env(
num_params => 1,
param_name => l_cgivar_name,
param_val => l_cgivar_val );
select workspace_id
into l_workspace_id
from apex_applications
where application_id = p_app_id;
wwv_flow_api.set_security_group_id(l_workspace_id);
if l_page_id is null then
-- Try to get the page_id from home link
select aa.home_link
into l_home_link
from apex_applications aa
where 1=1
and aa.application_id = p_app_id;
if l_home_link is not null then
l_url_arr := apex_util.string_to_table(l_home_link, ':');
if l_url_arr.count >= 2 then
l_page_id := l_url_arr(2);
end if;
end if;
if l_page_id is null then
l_page_id := 1;
end if;
end if; -- l_page_id is null
-- #49 Ensure that page exists
select count(1)
into l_count
from apex_application_pages aap
where 1=1
and aap.application_id = p_app_id
and aap.page_id = l_page_id
and l_page_id is not null;
oos_util.assert(l_count = 1, 'Page must exist in the application');
apex_application.g_instance := 1;
apex_application.g_flow_id := p_app_id;
apex_application.g_flow_step_id := l_page_id;
apex_custom_auth.post_login(
p_uname => p_user_name,
p_session_id => null, -- could use APEX_CUSTOM_AUTH.GET_NEXT_SESSION_ID
p_app_page => apex_application.g_flow_id || ':' || l_page_id);
-- Rejoin session
if p_session_id is not null then
-- This will only set the session but doesn't register the items
-- apex_custom_auth.set_session_id(p_session_id => p_session_id);
-- #42 Seems a second login is required to fully join session
apex_custom_auth.post_login(
p_uname => p_user_name,
p_session_id => p_session_id);
end if;
end create_session;
/**
* Join an existing APEX session
*
* Notes:
* - `v('P1_X')` won't work. Use `apex_util.get_session_state('P1_X')` instead
*
* @issue #7
*
* @author Martin Giffy D'Souza
* @created 29-Dec-2015
* @param p_session_id The session you want to join. Must be an existing active session.
* @param p_app_id Use if multiple applications are linked to the same session. If null, last used application will be used.
*/
procedure join_session(
p_session_id in apex_workspace_sessions.apex_session_id%type,
p_app_id in apex_applications.application_id%type default null)
as
l_app_id apex_applications.application_id%type := p_app_id;
l_user_name apex_workspace_sessions.user_name%type;
begin
oos_util.assert(p_session_id is not null, 'p_session_id is required');
if l_app_id is null then
select max(application_id)
into l_app_id
from (
select application_id, row_number() over (order by view_date desc) rn
from apex_workspace_activity_log
where apex_session_id = p_session_id)
where rn = 1;
end if;
oos_util.assert(l_app_id is not null, 'Can not find matching app_id for session: ' || p_session_id);
select user_name
into l_user_name
from apex_workspace_sessions
where apex_session_id = p_session_id;
create_session(
p_app_id => l_app_id,
p_user_name => l_user_name,
p_session_id => p_session_id);
end join_session;
/**
* Trims whitespace APEX page items (before and after).
* Useful when submitting a page to trim all items.
*
* Notes:
* - Suggested to run submit page process application wide
* - Excludes inputs that users shouldn't modify and password fields
* - Ex: select list, hidden values, files
*
* @issue 24
*
* @author Martin Giffy D'Souza
* @created 31-Dec-2015
* @param p_page_id Items on this page will be trimmed.
*/
procedure trim_page_items(
p_page_id in apex_application_pages.page_id%type default apex_application.g_flow_step_id)
as
begin
oos_util.assert(p_page_id is not null, 'p_page_id is required');
for x in (
select item_name, item_value_trim
from (
select
x.item_name,
x.item_value,
regexp_replace(x.item_value, '(^[[:space:]]*|[[:space:]]*$)') item_value_trim
from (
select pi.item_name, v(pi.item_name) item_value
from apex_application_page_items pi
where 1=1
and pi.page_id = p_page_id
and pi.display_as_code not in (
'NATIVE_HIDDEN', 'NATIVE_CHECKBOX',
'NATIVE_RADIOGROUP', 'NATIVE_DISPLAY_ONLY',
'NATIVE_PASSWORD', 'NATIVE_SELECT_LIST',
'NATIVE_SHUTTLE', 'NATIVE_FILE')
) x
) x
where 1=1
and x.item_value is not null
and (1=2
or x.item_value != x.item_value_trim
or x.item_value_trim is null) -- If item value is just white spaces then item_value_trim will be null
) loop
apex_util.set_session_state(
p_name => x.item_name,
p_value => x.item_value_trim
-- FUTURE mdsouza: make this an apex 5 compilation Optional
-- ,p_commit => false
);
end loop;
end trim_page_items;
/**
* Returns true/false if page item was rendered
*
* Notes:
* - This should only run on a page submit process otherwise it won't work. An error is raised otherwise
*
* @issue #39
*
* @author Daniel Hochleitner
* @created 06-Mar-2016
* @return true/false
*/
function is_page_item_rendered(
p_item_name in apex_application_page_items.item_name%type)
return boolean
as
l_item_id apex_application_page_items.item_id%type;
l_return boolean := false;
begin
-- Ensure that this is only done on page submit (otherwise it doesn't make sense)
oos_util.assert(
sys.owa_util.get_cgi_env('PATH_INFO') = '/wwv_flow.accept',
lower($$plsql_unit) || '.is_page_item_rendered can only be run on a page submit process');
select item_id
into l_item_id
from apex_application_page_items
where 1=1
and application_id = apex_application.g_flow_id
and page_id = apex_application.g_flow_step_id
and item_name = upper(p_item_name);
-- If a page item is rendered the internal id is stored in a hidden field
-- called p_arg_names. During submit the values are stored into the
-- g_arg_names array by the WWV_Flow.accept procedure.
-- By checking for existence of the page item id in the array, we are able
-- to determine if APEX has rendered the item as "Saves state".
-- Note: A item which is normally enterable but which is rendered
-- "Read Only" is also considered rendered, because it still saves state
if apex_application.g_arg_names.count > 0 then
for i in 1 .. apex_application.g_arg_names.count loop
if apex_application.g_arg_names(i) = l_item_id then
l_return := true;
exit;
end if;
end loop;
end if;
return l_return;
end is_page_item_rendered;
end oos_util_apex;
/
prompt oos_util_bit
create or replace package oos_util_bit
as
function bitand(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer;
function bitor(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer;
function bitxor(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer;
function bitnot(
p_x in binary_integer)
return binary_integer;
end;
/
create or replace package body oos_util_bit
as
/**
* [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND)
*
* The function signature is similar to [`bitand`](https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions021.htm#SQLRF00612)
*
* The arguments must be in the range -(2^(32-1)) .. ((2^(32-1))-1). If an
* argument is out of this range, the result is undefined.
*
* @example
*
* select oos_util_bit.bitand(1,3)
* from dual;
*
* OOS_UTIL_BIT.BITAND(1,3)
* ------------------------
* 1
*
* @issue #69
*
* @author Jani Hur <[email protected]>
* @created 11-Apr-2016
* @param p_x binary_integer
* @param p_y binary_integer
* @return binary_integer
*/
function bitand(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer
as
begin
return sys.standard.bitand(p_x, p_y);
end bitand;
/**
* [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR)
*
* Copied from [http://www.orafaq.com/wiki/Bit](http://www.orafaq.com/wiki/Bit)
*
* The function signature is similar to [`bitand`](https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions021.htm#SQLRF00612)
*
* The arguments must be in the range -(2^(32-1)) .. ((2^(32-1))-1). If an
* argument is out of this range, the result is undefined.
*
* @example
*
* select oos_util_bit.bitor(1,3)
* from dual;
*
* OOS_UTIL_BIT.BITOR(1,3)
* -----------------------
* 3
*
* @issue #44
*
* @author Jani Hur <[email protected]>
* @created 06-Apr-2016
* @param p_x binary_integer
* @param p_y binary_integer
* @return binary_integer
*/
function bitor(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer
as
begin
return p_x + p_y - bitand(p_x, p_y);
end bitor;
/**
* [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)
*
* Copied from [http://www.orafaq.com/wiki/Bit](http://www.orafaq.com/wiki/Bit)
*
* The function signature is similar to [`bitand`](https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions021.htm#SQLRF00612)
*
* The arguments must be in the range -(2^(32-1)) .. ((2^(32-1))-1). If an
* argument is out of this range, the result is undefined.
*
* @example
*
* select oos_util_bit.bitxor(1,3)
* from dual;
*
* OOS_UTIL_BIT.BITXOR(1,3)
* ------------------------
* 2
*
* @issue #44
*
* @author Jani Hur <[email protected]>
* @created 06-Apr-2016
* @param p_x binary_integer
* @param p_y binary_integer
* @return binary_integer
*/
function bitxor(
p_x in binary_integer,
p_y in binary_integer)
return binary_integer
as
begin
return bitor(p_x, p_y) - bitand(p_x, p_y);
end bitxor;
/**
* [bitwise NOT](https://en.wikipedia.org/wiki/Bitwise_operation#NOT)
*
* Copied from [http://www.orafaq.com/wiki/Bit](http://www.orafaq.com/wiki/Bit)
*
* The function signature is similar to [`bitand`](https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions021.htm#SQLRF00612)
*
* The arguments must be in the range -(2^(32-1)) .. ((2^(32-1))-1). If an
* argument is out of this range, the result is undefined.
*
* @example
*
* select oos_util_bit.bitnot(7)
* from dual;
*
* OOS_UTIL_BIT.BITNOT(7)
* ----------------------
* -8
*
* @issue #44
*
* @author Jani Hur <[email protected]>
* @created 06-Apr-2016
* @param p_x binary_integer
* @return binary_integer
*/
function bitnot(
p_x in binary_integer)
return binary_integer
as
begin
return (0 - p_x) - 1;
end bitnot;
end;
/
prompt oos_util_date
create or replace package oos_util_date
as
function date2epoch(
p_date in date)
return number;
function epoch2date(
p_epoch in number)
return date;
end oos_util_date;
/
create or replace package body oos_util_date
as
/**
* Coverts date to Unix Epoch time
*
* @issue #18
*
* @author Martin Giffy D'Souza
* @created 30-Dec-2015
* @param p_date Date to convert to Epoch format
* @return Unix Epoch time
*/
function date2epoch(
p_date in date)
return number
as
$if dbms_db_version.version >= 12 $then
pragma udf;
$end
begin
return
round(
(p_date - to_date ('19700101', 'yyyymmdd')) * 86400
- (to_number(substr (tz_offset (sessiontimezone), 1, 3))+1) * 3600);
end date2epoch;
/**
* Converts Unix linux time to Oracle date
*
* @issue 18
*
* @author Martin Giffy D'Souza
* @created 31-Dec-2015
* @param p_epoch Epoch Unix date (number)
* @return date
*/
function epoch2date(
p_epoch in number)
return date
as
begin
return
to_date ('19700101', 'yyyymmdd')
+ ((p_epoch + ((to_number(substr(tz_offset(sessiontimezone), 1, 3))+1) * 3600)) / 86400);
end epoch2date;
end oos_util_date;
/
prompt oos_util_lob
create or replace package oos_util_lob
as
-- CONSTANTS
/**
* gc_unit_b B
* gc_unit_kb KB
* gc_unit_mb MB
* gc_unit_gb GB
* gc_unit_tb TB
* gc_unit_pb PB
* gc_unit_eb EB
* gc_unit_zb ZB
* gc_unit_yb YB
*/
gc_unit_b constant varchar2(1) := 'B';
gc_unit_kb constant varchar2(2) := 'KB';
gc_unit_mb constant varchar2(2) := 'MB';
gc_unit_gb constant varchar2(2) := 'GB';
gc_unit_tb constant varchar2(2) := 'TB';
gc_unit_pb constant varchar2(2) := 'PB';
gc_unit_eb constant varchar2(2) := 'EB';
gc_unit_zb constant varchar2(2) := 'ZB';
gc_unit_yb constant varchar2(2) := 'YB';
--
gc_size_b constant number := 1024;
gc_size_kb constant number := power(1024, 2);
gc_size_mb constant number := power(1024, 3);
gc_size_gb constant number := power(1024, 4);
gc_size_tb constant number := power(1024, 5);
gc_size_pb constant number := power(1024, 6);
gc_size_eb constant number := power(1024, 7);
gc_size_zb constant number := power(1024, 8);
gc_size_yb constant number := power(1024, 9);
-- METHODS
function clob2blob(
p_clob in clob)
return blob;
function blob2clob(
p_blob in blob)
return clob;
function get_file_size(
p_file_size in number,
p_units in varchar2 default null)
return varchar2;
function get_lob_size(
p_lob in clob,
p_units in varchar2 default null)
return varchar2;
function get_lob_size(
p_lob in blob,
p_units in varchar2 default null)
return varchar2;
function replace_clob(
p_str in clob,
p_search in varchar2,
p_replace in clob)
return clob;
procedure write_to_file(
p_text in clob,
p_path in varchar2,
p_filename in varchar2);
function read_from_file(
p_path in varchar2,
p_filename in varchar2)
return clob;
end oos_util_lob;
/
create or replace package body oos_util_lob
as
/**
* Convers clob to blob
*
* @issue #12
*