/* SQLSRV$DRIVER.C */ /* */ /* Copyright (c) 1990,1996 by Oracle Corporation. All rights reserved. */ /* */ /* This module is part of an application program that demonstrates */ /* Oracle SQL/Services. It is provided for instructional purposes only. */ /* */ /* This module accepts a string from the terminal that contains an SQL */ /* statement and then calls the other module (SQLSRV$DYNAMIC) to process */ /* it. */ /* */ /* You can substitute your own module for this driver. Instead of using */ /* terminal I/O, your module could construct an SQL statement from */ /* parameters passed by a calling module. For example, your module could */ /* parse a non-SQL statement from a front-end system and build an SQL */ /* statement from it. */ /* */ /* However the module generates an SQL statement, it can be passed to a */ /* module similar to SQLSRV$DYNAMIC for processing. */ #include #include #if defined ( SQLSRV_LOCAL_INCLUDES ) #include "sqlsrv.h" /* Oracle SQL/Services structure definitions. */ #else #include /* Oracle SQL/Services structure definitions. */ #endif /* External function prototypes */ int create_association ( int argc, char *argv[], ASSOCIATE_ID *assoc_id, CHARPTR error_buf, int error_buf_len ); int release_association ( ASSOCIATE_ID assoc_id ); int execute_statement ( ASSOCIATE_ID assoc_id, char *sql_statement ); /* Local function prototypes */ static void get_statement ( char *sql_statement, int echo ); static void get_partial ( char *part_stmt, int *end_of_stmt, int echo, char *prompt ); static void trim ( char *string ); void getline ( char *prompt, char *line ); #if defined _WINDOWS || defined WIN32 int main_win( argc, argv ) #else int main( argc, argv ) #endif int argc; char *argv[]; { /* Variables for association */ ASSOCIATE_ID assoc_id; /* Association handle. */ unsigned char error_buf[512]; /* Alternative error buffer. */ /* Other variables */ char sql_statement[1024]; /* SQL statement text */ int sts; /* Returned status */ int echo = 0; /* Echo flag */ /* The definitions of the create_association and release_association */ /* functions are in SQLSRV$DYNAMIC. */ sts = create_association( argc, argv, &assoc_id, (CHARPTR)error_buf, sizeof( error_buf ) ); if (sts != SQL_SUCCESS) return sts; /* Print user instructions once. */ printf(" \n"); printf("Enter any dynamically-executable SQL statement, \n"); printf("continuing it on successive lines.\n"); printf("Terminate each statement with a semicolon.\n"); printf("Built-in commands are: [no]echo, [NO]ECHO, exit, and EXIT\n"); printf(" \n"); while (1) { get_statement(sql_statement, echo); /* these string comparisons are case-sensitive */ if (!strcmp(sql_statement, "echo")) echo = 1; else if (!strcmp(sql_statement, "ECHO")) echo = 1; else if (!strcmp(sql_statement, "noecho")) echo = 0; else if (!strcmp(sql_statement, "NOECHO")) echo = 0; else if (!strcmp(sql_statement, "exit")) break; else if (!strcmp(sql_statement, "EXIT")) break; else execute_statement(assoc_id,sql_statement); } /* while */ release_association(assoc_id); } /* main */ static void get_statement ( char *sql_statement, int echo ) { /* Get SQL statement from user, concatenating partial statements using */ /* one space character as a separator. */ char part_stmt[256]; /* temporaries */ int end_of_stmt = 0; /* flag for end of statement */ char *prompt; /* prompt string */ prompt = "SQL> "; /* init prompt string */ sql_statement[0] = '\0'; /* init statement string */ while (!end_of_stmt) { get_partial(part_stmt,&end_of_stmt,echo,prompt); if (strlen(sql_statement) != 0) strcat(sql_statement," "); /* add seperator character */ if (strlen(part_stmt) != 0) strcat(sql_statement,part_stmt); /* append next line */ if (!end_of_stmt) prompt = "cont> "; /* reset prompt string */ } /* while */ } /* get_statement */ static void get_partial ( char *part_stmt, int *end_of_stmt, int echo, char *prompt ) { /* Get partial statement from user. Accept semicolon as line terminator */ /* and exclamation point as comment line. */ int len; *end_of_stmt = 0; getline(prompt, part_stmt); if (echo) { char buf[132]; sprintf(buf,"%s\n",part_stmt); printf(buf); }; len = strlen(part_stmt); if (len > 0) { trim(&part_stmt[len-1]); /* zap trailing white space */ len = strlen(part_stmt); if (len > 0) { if (part_stmt[0] == '!') /* zap comments */ part_stmt[0] = '\0'; else *end_of_stmt = (part_stmt[len-1] == ';'); if (*end_of_stmt) { part_stmt[len-1] = '\0'; /* zap semicolon */ if (len > 1) trim(&part_stmt[len-2]); /* zap white space */ } /* if */ } /* if */ } /* if */ } /* get_partial */ static void trim ( char *string ) { /* Trim spaces and tabs */ if (*string == ' ' || *string == '\t') { *string = '\0'; trim(--string); } } #if !defined (_WINDOWS) && !defined (WIN32) /* * See module winivp for Windows version of getline. * */ void getline ( char *prompt, char *line ) { #ifdef MPW char input_line [255]; /* max input line size */ printf("%s", prompt); /* display prompt */ gets(input_line); /* accept an input line */ if (strncmp(input_line, prompt, strlen(prompt)) != 0) strcpy(line, input_line); /* Copy full line */ else strcpy(line, input_line + strlen(prompt)); /* Copy I/P w/out prompt */ #else printf( prompt ); if (gets( line ) == NULL) strcpy(line, "exit;"); #endif } #endif