// Pro*C/C++ sample program demonstrating a simple use of Cursor Variables
// implemented within a C++ class framework. Build this program as follows
//
// 1. Execute the cppdemo2.sql script within SQL*Plus
// 2. Precompile the empclass.pc program as follows
// > proc code=cpp sqlcheck=full user=scott/tiger lines=yes empclass
// 3. Precompile the cppdemo2.pc program as follows
// > proc code=cpp lines=yes cppdemo2
// 4. Compile and Link
//
// Note that you may have to specify various include directories using the
// include option when precompiling.
while (1)
{
// Fetch from the Cursor, catching the NOT FOUND condition
// thrown by the fetch() member function.
try { e->fetch(); } catch (int code)
{ if (code == 1403) break; }
printf("Employee: %s[%d]\n", e->ename, e->empno);
}
// This example uses a single (global) SQLCA that is shared by the
// emp class implementation as well as the main program for this
// application.
#define SQLCA_STORAGE_CLASS extern
#include <sqlca.h>
// Include the emp class specification in the implementation of the
// class body as well as the application program that makes use of it.
EXEC SQL INCLUDE empclass.h;
emp::emp()
{
// The scope of this WHENEVER statement spans the entire module.
// Note that the error handler function is really a member function
// of the emp class.
EXEC SQL WHENEVER SQLERROR DO emp_error();
EXEC SQL ALLOCATE :emp_cursor; // Constructor - ALLOCATE Cursor.
}
void emp::fetch() throw (int)
{
EXEC SQL FETCH :emp_cursor INTO :ename, :empno;
if (sqlca.sqlcode == 1403)
throw sqlca.sqlcode; // Like a WHENEVER NOT FOUND statement.
}
Rem This is the SQL script that accompanies the cppdemo2 C++ Demo
Rem Program. Run this prior to Precompiling the empclass.pc file.
/
CONNECT SCOTT/TIGER
/
CREATE OR REPLACE VIEW emp_view AS SELECT ename, empno FROM EMP
/
CREATE OR REPLACE PACKAGE emp_package AS
TYPE emp_cursor_type IS REF CURSOR RETURN emp_view%ROWTYPE;
PROCEDURE open_cursor(curs IN OUT emp_cursor_type);
END emp_package;
/
CREATE OR REPLACE PACKAGE BODY emp_package AS
PROCEDURE open_cursor(curs IN OUT emp_cursor_type) IS
BEGIN
OPEN curs FOR SELECT ename, empno FROM EMP ORDER BY ename ASC;
END;
END emp_package;
/
EXIT
/