home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api10.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  6KB  |  213 lines

  1. /*
  2.  *    Program type:  API
  3.  *
  4.  *    Description:
  5.  *        This program selects and updates an array type.
  6.  *        Projected head count is displayed and updated for
  7.  *        a set of projects.
  8.  * The contents of this file are subject to the Interbase Public
  9.  * License Version 1.0 (the "License"); you may not use this file
  10.  * except in compliance with the License. You may obtain a copy
  11.  * of the License at http://www.Inprise.com/IPL.html
  12.  *
  13.  * Software distributed under the License is distributed on an
  14.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15.  * or implied. See the License for the specific language governing
  16.  * rights and limitations under the License.
  17.  *
  18.  * The Original Code was created by Inprise Corporation
  19.  * and its predecessors. Portions created by Inprise Corporation are
  20.  *
  21.  * Copyright (C) 2000 Inprise Corporation
  22.  * All Rights Reserved.
  23.  * Contributor(s): ______________________________________.
  24.  */
  25.  
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ibase.h>
  29. #include <stdio.h>
  30. #include "example.h"
  31.  
  32. char    *sel_str =
  33.     "SELECT dept_no, quart_head_cnt FROM proj_dept_budget p \
  34.     WHERE fiscal_year = 1994 AND proj_id = 'VBASE' \
  35.     FOR UPDATE of quart_head_cnt";
  36.  
  37. char    *upd_str =
  38.     "UPDATE proj_dept_budget SET quart_head_cnt = ? WHERE CURRENT OF S";
  39.  
  40.  
  41. int main (ARG(int, argc), ARG(char **, argv))
  42. ARGLIST(int argc)
  43. ARGLIST(char **argv)
  44. {
  45.     long            hcnt[4];
  46.     ISC_QUAD        array_id;
  47.     ISC_ARRAY_DESC  desc;
  48.     long            len;
  49.     char            dept_no[6];
  50.     isc_db_handle   DB = NULL;
  51.     isc_tr_handle   trans = NULL;
  52.     long            status[20];
  53.     short           flag0 = 0, flag1 = 0;
  54.     isc_stmt_handle stmt = NULL;
  55.     isc_stmt_handle ustmt = NULL;
  56.     char ISC_FAR *  cursor = "S";
  57.     XSQLDA ISC_FAR *osqlda, *isqlda;
  58.     long            fetch_stat;
  59.     short           i;
  60.     char            empdb[128];
  61.  
  62.     if (argc > 1)
  63.         strcpy(empdb, argv[1]);
  64.     else
  65.         strcpy(empdb, "employee.gdb");
  66.  
  67.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  68.     {
  69.         ERREXIT(status, 1)
  70.     }
  71.  
  72.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  73.     {
  74.         ERREXIT(status, 1)
  75.     }          
  76.  
  77.     /*
  78.      *    Set up the array description structure
  79.      */
  80.  
  81.  
  82.     if (isc_array_lookup_bounds(status, &DB, &trans,
  83.                                 "PROJ_DEPT_BUDGET", "QUART_HEAD_CNT", &desc))
  84.     {
  85.         ERREXIT(status, 1)
  86.     }
  87.     
  88.     /*
  89.      *    Set-up the select statement.
  90.      */
  91.  
  92.     osqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  93.     osqlda->sqln = 2;
  94.     osqlda->version = 1;
  95.  
  96.     osqlda->sqlvar[0].sqldata = (char ISC_FAR *) dept_no;
  97.     osqlda->sqlvar[0].sqltype = SQL_TEXT + 1;
  98.     osqlda->sqlvar[0].sqlind  = &flag0;
  99.  
  100.     osqlda->sqlvar[1].sqldata = (char ISC_FAR *) &array_id;
  101.     osqlda->sqlvar[1].sqltype = SQL_ARRAY + 1;
  102.     osqlda->sqlvar[1].sqlind  = &flag1;
  103.  
  104.     isc_dsql_allocate_statement(status, &DB, &stmt);
  105.     isc_dsql_allocate_statement(status, &DB, &ustmt);
  106.  
  107.     /* Prepare and execute query */
  108.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, osqlda))
  109.     {
  110.         ERREXIT(status, 1)
  111.     }
  112.  
  113.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  114.     {
  115.         ERREXIT(status, 1)
  116.     }
  117.  
  118.     /* Needed for update current */
  119.     isc_dsql_set_cursor_name(status, &stmt, cursor, 0);
  120.  
  121.     /*
  122.      *    Set-up the update statement.
  123.      */
  124.  
  125.     isqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  126.     isqlda->sqln = 1;
  127.     isqlda->version = 1;
  128.  
  129.     /* Use describe_bind to set up input sqlda */
  130.  
  131.     if (isc_dsql_prepare(status, &trans, &ustmt, 0, upd_str, 1, NULL))
  132.     {
  133.         ERREXIT(status, 1)
  134.     }
  135.     isc_dsql_describe_bind(status, &ustmt, 1, isqlda);
  136.  
  137.     isqlda->sqlvar[0].sqldata = (char ISC_FAR *) &array_id;
  138.     isqlda->sqlvar[0].sqlind  = &flag1;
  139.                               
  140.     /*
  141.      *    Fetch the head count for each department's 4 quarters;
  142.      *    increase the head count by 1 for each quarter;
  143.      *    and save the new head count.
  144.      */
  145.  
  146.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, osqlda)) == 0)
  147.     {
  148.         /* Get the current array values. */
  149.         if (!flag1)
  150.         {                     
  151.             len = sizeof(hcnt);;
  152.             if (isc_array_get_slice(status, &DB, &trans,
  153.                                     (ISC_QUAD ISC_FAR *) &array_id,
  154.                                     (ISC_ARRAY_DESC ISC_FAR *) &desc,
  155.                                     (void ISC_FAR *) hcnt,
  156.                                     (long ISC_FAR *) &len))
  157.                                     {ERREXIT (status, 1)};
  158.  
  159.             dept_no [osqlda->sqlvar[0].sqllen] = '\0';
  160.             printf("Department #:  %s\n\n", dept_no);
  161.  
  162.             printf("\tCurrent counts: %ld %ld %ld %ld\n",
  163.                    hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  164.  
  165.             /* Add 1 to each count. */
  166.             for (i = 0; i < 4; i++)
  167.                 hcnt[i] = hcnt[i] + 1;
  168.  
  169.             /* Save new array values. */
  170.             if (isc_array_put_slice(status, &DB, &trans,
  171.                                     (ISC_QUAD ISC_FAR *) &array_id,
  172.                                     (ISC_ARRAY_DESC ISC_FAR *) &desc,
  173.                                     (void ISC_FAR *) hcnt,
  174.                                     (long ISC_FAR *) &len))
  175.                                     {ERREXIT (status, 1)};
  176.  
  177.             /* Update the array handle. */
  178.             if (isc_dsql_execute(status, &trans, &ustmt, 1, isqlda))
  179.             {
  180.                 ERREXIT(status, 1)
  181.             }
  182.              
  183.             printf("\tNew counts    : %ld %ld %ld %ld\n\n",
  184.                    hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  185.         }
  186.     }
  187.        
  188.     if (fetch_stat != 100L)
  189.     {
  190.         ERREXIT(status, 1)
  191.     }
  192.  
  193.     isc_dsql_free_statement(status, &stmt, DSQL_close);
  194.     isc_dsql_free_statement(status, &ustmt, DSQL_close);
  195.  
  196.     /* Do a rollback to keep from updating the sample db */
  197.  
  198.     if (isc_rollback_transaction(status, &trans))
  199.     {
  200.         ERREXIT(status, 1)
  201.     }
  202.  
  203.     if (isc_detach_database(status, &DB))
  204.     {
  205.         ERREXIT(status, 1)
  206.     }
  207.  
  208.     free(osqlda);
  209.     free(isqlda);
  210.  
  211.     return 0;
  212. }
  213.