home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api4.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  5KB  |  169 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.Inprise.com/IPL.html
  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 Inprise Corporation
  22.  * and its predecessors. Portions created by Inprise Corporation are
  23.  *
  24.  * Copyright (C) 2000 Inprise Corporation
  25.  * All Rights Reserved.
  26.  * Contributor(s): ______________________________________.
  27.  */
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ibase.h>
  32. #include <stdio.h>
  33. #include "example.h"
  34.  
  35. int get_input (char* , double*);
  36.  
  37. static char *Dept_data[] =
  38.      {"622", "100", "116", "900", 0};
  39.  
  40. static double Percent_data[] =
  41.     {0.05,  1.00,  0.075,  0.10, 0};
  42.  
  43. int Input_ptr = 0;
  44.  
  45. char *updstr =
  46.     "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  47.  
  48. isc_db_handle    DB = NULL;                       /* database handle */
  49. isc_tr_handle    trans = NULL;                    /* transaction handle */
  50. long             status[20];                      /* status vector */
  51.  
  52. int main (ARG(int, argc), ARG(char **, argv))
  53. ARGLIST(int argc)
  54. ARGLIST(char **argv)
  55. {
  56.     char            dept_no[4];
  57.     double          percent_inc;
  58.     short           flag0 = 0, flag1 = 0;
  59.     XSQLDA  ISC_FAR *sqlda;
  60.     long            sqlcode;
  61.     char            empdb[128];
  62.  
  63.     if (argc > 1)
  64.         strcpy(empdb, argv[1]);
  65.     else
  66.         strcpy(empdb, "employee.gdb");
  67.     
  68.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  69.     {
  70.         ERREXIT(status, 1)
  71.     }
  72.  
  73.     /* Allocate an input SQLDA.  There are two unknown parameters. */
  74.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(2));
  75.     sqlda->sqln = 2;
  76.     sqlda->sqld = 2;
  77.     sqlda->version = 1;
  78.  
  79.     sqlda->sqlvar[0].sqldata = (char ISC_FAR *) &percent_inc;
  80.     sqlda->sqlvar[0].sqltype = SQL_DOUBLE + 1;
  81.     sqlda->sqlvar[0].sqllen  = sizeof(percent_inc);
  82.     sqlda->sqlvar[0].sqlind  = &flag0;
  83.     flag0 = 0;
  84.  
  85.     sqlda->sqlvar[1].sqldata = dept_no;
  86.     sqlda->sqlvar[1].sqltype = SQL_TEXT + 1;
  87.     sqlda->sqlvar[1].sqllen  = 3;
  88.     sqlda->sqlvar[1].sqlind  = &flag1;
  89.     flag1 = 0;               
  90.  
  91.     /*
  92.      *  Get the next department-percent increase input pair.
  93.      */
  94.     while (get_input(dept_no, &percent_inc))
  95.     {
  96.         printf("\nIncreasing budget for department:  %s  by %5.2lf percent.\n",
  97.                dept_no, percent_inc);
  98.  
  99.         if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  100.         {
  101.             ERREXIT(status, 1)
  102.         }
  103.  
  104.         /* Update the budget. */
  105.         isc_dsql_execute_immediate(status, &DB, &trans, 0, updstr, 1, sqlda);
  106.         sqlcode = isc_sqlcode(status);
  107.         if (sqlcode)
  108.         {
  109.             /* Don't save the update, if the new budget exceeds the limit. */
  110.             if (sqlcode == -625)
  111.             {
  112.                 printf("\tExceeded budget limit -- not updated.\n");
  113.  
  114.                 if (isc_rollback_transaction(status, &trans))
  115.                 {
  116.                     ERREXIT(status, 1)
  117.                 }
  118.                 continue;
  119.             }
  120.             /* Undo all changes, in case of an error. */
  121.             else
  122.             {
  123.                 isc_print_status(status);
  124.                 printf("SQLCODE=%d\n", sqlcode);
  125.  
  126.                 isc_rollback_transaction(status, &trans);
  127.                 ERREXIT(status, 1)
  128.             }
  129.         }
  130.  
  131.         /* Save each department's update independently. 
  132.         ** Change to isc_commit_transaction to see changes
  133.          */
  134.         if (isc_rollback_transaction (status, &trans))
  135.         {
  136.             ERREXIT(status, 1)
  137.         }
  138.     }
  139.  
  140.     if (isc_detach_database(status, &DB))
  141.     {
  142.         ERREXIT(status, 1)
  143.     }
  144.  
  145.     free(sqlda);
  146.  
  147.     return 0;
  148. }
  149.  
  150.  
  151. /*
  152.  *  Get the department and percent parameters.
  153.  */
  154.  
  155. int get_input (ARG(char *,dept_no), ARG(double *,percent))
  156. ARGLIST(char *dept_no)
  157. ARGLIST(double *percent)
  158. {
  159.     if (Dept_data[Input_ptr] == 0)
  160.         return 0;
  161.  
  162.     strcpy(dept_no, Dept_data[Input_ptr]);
  163.  
  164.     if ((*percent = Percent_data[Input_ptr++]) == 0)
  165.         return 0;
  166.  
  167.     return 1;
  168. }
  169.