/*cppdemo2.pc*/// 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.#include <stdio.h>
#include <stdlib.h>
#include <sqlca.h>static void sql_error()
{
printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}// Physically include the emp class definition in this module.
EXEC SQL INCLUDE empclass.h;int main()
{
EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger";
EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR DO sql_error();
EXEC SQL CONNECT :uid; emp *e = new emp(); // Invoke Constructor - ALLOCATE Cursor Variable. e->open(); // Open the Cursor. 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);
} e->close(); // Close the Cursor. delete e; // Invoke Destructor - FREE Cursor Variable. EXEC SQL ROLLBACK WORK RELEASE;
return (0);
}
/*empclass.pc*/#include <stdio.h>
#include <stdlib.h>// 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.
}emp::~emp()
{
EXEC SQL FREE :emp_cursor; // Destructor - FREE Cursor.
}void emp::open()
{
EXEC SQL EXECUTE
BEGIN
emp_package.open_cursor(:emp_cursor);
END;
END-EXEC;
}void emp::close()
{
EXEC SQL CLOSE :emp_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.
}void emp::emp_error()
{
printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}/*cppdemo.sql*/
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
/
// 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.#include <stdio.h>
#include <stdlib.h>
#include <sqlca.h>static void sql_error()
{
printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}// Physically include the emp class definition in this module.
EXEC SQL INCLUDE empclass.h;int main()
{
EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger";
EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR DO sql_error();
EXEC SQL CONNECT :uid; emp *e = new emp(); // Invoke Constructor - ALLOCATE Cursor Variable. e->open(); // Open the Cursor. 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);
} e->close(); // Close the Cursor. delete e; // Invoke Destructor - FREE Cursor Variable. EXEC SQL ROLLBACK WORK RELEASE;
return (0);
}
/*empclass.pc*/#include <stdio.h>
#include <stdlib.h>// 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.
}emp::~emp()
{
EXEC SQL FREE :emp_cursor; // Destructor - FREE Cursor.
}void emp::open()
{
EXEC SQL EXECUTE
BEGIN
emp_package.open_cursor(:emp_cursor);
END;
END-EXEC;
}void emp::close()
{
EXEC SQL CLOSE :emp_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.
}void emp::emp_error()
{
printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}/*cppdemo.sql*/
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
/
请你好好看一下。
如果有不明白的地方,请给我写信。
[email protected]