home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / gpre / dyn2.e < prev    next >
Encoding:
Text File  |  2001-01-05  |  3.8 KB  |  175 lines

  1. /*
  2.  *    Program type:    Embedded Dynamic SQL
  3.  *
  4.  *    Description:
  5.  *        This program adds several departments with small default
  6.  *        budgets, using 'execute immediate' statement.
  7.  *        Then, a prepared statement, which doubles budgets for
  8.  *        departments with low budgets, is executed.
  9.  * The contents of this file are subject to the Interbase Public
  10.  * License Version 1.0 (the "License"); you may not use this file
  11.  * except in compliance with the License. You may obtain a copy
  12.  * of the License at http://www.Inprise.com/IPL.html
  13.  *
  14.  * Software distributed under the License is distributed on an
  15.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16.  * or implied. See the License for the specific language governing
  17.  * rights and limitations under the License.
  18.  *
  19.  * The Original Code was created by Inprise Corporation
  20.  * and its predecessors. Portions created by Inprise Corporation are
  21.  *
  22.  * Copyright (C) 2000 Inprise Corporation
  23.  * All Rights Reserved.
  24.  * Contributor(s): ______________________________________.
  25.  */
  26.  
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #define    MAXLEN        256
  32.  
  33. int get_line (char *line);
  34. void clean_up (void);
  35.  
  36. static char *Dept_data[] =
  37. {
  38.     "117", "Field Office: Hong Kong",   "110",
  39.     "118", "Field Office: Australia",   "110",
  40.     "119", "Field Office: New Zealand", "110",
  41.     0
  42. };
  43. int Dept_ptr = 0;
  44. char Db_name[128];
  45.  
  46. EXEC SQL 
  47.     SET DATABASE empdb = "employee.gdb" RUNTIME :Db_name;
  48.  
  49.  
  50. int main(ARG(int, argc), ARG(char **, argv))
  51. ARGLIST(int argc)
  52. ARGLIST(char **argv)
  53. {
  54.     BASED_ON department.department    dept_name;
  55.     BASED_ON department.dept_no        dept_id;
  56.     char    exec_str[MAXLEN], prep_str[MAXLEN];
  57.  
  58.     if (argc > 1)
  59.         strcpy(Db_name, argv[1]);
  60.     else
  61.         strcpy(Db_name, "employee.gdb");
  62.  
  63.     EXEC SQL
  64.         WHENEVER SQLERROR GO TO MainError;
  65.  
  66.     EXEC SQL
  67.         CONNECT empdb;
  68.  
  69.     clean_up();
  70.  
  71.  
  72.     EXEC SQL
  73.         SET TRANSACTION;
  74.  
  75.     /*
  76.      *    Prepare a statement, which may be executed more than once.
  77.      */
  78.     strcpy(prep_str,
  79.         "UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
  80.  
  81.     EXEC SQL
  82.         PREPARE double_small_budget FROM :prep_str;
  83.  
  84.     /*
  85.      *  Add new departments, using 'execute immediate'.
  86.      *  Build each 'insert' statement, using the supplied parameters.
  87.      *  Since these statements will not be needed after they are executed,
  88.      *  use 'execute immediate'.
  89.      */
  90.  
  91.  
  92.     while (get_line(exec_str))
  93.     {
  94.         printf("\nExecuting statement:\n\t%s;\n", exec_str);
  95.  
  96.         EXEC SQL
  97.             EXECUTE IMMEDIATE :exec_str;
  98.     }
  99.  
  100.     EXEC SQL
  101.         COMMIT RETAIN;
  102.  
  103.     /*
  104.      *    Execute a previously prepared statement.
  105.      */
  106.     printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
  107.  
  108.     EXEC SQL
  109.         EXECUTE double_small_budget;
  110.  
  111.     EXEC SQL
  112.         COMMIT;
  113.  
  114.     EXEC SQL
  115.         DISCONNECT empdb;
  116.  
  117.     exit(0);
  118.  
  119. MainError:
  120.     EXEC SQL
  121.         WHENEVER SQLERROR CONTINUE;
  122.  
  123.     isc_print_status(gds__status);
  124.     printf("SQLCODE=%d\n", SQLCODE);
  125.     EXEC SQL ROLLBACK;
  126.      
  127.         exit(1);
  128.  
  129. }
  130.  
  131.  
  132. /*
  133.  *  Construct an 'insert' statement from the supplied parameters.
  134.  */
  135. int get_line(ARG(char *, line))
  136. ARGLIST(char    *line)
  137. {
  138.     if (Dept_data[Dept_ptr] == 0)
  139.         return 0;
  140.     if (Dept_data[Dept_ptr + 1] == 0)
  141.         return 0;
  142.     if (Dept_data[Dept_ptr + 2] == 0)
  143.         return 0;
  144.  
  145.     sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
  146.                VALUES ('%s', '%s', '%s')", Dept_data[Dept_ptr],
  147.                Dept_data[Dept_ptr + 1], Dept_data[Dept_ptr + 2]);
  148.     Dept_ptr += 3;
  149.  
  150.     return(1);
  151. }
  152.  
  153.  
  154. /*
  155.  *    Delete old data.
  156.  */
  157. void clean_up (void)
  158. {
  159.     EXEC SQL WHENEVER SQLERROR GO TO CleanErr;
  160.  
  161.     EXEC SQL SET TRANSACTION;
  162.  
  163.     EXEC SQL EXECUTE IMMEDIATE
  164.         "DELETE FROM department WHERE dept_no IN ('117', '118', '119')";
  165.  
  166.     EXEC SQL COMMIT;
  167.     return;
  168.  
  169. CleanErr:
  170.  
  171.     isc_print_status(gds__status);
  172.     printf("SQLCODE=%d\n", SQLCODE);
  173.     exit(1);
  174. }
  175.