#include #include #define USERNAME "TEMP" #define PASSWORD "SECRET" #ifndef MSDOS extern char *malloc(); #else extern unsigned char *malloc(); #endif char *nullstring=" "; EXEC SQL BEGIN DECLARE SECTION; char *username = USERNAME; char *password = PASSWORD; VARCHAR statement[240]; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE sqlca; EXEC SQL INCLUDE sqlda; SQLDA *bind_dp; SQLDA *select_dp; extern SQLDA *sqlald(); extern void sqlprc(); int precision, scale; extern void sqlnul(); short null_ok; short *indicator; main() { int i,j,k,l; char variable[30]; char apple[45]; char parrot[50]; EXEC SQL WHENEVER SQLERROR GOTO sqlerror; EXEC SQL CONNECT :username IDENTIFIED BY :password; bind_dp = sqlald (40, 30, 30); bind_dp->N = 40; select_dp = sqlald (40, 30, 30); select_dp->N = 40; printf("\n\nEnter SQL statement: "); gets (statement.arr); statement.len = strlen(statement.arr); EXEC SQL PREPARE S FROM :statement; EXEC SQL DECLARE C CURSOR FOR S; EXEC SQL DESCRIBE BIND VARIABLES FOR S INTO bind_dp; if (bind_dp->F < 0) { printf ("\nToo many bind variables for descriptor."); goto sqlerror; } bind_dp->N = bind_dp->F; for (i=0; iF; i++) { printf("\nEnter value for bind variable %.*s\n? ", (int)bind_dp->C[i], bind_dp->S[i]); gets(variable); bind_dp->L[i] = strlen (variable); bind_dp->V[i] = malloc(bind_dp->L[i] + 1); strcpy (bind_dp->V[i], variable); bind_dp->I[i] = 0; bind_dp->T[i] = 1; } EXEC SQL OPEN C USING DESCRIPTOR bind_dp; if ((strncmp(statement.arr, "SELECT", 6) != 0) && (strncmp(statement.arr, "select", 6) != 0)) goto end_main; EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp; if (select_dp->F < 0) { printf("\nToo many select-list variables for descriptor: %d", -(select_dp->F)); goto sqlerror; } select_dp->N = select_dp->F; for (i=0; iF; i++) { sqlnul (&(select_dp->T[i]), &(select_dp->T[i]), &null_ok); switch (select_dp->T[i]) { case 1 : break; case 2 : sqlprc (&(select_dp->L[i]), &precision, &scale); if (precision == 0) precision = 40; select_dp->L[i] = precision + 2; if (scale < 0) select_dp->L[i] += -scale; break; case 8 : select_dp->L[i] = 240; break; case 12 : select_dp->L[i] = 9; break; case 23 : break; case 24 : select_dp->L[i] = 240; break; } select_dp->V[i] = malloc(select_dp->L[i]); #ifndef MSDOS select_dp->I[i] = (short *)malloc(sizeof(short)); #else select_dp->I[i] = (unsigned short *)malloc(sizeof (short)); #endif strncpy(parrot,select_dp->S[i],30); if (select_dp->T[i] == 2) { /* erase blanks ending parrot */ while (parrot[strlen(parrot)-1] == ' ') parrot[strlen(parrot)-1] = '\0'; printf ("%*.*s ", (int)select_dp->L[i], (int)select_dp->L[i], parrot); } else printf("%-*.*s ", (int)select_dp->L[i], (int)select_dp->L[i], parrot); } printf ("\n"); for (i=0; iF; i++) { strcpy(apple,"------------------------------------------"); printf ("%*.*s ", (int)select_dp->L[i], (int)select_dp->L[i], apple); if (select_dp->T[i] != 24) select_dp->T[i] = 1; } printf("\n"); EXEC SQL WHENEVER NOT FOUND GOTO end_main; for (;;) { EXEC SQL FETCH C USING DESCRIPTOR select_dp; for (i=0; iF; i++) { indicator = (short *)select_dp->I[i]; if (*indicator < 0) printf ("%-*.*s ", (int)select_dp->L[i], (int)select_dp->L[i], nullstring); else printf ("%-*.*s ",(int)select_dp->L[i], (int)select_dp->L[i], select_dp->V[i]); } printf("\n"); } end_main: printf ("\n\n# of rows processed = %d\n\n\n", sqlca.sqlerrd[2]); for (i=0; iF; i++) { free (select_dp->V[i]); free (select_dp->I[i]); } sqlclu (bind_dp); sqlclu (select_dp); EXEC SQL CLOSE C; EXEC SQL COMMIT WORK RELEASE; return; sqlerror: printf("\n%.70s",sqlca.sqlerrm.sqlerrmc); EXEC SQL WHENEVER SQLERROR CONTINUE; EXEC SQL ROLLBACK WORK RELEASE; printf("\n\n\n"); return; }