8.3 DBMS_LOB InterfaceThis section describes the programs available through the DBMS_LOB packages in several categories. 8.3.1 Working with BFILEsThe following sections describe the programs in the DBMS_LOB package that perform operations on BFILE objects. 8.3.1.1 The DBMS_LOB.FILEEXISTS functionThe FILEEXISTS function indicates whether the given BFILE locator points to a file that exists in the operating system. Here's the specification for this program: FUNCTION DBMS_LOB.FILEEXISTS ( file_loc IN BFILE ) RETURN INTEGER; The file_loc parameter is the name of the file locator. The function returns one of the following values:
8.3.1.1.1 ExceptionsOne of the following exceptions may be raised if the file_loc parameter contains an improper value (e.g., NULL): DBMS_LOB.NOEXIST_DIRECTORY DBMS_LOB.NOPRIV_DIRECTORY DBMS_LOB.INVALID_DIRECTORY 8.3.1.1.2 RestrictionsThe FILEEXISTS function asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (fileexists, WNDS, RNDS, WNPS, RNPS); 8.3.1.1.3 ExamplesThis block uses the FILEEXISTS function to see if chapter01.txt exists in the BOOK_TEXT directory:
This is the output of the script: chapter01.txt exists in BOOK_TEXT directory The following example selects the file locator for chapter01.txt from the my_book_files table and checks to see if the file exists:
This is the output of the script: Chapter 1 exists FILEEXISTS raises a VALUE_ERROR exception when passed a NULL file locator, so you should always include conditional logic and an exception section. The next example raises the NOEXIST_DIRECTORY exception. This can occur if the directory alias does not exist, or if the user has not been granted READ privilege on the directory.
Running this script results in this unhandled exception: ORA-22285: non-existent directory or file for FILEEXISTS operation If the directory exists and READ privileges have been granted to the user, but the specified file does not exist, FILEEXISTS returns zero.
This script produces the following: non_existent_file.txt does not exist FILEEXISTS can be called from SQL, for example:
Calls to FILEEXISTS should trap and handle the NOEXIST_DIRECTORY exception (directory alias does not exist) and the VALUE_ERROR exception (input file locator is NULL). 8.3.1.2 The DBMS_LOB.FILEGETNAME procedureGiven a file locator, the FILEGETNAME procedure determines its associated directory alias and filename. The specification for this program follows:
Parameters are summarized in the following table.
8.3.1.2.1 ExceptionsThe following VALUE_ERROR exception is raised if the file_loc parameter contains an improper value (e.g., NULL): INVALID_ARGVAL 8.3.1.2.2 ExamplesThe following example uses FILEGETNAME to get the directory alias and filename for the "Chapter 1" row in the my_book_files table:
This is the output of the script: File name is: chapter01.txt File is in Oracle directory: BOOK_TEXT FILEGETNAME raises a VALUE_ERROR exception when passed a NULL file locator, so be sure to check the value of the file locator and/or include an exception handler. Note that FILEGETNAME does not actually confirm that the physical file and directory alias exist. This can be done via FILEEXISTS. 8.3.1.3 The DBMS_LOB.FILEOPEN procedureGiven a file locator, the FILEOPEN procedure opens the BFILE for read-only access. Here's the header for this program:
Parameters are summarized in the following table.
8.3.1.3.1 ExceptionsThe following types of exceptions can be raised by the FILEOPEN procedure:
8.3.1.3.2 ExamplesThis example uses the FILEOPEN procedure to check whether chapter01.txt exists in the BOOK_TEXT directory, then opens and closes the file:
This is the output of the script: chapter01.txt exists opening the file closing the file The FILEOPEN procedure raises a VALUE_ERROR exception when passed to a NULL file. The procedure raises a NOEXIST_DIRECTORY exception when passed to a file locator associated with a nonexistent directory alias. Note that it is possible to open the same file using two different file locators, for example:
This is the output of the script: chapter01.txt exists opening the file via loc_1 opening the file via loc_2 closing the file via loc_1 closing the file via loc_2 To avoid exceeding the SESSION_MAX_OPEN_FILES limit, include a matching call to the FILECLOSE procedure for each BFILE that is opened. When an exception occurs after opening a file, it is possible that execution continues without closing the file (i.e., the matching call to FILECLOSE is not executed due to abnormal termination). In this case, the file remains open, and we run the risk of exceeding the SESSION_MAX_OPEN_FILES limit. It is good practice to include a call to the FILECLOSEALL procedure within an exception handler whenever FILEOPEN is used. See the " Section 8.3.1.6, "The DBMS_LOB.FILECLOSEALL procedure" " section for details. 8.3.1.4 The DBMS_LOB.FILEISOPEN functionThe FILEISOPEN function indicates whether the file was opened via the input file locator given by file_loc. The header for this program is, FUNCTION DBMS_LOB.FILEISOPEN ( file_loc IN BFILE ) RETURN INTEGER; where file_loc is the file locator for the file to be opened. The function returns one of the following values:
8.3.1.4.1 ExceptionsThe FILEISOPEN function will raise the VALUE_ERROR exception if the file_loc parameter contains an improper value (e.g., NULL). 8.3.1.4.2 RestrictionsThe program asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (fileisopen, WNDS, RNDS, WNPS, RNPS); 8.3.1.4.3 ExamplesThe following example uses the FILEISOPEN function to check whether chapter01.txt in the BOOK_TEXT directory is open. It opens the file if it is not already open.
This is the output of the script: chapter01.txt exists file is not open opening the file file is open closing the file The next example assigns two file locators to the same file, chapter01.txt, in the BOOK_TEXT directory. It opens the file using the first locator. When called with the first locator, FILEISOPEN indicates that the file is open. However, FILEISOPEN indicates that the file is not open when called with the second locator. Hence, FILEISOPEN indicates whether a file is open with respect to a specific locator .
This is the output of the script: chapter01.txt exists file is not open via loc_1 opening the file via loc_1 file is open via loc_1 file is not open via loc_2 closing the file via loc_1 FILEISOPEN raises a VALUE_ERROR exception when passed a NULL file locator. On the other hand, FILEISOPEN does not raise an exception when passed a file locator having a nonexistent directory alias or nonexistent file. 8.3.1.5 The DBMS_LOB.FILECLOSE procedureThe FILECLOSE procedure is used to close a file that has been opened via the input file locator indicated by the file_loc parameter. The header for this program is, PROCEDURE DBMS_LOB.FILECLOSE ( file_loc IN OUT BFILE ); where file_loc is the file locator for the file to be opened. 8.3.1.5.1 ExceptionsThe FILECLOSE procedure may raise a VALUE_ERROR exception if the file_loc parameter contains an improper value (e.g., NULL). 8.3.1.5.2 ExamplesThe following example demonstrates that FILECLOSE can be called with a locator for a file that has not been opened:
This is the output of the script: chapter01.txt exists file is not open closing the file The FILECLOSE procedure can be called with a locator for a nonexistent file or directory, for example:
This is the output of the script: closing non_existent_file.txt in NON_EXISTENT_DIRECTORY FILECLOSE raises a VALUE_ERROR exception when passed a NULL file locator. See the FILEOPEN and FILECLOSEALL sections for other examples of FILECLOSE usage. 8.3.1.6 The DBMS_LOB.FILECLOSEALL procedureThe FILECLOSEALL procedure is used to close all BFILEs that are open within a session. Here's the header for this program: PROCEDURE DBMS_LOB.FILECLOSEALL; 8.3.1.6.1 ExceptionsThe FILECLOSEALL procedure raises an UNOPENED_FILE exception if no files are open. 8.3.1.6.2 ExamplesWhen an exception occurs after opening a file, it is possible that execution continues without closing the file (i.e., a matching call to FILECLOSE is not executed due to abnormal termination of a block). In this case, the file remains open, and we run the risk of exceeding the SESSION_MAX_OPEN_FILES limit. It is good practice to include a call to FILECLOSEALL within an exception handler whenever FILEOPEN is used. The following anonymous block shows how you might construct an exception section that will close any open files:
This is the output of the script: opening chapter01.txt Clean up using FILECLOSEALL 8.3.1.7 The DBMS_LOB.LOADFROMFILE procedureThe LOADFROMFILE procedure is used to load all or part of a external LOB (source BFILE) to a destination internal LOB. This is the procedure used to load binary data stored in operating system files into internal LOBs, which reside in the database. The specification for this program is overloaded as follows:
The overloaded specification allows LOADFROMFILE to be used with BLOBs or CLOBs. The clause ANY_CS in the second specification allows acceptance of either CLOB or NCLOB locators as input. Parameters are summarized in the following table.
8.3.1.7.1 ExceptionsThe LOADFROMFILE procedure raises a VALUE_ERROR exception if dest_lob, src_lob, or amount are NULL or invalid. An INVALID_ARGVAL exception is raised if any of the following conditions are true:
LOADFROMFILE raises the ORA-22993 exception (specified input amount is greater than actual source amount) if the end of the source BFILE is reached before the specified amount of bytes has been copied. 8.3.1.7.2 ExamplesThe following example loads the CLOB chapter_text column of the my_book_text table with the contents of the first 100 bytes of the file chapter01.txt in the BOOK_TEXT directory. Note that the update of the chapter_text column occurs without the issue of an UPDATE statement. LOADFROMFILE accomplishes this via the chapter_text locator, which has been selected FOR UPDATE. LOB locators and DBMS_LOB allow changes to LOB columns in Oracle tables without issuing INSERT or UPDATE statements. If you want to take this approach, you must lock the row that contains the LOB prior to modification. The best way to obtain this lock is to use the FOR UPDATE clause in the SELECT statement (in this example, this translates to calling the book_text_forupdate function):
This is the output of the script:
The next example also loads the CLOB chapter_text column of the my_book_text table with the contents of the first 100 bytes of the file chapter01.txt in the BOOK_TEXT directory. This time, the LOB locator is not selected FOR UPDATE, but has been returned via the RETURNING clause.
This is the output of the script:
This example loads the BLOB diagram column of the by_book_diagrams table with the contents of the file ch01_01.bmp in the IMAGES directory. The LOB locator has been returned via the RETURNING clause. Note that the update of the diagram column occurs without the issue of an UPDATE statement. LOADFROMFILE accomplishes this via the diagram locator, which has been returned by the RETURNING clause. LOB locators and DBMS_LOB allow changes to LOB columns in Oracle tables without issuing INSERT or UPDATE statements.
This script produces the following: Diagram size: 481078 Inserting Empty Diagram Row Loading Diagram From File CHAPTER_DESCR DIAGRAM_NO DIAGRAM_SIZE --------------- ---------- ------------ Chapter 1 1 481078 8.3.2 Reading and Examining LOBsThe following sections describe the programs in the DBMS_LOB package that are used to read and examine LOBs. 8.3.2.1 The DBMS_LOB.COMPARE functionThe COMPARE function is used to compare two LOBs that are of the same type. Parts of LOBs can also be compared. The specification for this program takes the following forms for each LOB type that may be compared:
The overloaded specification allows COMPARE to be used with all types of LOBs. The clause ANY_CS in the specification allows either CLOB or NCLOB locators as input. Parameters are summarized in the following table.
The function returns one of the following values:
8.3.2.1.1 ExceptionsThe COMPARE function may raise the following exceptions:
8.3.2.1.2 RestrictionsThe program asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (compare, WNDS, RNDS, WNPS, RNPS); 8.3.2.1.3 ExamplesThe following example compares two BFILE locators that are pointing to the same file. Note that for BFILEs we must provide a number of bytes (in the amount parameter) to compare, which is determined via the GETLENGTH function. Note also that for BFILES we must first open the files.
This is the output of the script: file_loc_1 equals file_loc_2 This example compares two diagrams from the my_book_diagrams table:
This is the output of the script: diagrams are different 8.3.2.2 The DBMS_LOB.GETLENGTH functionThe GETLENGTH function returns the length of the input LOB. The length is in bytes for BFILEs and BLOBs, and in characters for CLOBs and NCLOBs. The headers for this program, for each corresponding LOB type, are the following: FUNCTION DBMS_LOB.GETLENGTH (lob_loc IN BLOB) RETURN INTEGER; FUNCTION DBMS_LOB.GETLENGTH (lob_loc IN CLOB CHARACTER SET ANY_CS) RETURN INTEGER; FUNCTION DBMS_LOB.GETLENGTH (lob_loc IN BFILE) RETURN INTEGER; The lob_loc parameter is the locator of the LOB whose length is to be determined. The overloaded specification allows GETLENGTH to be used with all types of LOBs. The clause ANY_CS in the specification allows either CLOB or NCLOB locators as input. The function returns the length (in bytes or characters) of the input LOB, or it returns NULL if the input LOB is NULL or invalid. 8.3.2.2.1 RestrictionsThe program asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (getlength, WNDS, RNDS, WNPS, RNPS); 8.3.2.2.2 ExamplesThe following example gets the size in bytes of the file ch01_01.bmp in the IMAGES directory:
This is the output of the script: Diagram size: 481078 This example gets the size in characters of "Chapter 1" from the my_book_text table:
This is the output of the script: Length of Chapter 1: 100 8.3.2.3 The DBMS_LOB.READ procedureThe READ procedure provides piece-wise read access to a LOB. A specified number of bytes (BFILE, BLOB) or characters (CLOB, NCLOB) is read into the buffer, starting from a specified location . The number of bytes or characters actually read by the operation is returned. The headers for this program, corresponding to each type, are the following:
The overloaded specification allows READ to be used with all types of LOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. The READ procedure and the DBMS_LOB .SUBSTR function provide similar functionality. READ is a procedure, while SUBSTR is a function. However, READ will raise NO_DATA_FOUND and INVALID_ARGVAL exceptions, while SUBSTR will ignore these exceptions and return NULL when they occur. DBMS_LOB.SUBSTR can also be called from within a SQL statement, but READ cannot be, since it is a procedure. Parameters are summarized in the following table.
8.3.2.3.1 ExceptionsThe READ procedure may raise any of the following exceptions:
8.3.2.3.2 ExamplesThe following example reads the first 60 characters of the CLOB chapter_text column of the my_book_texttable using the "Chapter 1" row:
This is the output of the script: Chapter 1: It was a dark and stormy night. Suddenly a scream rang out. The next example reads sixty characters at a time from the CLOB chapter_text column of the my_book_text table using the "Chapter 1" row. Note that the loop continues until READ raises the NO_DATA_FOUND exception.
This script produces the following: Chapter 1: It was a dark and stormy night. Suddenly a scream rang out. Chapter 1: An EXCEPTION had not been handled. End of Chapter Reached. Note that the maximum size of a VARCHAR2 or RAW variable is 32767 bytes. This is the size limit of the buffer to be used with READ. 8.3.2.4 The DBMS_LOB.SUBSTR functionThe SUBSTR function provides piece-wise access to a LOB. The specified number of bytes (BFILE, BLOB) or characters (CLOB, NCLOB) is returned, starting from the specified location. The headers for this program, corresponding to each LOB type, are the following:
The overloaded specification allows SUBSTR to be used with all types of LOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. The SUBSTR function and DBMS_LOB .READ procedure provide similar functionality. READ is a procedure, while SUBSTR is a function. However, READ will raise NO_DATA_FOUND and INVALID_ARGVAL exceptions, while SUBSTR will ignore these exceptions when they occur and will return NULL. SUBSTR can also be called from within a SQL statement, but DBMS_LOB.READ cannot, since it is a procedure. Parameters are summarized in the following table.
The SUBSTR function returns a NULL value for any of the following conditions:
8.3.2.4.1 ExceptionsThe SUBSTR function may raise one of the following exceptions:
8.3.2.4.2 RestrictionsThe program asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (substr, WNDS, RNDS, WNPS, RNPS); 8.3.2.4.3 ExamplesThe following example reads the first 60 characters of the CLOB chapter_text column of the my_book_texttable using the "Chapter 1" row:
This is the output of the script: Chapter 1: It was a dark and stormy night. Suddenly a scream rang out. The next example reads 60 characters at a time from the CLOB chapter_text column of the my_book_text table using the "Chapter 1" row. Note that the loop continues until SUBSTR returns NULL (i.e., SUBSTR does not raise the NO_DATA_FOUND exception).
This is the output of the script: Chapter 1: It was a dark and stormy night. Suddenly a scream rang out. Chapter 1: An EXCEPTION had not been handled. 8.3.2.5 The DBMS_LOB.INSTR functionThe INSTR function returns the matching offset location of the Nthoccurrence of the given pattern in the LOB. It returns zero if the pattern is not found. The headers for this program, corresponding to each LOB type, are the following:
The overloaded specification allows INSTR to be used with all types of LOBs. The clause ANY_CS in the specification allows either CLOB or NCLOB locators as input. Parameters are summarized in the following table.
The INSTR function returns NULL for any of the following conditions:
8.3.2.5.1 ExceptionsThe INSTR function may raise any of the following exceptions:
8.3.2.5.2 RestrictionsThe program asserts a purity level with the RESTRICT_REFERENCES pragma. PRAGMA RESTRICT_REFERENCES (instr, WNDS, RNDS, WNPS, RNPS); 8.3.2.5.3 ExampleThe following example searches for the first occurrence of the string "dark" in the first sixty characters of the CLOB chapter_text column of the "Chapter 1" row of the my_book_texttable:
This is the output of the script: buffer: It was a dark and stormy night. Suddenly a scream rang out. location of "dark": 10 8.3.3 Updating BLOBs, CLOBs, and NCLOBsThe following sections describe the programs in the DBMS_LOB package that are used to update or alter BLOB, CLOB, and NCLOB object types. 8.3.3.1 The DBMS_LOB.APPEND procedureThe APPEND procedure appends the contents of a source internal LOB to a destination internal LOB. The headers for this program, corresponding to each LOB type, are the following:
The overloaded specification allows APPEND to be used with BLOBs, CLOBs, and NCLOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. APPEND cannot be used with BFILEs because access to BFILEs is read-only. Parameters are summarized in the following table.
8.3.3.1.1 ExceptionsThe program may raise any of the following exceptions:
8.3.3.1.2 ExamplesThe following example shows that the destination LOB must be selected FOR UPDATE before calling APPEND. We attempt to append the chapter_textfor "Chapter 2" to the chapter_textfor "Chapter 1."
This is the output of the script: OTHERS Exception ORA-22920: row containing the LOB value is not locked In the next example we append the chapter_textfor "Chapter" to the chapter_textfor "Chapter 1." We display the appended text, roll back the changes, and display the original text. Internal LOBs can participate in database transactions.
This is the output of the script:
8.3.3.2 The DBMS_LOB.COPY procedureThe COPY procedure copies all or part of the contents of a source internal LOB to a destination internal LOB. An offset location in each LOB can be specified. The headers for this program, corresponding to each LOB type, are the following:
The overloaded specification allows COPY to be used with BLOBs, CLOBs, and NCLOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. COPY cannot be used with BFILEs, because access to BFILEs is read-only. Parameters are summarized in the following table.
8.3.3.2.1 ExceptionsThe COPY procedure may raise one of the following exceptions:
8.3.3.2.2 ExampleIn the following example, the text "Suddenly a scream rang out..." is copied from the "Chapter 1" row of the my_book_text table to the "Chapter 2" row. Note that the COPY operation replaces (i.e., does not append) existing text. We display the copied text, roll back the changes, and display the original text. Internal LOBs can participate in database transactions.
This is the output of the script:
8.3.3.3 The DBMS_LOB.ERASE procedureThe ERASE procedure removes all or part of the contents of an internal LOB. An offset location in the LOB can be specified. In the middle of a LOB, spaces are written for CLOBs and NCLOBs, and zero-byte filler is written for BLOBs.
The overloaded specification allows ERASE to be used with BLOBs, CLOBs, and NCLOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. ERASE cannot be used with BFILEs because access to BFILEs is read-only. Parameters are summarized in the following table.
8.3.3.3.1 ExceptionsThe ERASE procedure may raise any of the following exceptions:
8.3.3.3.2 ExampleIn the following example, we erase the string "brightly" from the "Chapter 2" chapter_textcolumn in the my_book_text table. Note that the string is replaced with spaces. We display the erased text, roll back the changes, and display the original text. Internal LOBs can participate in database transactions.
This is the output of the script:
8.3.3.4 The DBMS_LOB.TRIM procedureThe TRIM procedure trims the contents of an internal LOB to a specified length. The headers for this program, corresponding to each LOB type, are the following:
The overloaded specification allows TRIM to be used with BLOBs, CLOBs, and NCLOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. TRIM cannot be used with BFILEs because access to BFILEs is read-only. The parameters for this program are summarized in the following table.
8.3.3.4.1 ExceptionsThe TRIM procedure may raise any of the following exceptions:
8.3.3.4.2 ExampleIn the following example, we trim the "Chapter 1" chapter_textcolumn in the my_book_text table to 31 characters. We display the trimmed text, roll back the changes, and display the original text. Internal LOBs can participate in database transactions.
This is the output of the script:
8.3.3.5 The DBMS_LOB.WRITE procedureThe WRITE procedure writes a given number of bytes (BLOB) or characters (CLOB, NCLOB) to an internal LOB, beginning at a specified offset. The contents of the write operation are taken from the buffer. WRITE replaces (overlays) any data that exists in the LOB at the offset. The headers for this program, for each corresponding LOB type, are the following:
The overloaded specification allows WRITE to be used with BLOBs, CLOBs, and NCLOBs. The term ANY_CS in the specification allows either CLOB or NCLOB locators as input. WRITE cannot be used with BFILEs, because access to BFILEs is read-only. Parameters are summarized in the following table.
8.3.3.5.1 ExceptionsThe WRITE procedure may raise any of the following exceptions:
8.3.3.5.2 ExampleIn the following example, we write the string "The End" to the end of the "Chapter 2" chapter_textcolumn in the my_book_text table. We display the new text, roll back the changes, and display the original text. Internal LOBs can participate in database transactions.
This is the output of the script:
Copyright (c) 2000 O'Reilly & Associates. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|