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
/
dcur.sqc
< prev
next >
Wrap
Text File
|
1998-07-27
|
9KB
|
357 lines
/* DCUR.SQC General SQL code for Dcursex example (all platforms)
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE SQLDA;
#include "sqldef.h"
#include "example.h"
#define MAX_COL_WIDTH 50
extern int PageSize;
static char TableName[MAX_TABLE_NAME];
static SQLDA _fd_ *SqlDA;
#ifdef _SQL_OS_NETWARE
#define Stringncopy(x,y,z) strncpy(x,y,z)
#define Stringcat(x,y) strcat(x,y)
#else
#ifdef __SMALLDATA__
#define Stringncopy(x,y,z) _fstrncpy(x,y,z)
#define Stringcat(x,y) _fstrcat(x,y)
#else
#define Stringncopy(x,y,z) strncpy(x,y,z)
#define Stringcat(x,y) strcat(x,y)
#endif
#endif
EXEC SQL BEGIN DECLARE SECTION;
static char CursName[ 20 ];
static a_sql_statement_number stat;
EXEC SQL END DECLARE SECTION;
static void printSQLError()
{
char buffer[ 200 ];
Displaytext( 0, "SQL error -- %s" MY_NEWLINE_STR,
sqlerror_message( &sqlca, buffer, sizeof( buffer ) ) );
}
static int warning( char *msg )
{
if( SQLCODE == SQLE_NOTFOUND ) {
if( SQLCOUNT >= 0 ) {
Displaytext( 0, "Not found - past bottom of table" MY_NEWLINE_STR );
} else {
Displaytext( 0, "Not found - past top of table" MY_NEWLINE_STR );
}
} else {
Displaytext( 0,
"Unexpected warning %ld -- %s" MY_NEWLINE_STR, SQLCODE, msg );
}
return( TRUE );
}
EXEC SQL WHENEVER SQLERROR { printSQLError(); return( FALSE ); };
static int open_cursor()
{
EXEC SQL BEGIN DECLARE SECTION;
char buff[ 100 ];
EXEC SQL END DECLARE SECTION;
int n;
sprintf( buff, "select * from %s", TableName );
/* Note that database fills in statement number on prepare */
EXEC SQL PREPARE :stat FROM :buff;
/* Note that we must initialize the cursor name */
strcpy( CursName, "table_cursor" );
EXEC SQL DECLARE :CursName CURSOR FOR :stat;
EXEC SQL OPEN :CursName;
SqlDA = alloc_sqlda( 3 );
EXEC SQL DESCRIBE :stat INTO SqlDA;
if( SqlDA->sqld > SqlDA->sqln ) {
n = SqlDA->sqld;
free_sqlda( SqlDA );
SqlDA = alloc_sqlda( n );
EXEC SQL DESCRIBE :stat INTO SqlDA;
}
fill_s_sqlda( SqlDA, 1000 );
return( TRUE );
}
static int close_cursor()
{
EXEC SQL CLOSE :CursName;
free_filled_sqlda( SqlDA );
SqlDA = NULL;
EXEC SQL DROP STATEMENT :stat;
return( TRUE );
}
static int fetch_row()
{
EXEC SQL FETCH RELATIVE 1 :CursName USING DESCRIPTOR SqlDA;
if( SQLCODE < 0 || SQLCODE == SQLE_NOTFOUND ) {
warning( "Fetching" );
return( FALSE );
} else if( SQLCODE != 0 ) {
warning( "Fetching" );
return( TRUE );
} else {
return( TRUE );
}
}
static int move(
EXEC SQL BEGIN DECLARE SECTION;
int relpos
EXEC SQL END DECLARE SECTION;
)
{
EXEC SQL FETCH RELATIVE :relpos :CursName;
if( SQLCODE == SQLE_NOTFOUND && SQLCOUNT == 0 ) {
} else if( SQLCODE ) {
warning( "Moving" );
return( FALSE );
}
return( TRUE );
}
static int top()
{
EXEC SQL FETCH ABSOLUTE 0 :CursName;
return( TRUE );
}
static int bottom()
{
EXEC SQL FETCH ABSOLUTE -1 :CursName;
return( TRUE );
}
static void help()
{
Displaytext( 0, "DCursex Demonstration Program Commands:" MY_NEWLINE_STR );
Displaytext( 0, "p - Print current page" MY_NEWLINE_STR );
Displaytext( 0, "u - Move up a page" MY_NEWLINE_STR );
Displaytext( 0, "d - Move down a page" MY_NEWLINE_STR );
Displaytext( 0, "b - Move to bottom page" MY_NEWLINE_STR );
Displaytext( 0, "t - Move to top page" MY_NEWLINE_STR );
Displaytext( 0, "i - Insert a new row" MY_NEWLINE_STR );
Displaytext( 0, "n - New table" MY_NEWLINE_STR );
Displaytext( 0, "c - Commit changes and close cursor" MY_NEWLINE_STR );
Displaytext( 0, "q - Quit" MY_NEWLINE_STR );
Displaytext( 0, "h - Help (this screen)" MY_NEWLINE_STR );
}
static int col_width( SQLDA _fd_ *da, int col )
{
int col_name_len;
int data_len;
SQLDA_VARIABLE _fd_ *sqlvar;
sqlvar = &da->sqlvar[ col ];
col_name_len = sqlvar->sqlname.length;
data_len = sqlvar->sqllen;
if( data_len > col_name_len ) {
col_name_len = data_len;
}
if( strlen( NULL_TEXT ) > col_name_len ) {
col_name_len = strlen( NULL_TEXT );
}
if( col_name_len > MAX_COL_WIDTH ) {
return( MAX_COL_WIDTH );
}
return( col_name_len );
}
static void print_headings( SQLDA _fd_ *da )
{
int i;
int width;
int total;
char colname[ SQL_MAX_NAME_LEN + 1 ];
char _fd_ *sqlname;
total = 0;
for( i = 0; i < da->sqld; ++i ) {
width = col_width( da, i );
sqlname = da->sqlvar[ i ].sqlname.data;
Stringncopy( colname, sqlname,
da->sqlvar[ i ].sqlname.length );
colname[ da->sqlvar[ i ].sqlname.length ] = '\0';
Displaytext( total, "%-*.*s", width, width, colname );
total += width+1;
}
Displaytext( 0, MY_NEWLINE_STR );
}
static void print_data( SQLDA _fd_ *da )
{
int i;
int width;
int total;
SQLDA_VARIABLE _fd_ *sqlvar;
char _sqldafar *data;
total = 0;
for( i = 0; i < da->sqld; ++i ) {
width = col_width( da, i );
sqlvar = &da->sqlvar[ i ];
if( *( sqlvar->sqlind ) < 0 ) {
data = NULL_TEXT;
} else {
data = (char _sqldafar *)sqlvar->sqldata;
}
#if _sqlfar_isfar
Displaytext( total, "%-*.*Fs", width, width,
(char far *)data );
#else
Displaytext( total, "%-*.*s", width, width, data );
#endif
total += width+1;
}
Displaytext( 0, MY_NEWLINE_STR );
}
static void print()
{
int i;
if( SqlDA == NULL ) {
Displaytext( 0, "*** Error: Cursor not open" MY_NEWLINE_STR );
return;
}
print_headings( SqlDA );
for( i = 0; i < PageSize; ) {
++i;
if( fetch_row() ) {
print_data( SqlDA );
} else {
break;
}
}
move( -i );
}
static int insert()
{
char prompt[ 80 ];
char newvalue[ 80 ];
char _sqldafar *data;
int i;
for( i = 0; i < SqlDA->sqld; i++ ) {
strcpy( prompt, "Enter a value for '" );
Stringcat( prompt, SqlDA->sqlvar[i].sqlname.data);
strcat( prompt, "'" );
data = (char _sqldafar *) SqlDA->sqlvar[i].sqldata;
GetValue( prompt, newvalue, 80 );
if( newvalue[0] == '\0'
&& (SqlDA->sqlvar[i].sqltype & DT_NULLS_ALLOWED) != 0 ) {
*SqlDA->sqlvar[i].sqlind = -1;
} else {
*SqlDA->sqlvar[i].sqlind = 0;
Stringncopy( data, newvalue, SqlDA->sqlvar[i].sqllen );
}
}
EXEC SQL PUT :CursName USING DESCRIPTOR SqlDA;
return( TRUE );
}
extern int WSQLEX_Init()
{
char parmstr[ 251 ];
if( !db_init( &sqlca ) ) {
Display_systemerror(
"Unable to initialize database interface" MY_NEWLINE_STR );
return( FALSE );
}
GetValue( "Enter connection string", parmstr, 250 );
if( strlen( parmstr ) == 0 ) {
strcpy( parmstr,
"UID=dba;PWD=sql;DBF=c:\\sybase\\asa6\\asademo.db;ENG=asademo" );
}
db_string_connect( &sqlca, parmstr );
if( SQLCODE != SQLE_NOERROR ) {
printSQLError();
db_fini( &sqlca );
return( FALSE );
}
GetTableName( TableName, MAX_TABLE_NAME );
open_cursor();
help();
return( TRUE );
}
static int commit_and_close( void )
/*********************************/
{
EXEC SQL COMMIT;
close_cursor();
return( TRUE );
}
extern void WSQLEX_Process_Command( int selection )
{
switch( tolower( selection ) ) {
case 'c': commit_and_close();
break;
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;
case 'n': close_cursor();
GetTableName( TableName, MAX_TABLE_NAME );
open_cursor();
break;
case 'i': insert();
break;
default: Displaytext( 0,
"Invalid command, press 'h' for help" MY_NEWLINE_STR );
}
}
extern int WSQLEX_Finish()
{
close_cursor();
EXEC SQL ROLLBACK WORK;
EXEC SQL DISCONNECT;
db_fini( &sqlca );
return( TRUE );
}