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

  1. /*
  2.  *  Program type:  API Interface
  3.  *
  4.  *  Description:
  5.  *        This program adds several departments with small default
  6.  *        budgets, using 'execute immediate' statement.
  7.  *        Then, a prepared statement, which doubles budgets for
  8.  *        departments with low budgets, is executed.
  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 <stdio.h>
  30. #include "example.h"
  31. #include <ibase.h>
  32.  
  33. #define MAXLEN      256
  34. #define DNAMELEN    25
  35. #define DNOLEN      3
  36.  
  37.  
  38. int getline (char ISC_FAR *, int);
  39. int cleanup (void);
  40.  
  41.  
  42.  
  43. isc_db_handle    DB = NULL;             /* database handle */
  44. isc_tr_handle    trans = NULL;          /* transaction handle */
  45. long             status[20];            /* status vector */
  46. char             Db_name[128];
  47.  
  48. int main (ARG(int, argc), ARG(char **, argv))
  49. ARGLIST(int argc)
  50. ARGLIST(char **argv)
  51. {
  52.     int                 n = 0;
  53.     char                exec_str[MAXLEN];
  54.     char                prep_str[MAXLEN];
  55.     isc_stmt_handle     double_budget = NULL;    /* statement handle */
  56.  
  57.     if (argc > 1)
  58.         strcpy(Db_name, argv[1]);
  59.     else
  60.         strcpy(Db_name, "employee.gdb");
  61.  
  62.     if (isc_attach_database(status, 0, Db_name, &DB, 0, NULL))
  63.     {
  64.         ERREXIT(status, 1)
  65.     }
  66.  
  67.     cleanup();
  68.  
  69.     /*
  70.      *  Prepare a statement, which may be executed more than once.
  71.      */
  72.  
  73.     strcpy(prep_str,
  74.            "UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
  75.  
  76.     /* Allocate a statement. */
  77.     if (isc_dsql_allocate_statement(status, &DB, &double_budget))
  78.     {
  79.         ERREXIT(status, 1)
  80.     }
  81.  
  82.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  83.     {
  84.         ERREXIT(status, 1)
  85.     }
  86.  
  87.     /* Prepare the statement. */
  88.     if (isc_dsql_prepare(status, &trans, &double_budget, 0, prep_str, 1, NULL))
  89.     {
  90.         ERREXIT(status, 1)
  91.     }
  92.  
  93.     /*
  94.      *  Add new departments, using 'execute immediate'.
  95.      *  Build each 'insert' statement, using the supplied parameters.
  96.      *  Since these statements will not be needed after they are executed,
  97.      *  use 'execute immediate'.
  98.      */
  99.     
  100.     while (getline(exec_str, n++))
  101.     {
  102.         printf("\nExecuting statement:\n%d:\t%s;\n", n, exec_str);
  103.  
  104.         if (isc_dsql_execute_immediate(status, &DB, &trans, 0, exec_str, 1, NULL))
  105.         {
  106.             ERREXIT(status, 1)
  107.         }
  108.     }   
  109.  
  110.     /*
  111.      *    Execute a previously prepared statement.
  112.      */
  113.     printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
  114.  
  115.     if (isc_dsql_execute(status, &trans, &double_budget, 1, NULL))
  116.     {
  117.         ERREXIT(status, 1)
  118.     }
  119.  
  120.     if (isc_dsql_free_statement(status, &double_budget, DSQL_drop))
  121.     {
  122.         ERREXIT(status, 1)
  123.     }
  124.  
  125.     if (isc_commit_transaction(status, &trans))
  126.     {
  127.         ERREXIT(status, 1)
  128.     }
  129.  
  130.     if (isc_detach_database(status, &DB))
  131.     {
  132.         ERREXIT(status, 1)
  133.     }
  134.  
  135.     return 0;
  136. }
  137.  
  138. /*
  139.  *  Construct an 'insert' statement from the supplied parameters.
  140.  */
  141.  
  142. int getline (ARG(char ISC_FAR*, line), ARG(int, line_no))
  143. ARGLIST(char ISC_FAR* line)
  144. ARGLIST(int           line_no)
  145. {
  146.     static char ISC_FAR * Dept_data[] =
  147.         {
  148.             "117", "Field Office: Hong Kong",   "110",
  149.             "118", "Field Office: Australia",   "110",
  150.             "119", "Field Office: New Zealand", "110",
  151.             0
  152.         };
  153.  
  154.     char    dept_no[4];
  155.     char    department[30];
  156.     char    dept_head[4];
  157.  
  158.     if (Dept_data[3 * line_no] == 0)
  159.         return 0;
  160.  
  161.     strcpy(dept_no, Dept_data[3 * line_no]);
  162.     strcpy(department, Dept_data[3 * line_no + 1]);
  163.     strcpy(dept_head, Dept_data[3 * line_no + 2]);
  164.  
  165.     sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
  166.             VALUES ('%s', '%s', '%s')", dept_no, department, dept_head);
  167.  
  168.     return 1;
  169. }
  170.  
  171. /*
  172.  *  Delete old data.
  173.  */
  174. int cleanup (void)
  175. {
  176.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  177.     {
  178.         ERREXIT(status, 1)
  179.     }
  180.  
  181.     if (isc_dsql_execute_immediate(status, &DB, &trans, 0,
  182.         "DELETE FROM department WHERE dept_no IN ('117', '118', '119')", 1, 0L))
  183.     {
  184.         ERREXIT(status, 1)
  185.     }
  186.  
  187.     if (isc_commit_transaction(status, &trans))
  188.     {
  189.         ERREXIT(status, 1)
  190.     }
  191.  
  192.     return 0;
  193. }
  194.