Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
DB Access Module for Oracle OCI User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

2.17 Open SQL and Oracle OCI

The alternative Open SQL interface can be used with Oracle OCI. This section provides some specific details and examples.

2.17.1 Statement Type Hint

An Open SQL statement type, RWDBOSql::StatementType, can be NonQuery (the default), Query, or Procedure. This type can be set as the second parameter on:

Although this type setting is not required by the DB Access Module for Oracle OCI, it is recommended for application code portability.

2.17.2 Placeholder Syntax

The DB Access Module for Oracle OCI uses positional binding for binding program variables to the Oracle library. When input parameter values are provided through binding for RWDBOSql objects, applications must use proper placeholder syntax in their SQL statements. The proper placeholder syntax for host-variable format is:

where anything can be a name or a number. For example, to insert data into a two-column table, named MyTable, through binding, the SQL statement would look like this:

Placeholder syntax is not a portable feature. Different Access Modules use different placeholder syntax.

2.17.3 Supported Data Types for Input Binding

Please see Section 2.15, "Input Binding of Data," for information on the supported data types.

2.17.4 Supported Data Types for Output Binding

All types of the DB Interface Module are supported for output binding, with the exception of RWDBDuration. This type is not supported. Please see Section 3.1.1, "Data Type Classes," of the DB Interface Module User's Guide, for a list of the DB Interface Module data types.

In addition to the DB Interface Module types, Oracle cursors can be bound using the type RWDBNativeType1. See Section 2.17.6 and Section 2.17.7.2 for examples.

Applications must follow the type mapping rules as defined in Table 2 for output binding, and bind compatible types accordingly. For example, a RWDBValue::char type is stored internally as Oracle NUMBER(d) and retrieved as a C++ long. To retrieve the type properly, you must bind a buffer of type RWDBTBuffer<long>, or a compatible type. Trying to retrieve this as an RWDBTBuffer<char> will result in undefined errors.

2.17.5 RWDBDataCallback and Open SQL

The data callback classes can be used with Oracle to insert and fetch data. These classes are useful for very large data that may not fit in memory for a single database call and are described in Section 15.3, "The Data Callback Classes," in the DB Interface Module User's Guide.

Oracle supports sending multiple rows of data for one execution of an insert statement. Retrieving multiple rows of data on each call to RWDBOSql::fetch() is also supported.

2.17.5.1 Piece Size Setting

The DB Access Module for Oracle sets the piece size for inserting or fetching data to 32768. This value represents bytes in the case of binary data, and code units in the case of character data.

The piece size setting can be accessed and modified with the method RWDBOCISystemHandle::piecewiseSize(). Please refer to the class reference description for RWDBOCISystemHandle for more information.

2.17.5.2 RWDBDataCallback::getLength()

When inserting data using data callbacks, Oracle does not require the length of the data being inserted. Therefore, the method RWDBDataCallback::getLength() does not need to be overridden in derived classes.

2.17.5.3 Stored Procedures and RWDBDataCallback

Stored procedure execution is not supported with the data callback classes.

2.17.6 Stored Procedures and Open SQL

Apart from the data callback classes, stored procedures and PL/SQL block can be directly executed on an RWDBOSql object, just like an SQL statement. For cursor parameters, the proper binding type is RWDBNativeType1. Here is an example:

In this example, myStoredProc is the name of the stored procedure, and myOpenSqlObject is an RWDBOSql instance. Note the hint being given as Procedure.

Please see Section 2.17.7.2 for an example that uses RWDBOSql with stored procedures.

2.17.7 Open SQL Examples

This section provides several Open SQL examples.

2.17.7.1 An Open SQL Insert Example

This example shows how to use the Open SQL class RWDBOSql in an Oracle insert operation. The insert is done on a foo table with an int column and a float column.

//1-//2

Creates int and float buffers of size 10; in other words, the buffers hold up to 10 rows of data.

//3

Declares an Oracle-specific SQL syntax for inserting into a foo table. Note the placeholder syntax.

//4

The SQL is associated with the RWDBOSql object.

//5-//6

Associates the buffer with the RWDBOSql object.

//7

The function FillupValues() is assumed to be a user-defined function which places NUMBER_OF_ROWS data entries into the int and float buffers every time it is called and returns true if it is able to do so.

//8

If FillupValues() succeeds, this line executes the inserter and inserts NUMBER_OF_ROWS new rows into the table named foo. If the execution of the SQL statement fails, the error handler associated with the connection is invoked.

//9

Checks the validity of the execution and breaks out of the while loop on failure.

2.17.7.2 An Open SQL Stored Procedure Example

This example illustrates how to execute a stored procedure using an RWDBOSql object. The stored procedure used here is assumed to take one input-output parameter of type int. It returns one result set consisting of character strings. The example assumes an error handler is associated with the connection in use, and doesn't check for any errors after calls to the RWDBOSql object.

//1

Creates a buffer of size 1 and type int for the input-output parameter.

//2

Sets the parameter type of buffer to IN/OUT.

//3

Assigns value to the buffer.

//4

Creates a special buffer to hold the Oracle PL/SQL cursor parameter. The Oracle PL/SQL cursor is the special data type representing the result set. The DB Interface Module provides class RWDBNativeType1 to represent the Oracle PL/SQL cursor type.

//5

Creates a buffer of size NUMBER_OF_ROWS and type RWCString for output values. This stored procedure returns a result set in the form of the reference cursor consisting of a string column.

//6

Associates an SQL statement with the RWDBOSql object. The second argument of the constructor sets the statement type. This information helps optimize binding and performance.

//7

Associates the input buffers with the RWDBOSql object. The second bound-in parameter, cursor, acts as the result set's placeholder.

//8

Associates the output buffer (result in this case) with the openSQL object. We expect only one result set for this stored procedure execution, because there is only one PL/SQL cursor parameter in the given stored procedure. Consequently, this output buffer is bound to the first result set, indicated by index 0.

//9

The SQL statement is executed.

//10

Rows are fetched inside a while loop until all rows are fetched. A successful fetch places the rows in the buffer bound to the openSQL object (result in this case). Note that the number of rows fetched is guaranteed to be less than or equal to the buffer's row size (NUMBER_OF_ROWS in this case).

//11

Fetches the output values of the OUT and IN/OUT parameters by calling fetchReturnParams().

//12

Prints out the fetched output value of the IN/OUT parameter using the bound buffer.

2.17.8 OCIStmts

Each RWDBOSql object corresponds to an Oracle OCI OCIStmt structure. An OCIStmt structure is created and associated with an RWDBOSql object only when the execute() method is invoked on the RWDBOSql object. The OCIStmt structure remains associated until the RWDBOSql object goes out of scope, or an execute() is invoked on the object using a different connection. Invoking execute() on the same connection reuses the same OCIStmt structure.

2.17.8.1 Simultaneous Processing of Results on an RWDBOSql Object When Used for Stored Procedure

Multiple result sets are handled through additional OCIStmts. To simplify usage, Open SQL does not support simultaneous fetching of multiple result sets. You must complete the current result set, by fetching all the data or canceling, in order to move on to the next result set.

2.17.8.2 Simultaneous Processing of Results on High-Level Objects of the DB Interface Module Using the Same Connection

High-level DB Interface Module objects, such as RWDBSelector and RWDBInserter, are internally associated with a unique RWDBOSql object. For this reason, these high-level objects are also restricted from simultaneous processing on the same connection. Also see Section 2.17.8.1.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.