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
/
odbc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-07-27
|
8KB
|
327 lines
/* ODBCEX.C General ODBC code for Odbcex example (all platforms)
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <stdlib.h>
#include "example.h"
#if defined( __DOS__ )
#if defined( __386__ )
#include "ds32odbc.h"
#else
#include "dosodbc.h"
#endif
#elif defined( __QNX__ )
#include "qnxodbc.h"
#elif defined( __OS2__ )
#include "os2odbc.h"
#elif defined( __WINDOWS_386__ )
#include "winodbc.h"
#elif defined( __NT__ ) || defined( __386__ ) || defined( _M_I386 )
#include "ntodbc.h"
#elif defined( UNIX )
#include "unixodbc.h"
#else
#include "winodbc.h"
#endif
typedef struct a_column {
char *value;
SDWORD size; /* size of value fetched */
char *colname;
int width;
} a_column;
extern int PageSize;
char TableName[ NAME_LEN ];
a_column *Columns = NULL;
SWORD NumColumns;
HENV Environment;
HDBC Connection;
HSTMT Statement;
static int warning( char *msg )
{
Displaytext( 0, "Not found - %s\n", msg );
return( TRUE );
}
static int retcode( RETCODE rcode, HSTMT stmt )
{
UCHAR sqlstate[ 6 ];
UCHAR error_msg[ 100 ];
if( rcode == SQL_SUCCESS || rcode == SQL_SUCCESS_WITH_INFO ) {
return( TRUE );
} else if( rcode == SQL_NO_DATA_FOUND ) {
return( FALSE );
} else {
SQLError( Environment, Connection, stmt, sqlstate, NULL,
error_msg, sizeof( error_msg ), NULL );
Displaytext( 0, "SQL error %s -- %s\n", sqlstate, error_msg );
return( FALSE );
}
}
static void make_columns( HSTMT statement )
{
char colname[ NAME_LEN + 1 ];
SWORD namelen;
UWORD col;
SDWORD size;
retcode( SQLNumResultCols( statement, &NumColumns ), statement );
Columns = (a_column *) calloc( NumColumns, sizeof( a_column ) );
for( col = 0; col < NumColumns; ++col ) {
retcode( SQLColAttributes( statement, (UWORD) (col + 1), SQL_COLUMN_DISPLAY_SIZE,
NULL, 0, NULL, &size ), statement );
if( size > MAX_FETCH_SIZE ) {
size = MAX_FETCH_SIZE;
}
Columns[ col ].width = (int) size;
Columns[ col ].value = (char *) malloc( (int)(size + 1) );
retcode( SQLBindCol( statement, (UWORD) (col + 1), SQL_C_CHAR,
Columns[ col ].value, Columns[ col ].width + 1,
&Columns[ col ].size ), statement );
retcode( SQLColAttributes( statement, (UWORD) (col + 1), SQL_COLUMN_NAME,
colname, NAME_LEN, &namelen, NULL ), statement );
Columns[ col ].colname = (char *) malloc( namelen + 1 );
strcpy( Columns[ col ].colname, colname );
if( Columns[ col ].width < namelen ) {
Columns[ col ].width = namelen;
}
if( Columns[ col ].width < NULL_TEXT_LEN ) {
Columns[ col ].width = NULL_TEXT_LEN;
}
}
}
static void free_columns()
{
int col;
for( col = 0; col < NumColumns; ++col ) {
free( Columns[ col ].value );
free( Columns[ col ].colname );
}
free( Columns );
Columns = NULL;
NumColumns = 0;
}
static int open_cursor()
{
char buff[ 100 ];
if( !retcode( SQLAllocStmt( Connection, &Statement ), NULL ) ) {
return( FALSE );
}
if( !retcode( SQLSetScrollOptions( Statement, SQL_CONCUR_VALUES, SQL_SCROLL_DYNAMIC, 1 ), Statement ) ) {
return( FALSE );
}
sprintf( buff, "select * from %s", TableName );
if( !retcode( SQLPrepare( Statement, (UCHAR *)buff, SQL_NTS ), Statement ) ) {
return( FALSE );
}
SQLSetStmtOption( Statement, SQL_TXN_ISOLATION, 0 );
if( !retcode( SQLExecute( Statement ), Statement ) ) {
return( FALSE );
}
make_columns( Statement );
return( TRUE );
}
static int close_cursor()
{
free_columns();
SQLFreeStmt( Statement, SQL_DROP );
Statement = NULL;
return( TRUE );
}
static int fetch_row()
{
int okay;
UDWORD numfetch;
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_NEXT, 0,
&numfetch, NULL ), Statement );
if( !okay ) return( FALSE );
if( numfetch == 0 ) {
warning( "fetching" );
return( FALSE );
}
return( TRUE );
}
static void move( long relpos )
{
UDWORD numfetch;
int okay;
if( relpos == 0 ) return;
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_RELATIVE,
relpos, &numfetch, NULL ), Statement );
if( !okay ) return;
if( numfetch == 0 ) {
warning( "moving" );
}
}
static int top()
{
UDWORD numfetch;
int okay;
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_FIRST, 0,
&numfetch, NULL ), Statement );
if( okay ) {
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_RELATIVE,
-1, &numfetch, NULL ), Statement );
}
return( okay );
}
static int bottom()
{
UDWORD numfetch;
int okay;
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_LAST, 0,
&numfetch, NULL ), Statement );
return( okay );
}
static void print_headings()
{
int i;
int width;
int total;
total = 0;
for( i = 0; i < NumColumns; ++i ) {
width = Columns[ i ].width;
Displaytext( total, "%-*.*s", width, width, Columns[ i ].colname );
total += width + 1;
}
Displaytext( 0, "\n" );
}
static void print_data()
{
int i;
int width;
int total;
char *data;
total = 0;
for( i = 0; i < NumColumns; ++i ) {
width = Columns[ i ].width;
if( Columns[ i ].size == SQL_NULL_DATA ) {
data = NULL_TEXT;
} else {
data = Columns[ i ].value;
}
Displaytext( total, "%-*.*s", width, width, data );
total += width + 1;
}
Displaytext( 0, "\n" );
}
static void print()
{
int i;
if( Statement == NULL ) {
Displaytext( 0, "*** Error: Cursor not open." );
return;
}
print_headings();
for( i = 0; i < PageSize; ) {
++i;
if( !fetch_row() ) {
break;
}
print_data();
}
move( -i );
}
static void help()
{
Displaytext( 0, "ODBC Cursor 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, "n - New table\n" );
Displaytext( 0, "q - Quit\n" );
Displaytext( 0, "h - Help (this screen)\n" );
}
extern int WSQLEX_Init()
{
if( SQLAllocEnv( &Environment ) != SQL_SUCCESS ) return( FALSE );
if( SQLAllocConnect( Environment, &Connection ) != SQL_SUCCESS ) {
SQLFreeEnv( Environment );
return( FALSE );
}
retcode( SQLConnect( Connection, (UCHAR *)"ASA 6.0 Sample", SQL_NTS,
(UCHAR *)"DBA", SQL_NTS, (UCHAR *)"SQL", SQL_NTS ), NULL );
GetTableName( TableName, MAX_TABLE_NAME );
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;
case 'n': close_cursor();
GetTableName( TableName, MAX_TABLE_NAME);
open_cursor();
break;
default: Displaytext( 0,
"Invalid command, press 'h' for help\n" );
}
}
extern int WSQLEX_Finish()
{
close_cursor();
SQLTransact( Environment, Connection, SQL_ROLLBACK );
SQLDisconnect( Connection );
SQLFreeConnect( Connection );
SQLFreeEnv( Environment );
return( TRUE );
}