home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / gpre / stat11.e < prev    next >
Encoding:
Text File  |  2001-01-05  |  3.9 KB  |  174 lines

  1. /*
  2.  *  Program type:   Embedded Static SQL
  3.  *
  4.  *    Description:
  5.  *        This program demonstrates 'set transaction' statements
  6.  *        with the three isolation options:
  7.  *
  8.  *            - snapshot
  9.  *            - read committed
  10.  *            - shapshot table stability.
  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 "example.h"
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33.  
  34. EXEC SQL
  35.     BEGIN DECLARE SECTION;
  36.  
  37. long *t1;
  38. long *t2;
  39. long *t3;
  40. char Db_name[128];
  41.  
  42. EXEC SQL
  43.     SET DATABASE empdb = COMPILETIME "employee.gdb" RUNTIME :Db_name;
  44.  
  45. long        cust_no;
  46. long        tot;
  47. char    ord_stat[8];
  48.  
  49. EXEC SQL
  50.     END DECLARE SECTION;
  51.  
  52.  
  53.  
  54. int main(ARG(int, argc), ARG(char **, argv))
  55. ARGLIST(int argc)
  56. ARGLIST(char **argv)
  57. {
  58.  
  59.     if (argc > 1)
  60.         strcpy (Db_name, argv[1]);
  61.     else
  62.         strcpy (Db_name, "employee.gdb");
  63.     /* Connect to the database. */
  64.  
  65.     EXEC SQL 
  66.         WHENEVER SQLERROR GOTO :err;
  67.     EXEC SQL
  68.         CONNECT empdb;
  69.  
  70.     /*
  71.      *    Start a transaction with SNAPSHOT isolation option.
  72.      *    This transaction wants to see a stable, unchanging view
  73.      *    of the sales orders, while it computes the totals.
  74.      *    Name the transaction t1.
  75.      */
  76.  
  77.     printf("Starting a transaction with SNAPSHOT isolation option.\n\n");
  78.  
  79.     EXEC SQL
  80.         SET TRANSACTION NAME t1 READ WRITE SNAPSHOT;
  81.  
  82.     EXEC SQL
  83.         DECLARE s CURSOR FOR
  84.         SELECT cust_no, SUM(qty_ordered)
  85.         FROM sales GROUP BY cust_no;
  86.  
  87.     EXEC SQL
  88.         OPEN TRANSACTION t1 s;
  89.     EXEC SQL
  90.         FETCH s INTO :cust_no, :tot;        /* get the first row only */
  91.     if (!SQLCODE)
  92.         printf("\tCustomer: %ld    Quantity Ordered: %ld\n\n", cust_no, tot);
  93.     EXEC SQL
  94.         CLOSE s;
  95.  
  96.     EXEC SQL
  97.         COMMIT TRANSACTION t1;
  98.  
  99.  
  100.     /*
  101.      *    Start a transaction with READ COMMITTED isolation option.
  102.      *    This transaction wants to see changes for the order status
  103.      *    as they come in.
  104.      *    Name the transaction t2.
  105.      */
  106.  
  107.     printf("Starting a transaction with READ COMMITTED isolation option.\n\n");
  108.  
  109.     EXEC SQL
  110.         SET TRANSACTION NAME t2 READ WRITE READ COMMITTED;
  111.  
  112.     EXEC SQL
  113.         DECLARE c CURSOR FOR
  114.         SELECT cust_no, order_status
  115.         FROM sales
  116.         WHERE order_status IN ("open", "shipping");
  117.  
  118.     EXEC SQL
  119.         OPEN TRANSACTION t2 c;
  120.     EXEC SQL
  121.         FETCH c INTO :cust_no, :ord_stat;    /* get the first row only */
  122.     if (!SQLCODE)
  123.         printf("\tCustomer number: %ld   Status: %s\n\n", cust_no, ord_stat);
  124.         
  125.     EXEC SQL
  126.         CLOSE c;
  127.  
  128.     EXEC SQL
  129.         COMMIT TRANSACTION t2;
  130.  
  131.  
  132.     /*
  133.      *    Start a transaction with SNAPSHOT TABLE STABILITY isolation
  134.      *    option.  This transaction wants to lock out all other users
  135.      *    from making any changes to the customer table, while it goes
  136.      *    through and updates customer status.
  137.      *    Name the transaction t3.
  138.      */
  139.  
  140.     printf("Starting a transaction with SNAPSHOT TABLE STABILITY.\n\n");
  141.  
  142.     EXEC SQL
  143.         SET TRANSACTION NAME t3 READ WRITE SNAPSHOT TABLE STABILITY;
  144.  
  145.     EXEC SQL
  146.         DECLARE h CURSOR FOR
  147.         SELECT cust_no
  148.         FROM customer
  149.         WHERE on_hold = '*'
  150.         FOR UPDATE OF on_hold;
  151.  
  152.     EXEC SQL
  153.         OPEN TRANSACTION t3 h;
  154.     EXEC SQL
  155.         FETCH h INTO :cust_no;                /* get the first row only */
  156.     if (!SQLCODE)
  157.         printf("\tCustomer on hold: %ld\n\n", cust_no);
  158.         
  159.     EXEC SQL
  160.         CLOSE h;
  161.  
  162.     EXEC SQL
  163.         COMMIT TRANSACTION t3;
  164.  
  165.  
  166.     EXEC SQL
  167.         DISCONNECT empdb;
  168. return (0);
  169. err:
  170.     isc_print_status(isc_status);
  171.     return 1;
  172. }
  173.  
  174.