/* Copyright © 1995, 2002, Oracle Corporation. All Rights Reserved. */ #pragma module SQL_DYNAMIC_DRIVER_I "IDENT" /* **++ ** ABSTRACT: sql_dynamic_driver_i.c ** ** This program is an interactive demo which executes any valid ** SQL statement dynamically. ** ** It is the main driver for a set of Dynamic SQL routines which process ** any valid SQL statement. Associated with this program are ** the following modules: ** ** . sql_dynamic.c : processes statements passed by ** sql_dynamic_driver_i.c ** ** . sql_dynamic_c.sqlmod : SQL module language file ** that contains all the procedures called by ** sql_dynamic_driver_m.c ** **/ /* ** ** INCLUDE FILES ** **/ #include #include #include #include #include /* Declarations */ #define MAX_STMT_SIZE 2048 /* max length of a statement to be processed */ /* Main routine which processes the dynamic SQL statements passed to it. */ extern int sql_dynamic(); void read_from_file (); void call_main_program (); /* ** ** Main Program ** */ main () { char sql_statement[MAX_STMT_SIZE+1]; /* the sql statement input by the user */ char *pstmt = &sql_statement[0]; char * bp; char *null_stmt; /* template for blanking out buffer */ int i, status; char *semicolon; /* Use calloc to initialize a piece of zeroed memory */ null_stmt = (char *) calloc (MAX_STMT_SIZE+1, sizeof(char)); printf ("\nThis program processes valid SQL statements using Dynamic SQL."); sql_statement[0] = ' '; while (*pstmt != 0) { /* Prompt the user for the statement to be executed */ printf ("\n\nEnter the SQL statement to process, terminating your"); printf ("\nstatement with a semicolon <;> < or to exit>:"); printf ("\n\nDynamicSQL> "); bp = gets (pstmt); if (bp == NULL) /* the user entered or to terminate */ exit (EXIT_SUCCESS); if (*pstmt != 0) /* user does not want to exit */ { /* Check for ; to end the statement; else prompt for rest of stmt */ while ((semicolon = strchr(pstmt, ';')) == 0) { /* The gets function replaces the end of the input string * with a \0. Find it now and replace with space and * append the next line to the buffer */ pstmt = strchr (pstmt, 0); *pstmt = ' '; /* fetch into the buffer */ printf ("\ncont> "); bp = gets (pstmt+1); if (bp == NULL) /* the user entered or to terminate */ exit (EXIT_SUCCESS); } if ( sql_statement[0] == '@' ) { char filename[50]; sscanf( &(sql_statement[sql_statement[1]==' ' ? 2 : 1]), "%s", filename); read_from_file(filename); } else call_main_program(sql_statement); strncpy (sql_statement, null_stmt, MAX_STMT_SIZE+1); pstmt = &sql_statement[0]; sql_statement[0] = ' '; } /* end if *pstmt != 0 */ } /* end while */ } /* end main */ void read_from_file (char * fnam) { char sql_statement[MAX_STMT_SIZE+1]; /* the sql statement input by the user */ char *pstmt = &sql_statement[0]; char sql_statement2[MAX_STMT_SIZE+1]; /* the sql statement input by the user */ char *pstmt2 = &sql_statement2[0]; FILE *fid; char *newline; sql_statement[0] = ' '; sql_statement2[0] = ' '; /* Open the file */ if ((fid = fopen(fnam, "r", "dna=.COM")) == 0) { printf("\n\nCould not open '%s' for input.\n\n\7",fnam); exit (EXIT_FAILURE); } while ( fgets(pstmt,sizeof(sql_statement),fid) != 0) { if ((pstmt[0] == '\0') || (pstmt[0] == '\n')) /* blank like terminates input */ break; /* read lines until we get to the terimating ';' */ while (strchr(pstmt, ';') == 0) { newline = strchr(pstmt, '\n'); if (newline != 0) { *newline = ' '; pstmt = newline; } if (fgets(pstmt2,sizeof(sql_statement2),fid) == 0) break; strcat(sql_statement, sql_statement2); memset (sql_statement2, '\0', MAX_STMT_SIZE+1); sql_statement2[0] = ' '; } /* now execute the statement */ call_main_program(sql_statement); memset (sql_statement, '\0', MAX_STMT_SIZE+1); pstmt = &sql_statement[0]; sql_statement[0] = ' '; } fclose(fid); } void call_main_program (char * sqlstatement) { /* * The following "dummy" variables are needed as parameters to the routine * sql_dynamic. They would be needed if re-executing statements * without having to re-PREPARE the statement - a capability of * the sql_dynamic routine. Re-executing statements is not * a feature of this demo program (see instead sql_dynamic_driver_m.C) */ char *dummy_sqlda_in, *dummy_sqlda_out; long dummy_stmt_id; int dummy_flag = 0; int i, status; char *semicolon; printf ("\n\nThe SQL statement to be executed dynamically is:\n %s\n", sqlstatement); /* Some older versions of SQL didn't allow trailing ';' in the string * so mask it out */ semicolon = strchr(sqlstatement, ';'); *semicolon = ' '; dummy_sqlda_in = NULL; dummy_sqlda_out = NULL; dummy_stmt_id = 0; if ((status = sql_dynamic(sqlstatement, &dummy_sqlda_in,&dummy_sqlda_out, &dummy_stmt_id,&dummy_flag)) != 0) printf ("\nError returned from sql_dynamic()"); }