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

  1. /*
  2.  *    Program type:  API Interface
  3.  *
  4.  *    Description:
  5.  *        This program combines the three programming styles:
  6.  *        static SQL, dynamic SQL, and the API interface.
  7.  *
  8.  *        Employee information is retrieved and printed for some set
  9.  *        of employees.  A predefined set of columns is always printed.
  10.  *        However, the 'where' clause, defining which employees the report
  11.  *        is to be printed for, is unknown.
  12.  *
  13.  *        Dynamic SQL is utilized to construct the select statement.
  14.  *        The 'where' clause is assumed to be passed as a parameter.
  15.  *
  16.  *        Static SQL is used for known SQL statements.
  17.  *
  18.  *        The API interface is used to access the two databases, and
  19.  *        to control most transactions.  The database and transaction
  20.  *        handles are shared between the three interfaces.
  21.  * The contents of this file are subject to the Interbase Public
  22.  * License Version 1.0 (the "License"); you may not use this file
  23.  * except in compliance with the License. You may obtain a copy
  24.  * of the License at http://www.Inprise.com/IPL.html
  25.  *
  26.  * Software distributed under the License is distributed on an
  27.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  28.  * or implied. See the License for the specific language governing
  29.  * rights and limitations under the License.
  30.  *
  31.  * The Original Code was created by Inprise Corporation
  32.  * and its predecessors. Portions created by Inprise Corporation are
  33.  *
  34.  * Copyright (C) 2000 Inprise Corporation
  35.  * All Rights Reserved.
  36.  * Contributor(s): ______________________________________.
  37.  */
  38.  
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include "example.h"
  43.  
  44. #define BUFLEN        1024
  45.  
  46. char    *sel_str =
  47.     "SELECT full_name, dept_no, salary, job_code, job_grade, job_country \
  48.      FROM employee";
  49.  
  50. char    *where_str =
  51.     "WHERE job_code = 'SRep' AND dept_no IN (110, 140, 115, 125, 123, 121)";
  52.  
  53. /* This macro is used to declare structures representing SQL VARCHAR types */
  54. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  55.  
  56. char    Db_name[128];
  57.  
  58. EXEC SQL
  59.     SET DATABASE db1 = "employee.gdb" RUNTIME :Db_name;
  60.  
  61.  
  62.  
  63. int main(ARG(int, argc), ARG(char **, argv))
  64. ARGLIST(int argc)
  65. ARGLIST(char **argv)
  66. {
  67.     BASED_ON employee.salary        salary;
  68.     SQL_VARCHAR(5)                job_code;
  69.     BASED_ON employee.job_grade        job_grade;
  70.     SQL_VARCHAR(15)                job_country;
  71.     SQL_VARCHAR(37)                full_name;
  72.     BASED_ON country.currency        currency;
  73.     BASED_ON department.dept_no        dept_no;
  74.     BASED_ON department.department        department;
  75.     char    buf[BUFLEN + 1];
  76.     float    rate;
  77.     void    *trans1 = NULL;            /* transaction handle */
  78.     void    *trans2 = NULL;            /* transaction handle */
  79.     long    status[20];
  80.     XSQLDA    *sqlda;
  81.         char    empdb2[128];
  82.  
  83.         if (argc > 1)
  84.                 strcpy(Db_name, argv[1]);
  85.         else
  86.                 strcpy(Db_name, "employee.gdb");
  87.         if (argc > 2)
  88.                 strcpy(empdb2, argv[2]);
  89.         else
  90.                 strcpy(empdb2, "employe2.gdb");
  91.  
  92.  
  93.     EXEC SQL
  94.         WHENEVER SQLERROR GO TO Error;
  95.  
  96.     /*
  97.      *    Set-up the select query.  The select portion of the query is
  98.      *    static, while the 'where' clause is determined elsewhere and
  99.      *    passed as a parameter.
  100.      */
  101.      sprintf(buf, "%s %s", sel_str, where_str);
  102.  
  103.  
  104.     /*
  105.      *    Open the employee database.
  106.      */
  107.     if (isc_attach_database(status, 0, Db_name, &db1, 0, NULL))
  108.         isc_print_status(status);
  109.  
  110.     /*
  111.      *    Prepare the select query.
  112.      */
  113.  
  114.     sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(6));
  115.     sqlda->sqln = 6;
  116.     sqlda->sqld = 6;
  117.     sqlda->version = 1;
  118.  
  119.     EXEC SQL
  120.         SET TRANSACTION USING db1;
  121.     EXEC SQL
  122.         PREPARE q INTO SQL DESCRIPTOR sqlda FROM :buf;
  123.     EXEC SQL
  124.         COMMIT ;
  125.  
  126.     sqlda->sqlvar[0].sqldata = (char *)&full_name;
  127.     sqlda->sqlvar[0].sqltype = SQL_VARYING;
  128.  
  129.     sqlda->sqlvar[1].sqldata = dept_no;
  130.     sqlda->sqlvar[1].sqltype = SQL_TEXT;
  131.  
  132.     sqlda->sqlvar[2].sqldata = (char *) &salary;
  133.     sqlda->sqlvar[2].sqltype = SQL_DOUBLE;
  134.  
  135.     sqlda->sqlvar[3].sqldata = (char *)&job_code;
  136.     sqlda->sqlvar[3].sqltype = SQL_VARYING;
  137.  
  138.     sqlda->sqlvar[4].sqldata = (char *) &job_grade;
  139.     sqlda->sqlvar[4].sqltype = SQL_SHORT;
  140.  
  141.     sqlda->sqlvar[5].sqldata = (char *)&job_country;
  142.     sqlda->sqlvar[5].sqltype = SQL_VARYING;
  143.  
  144.  
  145.     /*
  146.      *    Open the second database.
  147.      */
  148.  
  149.     EXEC SQL
  150.             SET DATABASE db2 = "employe2.gdb";
  151.  
  152.     if (isc_attach_database(status, 0, empdb2, &db2, 0, NULL))
  153.         isc_print_status(status);
  154.  
  155.  
  156.     /*
  157.      *    Select the employees, using the dynamically allocated SQLDA.
  158.      */
  159.  
  160.     EXEC SQL
  161.         DECLARE emp CURSOR FOR q;
  162.      
  163.     if (isc_start_transaction(status, &trans1, 1, &db1, 0, NULL))
  164.         isc_print_status(status);
  165.  
  166.     EXEC SQL
  167.         OPEN TRANSACTION trans1 emp;
  168.  
  169.     while (SQLCODE == 0)
  170.     {
  171.         EXEC SQL
  172.             FETCH emp USING SQL DESCRIPTOR sqlda;
  173.  
  174.         if (SQLCODE == 100)
  175.             break;
  176.  
  177.         /*
  178.          *    Get the department name, using a static SQL statement.
  179.          */
  180.         EXEC SQL
  181.             SELECT TRANSACTION trans1 department
  182.             INTO :department
  183.             FROM department
  184.             WHERE dept_no = :dept_no;
  185.  
  186.         /*
  187.          *    If the job country is not USA, access the second database
  188.          *    in order to get the conversion rate between different money
  189.          *    types.  Even though the conversion rate may fluctuate, all
  190.          *    salaries will be presented in US dollars for relative comparison.
  191.          */
  192.         job_country.vary_string[job_country.vary_length] = '\0';
  193.         if (strcmp(job_country.vary_string, "USA"))
  194.         {
  195.             EXEC SQL
  196.                 SELECT TRANSACTION trans1 currency
  197.                 INTO :currency
  198.                 FROM country
  199.                 WHERE country = :job_country.vary_string :job_country.vary_length;
  200.  
  201.             if (isc_start_transaction(status, &trans2, 1, &db2, 0, NULL))
  202.                 isc_print_status(status);
  203.  
  204.             EXEC SQL
  205.                 SELECT TRANSACTION trans2 conv_rate
  206.                 INTO :rate
  207.                 FROM cross_rate
  208.                 WHERE from_currency = 'Dollar'
  209.                 AND to_currency = :currency;
  210.  
  211.             if (!SQLCODE)
  212.                 salary = salary / rate;
  213.  
  214.             if (isc_commit_transaction (status, &trans2))
  215.                 isc_print_status(status);
  216.         }
  217.  
  218.         /*
  219.          *    Print the results.
  220.          */
  221.  
  222.         printf("%-20.*s ", full_name.vary_length, full_name.vary_string);
  223.         fflush (stdout);
  224.         printf("%8.2f ", salary);
  225.         fflush (stdout);
  226.         printf("   %-5.*s %d", job_code.vary_length, job_code.vary_string, job_grade);
  227.         fflush (stdout);
  228.         printf("    %-20s\n", department);
  229.         fflush (stdout);
  230.     }
  231.  
  232.     EXEC SQL
  233.         CLOSE emp;
  234.      
  235.     if (isc_commit_transaction (status, &trans1))
  236.         isc_print_status(status);
  237.  
  238.     isc_detach_database(status, &db1);
  239.     isc_detach_database(status, &db2);
  240.  
  241.     return(0);
  242.  
  243.  
  244. Error:
  245.     printf("\n");
  246.     isc_print_sqlerror(SQLCODE, isc_status);
  247.     return(1);
  248. }
  249.  
  250.