stat12.e 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program utilizes the event mechanism for processing
  6. * newly entered sales orders. It initializes an event called
  7. * "new_order", and then loops waiting for and processing new
  8. * orders as they come in.
  9. *
  10. * When a new sales order is entered, a trigger defined in the
  11. * database posts the "new_order" event. When the program is
  12. * notified of the event, it opens the cursor for selecting all
  13. * orders with status "new". For each fetched order, a transaction
  14. * is started, which changes the order status from "new" to "open
  15. * and takes some action to initiate order processing. After all
  16. * the new orders (if there were more than one) are processed, it
  17. * goes back to waiting for new orders.
  18. *
  19. * To trigger the event, while running this program, run stat12t.
  20. * The contents of this file are subject to the Interbase Public
  21. * License Version 1.0 (the "License"); you may not use this file
  22. * except in compliance with the License. You may obtain a copy
  23. * of the License at http://www.Inprise.com/IPL.html
  24. *
  25. * Software distributed under the License is distributed on an
  26. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  27. * or implied. See the License for the specific language governing
  28. * rights and limitations under the License.
  29. *
  30. * The Original Code was created by Inprise Corporation
  31. * and its predecessors. Portions created by Inprise Corporation are
  32. * Copyright (C) Inprise Corporation.
  33. *
  34. * All Rights Reserved.
  35. * Contributor(s): ______________________________________.
  36. */
  37. #include "example.h"
  38. #include <stdlib.h>
  39. int process_order (char *);
  40. EXEC SQL
  41. BEGIN DECLARE SECTION;
  42. EXEC SQL
  43. SET DATABASE empdb = "employee.fdb";
  44. long *t1;
  45. long *t2;
  46. EXEC SQL
  47. END DECLARE SECTION;
  48. int main(int argc, char** argv)
  49. {
  50. int ret = 0;
  51. BASED_ON sales.po_number pon;
  52. EXEC SQL
  53. WHENEVER SQLERROR GO TO Error;
  54. EXEC SQL
  55. CONNECT empdb;
  56. /* Go with read committed to see updates */
  57. EXEC SQL
  58. SET TRANSACTION READ COMMITTED;
  59. EXEC SQL
  60. DECLARE get_order CURSOR FOR
  61. SELECT po_number
  62. FROM sales
  63. WHERE order_status = "new"
  64. FOR UPDATE OF order_status;
  65. EXEC SQL
  66. EVENT INIT order_wait empdb ("new_order");
  67. while (!ret)
  68. {
  69. printf("\nStat 12 Waiting ...\n\n");
  70. EXEC SQL
  71. EVENT WAIT order_wait;
  72. EXEC SQL
  73. OPEN get_order;
  74. for (;;)
  75. {
  76. EXEC SQL
  77. FETCH get_order INTO :pon;
  78. if (SQLCODE == 100)
  79. break;
  80. EXEC SQL
  81. UPDATE sales
  82. SET order_status = "open"
  83. WHERE CURRENT OF get_order;
  84. ret = process_order(pon);
  85. }
  86. EXEC SQL
  87. CLOSE get_order;
  88. }
  89. EXEC SQL
  90. COMMIT;
  91. EXEC SQL
  92. DISCONNECT empdb;
  93. exit(0);
  94. Error:
  95. isc_print_sqlerror(SQLCODE, gds__status);
  96. exit(1);
  97. }
  98. /*
  99. * Initiate order processing for a newly received sales order.
  100. */
  101. int process_order(char* pon)
  102. {
  103. /*
  104. * This function would start a back-ground job, such as
  105. * sending the new orders to the printer, or generating
  106. * e-mail messages to the appropriate departments.
  107. */
  108. printf("Stat12: Received order: %s\n", pon);
  109. if (!strncmp(pon, "VNEW4", 5))
  110. {
  111. printf ("Stat12: exiting\n");
  112. return 1;
  113. }
  114. return 0;
  115. }