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

  1. /*
  2.  *    Program type:   Embedded Static SQL
  3.  *
  4.  *    Description:
  5.  *        This program demonstrates 'set database', 'connect',
  6.  *        and 'set transaction' statements.
  7.  *        Each time a database is connected to, a sample table
  8.  *        is accessed as a test.
  9.  * The contents of this file are subject to the Interbase Public
  10.  * License Version 1.0 (the "License"); you may not use this file
  11.  * except in compliance with the License. You may obtain a copy
  12.  * of the License at http://www.Inprise.com/IPL.html
  13.  *
  14.  * Software distributed under the License is distributed on an
  15.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16.  * or implied. See the License for the specific language governing
  17.  * rights and limitations under the License.
  18.  *
  19.  * The Original Code was created by Inprise Corporation
  20.  * and its predecessors. Portions created by Inprise Corporation are
  21.  *
  22.  * Copyright (C) 2000 Inprise Corporation
  23.  * All Rights Reserved.
  24.  * Contributor(s): ______________________________________.
  25.  */
  26.  
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30.  
  31. int count_types (void);
  32. int count_records (void);
  33. long pr_error (void);
  34.  
  35. char *dbname = "employee.gdb";
  36.  
  37. EXEC SQL INCLUDE SQLCA;
  38.  
  39.  
  40.  
  41. int main  (void)
  42. {
  43.     /*
  44.      *    Declare 2 database handles for future use.
  45.      */
  46.  
  47.     EXEC SQL
  48.         SET DATABASE db1 = "employee.gdb";
  49.  
  50.     EXEC SQL
  51.         SET DATABASE db2 = "employe2.gdb";
  52.  
  53.  
  54.     /*
  55.      *    Open a single database.
  56.      */
  57.  
  58.     printf("\n1. Opening database employee.gdb.\n");
  59.  
  60.     EXEC SQL
  61.         CONNECT db1;
  62.  
  63.     if( pr_error())
  64.         return 1;;
  65.  
  66.     EXEC SQL
  67.         SET TRANSACTION USING db1;
  68.  
  69.     if (count_types())
  70.         return 1;
  71.  
  72.     EXEC SQL
  73.         COMMIT RELEASE;
  74.  
  75.     EXEC SQL
  76.         DISCONNECT db1;
  77.  
  78.  
  79.     /*
  80.      *    Use a database name supplied at run-time.
  81.      *    Connect to this database using an existing database handle.
  82.      */
  83.  
  84.     printf("\n2. Opening database with name: %s supplied at run-time.\n",
  85.                     dbname);
  86.     EXEC SQL
  87.         CONNECT TO :dbname AS db1;
  88.  
  89.     if( pr_error())
  90.         return 1;;
  91.  
  92.     EXEC SQL
  93.         SET TRANSACTION USING db1;
  94.  
  95.     if( count_types())
  96.         return 1;
  97.  
  98.     EXEC SQL
  99.         COMMIT RELEASE;
  100.  
  101.     EXEC SQL
  102.         DISCONNECT DEFAULT;
  103.  
  104.  
  105.     /*
  106.      *    Open the second database within the same program,
  107.      *    while the first database remains disconnected.
  108.      */
  109.     printf("\n3. Opening a second database after closing the first one.\n");
  110.  
  111.     EXEC SQL
  112.         CONNECT db2;
  113.  
  114.     if( pr_error())
  115.         return 1;;
  116.  
  117.     EXEC SQL
  118.         SET TRANSACTION USING db2;
  119.  
  120.     if (count_records())
  121.         return 1;
  122.  
  123.     EXEC SQL
  124.         COMMIT RELEASE;
  125.  
  126.     EXEC SQL
  127.         DISCONNECT db2;
  128.  
  129.  
  130.     /*
  131.      *    Open two databases simultaneously.
  132.      */
  133.     printf("\n4. Opening two databases simultaneously.\n");
  134.  
  135.     EXEC SQL
  136.         CONNECT TO db1, db2;
  137.  
  138.     if( pr_error())
  139.         return 1;;
  140.  
  141.     EXEC SQL
  142.         SET TRANSACTION USING db1, db2;
  143.  
  144.     if (count_types())
  145.         return 1;;
  146.     if (count_records())
  147.         return 1;
  148.  
  149.     EXEC SQL
  150.         COMMIT RELEASE;
  151.  
  152.     EXEC SQL
  153.         DISCONNECT db1, db2;
  154.  
  155.  
  156.     /*
  157.      *    Open all databases (in this case just two) at the same time.
  158.      */
  159.     printf("\n5. Opening all databases.\n");
  160.  
  161.     EXEC SQL
  162.         CONNECT TO ALL;
  163.  
  164.     if( pr_error())
  165.         return 1;;
  166.  
  167.     EXEC SQL
  168.         SET TRANSACTION;
  169.  
  170.     if (count_types())
  171.         return 1;
  172.     if (count_records())
  173.         return 1;
  174.  
  175.     EXEC SQL
  176.         COMMIT RELEASE;
  177.  
  178.     EXEC SQL
  179.         DISCONNECT ALL;
  180. return (0);
  181. }
  182.  
  183.  
  184. /*
  185.  *    Access a table in database 1.
  186.  */
  187. int count_types (void)
  188. {
  189.     long cnt;
  190.  
  191.     EXEC SQL
  192.         SELECT COUNT(DISTINCT currency) INTO :cnt FROM country;
  193.  
  194.     if (SQLCODE == 0)
  195.         printf("\tNumber of currency types    :  %d\n", cnt);
  196.     else 
  197.         if( pr_error())
  198.             return 1;;
  199. return (0);
  200. }
  201.  
  202.  
  203. /*
  204.  *    Access a table in database 2.
  205.  */
  206. int count_records (void)
  207. {
  208.     long cnt;
  209.  
  210.     /* Use the database handle along with the table name. */
  211.     EXEC SQL
  212.         SELECT COUNT(DISTINCT to_currency) INTO :cnt FROM db2.cross_rate;
  213.  
  214.     if (SQLCODE == 0)
  215.         printf("\tNumber of conversion records:  %d\n", cnt);
  216.     else 
  217.         if( pr_error())
  218.             return 1;;
  219. return (0);
  220. }
  221.  
  222.  
  223. /*
  224.  *    Print an error message.
  225.  */
  226. long pr_error (void)
  227. {
  228.     if (SQLCODE)
  229.     {
  230.         isc_print_sqlerror(SQLCODE, isc_status);
  231.         printf("\n");
  232.     }
  233. return (SQLCODE);
  234. }
  235.