DBA - BFILES
exemple d'utilisation des bfiles, lecture, insertion dans un blob
function load_livre ( vdocument_orig_dir bibliotheque.document_orig_dir%type, vdocument_name bibliotheque.document_name%type, vdocument_type varchar2 ) return integer AS vsource bfile; vdocument bibliotheque.document_html%type; vdocument_bin bibliotheque.document_bin%type; vdocument_id bibliotheque.document_id%type; bool boolean; cursor cbib (pdocument_id vdocument_id%type) is select document_html from bibliotheque where document_id = pdocument_id for update; cursor bbib (pdocument_id vdocument_id%type) is select document_bin from bibliotheque where document_id = pdocument_id for update; xtype bibliotheque.dk_type%type; vtype varchar2(15):= lower(vdocument_type); begin if vtype is null then xtype := 3; /* blob */ elsif (vtype = '.html' or vtype = '.htm' or vtype = '.txt' or vtype = '.sql') then xtype := 0; /* clob */ elsif (vtype = '.doc' or vtype = '.pdf' or vtype = '.ppt' or vtype = '.') then xtype := 1; /* clob */ else xtype := 3; /* blob */ end if; if (xtype = 0 or xtype = 1) then null; else return (1); end if; vsource := bfilename('SOURCES',vdocument_name); loglog('Load Livre: ' || vdocument_orig_dir|| ' - ' || vdocument_name); if dbms_lob.fileexists(vsource) = 1 then -- loglog('Load Livre: ' || 'loading ' || vdocument_orig_dir|| ' - ' || vdocument_name); vdocument_id := imutil.exist_file(vdocument_orig_dir); loglog('Load Livre: ' || vtype || ':' || xtype); /* punem un indicator numeric: 0 clob, 1 blob nu e inca in descrierea tablei*/ if vdocument_id = 0 then /* pas de document_trouve, allocation d'un numéro */ vdocument_id := imutil.loaddocumentgetid(vdocument_name, vdocument_orig_dir); if xtype = 0 then /* clob*/ loglog('Load Livre: CLOB'); insert into bibliotheque (document_id, document_machine, document_orig_dir, document_name, dk_type, document_html,dk_suffix) values (vdocument_id,'MASTER',vdocument_orig_dir,vdocument_name,0,empty_clob(),vtype); commit; elsif xtype = 1 then loglog('Load Livre: BLOB'); insert into bibliotheque (document_id, document_machine, document_orig_dir, document_name, dk_type, document_bin,dk_suffix) values (vdocument_id,'MASTER',vdocument_orig_dir,vdocument_name,1,empty_blob(),vtype); commit; end if; end if; /* no document found - insert empty clob */ if xtype = 0 then /* clob*/ loglog('Load Livre: Update CLOB'); open cbib (vdocument_id); fetch cbib into vdocument; dbms_lob.open(vsource,DBMS_LOB.LOB_READONLY); dbms_lob.loadfromfile(vdocument,vsource,dbms_lob.getlength(vsource)); dbms_lob.trim(vdocument,dbms_lob.getlength(vsource)); dbms_lob.close(vsource); update bibliotheque set document_html = vdocument where current of cbib; commit; close cbib; loglog(' loaded ' || vdocument_id|| ' (clob) taille ' || dbms_lob.getlength(vsource)); elsif xtype = 1 then /*blob*/ loglog('Load Livre: Update BLOB'); open bbib (vdocument_id); fetch bbib into vdocument_bin; dbms_lob.open(vsource,DBMS_LOB.LOB_READONLY); dbms_lob.loadfromfile(vdocument_bin,vsource,dbms_lob.getlength(vsource)); dbms_lob.trim(vdocument_bin,dbms_lob.getlength(vsource)); dbms_lob.close(vsource); update bibliotheque set document_bin = vdocument_bin where current of bbib; commit; close bbib; loglog(' loaded ' || vdocument_id|| ' (blob) taille ' || dbms_lob.getlength(vsource)); end if; return (1); -- else loglog('Load Livre: Not Found on directory! ' || vdocument_orig_dir|| '\' || vdocument_name); return(2); end if; loglog('Load Livre: ' || sqlerrm); return (0); exception when others then if dbms_lob.isopen(vsource) = 1 then dbms_lob.close(vsource); end if; loglog('Load Livre: ' || sqlerrm); return (0); end;