Pro*C/C++ Precompiler Getting Started
Release 8.0.5 for Windows NT and Windows 95/98 A64423-01 |
|
This chapter describes how to build Oracle database applications
with Pro*C/C++ using the sample programs that are included with this release,
and provides an overview of how to build multi-threaded applications.
Specific topics discussed are:
When you install Pro*C/C++, Oracle Installer copies a set of sample programs to the ORACLE_HOME\PRO80\C\SAMPLES directory. Oracle recommends that you build and run these sample programs to verify that Pro*C/C++ has been installed successfully and operates correctly. You can delete the programs after you use them.
Note: The Pro*C/C++ Precompiler Programmer's Guide
indicates that the sample programs are located in a directory named DEMO.
In Pro*C/C++ for Windows NT and Windows 95/98, the sample programs are located in the directory
named SAMPLES.
|
.DSP and .IDE files help you build application programs.
Sample .DSP files in this release were created with Microsoft
Visual C++ version 5.0. Sample .IDE files in this release were created
with Borland C++ version 5.0.
When built, the sample programs that Oracle provides in Pro*C/C++
produce .EXE executables. For some sample programs, you must run the SQL
script(s) in the sample directory before you precompile and run the sample
program. The SQL scripts set up the correct tables and data so that the
sample programs run correctly.
Also, some samples require the Object Type Translator (OTT) to be executed before precompilation. Use OTT to create header files with C-struct representations of Abstract Data Types that have been created and stored in an Oracle8 database.
Additional Information: For more information on OTT, see Chapter
16 of the Pro*C/C++ Precompiler Programmer's
Guide.
|
The sample programs are described below.
Fetches census information for California counties. This
program demonstrates a number of ways to navigate through collection-typed
database columns.
Prompts the user for an employee number, then queries the
EMP table for the employee's name, salary, and commission. It uses indicator
variables (in an indicator struct) to determine whether the commission
is NULL.
Retrieves the names of all employees in a given department
from the EMP table (Dynamic SQL Method 3).
An example of C++ inheritance, this program finds all salespeople
and prints their names and total earnings (including commissions).
Fetches data from the EMP table, using a cursor variable.
The cursor is opened in the stored PL/SQL procedure open_cur, in the EMP_DEMO_PKG
package.
Fetches and adds crime records to the database based on the
person's Social Security number. This program demonstrates the mechanisms
for accessing and storing large objects (LOBs) to tables and manipulating
LOBs through the stored procedures available via the DBMS_LOB package.
Shows how to use threading in conjunction with precompilers.
The program creates as many sessions as there are threads.
Demonstrates navigational access to objects in the object
cache.
Demonstrates the use of objects. This program manipulates
the object types "person" and "address".
Demonstrates how to use the ORACA to determine various performance
parameters at runtime.
Demonstrates the use of embedded PL/SQL blocks. This program
prompts you for an employee name that already resides in a database. It
then executes a PL/SQL block, which returns the results of four SELECT
statements.
Adds new employee records to the personnel database and checks
database integrity. The employee numbers in the database are automatically
selected using the current maximum employee number +10.
Logs on to an Oracle database, prompts the user for an employee
number, queries the database for the employee's name, salary, and commission,
and displays the result. The program continues until you enter 0 as the
employee number.
Logs on to an Oracle database, declares and opens a cursor,
fetches the names, salaries, and commissions of all salespeople, displays
the results, and closes the cursor.
Logs on to an Oracle database, declares and opens a cursor,
fetches in batches using arrays, and prints the results using the print_rows()
function.
Demonstrates the use of type equivalencing using the LONG
RAW external datatype.
Prompts the user for an account number and a debit amount.
The program verifies that the account number is valid and that there are
sufficient funds to cover the withdrawal before it debits the account.
Creates a table, inserts a row, commits the insert, and drops
the table (Dynamic SQL Method 1).
Inserts two rows into the EMP table and deletes them (Dynamic
SQL Method 2).
Retrieves the names of all employees in a given department
from the EMP table (Dynamic SQL Method 3).
Connects to an Oracle database using the SCOTT/TIGER account.
The program declares several host arrays and calls a PL/SQL stored procedure
(GET_EMPLOYEES in the CALLDEMO package). The PL/SQL procedure returns up
to ASIZE values. The program keeps calling GET_EMPLOYEES, getting ASIZE
arrays each time, and printing the values, until all rows have been retrieved.
Connects to an Oracle database using your user name and password
and prompts for a SQL statement. You can enter any legal SQL statement,
but you must use regular SQL syntax, not embedded SQL. Your statement is
processed. If the statement is a query, the rows fetched are displayed
(Dynamic SQL Method 4).
Fetches from the EMP table, using a cursor variable. The
cursor is opened in the stored PL/SQL procedure open_cur, in the EMP_DEMO_PKG
package.
This is a very simple program that demonstrates how to do
array fetches using dynamic SQL Method 4.
Demonstrates how you can use the sqlvcp() function
to determine the actual size of a VARCHAR struct. The size is then used
as an offset to increment a pointer that steps through an array of VARCHARs.
This program also demonstrates how to use the sqlgls()
function to retrieve the text of the last SQL statement that was executed.
This GUI version of the SAMPLE.PC program adds new employee
records to the personnel database and checks database integrity. You can
enter as many employee names as you want and perform the SQL commands by
selecting the appropriate buttons in the Employee Record box.
To precompile, compile, and link a sample program:
Note: All of the sample projects were created with C as the default
drive.
|
If you use Borland C++, some samples require SQL scripts and OTT to be executed individually before precompilation.
sqlplus @script.sql
where script is the name of a SQL script for a sample. SQL scripts for samples can be found in the corresponding sample directory. See the table earlier in this chapter for the name of the SQL script that needs to be executed for a particular sample, if any.
ott intype=demo.typ outtype=demo_o.typ hfile=demo.h code=c userid=scott/tiger
where demo is objdemo1, for example. When precompiling samples that require OTT, be sure to set intype=demo_o.typ, parse=full, and include directory to the location of demo.h.
Additional Information: See Chapter 16 of the Pro*C/C++
Precompiler Programmer's Guide for more information about OTT.
|
The Pro*C/C++ precompile environment appears with the project file (for example, WINSAM.PRE) already loaded.
To run a sample program (in this case, WINSAM) after building it:
A dialog box appears notifying you whether the connection is successful.
An Employee Record dialog box appears. You can use SQL buttons such as SELECT, FETCH, or INSERT to manipulate the sample tables.
A dialog box notifies you whether the disconnect is successful.
Note: All sample programs are written to be run on the local database.
To connect to a remote database, use the LOCAL variable in the registry.
|
To run the Pro*C/C++ sample programs, you must have a database account with the user name SCOTT and the password TIGER and a database with the demonstration tables EMP and DEPT. This account is included in the seed database (named ORACLE) that is included with your Oracle server. If no such account exists on your database, install one before running the sample programs.
Pro*C/C++ application program interface (API) calls are implemented
in DLL files provided with your Pro*C/C++ software. To use the DLLs, you
must link your application with the import libraries (.LIB files) that
correspond to the Pro*C/C++ DLLs. Also, you must ensure that the DLL files
are installed on the workstation that is running your Pro*C/C++ application.
Applications linked with DLLs provide the following benefits over static libraries:
Microsoft provides you with three libraries: LIBC.LIB, LIBCMT.LIB,
and MSVCRT.LIB. The Oracle DLLs use the MSVCRT.LIB runtime library. You
must link with MSVCRT.LIB rather than the other two Microsoft libraries.
Build multi-threaded applications if you are planning to
perform concurrent database operations.
Windows NT and Windows 95/98 schedule and allocate threads belonging to processes. A thread is a path of a program's execution. It consists of a kernel stack, the state of the CPU registers, a thread environment block, and a users stack. Each thread shares the resources of a process. Multi-threaded applications use the resources of a process to coordinate the activities of individual threads.
Additional
Information:
See the Pro*C/C++ Precompiler
Programmer's Guide for additional information on how to write multi-threaded
applications with Pro*C/C++.
|
When building a multi-threaded application, make sure that
your C/C++ code is reentrant. This means that access to static or global
data must be restricted to one thread at a time. If you mix multi-threaded
and non-reentrant functions, one thread can modify information that is
required by another thread.
The Pro*C/C++ precompiler automatically creates variables
on the local stack of the thread. This ensures that each thread using the
Pro*C/C++ function has access to a unique set of variables and is reentrant.