-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKG_CONVERT_(BODY).SQL
102 lines (94 loc) · 2.79 KB
/
PKG_CONVERT_(BODY).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
CREATE OR REPLACE PACKAGE BODY PKG_CONVERT AS
FUNCTION blob2clob (p_blob IN BLOB) RETURN CLOB
AS
n_inputlength NUMBER;
n_chunklength NUMBER := 4000;
c_clobinput CLOB;
v_chunkcontent VARCHAR2(4000);
BEGIN
n_inputlength := dbms_lob.getlength(p_blob);
IF n_inputlength > 0 THEN
DBMS_LOB.CREATETEMPORARY(c_clobinput, TRUE);
ELSE
RETURN NULL;
END IF;
FOR i IN 1..CEIL(n_inputlength/n_chunklength) LOOP
v_chunkcontent := utl_raw.cast_to_varchar2(dbms_lob.SUBSTR(p_blob,n_chunklength,(i-1)*n_chunklength + 1));
DBMS_LOB.WRITEAPPEND(c_clobinput, LENGTH(v_chunkcontent), v_chunkcontent);
END LOOP;
RETURN c_clobinput;
END blob2clob;
FUNCTION clob2blob (p_clob IN CLOB) RETURN BLOB
IS
b_blob BLOB;
n_offset NUMBER DEFAULT 1;
n_amount NUMBER DEFAULT 4096;
n_offsetwrite NUMBER DEFAULT 1;
n_amountwrite NUMBER;
v_buffer VARCHAR2(4096 CHAR);
BEGIN
dbms_lob.createtemporary(b_blob, TRUE);
BEGIN
LOOP
dbms_lob.READ (lob_loc => p_clob,
amount => n_amount,
offset => n_offset,
buffer => v_buffer);
n_amountwrite := utl_raw.length (r => utl_raw.cast_to_raw(c => v_buffer));
dbms_lob.WRITE (lob_loc => b_blob,
amount => n_amountwrite,
offset => n_offsetwrite,
buffer => utl_raw.cast_to_raw(v_buffer));
n_offsetwrite := n_offsetwrite + n_amountwrite;
n_offset := n_offset + n_amount;
n_amount := 4096;
END LOOP;
EXCEPTION WHEN no_data_found THEN
NULL;
END;
RETURN b_blob;
END clob2blob;
FUNCTION clob2blob_cs(p_clob IN CLOB,
p_charset_id IN INTEGER DEFAULT DBMS_LOB.DEFAULT_CSID) RETURN BLOB
AS
b_blob BLOB;
n_dest_offset INTEGER := 1;
n_src_offset INTEGER := 1;
n_lang_context INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
n_warning INTEGER;
BEGIN
DBMS_LOB.CREATETEMPORARY(lob_loc => b_blob,
cache => TRUE);
DBMS_LOB.CONVERTTOBLOB(dest_lob => b_blob,
src_clob => p_clob,
amount => LENGTH(p_clob),
dest_offset => n_dest_offset,
src_offset => n_src_offset,
blob_csid => p_charset_id,
lang_context => n_lang_context,
warning => n_warning);
RETURN b_blob;
END clob2blob_cs;
FUNCTION blob2clob_cs(p_blob IN BLOB,
p_charset_id IN INTEGER DEFAULT DBMS_LOB.DEFAULT_CSID) RETURN CLOB
IS
c_clob CLOB;
n_file_size INTEGER := DBMS_LOB.LOBMAXSIZE;
n_dest_offset INTEGER := 1;
n_src_offset INTEGER := 1;
n_lang_context INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
n_warning INTEGER;
BEGIN
DBMS_LOB.CREATETEMPORARY(lob_loc => c_clob,
cache => TRUE);
DBMS_LOB.CONVERTTOCLOB(dest_lob =>c_clob,
src_blob =>p_blob,
amount =>n_file_size,
dest_offset =>n_dest_offset,
src_offset =>n_src_offset,
blob_csid =>p_charset_id,
lang_context =>n_lang_context,
warning =>n_warning);
RETURN c_clob;
END blob2clob_cs;
END PKG_CONVERT;