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

  1. /*
  2.  *  Program type:   Embedded Static SQL
  3.  *
  4.  *  Description:
  5.  *        This program declares and creates a new table.
  6.  *        Some rows, describing a department structure,
  7.  *        are added to the new table as a test.
  8.  *
  9.  *        The table is created temporarily for figuring
  10.  *        out which level in the department structure each
  11.  *        department belongs too.
  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 "example.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33.  
  34. void build_tree (void);
  35. void pr_error (void);
  36.  
  37. EXEC SQL
  38.     BEGIN DECLARE SECTION;
  39. EXEC SQL
  40.     END DECLARE SECTION;
  41.  
  42.  
  43. int main (void)
  44. {
  45.     char    dept[4];
  46.     long        lvl = 1;
  47.  
  48.     /* Describe the new table's structure. */
  49.     EXEC SQL
  50.         DECLARE tmp_dept_tree TABLE (
  51.             dept_no CHAR(3) NOT NULL PRIMARY KEY,
  52.             tree_level INTEGER);
  53.  
  54.     /* Drop the table in case it exists. Ignore error */
  55.     EXEC SQL
  56.         DROP TABLE tmp_dept_tree;
  57.  
  58.     EXEC SQL
  59.         WHENEVER SQLERROR GO TO Error1;
  60.     /* Create the new table. */
  61.     printf ("creating tmp_dept_tree\n");
  62.     EXEC SQL
  63.         CREATE TABLE tmp_dept_tree (
  64.             dept_no CHAR(3) NOT NULL PRIMARY KEY,
  65.             tree_level INTEGER);
  66.  
  67.     /* Fill the new table. */
  68.     build_tree();
  69.  
  70.     /* Look at it */
  71.     EXEC SQL DECLARE dc CURSOR FOR
  72.         SELECT dept_no, tree_level
  73.         FROM tmp_dept_tree;
  74.  
  75.     EXEC SQL 
  76.         OPEN dc;
  77.  
  78.     EXEC SQL 
  79.         FETCH dc INTO :dept, :lvl;
  80.  
  81.     while (SQLCODE == 0)
  82.     {
  83.     printf ("Dept = %s:  Level = %d\n", dept, lvl);
  84.     EXEC SQL 
  85.         FETCH dc INTO :dept, :lvl;
  86.     }
  87.     EXEC SQL
  88.         COMMIT RELEASE;
  89.  
  90. return 0;
  91.  
  92. Error1:
  93.         pr_error();
  94. return 1;
  95. }
  96.  
  97.  
  98. /*
  99.  *  For each department find its sub-departments and mark them
  100.  *  with the next level number.
  101.  */
  102. void build_tree (void)
  103. {
  104.     char    dept[4];
  105.     long        lvl = 1;
  106.  
  107.     EXEC SQL
  108.         WHENEVER SQLERROR GO TO Error2;
  109.  
  110.     /* Initialize the department structure by adding the first level.  */
  111.     EXEC SQL
  112.         INSERT INTO tmp_dept_tree VALUES ('000', :lvl);
  113.  
  114.     /* Declare the cursor for selecting all departments with level 'lvl'. */
  115.     EXEC SQL
  116.         DECLARE ds CURSOR FOR
  117.         SELECT dept_no
  118.         FROM tmp_dept_tree
  119.         WHERE tree_level = :lvl;
  120.     
  121.     /* For each department with level 'lvl', find its sub-departments. */
  122.     while (SQLCODE == 0)
  123.     {
  124.         EXEC SQL
  125.             OPEN ds;
  126.  
  127.         /* Initialize the next level. */
  128.         lvl++;
  129.  
  130.         /* Add all the sub-departments of the next level to the table. */
  131.         for (;;)
  132.         {
  133.             EXEC SQL
  134.                 FETCH ds INTO :dept;
  135.  
  136.             if (SQLCODE == 100)
  137.                 break;
  138.  
  139.             EXEC SQL
  140.                 INSERT INTO tmp_dept_tree
  141.                 SELECT dept_no, :lvl
  142.                 FROM department
  143.                 WHERE head_dept = :dept;
  144.         }
  145.  
  146.         EXEC SQL
  147.             CLOSE ds;
  148.         EXEC SQL
  149.             COMMIT;
  150.  
  151.         /* Done, if no next level was created by the INSERT above. */
  152.         EXEC SQL
  153.             SELECT tree_level
  154.             FROM tmp_dept_tree
  155.             WHERE tree_level = :lvl;
  156.  
  157.         if (SQLCODE == 100)
  158.             return;
  159.     };
  160.  
  161. Error2:
  162.     pr_error();
  163. return ;
  164. }
  165.  
  166.  
  167. /*
  168.  *    Print any error and exit.
  169.  */
  170. void pr_error (void)
  171. {
  172.     isc_print_sqlerror((short)SQLCODE, gds__status);
  173. }
  174.