home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api13.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  6KB  |  198 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.Inprise.com/IPL.html
  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 Inprise Corporation
  23.  * and its predecessors. Portions created by Inprise Corporation are
  24.  *
  25.  * Copyright (C) 2000 Inprise Corporation
  26.  * All Rights Reserved.
  27.  * Contributor(s): ______________________________________.
  28.  */
  29.  
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ibase.h>
  33. #include <stdio.h>
  34. #include "example.h"
  35.  
  36. #define BUFLEN        512
  37. #define CURRENLEN    10
  38.  
  39. char    *sel_str1 =
  40.     "SELECT currency FROM country WHERE country = 'Canada'";
  41.  
  42.  
  43. int main (ARG(int, argc), ARG(char **, argv))
  44. ARGLIST(int argc)
  45. ARGLIST(char **argv)
  46. {
  47.     char                *country = "Canada";        /* passed as a parameter */
  48.     char                *new_name = "CdnDollar";    /* passed as a parameter */
  49.     char                orig_name[CURRENLEN + 1];
  50.     char                buf[BUFLEN + 1];
  51.     isc_db_handle       db1 = NULL,        /* handle for database 1 */
  52.                         db2 = NULL;        /* handle for database 2 */
  53.     isc_tr_handle       trans1 = NULL;     /* transaction handle */
  54.     long                status[20];
  55.     XSQLDA ISC_FAR *    sel_sqlda;
  56.     isc_stmt_handle     stmt = NULL;
  57.     long                stat1, stat2, stat3;
  58.     char                empdb[128], empdb2[128];
  59.  
  60.     if (argc > 1)
  61.         strcpy(empdb, argv[1]);
  62.     else
  63.         strcpy(empdb, "employee.gdb");
  64.     if (argc > 2)
  65.         strcpy(empdb2, argv[2]);
  66.     else
  67.         strcpy(empdb2, "employe2.gdb");
  68.  
  69.  
  70.     /* Open database 1. */
  71.     printf("Attaching to database %s\n", empdb);
  72.     if (isc_attach_database(status, 0, empdb, &db1, 0, NULL))
  73.     {
  74.         ERREXIT(status, 1)
  75.     }
  76.  
  77.     /* Open database 2. */
  78.     printf ("Attaching to database %s\n", empdb2);
  79.     if (isc_attach_database(status, 0, empdb2, &db2, 0, NULL))
  80.     {
  81.         ERREXIT(status, 1)
  82.     }
  83.  
  84.     /* Start a two-database transaction. */
  85.     if (isc_start_transaction(status, &trans1, 2, &db1, 0, NULL, &db2, 0, NULL))
  86.     {
  87.         ERREXIT(status, 1)
  88.     }          
  89.  
  90.     /*
  91.      *  Get the string, which is to be globally changed.
  92.      */
  93.  
  94.     sprintf(buf, "SELECT currency FROM country WHERE country = '%s'", country);
  95.  
  96.     sel_sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  97.     sel_sqlda->sqln = 1;
  98.     sel_sqlda->version = 1;
  99.  
  100.     if (isc_dsql_allocate_statement(status, &db1, &stmt))
  101.     {
  102.         ERREXIT(status, 1)
  103.     }
  104.     if (isc_dsql_prepare(status, &trans1, &stmt, 0, sel_str1, 1, sel_sqlda))
  105.     {
  106.         ERREXIT(status, 1)
  107.     }
  108.  
  109.     sel_sqlda->sqlvar[0].sqldata = orig_name;
  110.     sel_sqlda->sqlvar[0].sqltype = SQL_TEXT;
  111.     sel_sqlda->sqlvar[0].sqllen = CURRENLEN;
  112.  
  113.     if (isc_dsql_execute(status, &trans1, &stmt, 1, NULL))
  114.     {
  115.         ERREXIT(status, 1)
  116.     }
  117.     if (isc_dsql_fetch(status, &stmt, 1, sel_sqlda))
  118.     {
  119.         ERREXIT(status, 1)
  120.     }
  121.  
  122.     orig_name[CURRENLEN] = '\0';
  123.     printf("Modifying currency string:  %s\n", orig_name);
  124.                    
  125.     /*
  126.      *  Change the string in database 1.
  127.      */
  128.  
  129.     sprintf(buf, "UPDATE country SET currency = '%s' WHERE country = 'Canada'",
  130.             new_name);
  131.  
  132.     stat1 = 0L;
  133.     if (isc_dsql_execute_immediate(status, &db1, &trans1, 0, buf, 1, NULL))
  134.     {
  135.         isc_print_status(status);
  136.         stat1 = isc_sqlcode(status);
  137.     }       
  138.  
  139.     /*
  140.      *  Change all corresponding occurences of the string in database 2.
  141.      */
  142.  
  143.     sprintf(buf, "UPDATE cross_rate SET from_currency = '%s' WHERE \
  144.             from_currency = '%s'", new_name, orig_name);
  145.  
  146.     stat2 = 0L;
  147.     if (isc_dsql_execute_immediate(status, &db2, &trans1, 0, buf, 1, NULL))
  148.     {
  149.         isc_print_status(status);
  150.         stat2 = isc_sqlcode(status);
  151.     }
  152.     
  153.     sprintf(buf, "UPDATE cross_rate SET to_currency = '%s' WHERE \
  154.             to_currency = '%s'", new_name, orig_name);
  155.  
  156.     stat3 = 0L;
  157.     if (isc_dsql_execute_immediate(status, &db2, &trans1, 0, buf, 1, NULL))
  158.     {
  159.         isc_print_status(status);
  160.         stat3 = isc_sqlcode(status);
  161.     }
  162.  
  163.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  164.         isc_print_status(status);
  165.  
  166.     /*
  167.      *    If all statements executed successfully, commit the transaction.
  168.      *    Otherwise, undo all work.
  169.      */
  170.     if (!stat1 && !stat2 && !stat3)
  171.     {
  172.         if (isc_commit_transaction (status, &trans1))
  173.             isc_print_status(status);
  174.         printf("Changes committed.\n");
  175.     }
  176.     else
  177.     {
  178.         printf("update1:  %d\n", stat1);
  179.         printf("update2:  %d\n", stat2);
  180.         printf("update3:  %d\n", stat3);
  181.         if (isc_rollback_transaction(status, &trans1))
  182.             isc_print_status(status);
  183.         printf("Changes undone.\n");
  184.     }
  185.  
  186.     /* Close database 1. */
  187.     if (isc_detach_database(status, &db1))
  188.         isc_print_status(status);
  189.  
  190.     /* Close database 2. */
  191.     if (isc_detach_database(status, &db2))
  192.         isc_print_status(status);
  193.     
  194.     free(sel_sqlda);
  195.  
  196.     return 0;
  197. }
  198.