stat11.e 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * Copyright (C) Inprise Corporation.
  24. *
  25. * All Rights Reserved.
  26. * Contributor(s): ______________________________________.
  27. */
  28. #include "example.h"
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. EXEC SQL
  33. BEGIN DECLARE SECTION;
  34. long *t1;
  35. long *t2;
  36. long *t3;
  37. char Db_name[128];
  38. EXEC SQL
  39. SET DATABASE empdb = COMPILETIME "employee.fdb" RUNTIME :Db_name;
  40. int cust_no;
  41. int tot;
  42. char ord_stat[8];
  43. EXEC SQL
  44. END DECLARE SECTION;
  45. int main(int argc, char** argv)
  46. {
  47. if (argc > 1)
  48. strcpy (Db_name, argv[1]);
  49. else
  50. strcpy (Db_name, "employee.fdb");
  51. /* Connect to the database. */
  52. EXEC SQL
  53. WHENEVER SQLERROR GOTO :err;
  54. EXEC SQL
  55. CONNECT empdb;
  56. /*
  57. * Start a transaction with SNAPSHOT isolation option.
  58. * This transaction wants to see a stable, unchanging view
  59. * of the sales orders, while it computes the totals.
  60. * Name the transaction t1.
  61. */
  62. printf("Starting a transaction with SNAPSHOT isolation option.\n\n");
  63. EXEC SQL
  64. SET TRANSACTION NAME t1 READ WRITE SNAPSHOT;
  65. EXEC SQL
  66. DECLARE s CURSOR FOR
  67. SELECT cust_no, SUM(qty_ordered)
  68. FROM sales GROUP BY cust_no;
  69. EXEC SQL
  70. OPEN TRANSACTION t1 s;
  71. EXEC SQL
  72. FETCH s INTO :cust_no, :tot; /* get the first row only */
  73. if (!SQLCODE)
  74. printf("\tCustomer: %ld Quantity Ordered: %ld\n\n", cust_no, tot);
  75. EXEC SQL
  76. CLOSE s;
  77. EXEC SQL
  78. COMMIT TRANSACTION t1;
  79. /*
  80. * Start a transaction with READ COMMITTED isolation option.
  81. * This transaction wants to see changes for the order status
  82. * as they come in.
  83. * Name the transaction t2.
  84. */
  85. printf("Starting a transaction with READ COMMITTED isolation option.\n\n");
  86. EXEC SQL
  87. SET TRANSACTION NAME t2 READ WRITE READ COMMITTED;
  88. EXEC SQL
  89. DECLARE c CURSOR FOR
  90. SELECT cust_no, order_status
  91. FROM sales
  92. WHERE order_status IN ("open", "shipping");
  93. EXEC SQL
  94. OPEN TRANSACTION t2 c;
  95. EXEC SQL
  96. FETCH c INTO :cust_no, :ord_stat; /* get the first row only */
  97. if (!SQLCODE)
  98. printf("\tCustomer number: %ld Status: %s\n\n", cust_no, ord_stat);
  99. EXEC SQL
  100. CLOSE c;
  101. EXEC SQL
  102. COMMIT TRANSACTION t2;
  103. /*
  104. * Start a transaction with SNAPSHOT TABLE STABILITY isolation
  105. * option. This transaction wants to lock out all other users
  106. * from making any changes to the customer table, while it goes
  107. * through and updates customer status.
  108. * Name the transaction t3.
  109. */
  110. printf("Starting a transaction with SNAPSHOT TABLE STABILITY.\n\n");
  111. EXEC SQL
  112. SET TRANSACTION NAME t3 READ WRITE SNAPSHOT TABLE STABILITY;
  113. EXEC SQL
  114. DECLARE h CURSOR FOR
  115. SELECT cust_no
  116. FROM customer
  117. WHERE on_hold = '*'
  118. FOR UPDATE OF on_hold;
  119. EXEC SQL
  120. OPEN TRANSACTION t3 h;
  121. EXEC SQL
  122. FETCH h INTO :cust_no; /* get the first row only */
  123. if (!SQLCODE)
  124. printf("\tCustomer on hold: %ld\n\n", cust_no);
  125. EXEC SQL
  126. CLOSE h;
  127. EXEC SQL
  128. COMMIT TRANSACTION t3;
  129. EXEC SQL
  130. DISCONNECT empdb;
  131. return (0);
  132. err:
  133. isc_print_status(isc_status);
  134. return 1;
  135. }