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

  1. /*
  2.  *  Program type:   Embedded Dynamic SQL
  3.  *
  4.  *  Description:
  5.  *        This program updates departments' budgets, given
  6.  *        the department and the new budget information parameters.
  7.  *
  8.  *        An input SQLDA is allocated for the update query
  9.  *        with parameter markers.
  10.  * The contents of this file are subject to the Interbase Public
  11.  * License Version 1.0 (the "License"); you may not use this file
  12.  * except in compliance with the License. You may obtain a copy
  13.  * of the License at http://www.Inprise.com/IPL.html
  14.  *
  15.  * Software distributed under the License is distributed on an
  16.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  17.  * or implied. See the License for the specific language governing
  18.  * rights and limitations under the License.
  19.  *
  20.  * The Original Code was created by Inprise Corporation
  21.  * and its predecessors. Portions created by Inprise Corporation are
  22.  *
  23.  * Copyright (C) 2000 Inprise Corporation
  24.  * All Rights Reserved.
  25.  * Contributor(s): ______________________________________.
  26.  */
  27.  
  28. #include "example.h"
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. int get_input (char *dept_no, double *percent);
  33.  
  34. static char *Dept_data[] =
  35.     {"622", "100", "116", "900", 0};
  36.  
  37. static double Percent_data[] =
  38.     {0.05,  1.00,  0.075,  0.10, 0};
  39.  
  40. int    Input_ptr = 0;
  41. char Db_name[128];
  42.  
  43. EXEC SQL
  44.     SET DATABASE empdb = "employee.gdb" RUNTIME :Db_name;
  45.  
  46. char *upd_str =
  47.     "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  48.  
  49.  
  50. int main(ARG(int, argc), ARG(char **, argv))
  51. ARGLIST(int argc)
  52. ARGLIST(char **argv)
  53. {
  54.     BASED_ON department.dept_no    dept_no;
  55.     double    percent_inc;
  56.     short    nullind = 0;
  57.     XSQLDA    *sqlda;
  58.  
  59.         if (argc > 1)
  60.                 strcpy(Db_name, argv[1]);
  61.         else
  62.                 strcpy(Db_name, "employee.gdb");
  63.  
  64.  
  65.     EXEC SQL
  66.         WHENEVER SQLERROR GO TO Error;
  67.  
  68.     EXEC SQL
  69.         CONNECT empdb;
  70.  
  71.     EXEC SQL
  72.         SET TRANSACTION USING empdb;
  73.  
  74.     /* Allocate an input SQLDA.  There are two unknown parameters. */
  75.     sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  76.     sqlda->sqln = 2;
  77.     sqlda->sqld = 2;
  78.     sqlda->version = 1;
  79.  
  80.     /* Prepare the query. */
  81.     EXEC SQL
  82.         PREPARE q FROM :upd_str;
  83.  
  84.     /* Prepare the input sqlda, only data and indicator to set */
  85.  
  86.     EXEC SQL 
  87.         DESCRIBE INPUT q USING SQL DESCRIPTOR sqlda;
  88.  
  89.     sqlda->sqlvar[0].sqldata = (char *) &percent_inc;
  90.     sqlda->sqlvar[0].sqlind = &nullind;
  91.  
  92.     sqlda->sqlvar[1].sqldata = dept_no;
  93.     /* FOrce the type to char instead of varchar */
  94.     sqlda->sqlvar[1].sqltype = SQL_TEXT +1;
  95.     sqlda->sqlvar[1].sqlind = &nullind;
  96.  
  97.     /* Expect an error, trap it */
  98.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  99.  
  100.     /*
  101.      *    Get the next department-percent increase input pair.
  102.      */
  103.     while (get_input(dept_no, &percent_inc))
  104.     {
  105.         printf("\nIncreasing budget for department:  %s  by %5.2f percent.\n",
  106.                 dept_no, percent_inc);
  107.  
  108.  
  109.         /* Update the budget. */
  110.         EXEC SQL
  111.             EXECUTE q USING SQL DESCRIPTOR sqlda;
  112.  
  113.         /* Don't save the update, if the new budget exceeds 
  114.         ** the limit. Detect an integrity violation 
  115.         */
  116.         if (SQLCODE == -625) 
  117.         {
  118.             printf("\tExceeded budget limit -- not updated.\n");
  119.             continue;
  120.         }
  121.         /* Undo all changes, in case of an error. */
  122.         else if (SQLCODE)
  123.             goto Error;
  124.  
  125.         /* Save each department's update independently. */
  126.         EXEC SQL
  127.             COMMIT RETAIN;
  128.     }
  129.  
  130.     EXEC SQL
  131.         COMMIT RELEASE;
  132.     return (0);
  133. Error:
  134.     isc_print_status(gds__status);
  135.     printf("SQLCODE=%d\n", SQLCODE);
  136.  
  137.     EXEC SQL
  138.         ROLLBACK RELEASE;
  139.  
  140.     EXEC SQL
  141.         DISCONNECT empdb;
  142.  
  143.     free( sqlda);
  144.     return(1);
  145. }
  146.  
  147.  
  148. /*
  149.  *    Get the department and percent parameters.
  150.  */
  151. int get_input(ARG(char *, dept_no), ARG(double *, percent))
  152. ARGLIST(char    *dept_no)
  153. ARGLIST(double    *percent)
  154. {
  155.     if (Dept_data[Input_ptr] == 0)
  156.         return 0;
  157.  
  158.     strcpy(dept_no, Dept_data[Input_ptr]);
  159.  
  160.     if ((*percent = Percent_data[Input_ptr++]) == 0)
  161.         return 0;
  162.  
  163.     return(1);
  164. }
  165.