home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api11.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  6KB  |  242 lines

  1. /*
  2.  *    Program type:  API Interface
  3.  *
  4.  *    Description:
  5.  *        This program executes a stored procedure and selects from
  6.  *        a stored procedure.  First, a list of projects an employee
  7.  *        is involved in is printed.  Then the employee is added to
  8.  *        another project.  The new list of projects is printed again.
  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 <stdlib.h>
  28. #include <string.h>
  29. #include <ibase.h>
  30. #include <stdio.h>
  31. #include "example.h"
  32.  
  33. #define    PROJLEN        5
  34. #define    BUFLEN        128
  35.  
  36. int select_projects (isc_db_handle db, int emp_no);
  37. int add_emp_proj (isc_db_handle db, int emp_no, char ISC_FAR * proj_id);
  38. int get_params (isc_db_handle db, int ISC_FAR * emp_no, char ISC_FAR * proj_id);
  39.  
  40. int main (ARG(int, argc), ARG(char **, argv))
  41. ARGLIST(int argc)
  42. ARGLIST(char **argv)
  43. {
  44.     int             emp_no;
  45.     char            proj_id[PROJLEN + 2];
  46.     isc_db_handle    db = NULL;
  47.     long            status[20];
  48.     char            empdb[128];
  49.  
  50.     if (argc > 1)
  51.         strcpy(empdb, argv[1]);
  52.     else
  53.         strcpy(empdb, "employee.gdb");
  54.  
  55.     if (isc_attach_database(status, 0, empdb, &db, 0, NULL))
  56.     {
  57.         ERREXIT(status, 1)
  58.     }
  59.  
  60.     /*
  61.      *  Add employee with id 8 to project 'MAPDB'.
  62.      */
  63.     if (get_params(db, &emp_no, proj_id))
  64.         return 1;
  65.  
  66.     /*
  67.      *  Display employee's current projects.
  68.      */
  69.     printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  70.     if (select_projects(db, emp_no))
  71.         return 1;
  72.  
  73.     /*
  74.      *  Insert a new employee project row.
  75.      */
  76.     printf("\nAdd employee id: %d to project: %s\n", emp_no, proj_id);
  77.     if (add_emp_proj(db, emp_no, proj_id))
  78.         return 1;
  79.  
  80.     /*
  81.      *  Display employee's new current projects.
  82.      */
  83.     printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  84.     if (select_projects(db, emp_no))
  85.         return 1;
  86.  
  87.  
  88.     if (isc_detach_database(status, &db))
  89.     {
  90.         ERREXIT(status, 1)
  91.     }
  92.  
  93.     return  0 ;
  94. }
  95.  
  96. /*
  97.  *    Select from a stored procedure.
  98.  *    Procedure 'get_emp_proj' gets employee's projects.
  99.  */
  100. int select_projects (ARG(isc_db_handle, db), ARG(int, emp_no))
  101. ARGLIST(void    *db)
  102. ARGLIST(int     emp_no)
  103. {
  104.     char            proj_id[PROJLEN + 2];
  105.     char            selstr[BUFLEN];
  106.     short           flag0 = 0;
  107.     isc_tr_handle   trans = NULL;
  108.     long            status[20];
  109.     isc_stmt_handle stmt = NULL;
  110.     XSQLDA  ISC_FAR *sqlda;
  111.     long            fetch_stat;
  112.  
  113.     sprintf(selstr, "SELECT proj_id FROM get_emp_proj (%d) ORDER BY proj_id",
  114.             emp_no);
  115.  
  116.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  117.     sqlda->sqln = 1;
  118.     sqlda->version = 1;
  119.  
  120.     if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  121.     {
  122.         ERREXIT(status, 1)
  123.     }
  124.  
  125.     if (isc_dsql_allocate_statement(status, &db, &stmt))
  126.     {
  127.         ERREXIT(status, 1)
  128.     }
  129.  
  130.     if (isc_dsql_prepare(status, &trans, &stmt, 0, selstr, 1, sqlda))
  131.     {
  132.         ERREXIT(status, 1)
  133.     }
  134.  
  135.     sqlda->sqlvar[0].sqldata = (char ISC_FAR *) proj_id;
  136.     sqlda->sqlvar[0].sqlind  = &flag0;
  137.     sqlda->sqlvar[0].sqltype = SQL_TEXT + 1;
  138.  
  139.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  140.     {
  141.         ERREXIT(status, 1)
  142.     }
  143.  
  144.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  145.     {
  146.         proj_id[PROJLEN] = '\0';
  147.         printf("\t%s\n", proj_id);
  148.     }
  149.  
  150.     if (fetch_stat != 100L)
  151.     {
  152.         ERREXIT(status, 1)
  153.     }
  154.  
  155.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  156.     {
  157.         ERREXIT(status, 1)
  158.     }
  159.                               
  160.     if (isc_commit_transaction(status, &trans))
  161.     {
  162.         ERREXIT(status, 1)
  163.     }
  164.  
  165.     free(sqlda);
  166.  
  167.     return 0;
  168. }
  169.  
  170. /*
  171.  *    Execute a stored procedure.
  172.  *    Procedure 'add_emp_proj' adds an employee to a project.
  173.  */
  174. int add_emp_proj (ARG(isc_db_handle, db), ARG(int, emp_no), ARG(char ISC_FAR *, proj_id))
  175. ARGLIST(void    *db)
  176. ARGLIST(int     emp_no)
  177. ARGLIST(char    *proj_id)
  178. {
  179.     char            exec_str[BUFLEN];
  180.     isc_tr_handle   trans = NULL;
  181.     long            status[20];
  182.  
  183.     sprintf(exec_str, "EXECUTE PROCEDURE add_emp_proj %d, \"%s\"",
  184.             emp_no, proj_id);
  185.  
  186.     if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  187.     {
  188.         ERREXIT(status, 1)
  189.     }
  190.  
  191.     if (isc_dsql_execute_immediate(status, &db, &trans, 0, exec_str, 1, NULL))
  192.     {
  193.         ERREXIT(status, 1)
  194.     }
  195.  
  196.     if (isc_commit_transaction(status, &trans))
  197.     {
  198.         ERREXIT (status, 1)
  199.     }
  200.  
  201.     return 0;
  202. }   
  203.  
  204. /*
  205.  *    Set-up procedure parameters and clean-up old data.
  206.  */
  207. int get_params (ARG(void ISC_FAR *, db),
  208.                 ARG(int ISC_FAR *, emp_no),
  209.                 ARG(char ISC_FAR *, proj_id))
  210. ARGLIST(void    *db)
  211. ARGLIST(int     *emp_no)
  212. ARGLIST(char    *proj_id)
  213. {
  214.     isc_tr_handle   trans = NULL;
  215.     long            status[20];
  216.  
  217.     *emp_no = 8;
  218.     strcpy(proj_id, "MAPDB");
  219.                      
  220.     /* Cleanup:  delete row from the previous run. */
  221.  
  222.     if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  223.     {
  224.         ERREXIT(status, 1)
  225.     }
  226.  
  227.     if (isc_dsql_execute_immediate(status, &db, &trans, 0,
  228.                                    "DELETE FROM employee_project \
  229.                                    WHERE emp_no = 8 AND proj_id = 'MAPDB'", 1,
  230.                                    NULL))
  231.     {
  232.         ERREXIT(status, 1)
  233.     }
  234.  
  235.     if (isc_commit_transaction(status, &trans))
  236.     {
  237.         ERREXIT(status, 1)
  238.     }
  239.  
  240.     return 0;
  241. }
  242.