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 / api / api4.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  5KB  |  172 lines

  1. /*
  2.  *  Program type:  API Interface
  3.  *
  4.  *  Desription:
  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.  *      Note that all updates are rolled back in this version.
  11.  * The contents of this file are subject to the Interbase Public
  12.  * License Version 1.0 (the "License"); you may not use this file
  13.  * except in compliance with the License. You may obtain a copy
  14.  * of the License at http://www.Interbase.com/IPL/
  15.  *
  16.  * Software distributed under the License is distributed on an
  17.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  18.  * or implied. See the License for the specific language governing
  19.  * rights and limitations under the License.
  20.  *
  21.  * The Original Code was created by Interbase Software Corporation
  22.  * and its successors. Portions created by Borland/Inprise are
  23.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  24.  * created by InterBase Software Corporation are Copyright (C)
  25.  * 1998-1999 InterBase Software Corporation.
  26.  *
  27.  * Copyright (C) 2000 InterBase Software Corporation
  28.  * All Rights Reserved.
  29.  * Contributor(s): ______________________________________.
  30.  */
  31.  
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ibase.h>
  35. #include <stdio.h>
  36. #include "example.h"
  37.  
  38. int get_input (char* , double*);
  39.  
  40. static char *Dept_data[] =
  41.      {"622", "100", "116", "900", 0};
  42.  
  43. static double Percent_data[] =
  44.     {0.05,  1.00,  0.075,  0.10, 0};
  45.  
  46. int Input_ptr = 0;
  47.  
  48. char *updstr =
  49.     "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  50.  
  51. isc_db_handle    DB = NULL;                       /* database handle */
  52. isc_tr_handle    trans = NULL;                    /* transaction handle */
  53. long             status[20];                      /* status vector */
  54.  
  55. int main (ARG(int, argc), ARG(char **, argv))
  56. ARGLIST(int argc)
  57. ARGLIST(char **argv)
  58. {
  59.     char            dept_no[4];
  60.     double          percent_inc;
  61.     short           flag0 = 0, flag1 = 0;
  62.     XSQLDA  ISC_FAR *sqlda;
  63.     long            sqlcode;
  64.     char            empdb[128];
  65.  
  66.     if (argc > 1)
  67.         strcpy(empdb, argv[1]);
  68.     else
  69.         strcpy(empdb, "employee.gdb");
  70.     
  71.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  72.     {
  73.         ERREXIT(status, 1)
  74.     }
  75.  
  76.     /* Allocate an input SQLDA.  There are two unknown parameters. */
  77.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(2));
  78.     sqlda->sqln = 2;
  79.     sqlda->sqld = 2;
  80.     sqlda->version = 1;
  81.  
  82.     sqlda->sqlvar[0].sqldata = (char ISC_FAR *) &percent_inc;
  83.     sqlda->sqlvar[0].sqltype = SQL_DOUBLE + 1;
  84.     sqlda->sqlvar[0].sqllen  = sizeof(percent_inc);
  85.     sqlda->sqlvar[0].sqlind  = &flag0;
  86.     flag0 = 0;
  87.  
  88.     sqlda->sqlvar[1].sqldata = dept_no;
  89.     sqlda->sqlvar[1].sqltype = SQL_TEXT + 1;
  90.     sqlda->sqlvar[1].sqllen  = 3;
  91.     sqlda->sqlvar[1].sqlind  = &flag1;
  92.     flag1 = 0;               
  93.  
  94.     /*
  95.      *  Get the next department-percent increase input pair.
  96.      */
  97.     while (get_input(dept_no, &percent_inc))
  98.     {
  99.         printf("\nIncreasing budget for department:  %s  by %5.2lf percent.\n",
  100.                dept_no, percent_inc);
  101.  
  102.         if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  103.         {
  104.             ERREXIT(status, 1)
  105.         }
  106.  
  107.         /* Update the budget. */
  108.         isc_dsql_execute_immediate(status, &DB, &trans, 0, updstr, 1, sqlda);
  109.         sqlcode = isc_sqlcode(status);
  110.         if (sqlcode)
  111.         {
  112.             /* Don't save the update, if the new budget exceeds the limit. */
  113.             if (sqlcode == -625)
  114.             {
  115.                 printf("\tExceeded budget limit -- not updated.\n");
  116.  
  117.                 if (isc_rollback_transaction(status, &trans))
  118.                 {
  119.                     ERREXIT(status, 1)
  120.                 }
  121.                 continue;
  122.             }
  123.             /* Undo all changes, in case of an error. */
  124.             else
  125.             {
  126.                 isc_print_status(status);
  127.                 printf("SQLCODE=%d\n", sqlcode);
  128.  
  129.                 isc_rollback_transaction(status, &trans);
  130.                 ERREXIT(status, 1)
  131.             }
  132.         }
  133.  
  134.         /* Save each department's update independently. 
  135.         ** Change to isc_commit_transaction to see changes
  136.          */
  137.         if (isc_rollback_transaction (status, &trans))
  138.         {
  139.             ERREXIT(status, 1)
  140.         }
  141.     }
  142.  
  143.     if (isc_detach_database(status, &DB))
  144.     {
  145.         ERREXIT(status, 1)
  146.     }
  147.  
  148.     free(sqlda);
  149.  
  150.     return 0;
  151. }
  152.  
  153.  
  154. /*
  155.  *  Get the department and percent parameters.
  156.  */
  157.  
  158. int get_input (ARG(char *,dept_no), ARG(double *,percent))
  159. ARGLIST(char *dept_no)
  160. ARGLIST(double *percent)
  161. {
  162.     if (Dept_data[Input_ptr] == 0)
  163.         return 0;
  164.  
  165.     strcpy(dept_no, Dept_data[Input_ptr]);
  166.  
  167.     if ((*percent = Percent_data[Input_ptr++]) == 0)
  168.         return 0;
  169.  
  170.     return 1;
  171. }
  172.