home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 December
/
PCWorld_1998-12_cd.iso
/
software
/
sybase
/
ASA
/
asa60.exe
/
data1.cab
/
cxmp_files
/
cur.sqc
next >
Wrap
Text File
|
1998-07-27
|
4KB
|
189 lines
/* CUR.SQC General SQL code for Cursex example (all platforms)
*/
#include <stdio.h>
#include <ctype.h>
EXEC SQL INCLUDE SQLCA;
#include "sqldef.h"
#include "example.h"
extern int PageSize;
typedef struct an_employee {
unsigned long emp_id;
char name[ 41 ];
char sex[ 2 ]; /* M or F plus null char */
char birthdate[ 15 ];
} an_employee;
static void printSQLError()
{
char buffer[ 200 ];
Displaytext( 0, "SQL error -- %s\n",
sqlerror_message( &sqlca, buffer, sizeof( buffer ) ) );
}
EXEC SQL WHENEVER SQLERROR { printSQLError(); return( FALSE ); };
static int connect()
{
EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";
return( TRUE );
/* errors will return FALSE: - see WHENEVER above */
}
static int release()
{
EXEC SQL ROLLBACK WORK;
EXEC SQL DISCONNECT;
db_fini( &sqlca );
return( TRUE );
}
static int open_cursor()
{
EXEC SQL DECLARE C1 CURSOR FOR
SELECT emp_id, emp_fname || ' ' || emp_lname, sex, birth_date
FROM "dba".employee;
EXEC SQL OPEN C1;
return( TRUE );
}
static int close_cursor()
{
EXEC SQL CLOSE C1;
return( TRUE );
}
static int fetch_row(
EXEC SQL BEGIN DECLARE SECTION;
unsigned long *emp_id,
char *name,
char *sex,
char *birthdate
EXEC SQL END DECLARE SECTION;
)
{
EXEC SQL FETCH RELATIVE 1 C1
INTO :emp_id, :name, :sex, :birthdate;
if( SQLCODE ) {
return( FALSE );
} else {
return( TRUE );
}
}
static int move(
EXEC SQL BEGIN DECLARE SECTION;
int relpos
EXEC SQL END DECLARE SECTION;
)
{
EXEC SQL FETCH RELATIVE :relpos C1;
return( TRUE );
}
static int top()
{
EXEC SQL FETCH ABSOLUTE 0 C1;
return( TRUE );
}
static int bottom()
{
EXEC SQL FETCH ABSOLUTE -1 C1;
return( TRUE );
}
static void help()
{
Displaytext( 0, "Cursex Demonstration Program Commands:\n" );
Displaytext( 0, "p - Print current page\n" );
Displaytext( 0, "u - Move up a page\n" );
Displaytext( 0, "d - Move down a page\n" );
Displaytext( 0, "b - Move to bottom page\n" );
Displaytext( 0, "t - Move to top page\n" );
Displaytext( 0, "q - Quit\n" );
Displaytext( 0, "h - Help (this screen)\n" );
}
static void print()
{
an_employee s;
int i;
int status;
for( i = 0; i < PageSize; ) {
++i;
status = fetch_row( &s.emp_id, s.name, s.sex, s.birthdate );
if( status ) {
Displaytext( 0, "%6ld", s.emp_id );
Displaytext( 10, "%-30.30s", s.name );
Displaytext( 30, "%-3.3s", s.sex );
Displaytext( 40, "%-15.15s\n", s.birthdate );
} else {
break;
}
}
move( -i );
}
extern int WSQLEX_Init()
{
if( !db_init( &sqlca ) ) {
Display_systemerror(
"Unable to initialize database interface\n" );
return( FALSE );
}
if( !db_find_engine( &sqlca, NULL ) ) {
Display_systemerror( "Database Engine/Station not running" );
return( FALSE );
}
if( !connect() ) {
Display_systemerror( "Could not connect" );
return( FALSE );
}
open_cursor();
help();
return( TRUE );
}
extern void WSQLEX_Process_Command( int selection )
{
switch( tolower( selection ) ) {
case 'p': print();
break;
case 'u': move( -PageSize );
print();
break;
case 'd': move( PageSize );
print();
break;
case 't': top();
print();
break;
case 'b': bottom();
move( -PageSize );
print();
break;
case 'h': help();
break;
default: Displaytext( 0, "Invalid command, press 'h' for help\n" );
}
}
extern int WSQLEX_Finish()
{
close_cursor();
release();
return( TRUE );
}