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 / api10.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  7KB  |  216 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.Interbase.com/IPL/
  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 Interbase Software Corporation
  19.  * and its successors. Portions created by Borland/Inprise are
  20.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  21.  * created by InterBase Software Corporation are Copyright (C)
  22.  * 1998-1999 InterBase Software Corporation.
  23.  *
  24.  * Copyright (C) 2000 InterBase Software 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. char    *sel_str =
  36.     "SELECT dept_no, quart_head_cnt FROM proj_dept_budget p \
  37.     WHERE fiscal_year = 1994 AND proj_id = 'VBASE' \
  38.     FOR UPDATE of quart_head_cnt";
  39.  
  40. char    *upd_str =
  41.     "UPDATE proj_dept_budget SET quart_head_cnt = ? WHERE CURRENT OF S";
  42.  
  43.  
  44. int main (ARG(int, argc), ARG(char **, argv))
  45. ARGLIST(int argc)
  46. ARGLIST(char **argv)
  47. {
  48.     long            hcnt[4];
  49.     ISC_QUAD        array_id;
  50.     ISC_ARRAY_DESC  desc;
  51.     long            len;
  52.     char            dept_no[6];
  53.     isc_db_handle   DB = NULL;
  54.     isc_tr_handle   trans = NULL;
  55.     long            status[20];
  56.     short           flag0 = 0, flag1 = 0;
  57.     isc_stmt_handle stmt = NULL;
  58.     isc_stmt_handle ustmt = NULL;
  59.     char ISC_FAR *  cursor = "S";
  60.     XSQLDA ISC_FAR *osqlda, *isqlda;
  61.     long            fetch_stat;
  62.     short           i;
  63.     char            empdb[128];
  64.  
  65.     if (argc > 1)
  66.         strcpy(empdb, argv[1]);
  67.     else
  68.         strcpy(empdb, "employee.gdb");
  69.  
  70.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  71.     {
  72.         ERREXIT(status, 1)
  73.     }
  74.  
  75.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  76.     {
  77.         ERREXIT(status, 1)
  78.     }          
  79.  
  80.     /*
  81.      *    Set up the array description structure
  82.      */
  83.  
  84.  
  85.     if (isc_array_lookup_bounds(status, &DB, &trans,
  86.                                 "PROJ_DEPT_BUDGET", "QUART_HEAD_CNT", &desc))
  87.     {
  88.         ERREXIT(status, 1)
  89.     }
  90.     
  91.     /*
  92.      *    Set-up the select statement.
  93.      */
  94.  
  95.     osqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  96.     osqlda->sqln = 2;
  97.     osqlda->version = 1;
  98.  
  99.     osqlda->sqlvar[0].sqldata = (char ISC_FAR *) dept_no;
  100.     osqlda->sqlvar[0].sqltype = SQL_TEXT + 1;
  101.     osqlda->sqlvar[0].sqlind  = &flag0;
  102.  
  103.     osqlda->sqlvar[1].sqldata = (char ISC_FAR *) &array_id;
  104.     osqlda->sqlvar[1].sqltype = SQL_ARRAY + 1;
  105.     osqlda->sqlvar[1].sqlind  = &flag1;
  106.  
  107.     isc_dsql_allocate_statement(status, &DB, &stmt);
  108.     isc_dsql_allocate_statement(status, &DB, &ustmt);
  109.  
  110.     /* Prepare and execute query */
  111.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, osqlda))
  112.     {
  113.         ERREXIT(status, 1)
  114.     }
  115.  
  116.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  117.     {
  118.         ERREXIT(status, 1)
  119.     }
  120.  
  121.     /* Needed for update current */
  122.     isc_dsql_set_cursor_name(status, &stmt, cursor, 0);
  123.  
  124.     /*
  125.      *    Set-up the update statement.
  126.      */
  127.  
  128.     isqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  129.     isqlda->sqln = 1;
  130.     isqlda->version = 1;
  131.  
  132.     /* Use describe_bind to set up input sqlda */
  133.  
  134.     if (isc_dsql_prepare(status, &trans, &ustmt, 0, upd_str, 1, NULL))
  135.     {
  136.         ERREXIT(status, 1)
  137.     }
  138.     isc_dsql_describe_bind(status, &ustmt, 1, isqlda);
  139.  
  140.     isqlda->sqlvar[0].sqldata = (char ISC_FAR *) &array_id;
  141.     isqlda->sqlvar[0].sqlind  = &flag1;
  142.                               
  143.     /*
  144.      *    Fetch the head count for each department's 4 quarters;
  145.      *    increase the head count by 1 for each quarter;
  146.      *    and save the new head count.
  147.      */
  148.  
  149.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, osqlda)) == 0)
  150.     {
  151.         /* Get the current array values. */
  152.         if (!flag1)
  153.         {                     
  154.             len = sizeof(hcnt);;
  155.             if (isc_array_get_slice(status, &DB, &trans,
  156.                                     (ISC_QUAD ISC_FAR *) &array_id,
  157.                                     (ISC_ARRAY_DESC ISC_FAR *) &desc,
  158.                                     (void ISC_FAR *) hcnt,
  159.                                     (long ISC_FAR *) &len))
  160.                                     {ERREXIT (status, 1)};
  161.  
  162.             dept_no [osqlda->sqlvar[0].sqllen] = '\0';
  163.             printf("Department #:  %s\n\n", dept_no);
  164.  
  165.             printf("\tCurrent counts: %ld %ld %ld %ld\n",
  166.                    hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  167.  
  168.             /* Add 1 to each count. */
  169.             for (i = 0; i < 4; i++)
  170.                 hcnt[i] = hcnt[i] + 1;
  171.  
  172.             /* Save new array values. */
  173.             if (isc_array_put_slice(status, &DB, &trans,
  174.                                     (ISC_QUAD ISC_FAR *) &array_id,
  175.                                     (ISC_ARRAY_DESC ISC_FAR *) &desc,
  176.                                     (void ISC_FAR *) hcnt,
  177.                                     (long ISC_FAR *) &len))
  178.                                     {ERREXIT (status, 1)};
  179.  
  180.             /* Update the array handle. */
  181.             if (isc_dsql_execute(status, &trans, &ustmt, 1, isqlda))
  182.             {
  183.                 ERREXIT(status, 1)
  184.             }
  185.              
  186.             printf("\tNew counts    : %ld %ld %ld %ld\n\n",
  187.                    hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  188.         }
  189.     }
  190.        
  191.     if (fetch_stat != 100L)
  192.     {
  193.         ERREXIT(status, 1)
  194.     }
  195.  
  196.     isc_dsql_free_statement(status, &stmt, DSQL_close);
  197.     isc_dsql_free_statement(status, &ustmt, DSQL_close);
  198.  
  199.     /* Do a rollback to keep from updating the sample db */
  200.  
  201.     if (isc_rollback_transaction(status, &trans))
  202.     {
  203.         ERREXIT(status, 1)
  204.     }
  205.  
  206.     if (isc_detach_database(status, &DB))
  207.     {
  208.         ERREXIT(status, 1)
  209.     }
  210.  
  211.     free(osqlda);
  212.     free(isqlda);
  213.  
  214.     return 0;
  215. }
  216.