我从stackoverflow得到了下面的sql base64解码器(忘了链接)。我略微修改了我的要求。当我尝试执行时,解码只是部分工作。我的数据库中每行最多有10MB的 base64编码 clob数据。
不确定我错过了什么。
create or replace FUNCTION Decode64_CLOB3(IN_CLOB CLOB) RETURN CLOB IS
clobOriginal clob;
clobInBase64 clob;
substring varchar2(2000);
n pls_integer := 0;
substring_length pls_integer := 2000;
function from_base64(t in varchar2) return varchar2 is
begin
return
utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(t)));
end from_base64;
begin
select myCLobData into clobInBase64 from myClobTable where ID = 3;
clobInBase64 :=IN_CLOB;
n := 0;
clobOriginal := null;
while true loop
substring := dbms_lob.substr(clobInBase64,
least(substring_length, length(clobInBase64) - (substring_length * n + 1)));
if substring is null then
DBMS_OUTPUT.put_line('this is me 2');
exit;
end if;
clobOriginal := clobOriginal || from_base64(substring);
n := n + 1;
end loop;
return clobOriginal;
end;
UPDATE2
我做了一些调试,发现问题出在子字符串上。 子字符串适用于(2000)char的firstloop。但它无法移动下一个2000字符。不确定是什么问题。有人可以提出建议。
答案 0 :(得分:0)
您已将呼叫更改为dbms_lob.substr()
,并且在此过程中您已删除第三个参数,即偏移量。由于您没有提供默认值为1,因此每次循环时您都会获得相同的值 - 编码字符串中的前2000个字符。
好吧,你会在很多电话中获得2000,然后一旦n
变得足够大,你就可以使用你发布的代码获得null。既然你说它进入了一个“挂”的状态。建议您运行的内容略有不同。
稍作修改后,这似乎有效:
create or replace function decode64_clob3(p_clob_encoded clob)
return clob
is
l_clob_decoded clob;
substring varchar2(2048);
substring_length pls_integer := 2048;
n pls_integer := 0;
function from_base64(t in varchar2) return varchar2
is
begin
return utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(t)));
end from_base64;
begin
dbms_lob.createtemporary(l_clob_decoded, false);
while true
loop
substring := dbms_lob.substr(p_clob_encoded, substring_length,
(substring_length * n) + 1);
if substring is null then
exit;
end if;
dbms_lob.append(l_clob_decoded, from_base64(substring));
n := n + 1;
/* protective check for infinite loop while debugging
if n > 10000 then
dbms_output.put_line('n is ' || n);
exit;
end if;
*/
end loop;
return l_clob_decoded;
end;
/
我稍微改变了CLOB处理;连接很好,但这更明确一点。我个人并不确定我是否对嵌套功能感到困扰。而且我不确定least()
计算是否真的在添加任何内容。
但如果base64编码的值是换行符,则ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275
或ORA-14553: cannot perform a lob write operation inside a query
会出错,通常情况就是如此。第一个块被解码为OK,但是下一个块以一个未消耗的换行开始,该换行抛出一切并产生垃圾;最终垃圾为空或导致其中一个例外的其他值。
因此,您需要在穿过CLOB时跟踪位置,以换行长度的倍数;并调整跟踪位置以考虑额外的换行符:
create or replace function decode64_clob3(p_clob_encoded clob)
return clob
is
l_clob_decoded clob;
l_substring varchar2(2048);
l_substring_length pls_integer := 2048;
l_pos pls_integer := 1;
begin
dbms_lob.createtemporary(l_clob_decoded, false);
while l_pos <= dbms_lob.getlength(p_clob_encoded)
loop
l_substring := dbms_lob.substr(p_clob_encoded, l_substring_length, l_pos);
if l_substring is null then
exit;
end if;
dbms_lob.append(l_clob_decoded,
utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(l_substring))));
/* Adjust l_pos to skip over CR/LF chars if base64 is line-wrapped */
l_pos := l_pos + length(l_substring);
while dbms_lob.substr(p_clob_encoded, 1, l_pos) in (chr(10), chr(13)) loop
l_pos := l_pos + 1;
end loop;
end loop;
return l_clob_decoded;
end;
/
首先删除任何潜在的换行符/回车符:
create or replace function decode64_clob3(p_clob_encoded clob)
return clob
is
l_clob_encoded clob;
l_clob_decoded clob;
l_substring varchar2(2048);
l_substring_length pls_integer := 2048;
l_pos pls_integer := 1;
begin
l_clob_encoded := replace(replace(p_clob_encoded, chr(10), null), chr(13), null);
dbms_lob.createtemporary(l_clob_decoded, false);
while l_pos <= dbms_lob.getlength(l_clob_encoded)
loop
l_substring := dbms_lob.substr(l_clob_encoded, l_substring_length, l_pos);
if l_substring is null then
exit;
end if;
dbms_lob.append(l_clob_decoded,
utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(l_substring))));
l_pos := l_pos + length(l_substring);
end loop;
return l_clob_decoded;
end;
/
这适用于包装和解包的base64值。
set serveroutput on
with t (c) as (
select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('Hello world'))) from dual
)
select c, decode64_clob3(c)
from t;
C
--------------------------------------------------------------------------------
DECODE64_CLOB3(C)
--------------------------------------------------------------------------------
SGVsbG8gd29ybGQ=
Hello world
还使用大于substring_length
值的编码值进行测试。