Oracle Call Interface Programmer's Guide Release 8.1.5 A67846-01 |
|
This chapter introduces you to the Oracle Call Interface (OCI). It provides background information that you need to develop applications using the OCI. This chapter also introduces special terms that are used in discussing the OCI and describes the changes in the new OCI release. The following topics are covered:
The Oracle Call Interface (OCI) is an application programming interface (API) that allows you to create applications that use the native procedures or function calls of a third-generation language to access an Oracle database server and control all phases of SQL statement execution. OCI supports the datatypes, calling conventions, syntax, and semantics of a number of third-generation languages including C, C++, COBOL and FORTRAN.
OCI provides:
OCI allows you to manipulate data and schemas in an Oracle database using a host programming language, such as C. It provides a library of standard database access and retrieval functions in the form of a dynamic runtime library (OCI library) that can be linked in an application at runtime. This eliminates the need to embed SQL or PL/SQL within 3GL programs.
OCI has many new features that can be categorized into several primary areas:
OCI provides significant advantages over other methods of accessing an Oracle database:
As Figure 1-1 shows, you compile and link an OCI program in the same way that you compile and link a non-database application. There is no need for a separate preprocessing or precompilation step.
Oracle Corporation supports most popular third-party compilers. The details of linking an OCI program vary from system to system. On some platforms, it may be necessary to include other libraries, in addition to the OCI library, to properly link your OCI programs. See your Oracle system-specific documentation and the installation guide for more information about compiling and linking an OCI application for your specific platform.
The OCI encompasses four main sets of functionality:
The Oracle Call Interface (OCI) allows you to develop scalable, multi-threaded applications on multi-tiered architecture that combine the non-procedural data access power of Structured Query Language (SQL) with the procedural capabilities of most programming languages, such as C and C++.
The combination of both non-procedural and procedural language elements in an OCI program provides easy access to an Oracle database in a structured programming environment.
The OCI supports all SQL data definition, data manipulation, query, and transaction control facilities that are available through an Oracle database server. For example, an OCI program can run a query against an Oracle database. The queries can require the program to supply data to the database using input (bind) variables, as follows:
SELECT name FROM employees WHERE empno = :empnumber
In the above SQL statement, :empnumber
is a placeholder for a value that will be supplied by the application.
You can also take advantage of PL/SQL, Oracle's procedural extension to SQL. The applications you develop can be more powerful and flexible than applications written in SQL alone. The OCI also provides facilities for accessing and manipulating objects in an Oracle database server.
With release 8i, the Oracle server has facilities for working with object types and objects. An object type is a user-defined data structure representing an abstraction of a real-world entity. For example, the database might contain a definition of a person
object. That object might have attributes--first_name
, last_name
, and age
--which represent a person's identifying characteristics.
The object type definition serves as the basis for creating objects, which represent instances of the object type. Using the object type as a structural definition, a person
object could be created with the attributes 'John', 'Bonivento', and '30'. Object types may also contain methods--programmatic functions that represent the behavior of that object type.
See Also: For a more detailed explanation of object types and objects, see Oracle8i Concepts, and Oracle8i Application Developer's Guide - Fundamentals.
The Oracle OCI includes functions that extend the capabilities of the OCI to handle objects in an Oracle database server. Specifically, the following capabilities have been added to the OCI:
Additional OCI calls are provided to support manipulation of objects after they have been accessed by way of SQL statements. For a more detailed description of enhancements and new features, refer to "New Features".
One of the main tasks of an OCI application is to process SQL statements. Different types of SQL statements require different processing steps in your program. It is important to take this into account when coding your OCI application. Oracle recognizes several types of SQL statements:
Note: Queries are often classified as DML statements, but OCI applications process queries differently, so they are considered separately here.
Data Definition Language (DDL) statements manage schema objects in the database. DDL statements create new tables, drop old tables, and establish other schema objects. They also control access to schema objects.
The following is an example of creating and specifying access to a table:
CREATE TABLE employees (name VARCHAR2(20), ssn VARCHAR2(12), empno NUMBER(6), mgr NUMBER(6), salary NUMBER(6)) GRANT UPDATE, INSERT, DELETE ON employees TO donna REVOKE UPDATE ON employees FROM jamie
DDL statements also allow you to work with objects in the Oracle database server, as in the following series of statements which creates an object table:
CREATE TYPE person_t AS OBJECT ( name VARCHAR2(30), ssn VARCHAR2(12), address VARCHAR2(50)) CREATE TABLE person_tab OF person_t
OCI applications treat transaction control, session control, and system control statements like DML statements. See the Oracle8i SQL Reference for information about these types of statements.
Data manipulation language (DML) statements can change data in the database tables. For example, DML statements are used to
DML statements can require an application to supply data to the database using input (bind) variables. See the section "Binding" for more information about input bind variables.
DML statements also allow you to work with objects in the Oracle database server, as in the following example, which inserts an instance of type person_t
into the object table person_tab
:
INSERT INTO person_tab VALUES (person_t('Steve May','123-45-6789','146 Winfield Street'))
Queries are statements that retrieve data from a database. A query can return zero, one, or many rows of data. All queries begin with the SQL keyword SELECT, as in the following example:
SELECT dname FROM dept WHERE deptno = 42
Queries access data in tables, and they are often classified with DML statements. However, OCI applications process queries differently, so they are considered separately in this guide.
Queries can require the program to supply data to the database using input (bind) variables, as in the following example:
SELECT name FROM employees WHERE empno = :empnumber
In the above SQL statement, :empnumber
is a placeholder for a value that will be supplied by the application.
When processing a query, an OCI application also needs to define output variables to receive the returned results. In the above statement, you would need to define an output variable to receive any name
values returned from the query.
See Also: See the section "Binding" for more information about input bind variables. See the section "Defining" for information about defining output variables.
See Chapter 4, "SQL Statement Processing", for detailed information about how SQL statements are processed in an OCI program.
PL/SQL is Oracle's procedural extension to the SQL language. PL/SQL processes tasks that are more complicated than simple queries and SQL data manipulation language statements. PL/SQL allows a number of constructs to be grouped into a single block and executed as a unit. Among these are:
You can use PL/SQL blocks in your OCI program to
The following PL/SQL example issues a SQL statement to retrieve values from a table of employees, given a particular employee number. This example also demonstrates the use of placeholders in PL/SQL statements.
BEGIN SELECT ename, sal, comm INTO :emp_name, :salary, :commission FROM emp WHERE ename = :emp_number; END;
Note that the placeholders in this statement are not PL/SQL variables. They represent input values passed to Oracle when the statement is processed. These placeholders need to be bound to C language variables in your program.
See Also: See the PL/SQL User's Guide and Reference for information about coding PL/SQL blocks.
See the section "Binding Placeholders in PL/SQL" for information about working with placeholders in PL/SQL.
The OCI processes SQL statements as text strings, which an application passes to Oracle on execution. The Oracle precompilers (Pro*C/C++, Pro*COBOL, Pro*FORTRAN) allow programmers to embed SQL statements directly into their application code. A separate precompilation step is then necessary to generate an executable application.
It is possible to mix OCI calls and embedded SQL in a precompiler program. Refer to the Pro*C/C++ Precompiler Programmer's Guide for more information.
This guide uses special terms to refer to the different parts of a SQL statement. For example, a SQL statement such as
SELECT customer, address FROM customers WHERE bus_type = 'SOFTWARE' AND sales_volume = :sales
contains the following parts:
SELECT
customer
and address
customers
bus_type
and sales_volume
SOFTWARE
'
:sales
When you develop your OCI application, you call routines that specify to the Oracle database server the address (location) of input and output variables in your program. In this guide, specifying the address of a placeholder variable for data input is called a bind operation. Specifying the address of a variable to receive select-list items is called a define operation.
For PL/SQL, both input and output specifications are called bind operations. These terms and operations are described in Chapter 4, "SQL Statement Processing".
This release of OCI provides the following new features and performance advantages:
Each of these features is discussed in greater detail in later chapters of this guide. See the section "Compatibility, Upgrading, and Migration" for information about new calls that supersede existing routines. See chapters 15, 16, 17, and 18 in Part 3 for listings of OCI calls. Note that new calls, such as "Advanced Queuing and Publish-Subscribe Functions" and "Direct Path Loading Functions", have been added and various existing calls have updated.
All the data structures that are used by OCI are encapsulated in the form of opaque interfaces that are called handles. A handle is an opaque pointer to a storage area allocated by the OCI library that stores context information, connection information, error information, or bind information about a SQL or PL/SQL statement. A client allocates a certain type of handle, populates one or more of those handles through well-defined interfaces, and sends requests to the server using those handles. In turn, applications can access the specific information contained in the handle by using accessor functions. The OCI library manages a hierarchy of handles. Encapsulating the OCI interfaces using these handles has several benefits to the application developer including:
The Oracle OCI provides application developers simplified user authentication and password management in several ways:
The Oracle OCI supports two types of login sessions:
The Oracle OCI has several enhancements to improve application performance and scalability. Application performance has been improved by reducing the number of client to server round trips required and scalability improvements have been facilitated by reducing the amount of state information that needs to be retained on the server side. Some of these features include:
The Oracle OCI provides the most comprehensive application programming interface for programmers seeking to use the Oracle server's object capabilities. These features can be divided into five major categories:
The object cache is a client-side memory buffer that provides lookup and memory management support for objects. It stores and tracks objects instances which have been fetched by an OCI application from the server to the client side. The object cache is created when the OCI environment is initialized. Multiple applications running against the same server will each have their own object cache. The cache tracks the objects which are currently in memory, maintains references to objects, manages automatic object swapping and tracks the meta-attributes or type information about objects. The cache provides the following OCI applications:
Applications using the Oracle OCI can access objects in the Oracle server through several types of interfaces:
The Oracle OCI provides a set of functions with extensions to support object manipulation using SQL SELECT, INSERT, and UPDATE statements. To access Oracle objects these SQL statements use a consistent set of steps as if they were accessing relational tables. The Oracle OCI provides the following sets of functions required to access objects using SQL statements for:
The Oracle OCI also provides a set of functions using a C-style pointer chasing scheme to access objects once they have been fetched into the client-side cache by traversing the corresponding smart pointers or REFs. This navigational interface provides functions for:
The Oracle OCI provides a runtime environment for objects that offers a set of functions for managing how Oracle objects are used on the client-side. These functions provide the necessary functionality for:
The Oracle OCI provides two sets of functions to work with Oracle objects:
Additionally, the OCIDescribeAny() function can provide information about objects stored in the database.
The Object Type Translator (OTT) utility translates schema information about Oracle object types into client-side language bindings. That is, the Oracle OTT translates type information into declarations of host language variables, such as structures and classes. The OTT takes an intype file which contains metadata information about Oracle schema objects (an Oracle data dictionary) and generates an outtype file and the necessary header and implementation files that must be included in a C application that runs against the object schema. Both OCI applications and Pro*C precompiler applications may include code generated by the OTT. The OTT has many benefits including:
OTT is typically invoked from the command line by specifying the intype file, the outtype file and the specific database connection. With Oracle, OTT can only generate C structs which can either be used with OCI programs or with the Pro*C precompiler programs.
The OCI provides an interface to Oracle's Advanced Queueing (AQ) feature. Oracle AQ provides message queuing as an integrated part of the Oracle server. Oracle AQ provides this functionality by integrating the queuing system with the database, thereby creating a message-enabled database. By providing an integrated solution Oracle AQ frees application developers to devote their efforts to their specific business logic rather than having to construct a messaging infrastructure.
For more information about the OCI advanced queueing features, refer to "OCI and Advanced Queuing".
The OCI has been significantly improved with many features. Applications written to work with Oracle OCI release 7 have a smooth migration path to Oracle OCI release 8i due to the interoperability of Oracle OCI release 7 client with Oracle8i and Oracle OCI release 8i client with Oracle7 database server. Specifically:
As a result, customers migrating an existing Oracle OCI release 7 application have the following three alternatives:
Further, if application developers need to use any of the object capabilities of the Oracle8i server, they will need to upgrade their client to use Oracle8i OCI.
The OCI release 8i provides support for applications written with either the 7.x OCI and the 8.x OCI. This section discusses changes in the OCI library routines, issues concerning compatibility between different versions of the OCI and server, as well as issues involved in migrating an application from the release 7.x OCI to the 8.x OCI.
Note: For the most recently updated information about compatibility, upgrading, and migration, refer to the Oracle8i Migration manual.
Release 8.0 of the Oracle Call Interface introduced an entirely new set of functions which were not available in release 7.3. Release 8i adds more new functions. The earlier 7.x calls are still available, but Oracle strongly recommends that existing applications use the new calls to improve performance and provide increased functionality.
Table 1-1, "Obsolescent OCI Routines" lists the 7.x OCI calls with their release 8i equivalents. For more information about the Oracle OCI calls, see the function descriptions in Part III of this guide. For more information about the 7.x calls, see the Programmer's Guide to the Oracle Call Interface, Release 7.3. These 7.x calls are obsoleted, meaning that OCI has replaced them with newer calls. While the obsoleted calls are supported at this time, they may not be supported in all future versions of the OCI.
Note: In many cases the new OCI routines do not map directly onto the 7.x routines, so it may not be possible to simply replace one function call and parameter list with another. Additional program logic may be required before or after the new call is made. See the remaining chapters of this guide for more information.
See Also: For information about the additional functionality provided by new functions not listed here, see the remaining chapters of this guide.
Some OCI routines that were available in previous versions of the OCI are not supported in Oracle8i. They are listed in Table 1-2, "OCI Routines Not Supported"
OCI Routine | Equivalent or Similar 8.x Oracle OCI Routine |
---|---|
obind() |
OCIBindByName(), OCIBindByPos() (Note: additional bind calls may be necessary for some data types) |
obindn() |
OCIBindByName(), OCIBindByPos() (Note: additional bind calls may be necessary for some data types) |
odfinn() |
OCIDefineByPos() (Note: additional define calls may be necessary for some data types) |
odsrbn() |
Note: see odescr() in Table 1-1 |
ologon() |
OCILogon() |
osql() |
Note: see oparse() Table 1-1 |
This section addresses compatibility between different versions of the OCI and Oracle server.
Existing 7.x applications with no new release 8.x OCI calls have two choices:
In either case, the application will work against both Oracle7 and Oracle8i. The application will not be able to use the object features of Oracle8i, and will not get any of the performance or scalability benefits provided by the Oracle OCI release 8i.
New applications written completely in the Oracle OCI will work seamlessly against both Oracle7 and Oracle8i with the following exceptions:
Programmers who wish to incorporate new release 8i functionality into existing OCI applications have two options:
This manual should provide the information necessary to rewrite an existing application to use only new OCI calls.
The following guidelines apply to programmers who want to incorporate new Oracle datatypes and features by using new OCI calls, while keeping 7.x calls for some operations:
Note: See the description of OCIServerAttach() and the description of OCISessionBegin() for information about the logon calls necessary for applications which are maintaining multiple sessions.
Note: If there are multiple service contexts which share the same server handle, only one can be in Oracle7 mode at any time.
This approach allows an application to use a single connection, but two different APIs, to accomplish different tasks.
You can mix and match OCI 7.x and OCI 8i calls within a transaction, but not within a statement. This allows you to execute one SQL or PL/SQL statement with OCI 7.x calls and the next SQL or PL/SQL statement within that transaction with Oracle8i OCI calls.
Warning: You can not open a cursor, and parse with OCI 7.x calls and then execute the statement with OCI 8i calls.
This section discusses issues related to application linking, including the use of non-deferred linking and single-task linking with various OCI versions.
Application developers are cautioned that Oracle plans to desupport non-deferred mode linking beginning with a future release of Oracle. It will continue to be supported with all the releases of Oracle8i. Recognizing these plans, application developers should no longer use non-deferred mode linking in developing new applications. Version 7.3 of the OCI supports two linking modes:
Deferred mode linking therefore significantly reduces the number of round trips between the client and the server and as a result improves the performance and scalability of OCI applications. The default behavior of Oracle7 OCI connected to the Oracle7 server is deferred mode linking. However, Oracle7 OCI also supports non-deferred linking by setting specific link time options.
All the Oracle7 OCI calls are supported with Oracle8i OCI. This means that they will work with a Oracle8i OCI client by relinking the release 8i OCI libraries.The default mode with these calls continues to be deferred mode linking; however, non-deferred mode linking is supported for these calls through all releases of Oracle8i by setting link time options. However, Oracle8i-specific calls use a different paradigm and as a result non-deferred mode linking is not necessary.
The various combinations of client-side libraries and server with which non-deferred linking is currently supported are summarized in the following table:
Oracle will continue to support deferred-mode linking with all the releases of Oracle8i. This has varying implications depending on the version of the OCI library that is used.
Because the Oracle OCI 6.x library is not supported against Oracle8i, applications using the Oracle OCI 6.x library cannot be run against Oracle8i.
Applications using Oracle7 OCI libraries can run in two configurations against an Oracle8i database:
Applications using Oracle release 8i OCI calls, such as those used to access Oracle's object types, do not need to use non-deferred mode linking since the Oracle OCI release 8i uses a different paradigm. Applications using only Oracle7 OCI calls will be able to use non-deferred mode linking but only through release 8.1
Single-task linking is a feature used by a limited number of Oracle's customers, primarily on the OpenVMS platform. Some Oracle platforms support single-task linking, others no longer support it. Application developers are cautioned that Oracle will desupport single task on ALL platforms beginning with the first server release after Oracle8i. Oracle will continue to support single-task linking for all Oracle 8.x releases on those platforms that do support it today. Application developers are referred to the product-line specific documentation to determine whether or not their platform supports single-task linking today.
With single-task linking, Oracle supports two configurations to link Oracle products and user-written applications against the Oracle database:
Oracle will continue to support single-task linking with all the releases of the Oracle server (all 8.* releases) but will desupport it beginning with the first release after Oracle8i. Application developers who would like to use single-task linking to run their applications will not be able to do so against the first server release after Oracle8i.