| Oracle9i Application Developer's Guide - Large Objects (LOBs) Release 2 (9.2) Part Number A96591-01 |
|
This chapter discusses the following topics:
Table 3-1 lists the eight programmatic environments (languages) that support LOB functionality. Chapter 10, Chapter 11, and Chapter 12 cover the supported LOB functions in terms of use cases. Examples are provided in each programmatic environment for most LOB use cases.
| Language | Precompiler or Interface Program | Syntax Reference | In This Chapter See... |
|---|---|---|---|
|
PL/SQL |
DBMS_LOB Package |
||
|
C |
Oracle Call Interface for C (OCI) |
||
|
C++ |
Oracle Call Interface for C++ (OCCI) |
"Using C++ (OCCI) To Work With LOBs" |
|
|
C/C++ |
Pro*C/C++ Precompiler |
||
|
COBOL |
Pro*COBOL Precompiler |
||
|
Visual Basic |
Oracle Objects For OLE (OO4O) |
Oracle Objects for OLE (OO4O) is a Windows-based product included with Oracle9i Client for Windows NT. There are no manuals for this product, only online help. Online help is available through the Application Development submenu of the Oracle9i installation. |
"Using Visual Basic (Oracle Objects for OLE (OO4O)) to Work with LOBs"." |
|
Java |
JDBC Application Programmatic Interface (API) |
Oracle9i SQLJ Developer's Guide and Reference and Oracle9i JDBC Developer's Guide and Reference. |
|
|
OLEDB |
OraOLEDB, an OLE DB provider for Oracle. |
Oracle Provider for OLE DB Developer's Guide |
Table 3-2 and Table 3-3compare the eight LOB programmatic interfaces by listing their functions and methods used to operate on LOBs. The tables are split in two simply to accommodate all eight interfaces. The interfaces' functionality, with regards LOBs, is described in the following sections.
The PL/SQL DBMS_LOB package can be used for the following operations:
| See Also:
Oracle9i Supplied PL/SQL Packages and Types Reference for detailed documentation, including parameters, parameter types, return values, and example code. |
As described in more detail in the following, DBMS_LOB routines work based on LOB locators. For the successful completion of DBMS_LOB routines, you must provide an input locator representing a LOB that exists in the database tablespaces or external file system, before you invoke the routine.
LOB columns, and subsequently you can use SQL to initialize or populate the locators in these LOB columns.DIRECTORY object that maps to a valid physical directory containing the external LOBs that you intend to access. These files must exist, and have READ permission for Oracle Server to process. If your operating system uses case-sensitive path names, specify the directory in the correct case. See Chapter 12, "External LOBs (BFILEs)", "Directory Object" for more information.
Once the LOBs are defined and created, you may then SELECT a LOB locator into a local PL/SQL LOB variable and use this variable as an input parameter to DBMS_LOB for access to the LOB value.
Examples provided with each DBMS_LOB routine will illustrate this in the following sections.
Client-side PL/SQL procedures cannot call DBMS_LOB package routines.
However, you can use server-side PL/SQL procedures or anonymous blocks in Pro*C/C++ to call DBMS_LOB package routines.
For DBMS_LOB package, for example, for both fixed and varying-width character sets, the following rules apply:
When using DBMS_LOB.LOADFROMFILE, you cannot specify an amount parameter value larger than the size of the BFILE. (Although you can specify the LOBMAXSIZE constant for the amount parameter value to load the entire BFILE.)
When using DBMS_LOB.READ, the amount parameter can be larger than the size of the data. In PL/SQL, the amount should be less than or equal to the size of the buffer, and the buffer size is limited to 32K.
PL/SQL functions and procedures that operate on BLOBs, CLOBs, NCLOBs, and BFILEs are summarized in the following:
| Function/Procedure | Description |
|---|---|
|
CREATETEMPORARY() |
Creates a temporary LOB |
|
ISTEMPORARY() |
Checks if a LOB locator refers to a temporary LOB |
|
FREETEMPORARY() |
Frees a temporary LOB |
| Function/Procedure | Description |
|---|---|
|
OPEN() |
Opens a LOB |
|
ISOPEN() |
Sees if a LOB is open |
|
CLOSE() |
Closes a LOB |
These procedures are described in detail for specific LOB operations, such as, INSERT a row containing a LOB, in these chapters:
You can access many of the PL/SQL LOB example scripts from these chapters, at $ORACLE_HOME/rdbms/demo/lobs/plsql.
Oracle Call Interface (OCI) can be used to make changes to an entire internal LOB, or to pieces of the beginning, middle or end of it through OCI, as follows:
OCI also includes functions that you can use to:
BLOBs, CLOBs, NCLOBs) and external LOBs (BFILEs)These functions are listed in the following tables, and are discussed in greater detail later in this section.
If you want to read or write data in 2 byte unicode (UCS2) format, set the csid (character set ID) parameter in OCILobRead and OCILobWrite to OCI_UCS2ID. The csid parameter indicates the character set id for the buffer parameter. You can set the csid parameter to any character set ID. If the csid parameter is set, it will override the NLS_LANG environment variable.
See Also:
|
In OCI, for fixed-width client-side character sets, the following rules apply:
The following rules apply only to varying-width client-side character sets:
CLOBs and NCLOBs:
CLOBs and NCLOBs:
For all other LOB operations, irrespective of the client-side character set, the amount parameter is in characters for CLOBs and NCLOBs. These include OCILobCopy, OCILobErase, OCILobLoadFromFile, and OCILobTrim. All these operations refer to the amount of LOB data on the server.
NCLOBs parameters are allowed in methods.
When using OCILobLoadFromFile you cannot specify amount larger than the length of the BFILE.
In OCILobRead, you can specify amount = 4 gigabytes-1, and it will read to the end of the LOB.
Further OCI examples are provided in:
You can access most of the OCI LOB example scripts from your Oracle9i distribution software at $ORACLE_HOME/rdbms/demo/lobs/oci.
There are further example OCI scripts:
See also Appendix B, "OCI Demonstration Programs" in Oracle Call Interface Programmer's Guide, for further OCI demonstration script listings.
For further information and features of OCI, refer to the OTN Web site, http://otn.oracle.com/ for OCI features and FAQs.
OCI functions that operate on BLOBs, CLOBs, NCLOBs, and BFILEs are as follows:
| Function/Procedure | Description |
|---|---|
|
|
Creates a temporary LOB |
|
|
Sees if a temporary LOB exists |
|
|
Frees a temporary LOB |
| Function/Procedure | Description |
|---|---|
|
OCILobOpen() |
Opens a LOB |
|
OCILobIsOpen() |
Sees if a LOB is open |
|
OCILobClose() |
Closes a LOB |
To work with the OCI examples in the remainder of the book, you can use a main() like the following. Here, it is used with seeIfLOBIsOpen as an example.
int main(char *argv, int argc) { /* Declare OCI Handles to be used */ OCIEnv *envhp; OCIServer *srvhp; OCISvcCtx *svchp; OCIError *errhp; OCISession *authp; OCIStmt *stmthp; OCILobLocator *Lob_loc; /* Create and Initialize an OCI Environment: */ (void) OCIEnvCreate(&envhp, (ub4)OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0, (size_t) 0, (dvoid **) 0); /* Allocate error handle: */ (void) OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0); /* Allocate server contexts: */ (void) OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0); /* Allocate service context: */ (void) OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0); /* Attach to the Oracle database: */ (void) OCIServerAttach(srvhp, errhp, (text *)"", strlen(""), 0); /* Set the server context attribute in the service context: */ (void) OCIAttrSet ((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp); /* Allocate the session handle: */ (void) OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); /* Set the username in the session handle:*/ (void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) "samp", (ub4)4, (ub4) OCI_ATTR_USERNAME, errhp); /* Set the password in the session handle: */ (void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) "samp", (ub4) 4, (ub4) OCI_ATTR_PASSWORD, errhp); /* Authenticate and begin the session: */ checkerr(errhp, OCISessionBegin (svchp, errhp, authp, OCI_CRED_RDBMS, (ub4) OCI_DEFAULT)); /* Set the session attribute in the service context: */ (void) OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION, errhp); /* ------- At this point a valid session has been created -----------*/ printf ("user session created \n"); /* Allocate a statement handle: */ checkerr(errhp, OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)); /* ============= Sample procedure call begins here ===================*/ printf ("calling seeIfLOBIsOpen...\n"); seeIfLOBIsOpen(envhp, errhp, svchp, stmthp); return 0; } void checkerr(errhp, status) OCIError *errhp; sword status; { text errbuf[512]; sb4 errcode = 0; switch (status) { case OCI_SUCCESS: break; case OCI_SUCCESS_WITH_INFO: (void) printf("Error - OCI_SUCCESS_WITH_INFO\n"); break; case OCI_NEED_DATA: (void) printf("Error - OCI_NEED_DATA\n"); break; case OCI_NO_DATA: (void) printf("Error - OCI_NODATA\n"); break; case OCI_ERROR: (void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); (void) printf("Error - %.*s\n", 512, errbuf); break; case OCI_INVALID_HANDLE: (void) printf("Error - OCI_INVALID_HANDLE\n"); break; case OCI_STILL_EXECUTING: (void) printf("Error - OCI_STILL_EXECUTE\n"); break; case OCI_CONTINUE: (void) printf("Error - OCI_CONTINUE\n"); break; default: break; } } /* Select the locator into a locator variable */ sb4 select_frame_locator(Lob_loc, errhp, svchp, stmthp) OCILobLocator *Lob_loc; OCIError *errhp; OCISvcCtx *svchp; OCIStmt *stmthp; { text *sqlstmt = (text *)"SELECT Frame FROM Multimedia_tab WHERE Clip_ID=1"; OCIDefine *defnp1; checkerr (errhp, OCIStmtPrepare(stmthp, errhp, sqlstmt, (ub4)strlen((char *)sqlstmt), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT)); checkerr (errhp, OCIDefineByPos(stmthp, &defnp1, errhp, (ub4) 1, (dvoid *)&Lob_loc, (sb4)0, (ub2) SQLT_BLOB,(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) OCI_DEFAULT)); /* execute the select and fetch one row */ checkerr(errhp, OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0, (ub4) OCI_DEFAULT)); return (0); } void seeIfLOBIsOpen(envhp, errhp, svchp, stmthp) OCIEnv *envhp; OCIError *errhp; OCISvcCtx *svchp; OCIStmt *stmthp; { OCILobLocator *Lob_loc; int isOpen; /* allocate locator resources */ (void) OCIDescriptorAlloc((dvoid *)envhp, (dvoid **)&Lob_loc, (ub4)OCI_DTYPE_LOB, (size_t)0, (dvoid **)0); /* Select the locator */ (void)select_frame_locator(Lob_loc, errhp, svchp, stmthp); /* See if the LOB is Open */ checkerr (errhp, OCILobIsOpen(svchp, errhp, Lob_loc, &isOpen)); if (isOpen) { printf(" Lob is Open\n"); /* ... Processing given that the LOB has already been Opened */ } else { printf(" Lob is not Open\n"); /* ... Processing given that the LOB has not been Opened */ } /* Free resources held by the locators*/ (void) OCIDescriptorFree((dvoid *) Lob_loc, (ub4) OCI_DTYPE_LOB); return; }
Oracle C++ Call Interface (OCCI) is a C++ API for manipulating data in an Oracle database. OCCI is organized as an easy-to-use collection of C++ classes which enable a C++ program to connect to a database, execute SQL statements, insert/update values in database tables, retrieve results of a query, execute stored procedures in the database, and access metadata of database schema objects. OCCI also provides a seamless interface to manipulate objects of user-defined types as C++ class instances.
Oracle C++ Call Interface (OCCI) is designed so that you can use OCI and OCCI together to build applications.
The OCCI API provides the following advantages over JDBC and ODBC:
You can use Oracle C++ Call Interface (OCCI) to make changes to an entire internal LOB, or to pieces of the beginning, middle, or end of it, as follows:
Unlike OCI which uses a common API for operations on BLOBs, CLOBs, and BFILEs, OCCI has distinct classes for each LOB types, as follows:
OCCIClob Class to access and modify data stored in internal CLOBs and NCLOBsThe OCCIClob driver implements a CLOB object using an SQL locator(CLOB). This means that a CLOB object contains a logical pointer to the SQL CLOB data rather than the data itself.
The CLOB interface provides methods for getting the length of an SQL CLOB (Character Large Object) value, for materializing a CLOB value on the client, and getting a substring. Methods in the interfaces ResultSet statement such as getClob() and setClob() allow you to access SQL CLOB values.
Methods in the OCCIResultSet and OCCIStatement interfaces, such as getBlob() and setBlob(), allow you to access SQL BLOB values. The OCCIBlob interface provides methods for getting the length of a SQL BLOB value, for materializing a BLOB value on the client, and for extracting a part of the BLOB.
These methods are listed in the following tables.
See Also:
|
In OCCI, for fixed-width client-side character sets, the following rules apply:
The following rules apply only to varying-width client-side character sets:
CLOBs and NCLOBs:
CLOBs and NCLOBs:
For all other OCCI LOB operations, irrespective of the client-side character set, the amount parameter is in characters for CLOBs and NCLOBs. These include the following:
OCCIClob.copy()OCCIClob.erase()OCCIClob.trim()OCCICLob.copy()All these operations refer to the amount of LOB data on the server.
NCLOBs parameters are allowed in methodsNCLOBs parameters are not allowed as attributes in object typesThe LoadFromFile functionality in OCCI is provided though the OCCIClob.copy() and OCCIBlob.copy() methods. These take an OCCIBfile argument.
You cannot specify amounts larger than the length of the BFILE. You must specify an amount parameter less than length of the BFILE.
When reading in OCCIClobs, OCCIBlobs, and OCCIBfiles, specify the amount = 4 gigabytes-1, and it will read to the end of the LOB.
See Also:
|
OCCI methods that operate on BLOBs, CLOBs, NCLOBs, and BFILEs are as follows:
| Function/Procedure | Description |
|---|---|
|
OCCIBlob.append() |
Appends C |