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 / api13.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  6KB  |  201 lines

  1. /*
  2.  *    Program type:  API Interface
  3.  *
  4.  *    Description:
  5.  *        This program performs a multi-database transaction
  6.  *        with a two-phase commit.  A 'currency' field is updated
  7.  *        in database 1 for table country, and corresponding
  8.  *        'from_currency' or 'to_currency' fields are updated in
  9.  *        database 2 for table cross_rate.  The transaction is
  10.  *        committed only if all instances of the string are
  11.  *        changed successfully in both databases.
  12.  * The contents of this file are subject to the Interbase Public
  13.  * License Version 1.0 (the "License"); you may not use this file
  14.  * except in compliance with the License. You may obtain a copy
  15.  * of the License at http://www.Interbase.com/IPL/
  16.  *
  17.  * Software distributed under the License is distributed on an
  18.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  19.  * or implied. See the License for the specific language governing
  20.  * rights and limitations under the License.
  21.  *
  22.  * The Original Code was created by Interbase Software Corporation
  23.  * and its successors. Portions created by Borland/Inprise are
  24.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  25.  * created by InterBase Software Corporation are Copyright (C)
  26.  * 1998-1999 InterBase Software Corporation.
  27.  *
  28.  * Copyright (C) 2000 InterBase Software Corporation
  29.  * All Rights Reserved.
  30.  * Contributor(s): ______________________________________.
  31.  */
  32.  
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ibase.h>
  36. #include <stdio.h>
  37. #include "example.h"
  38.  
  39. #define BUFLEN        512
  40. #define CURRENLEN    10
  41.  
  42. char    *sel_str1 =
  43.     "SELECT currency FROM country WHERE country = 'Canada'";
  44.  
  45.  
  46. int main (ARG(int, argc), ARG(char **, argv))
  47. ARGLIST(int argc)
  48. ARGLIST(char **argv)
  49. {
  50.     char                *country = "Canada";        /* passed as a parameter */
  51.     char                *new_name = "CdnDollar";    /* passed as a parameter */
  52.     char                orig_name[CURRENLEN + 1];
  53.     char                buf[BUFLEN + 1];
  54.     isc_db_handle       db1 = NULL,        /* handle for database 1 */
  55.                         db2 = NULL;        /* handle for database 2 */
  56.     isc_tr_handle       trans1 = NULL;     /* transaction handle */
  57.     long                status[20];
  58.     XSQLDA ISC_FAR *    sel_sqlda;
  59.     isc_stmt_handle     stmt = NULL;
  60.     long                stat1, stat2, stat3;
  61.     char                empdb[128], empdb2[128];
  62.  
  63.     if (argc > 1)
  64.         strcpy(empdb, argv[1]);
  65.     else
  66.         strcpy(empdb, "employee.gdb");
  67.     if (argc > 2)
  68.         strcpy(empdb2, argv[2]);
  69.     else
  70.         strcpy(empdb2, "employe2.gdb");
  71.  
  72.  
  73.     /* Open database 1. */
  74.     printf("Attaching to database %s\n", empdb);
  75.     if (isc_attach_database(status, 0, empdb, &db1, 0, NULL))
  76.     {
  77.         ERREXIT(status, 1)
  78.     }
  79.  
  80.     /* Open database 2. */
  81.     printf ("Attaching to database %s\n", empdb2);
  82.     if (isc_attach_database(status, 0, empdb2, &db2, 0, NULL))
  83.     {
  84.         ERREXIT(status, 1)
  85.     }
  86.  
  87.     /* Start a two-database transaction. */
  88.     if (isc_start_transaction(status, &trans1, 2, &db1, 0, NULL, &db2, 0, NULL))
  89.     {
  90.         ERREXIT(status, 1)
  91.     }          
  92.  
  93.     /*
  94.      *  Get the string, which is to be globally changed.
  95.      */
  96.  
  97.     sprintf(buf, "SELECT currency FROM country WHERE country = '%s'", country);
  98.  
  99.     sel_sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  100.     sel_sqlda->sqln = 1;
  101.     sel_sqlda->version = 1;
  102.  
  103.     if (isc_dsql_allocate_statement(status, &db1, &stmt))
  104.     {
  105.         ERREXIT(status, 1)
  106.     }
  107.     if (isc_dsql_prepare(status, &trans1, &stmt, 0, sel_str1, 1, sel_sqlda))
  108.     {
  109.         ERREXIT(status, 1)
  110.     }
  111.  
  112.     sel_sqlda->sqlvar[0].sqldata = orig_name;
  113.     sel_sqlda->sqlvar[0].sqltype = SQL_TEXT;
  114.     sel_sqlda->sqlvar[0].sqllen = CURRENLEN;
  115.  
  116.     if (isc_dsql_execute(status, &trans1, &stmt, 1, NULL))
  117.     {
  118.         ERREXIT(status, 1)
  119.     }
  120.     if (isc_dsql_fetch(status, &stmt, 1, sel_sqlda))
  121.     {
  122.         ERREXIT(status, 1)
  123.     }
  124.  
  125.     orig_name[CURRENLEN] = '\0';
  126.     printf("Modifying currency string:  %s\n", orig_name);
  127.                    
  128.     /*
  129.      *  Change the string in database 1.
  130.      */
  131.  
  132.     sprintf(buf, "UPDATE country SET currency = '%s' WHERE country = 'Canada'",
  133.             new_name);
  134.  
  135.     stat1 = 0L;
  136.     if (isc_dsql_execute_immediate(status, &db1, &trans1, 0, buf, 1, NULL))
  137.     {
  138.         isc_print_status(status);
  139.         stat1 = isc_sqlcode(status);
  140.     }       
  141.  
  142.     /*
  143.      *  Change all corresponding occurences of the string in database 2.
  144.      */
  145.  
  146.     sprintf(buf, "UPDATE cross_rate SET from_currency = '%s' WHERE \
  147.             from_currency = '%s'", new_name, orig_name);
  148.  
  149.     stat2 = 0L;
  150.     if (isc_dsql_execute_immediate(status, &db2, &trans1, 0, buf, 1, NULL))
  151.     {
  152.         isc_print_status(status);
  153.         stat2 = isc_sqlcode(status);
  154.     }
  155.     
  156.     sprintf(buf, "UPDATE cross_rate SET to_currency = '%s' WHERE \
  157.             to_currency = '%s'", new_name, orig_name);
  158.  
  159.     stat3 = 0L;
  160.     if (isc_dsql_execute_immediate(status, &db2, &trans1, 0, buf, 1, NULL))
  161.     {
  162.         isc_print_status(status);
  163.         stat3 = isc_sqlcode(status);
  164.     }
  165.  
  166.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  167.         isc_print_status(status);
  168.  
  169.     /*
  170.      *    If all statements executed successfully, commit the transaction.
  171.      *    Otherwise, undo all work.
  172.      */
  173.     if (!stat1 && !stat2 && !stat3)
  174.     {
  175.         if (isc_commit_transaction (status, &trans1))
  176.             isc_print_status(status);
  177.         printf("Changes committed.\n");
  178.     }
  179.     else
  180.     {
  181.         printf("update1:  %d\n", stat1);
  182.         printf("update2:  %d\n", stat2);
  183.         printf("update3:  %d\n", stat3);
  184.         if (isc_rollback_transaction(status, &trans1))
  185.             isc_print_status(status);
  186.         printf("Changes undone.\n");
  187.     }
  188.  
  189.     /* Close database 1. */
  190.     if (isc_detach_database(status, &db1))
  191.         isc_print_status(status);
  192.  
  193.     /* Close database 2. */
  194.     if (isc_detach_database(status, &db2))
  195.         isc_print_status(status);
  196.     
  197.     free(sel_sqlda);
  198.  
  199.     return 0;
  200. }
  201.