Reputation: 377
I think that maybe I am starting to get the process here, but it is so odd that I doubt myself. In the following, see the comments for what the point of confusions are.
DECLARE
srcFile BFILE := BFILENAME('SOME_DIR', 'xyz.pdf');
fLen NUMBER;
dest BLOB;
BEGIN
INSERT INTO SomeTable
VALUES ( 1, 'xyz.pdf', EMPTY_BLOB(), null ) -- Does this establish an active ref.?
RETURNING pdf_file INTO dest;
DBMS_LOB.OPEN(srcFile, dbms_lob.file_readonly);
fLen := dbms_lob.getlength(srcFile);
DBMS_LOB.LOADFROMFILE(dest, srcFile, fLen); -- Does this reach into the table row,
-- and fill it with the file’s contents?
dbms_lob.close(srcFile);
commit;
END;
And is the following the way to update the BLOB in a row that already exists?
DECLARE
srcFile BFILE := BFILENAME('SOME_DIR', 'xyz.pdf');
fLen NUMBER;
dest BLOB;
BEGIN
SELECT pdf_file INTO dest -- Does this est. an active reference?
FROM SomeTable
WHERE ID = 1; -- ( <———<<< ' = 1' is just for example.)
DBMS_LOB.OPEN(srcFile, dbms_lob.file_readonly);
fLen := dbms_lob.getlength(srcFile);
DBMS_LOB.LoadFromFile(dest, srcFile, fLen); -- Does this reach into the row,
-- filling it w/ the file’s contents?
dbms_lob.close(srcFile);
commit;
END;
This seems a lot more like .NET's handling of database adapters, and the FileInfo, DirInfo functions. But I haven't seen that kind of philosophy in Oracle anywhere else.
I would have expected
BLOBVariable = LoadFromFile(srcLocator, byteCnt); -- where the func. rtrns a val,
fol’d by
INSERT INTO SomeTable (pdf_file)
VALUES (BLOBVariable);
Am I seeing this right? If so, when is the reference abandoned? I seem to recall reading that 'commit' is not necessary when using 'LoadFromFile?'
And now, this morning I see an example (with no explanation) that selects the BLOB field into a variable, using FOR UPDATE to lock the record. Might be good to understand what's up with that...
-- Lock the record
execute immediate 'SELECT pdf_file INTO dest
FROM image_blobs
WHERE itl_image_blob_id = :1
FOR UPDATE'
INTO v_blob_data
using < the row identifier goes here >;
-- Read the file
dbms_lob.loadfromfile(dests, srcFile, fLen);
-- update the blob field
execute immediate '
UPDATE image_blobs
SET pdf_file = :1
WHERE itl_image_blob_id = :2'
using dest, < row identifier >;
Upvotes: 3
Views: 4638
Reputation: 377
The following showed me that my sanity is intact, and that the INSERT statement really does create a reference that loadfromfile()
uses. This answer with respect to an INSERT statement is good enough for my purposes, so I am going to assume that the UPDATE functionality is similar. 'IMPORT_DIR'
is a named database directory in the database.
DROP TABLE TEST_BLOBS purge; -- 'purge' prevents loading up the recycle bin.
CREATE TABLE TEST_BLOBS (
ID VARCHAR(4),
F_NAME VARCHAR2(50 BYTE),
CONTENT_TYPE VARCHAR2(50 BYTE),
F_SIZE NUMBER(10),
BLOB_DATA BLOB DEFAULT empty_blob() );
DECLARE
dest blob; tmp_id varchar2(4);
v_f_sz NUMBER(10); b_file BFILE := NULL;
fName VARCHAR2 (50);
CURSOR get_items IS SELECT filename FROM My_Table_of_names;
BEGIN
FOR a_rec IN get_items LOOP
fName := a_rec.filename;
b_file := BFILENAME ( 'IMPORT_DIR', fName );
IF DBMS_LOB.fileexists (b_file) = 0 THEN
-- Report the problem and exit.
END IF;
DBMS_LOB.fileopen ( b_file, DBMS_LOB.file_readonly );
v_f_sz := DBMS_LOB.getlength (b_file);
INSERT INTO test_blobs ( ID, content_type, f_name, f_size, blob_data )
VALUES ( <Some_ID_val>, 'application/pdf', fName, v_f_sz, empty_blob() )
RETURNING blob_data, ID INTO dest, tmp_id;
-- Table field contains data, (HUGEBLOB),
-- when debugging and loop ends here.
DBMS_LOB.loadfromfile(dest, b_file, v_f_sz);
DBMS_LOB.close(b_file);
-- After running this, the variable tmp_id has the value that was assigned in the INSERT statement,
-- and the table’s fields, 'BLOB_Data', have the contents of the files. dest is indeed a reference
-- to the row and field, allowing LoadFromFile() to put data into the table.
END LOOP;
END;
/
Upvotes: 1