DBMS_LOB.LoadFromFile完整性检查

时间:2018-06-11 20:49:14

标签: oracle plsql blob

我想也许我开始在这里开始这个过程,但是我很怀疑自己。在下文中,请参阅有关混淆点的评论。

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;

以下是在已经存在的行中更新BLOB的方法吗?

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;

这似乎更像是.NET对数据库适配器的处理,以及FileInfo,DirInfo函数。但我还没有在其他任何地方看到过这种哲学。

我原本预计

  BLOBVariable = LoadFromFile(srcLocator, byteCnt); -- where the func. rtrns a val,

  INSERT INTO SomeTable (pdf_file)
  VALUES              (BLOBVariable);

我看到了吗? 如果是这样,什么时候放弃参考? 我似乎记得读过“承诺”#39;使用&#39; LoadFromFile?&#39;

时没有必要

现在,今天早上我看到一个示例(没有解释)将BLOB字段选择为变量,使用FOR UPDATE锁定记录。可能会很好理解那个...... ...

    -- 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 >;

1 个答案:

答案 0 :(得分:1)

以下向我展示了我的完整性,并且INSERT语句确实创建了loadfromfile()使用的引用。关于INSERT语句的这个答案足以满足我的目的,因此我将假设UPDATE功能类似。 'IMPORT_DIR'是数据库中的命名数据库目录。

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;
/
相关问题