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