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

  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.  *
  33.  * Copyright (C) 2000 Inprise Corporation
  34.  * All Rights Reserved.
  35.  * Contributor(s): ______________________________________.
  36.  */
  37.  
  38. #include "example.h"
  39. #include <stdlib.h>
  40.  
  41. int process_order (char *);
  42.  
  43. EXEC SQL
  44.     BEGIN DECLARE SECTION;
  45.  
  46. EXEC SQL
  47.     SET DATABASE empdb = "employee.gdb";
  48.  
  49. long    *t1;
  50. long    *t2;
  51.  
  52. EXEC SQL
  53.     END DECLARE SECTION;
  54.  
  55.  
  56. int main(ARG(int, argc), ARG(char **, argv))
  57. ARGLIST(int argc)
  58. ARGLIST(char **argv)
  59. {
  60.     int    ret = 0;
  61.     BASED_ON sales.po_number pon;
  62.  
  63.     EXEC SQL
  64.         WHENEVER SQLERROR GO TO Error;
  65.  
  66.     EXEC SQL
  67.         CONNECT empdb;
  68.  
  69.     /* Go with read committed to see updates */
  70.     EXEC SQL
  71.         SET TRANSACTION READ COMMITTED;
  72.  
  73.     EXEC SQL
  74.         DECLARE get_order CURSOR FOR
  75.         SELECT po_number
  76.         FROM sales
  77.         WHERE order_status = "new"
  78.         FOR UPDATE OF order_status;
  79.  
  80.     EXEC SQL
  81.         EVENT INIT order_wait empdb ("new_order");
  82.  
  83.     while (!ret)
  84.     {
  85.         printf("\nStat 12 Waiting ...\n\n");
  86.         EXEC SQL
  87.             EVENT WAIT order_wait;
  88.  
  89.         EXEC SQL
  90.             OPEN get_order;
  91.         for (;;)    
  92.         {
  93.             EXEC SQL
  94.                 FETCH get_order INTO :pon;
  95.  
  96.             if (SQLCODE == 100)
  97.                 break;
  98.  
  99.             EXEC SQL
  100.                 UPDATE sales
  101.                 SET order_status = "open"
  102.                 WHERE CURRENT OF get_order;
  103.  
  104.             ret = process_order(pon);
  105.  
  106.         }
  107.         EXEC SQL
  108.             CLOSE get_order;
  109.  
  110.     }
  111.     EXEC SQL 
  112.         COMMIT;
  113.  
  114.     EXEC SQL
  115.         DISCONNECT empdb;
  116.  
  117.     exit(0);
  118. Error:
  119.     isc_print_sqlerror(SQLCODE, gds__status);
  120.     exit(1);
  121. }
  122.  
  123.  
  124. /*
  125.  *    Initiate order processing for a newly received sales order.
  126.  */
  127. int process_order(ARG(char *, pon))
  128. ARGLIST(char * pon)
  129. {
  130.     /*
  131.      *    This function would start a back-ground job, such as
  132.      *    sending the new orders to the printer, or generating
  133.      *    e-mail messages to the appropriate departments.
  134.      */
  135.  
  136.     printf("Stat12:  Received order:  %s\n", pon);
  137.     if (!strncmp(pon, "VNEW4", 5))
  138.     {
  139.         printf ("Stat12: exiting\n");
  140.         return 1;
  141.     }
  142.     return 0;
  143. }
  144.