1 /******************************************************************* 2 Sample Program 10: Dynamic SQL Method 4 3 4 This program connects you to ORACLE using your username and 5 password, then prompts you for a SQL statement. You can enter 6 any legal SQL statement. Use regular SQL syntax, not embedded SQL. 7 Your statement will be processed. If it is a query, the rows 8 fetched are displayed. 9 You can enter multiline statements. The limit is 1023 characters. 10 This sample program only processes up to MAX_ITEMS bind variables and 11 MAX_ITEMS select-list items. MAX_ITEMS is #defined to be 40. 12 *******************************************************************/ 13 14 #include <stdio.h> 15 #include <string.h> 16 #include <setjmp.h> 17 #include <sqlda.h> 18 #include <stdlib.h> 19 #include <sqlcpr.h> 20 21 #define SQL_SINGLE_RCTX ((void *)0) 22 23 /* Maximum number of select-list items or bind variables. */ 24 #define MAX_ITEMS 40 25 26 /* Maximum lengths of the _names_ of the 27 select-list items or indicator variables. */ 28 #define MAX_VNAME_LEN 30 29 #define MAX_INAME_LEN 30 30 31 #ifndef NULL 32 #define NULL 0 33 #endif 34 35 /* Prototypes */ 36 #if defined(__STDC__) 37 void sql_error(void); 38 int oracle_connect(void); 39 int alloc_descriptors(int, int, int); 40 int get_dyn_statement(void); 41 void set_bind_variables(void); 42 void process_select_list(void); 43 void help(void); 44 #else 45 void sql_error(/*_ void _*/); 46 int oracle_connect(/*_ void _*/); 47 int alloc_descriptors(/*_ int, int, int _*/); 48 int get_dyn_statement(/* void _*/); 49 void set_bind_variables(/*_ void -*/); 50 void process_select_list(/*_ void _*/); 51 void help(/*_ void _*/); 52 #endif 53 54 char *dml_commands[] = {"SELECT", "select", "INSERT", "insert", 55 "UPDATE", "update", "DELETE", "delete"}; 56 57 EXEC SQL INCLUDE sqlda; 58 EXEC SQL INCLUDE sqlca; 59 60 EXEC SQL BEGIN DECLARE SECTION; 61 char dyn_statement[1024]; 62 EXEC SQL VAR dyn_statement IS STRING(1024); 63 EXEC SQL END DECLARE SECTION; 64 65 SQLDA *bind_dp; 66 SQLDA *select_dp; 67 68 /* Define a buffer to hold longjmp state info. */ 69 jmp_buf jmp_continue; 70 71 /* A global flag for the error routine. */ 72 int parse_flag = 0; 73 74 void main() 75 { 76 int i; 77 78 /* Connect to the database. */ 79 if (oracle_connect() != 0) 80 exit(1); 81 82 /* Allocate memory for the select and bind descriptors. */ 83 if (alloc_descriptors(MAX_ITEMS, MAX_VNAME_LEN, MAX_INAME_LEN) != 0) 84 exit(1); 85 86 /* Process SQL statements. */ 87 for (;;) 88 { 89 (void) setjmp(jmp_continue); 90 91 /* Get the statement. Break on "exit". */ 92 if (get_dyn_statement() != 0) 93 break; 94 95 /* Prepare the statement and declare a cursor. */ 96 EXEC SQL WHENEVER SQLERROR DO sql_error(); 97 98 parse_flag = 1; /* Set a flag for sql_error(). */ 99 EXEC SQL PREPARE S FROM :dyn_statement; 100 parse_flag = 0; /* Unset the flag. */ 101 102 EXEC SQL DECLARE C CURSOR FOR S; 103 104 /* Set the bind variables for any placeholders in the 105 SQL statement. */ 106 set_bind_variables(); 107 108 /* Open the cursor and execute the statement. 109 * If the statement is not a query (SELECT), the 110 * statement processing is completed after the 111 * OPEN. 112 */ 113 114 EXEC SQL OPEN C USING DESCRIPTOR bind_dp; 115 116 /* Call the function that processes the select-list. 117 * If the statement is not a query, this function 118 * just returns, doing nothing. 119 */ 120 process_select_list(); 121 122 /* Tell user how many rows processed. */ 123 for (i = 0; i < 8; i++) 124 { 125 if (strncmp(dyn_statement, dml_commands[i], 6) == 0) 126 { 127 printf(" %d row%c processed. ", sqlca.sqlerrd[2], 128 sqlca.sqlerrd[2] == 1 ? '