home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / interb / InterBase_WI-V6.0-server.exe / server / examples / gpre / dyn2.e < prev    next >
Text File  |  2000-06-23  |  4KB  |  178 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.Interbase.com/IPL/
  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 Interbase Software Corporation
  20.  * and its successors. Portions created by Borland/Inprise are
  21.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  22.  * created by InterBase Software Corporation are Copyright (C)
  23.  * 1998-1999 InterBase Software Corporation.
  24.  *
  25.  * Copyright (C) 2000 InterBase Software Corporation
  26.  * All Rights Reserved.
  27.  * Contributor(s): ______________________________________.
  28.  */
  29.  
  30. #include "example.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. #define    MAXLEN        256
  35.  
  36. int get_line (char *line);
  37. void clean_up (void);
  38.  
  39. static char *Dept_data[] =
  40. {
  41.     "117", "Field Office: Hong Kong",   "110",
  42.     "118", "Field Office: Australia",   "110",
  43.     "119", "Field Office: New Zealand", "110",
  44.     0
  45. };
  46. int Dept_ptr = 0;
  47. char Db_name[128];
  48.  
  49. EXEC SQL 
  50.     SET DATABASE empdb = "employee.gdb" RUNTIME :Db_name;
  51.  
  52.  
  53. int main(ARG(int, argc), ARG(char **, argv))
  54. ARGLIST(int argc)
  55. ARGLIST(char **argv)
  56. {
  57.     BASED_ON department.department    dept_name;
  58.     BASED_ON department.dept_no        dept_id;
  59.     char    exec_str[MAXLEN], prep_str[MAXLEN];
  60.  
  61.     if (argc > 1)
  62.         strcpy(Db_name, argv[1]);
  63.     else
  64.         strcpy(Db_name, "employee.gdb");
  65.  
  66.     EXEC SQL
  67.         WHENEVER SQLERROR GO TO MainError;
  68.  
  69.     EXEC SQL
  70.         CONNECT empdb;
  71.  
  72.     clean_up();
  73.  
  74.  
  75.     EXEC SQL
  76.         SET TRANSACTION;
  77.  
  78.     /*
  79.      *    Prepare a statement, which may be executed more than once.
  80.      */
  81.     strcpy(prep_str,
  82.         "UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
  83.  
  84.     EXEC SQL
  85.         PREPARE double_small_budget FROM :prep_str;
  86.  
  87.     /*
  88.      *  Add new departments, using 'execute immediate'.
  89.      *  Build each 'insert' statement, using the supplied parameters.
  90.      *  Since these statements will not be needed after they are executed,
  91.      *  use 'execute immediate'.
  92.      */
  93.  
  94.  
  95.     while (get_line(exec_str))
  96.     {
  97.         printf("\nExecuting statement:\n\t%s;\n", exec_str);
  98.  
  99.         EXEC SQL
  100.             EXECUTE IMMEDIATE :exec_str;
  101.     }
  102.  
  103.     EXEC SQL
  104.         COMMIT RETAIN;
  105.  
  106.     /*
  107.      *    Execute a previously prepared statement.
  108.      */
  109.     printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
  110.  
  111.     EXEC SQL
  112.         EXECUTE double_small_budget;
  113.  
  114.     EXEC SQL
  115.         COMMIT;
  116.  
  117.     EXEC SQL
  118.         DISCONNECT empdb;
  119.  
  120.     exit(0);
  121.  
  122. MainError:
  123.     EXEC SQL
  124.         WHENEVER SQLERROR CONTINUE;
  125.  
  126.     isc_print_status(gds__status);
  127.     printf("SQLCODE=%d\n", SQLCODE);
  128.     EXEC SQL ROLLBACK;
  129.      
  130.         exit(1);
  131.  
  132. }
  133.  
  134.  
  135. /*
  136.  *  Construct an 'insert' statement from the supplied parameters.
  137.  */
  138. int get_line(ARG(char *, line))
  139. ARGLIST(char    *line)
  140. {
  141.     if (Dept_data[Dept_ptr] == 0)
  142.         return 0;
  143.     if (Dept_data[Dept_ptr + 1] == 0)
  144.         return 0;
  145.     if (Dept_data[Dept_ptr + 2] == 0)
  146.         return 0;
  147.  
  148.     sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
  149.                VALUES ('%s', '%s', '%s')", Dept_data[Dept_ptr],
  150.                Dept_data[Dept_ptr + 1], Dept_data[Dept_ptr + 2]);
  151.     Dept_ptr += 3;
  152.  
  153.     return(1);
  154. }
  155.  
  156.  
  157. /*
  158.  *    Delete old data.
  159.  */
  160. void clean_up (void)
  161. {
  162.     EXEC SQL WHENEVER SQLERROR GO TO CleanErr;
  163.  
  164.     EXEC SQL SET TRANSACTION;
  165.  
  166.     EXEC SQL EXECUTE IMMEDIATE
  167.         "DELETE FROM department WHERE dept_no IN ('117', '118', '119')";
  168.  
  169.     EXEC SQL COMMIT;
  170.     return;
  171.  
  172. CleanErr:
  173.  
  174.     isc_print_status(gds__status);
  175.     printf("SQLCODE=%d\n", SQLCODE);
  176.     exit(1);
  177. }
  178.