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 / api16t.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  6KB  |  185 lines

  1. /*
  2.  *  Program type:   API
  3.  *
  4.  *  Description:
  5.  *        If run from a Windows 3.1 Client, the winevent program
  6.  *        should be started before running this program and should
  7.  *        be terminated upon the completion of this program.
  8.  *        For other platforms, this program should be run in
  9.  *        conjunction with api16.
  10.  *
  11.  *        This Program adds some sales records, in order to trigger
  12.  *        the event that api16 or winevent is waiting for.
  13.  * The contents of this file are subject to the Interbase Public
  14.  * License Version 1.0 (the "License"); you may not use this file
  15.  * except in compliance with the License. You may obtain a copy
  16.  * of the License at http://www.Interbase.com/IPL/
  17.  *
  18.  * Software distributed under the License is distributed on an
  19.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  20.  * or implied. See the License for the specific language governing
  21.  * rights and limitations under the License.
  22.  *
  23.  * The Original Code was created by Interbase Software Corporation
  24.  * and its successors. Portions created by Borland/Inprise are
  25.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  26.  * created by InterBase Software Corporation are Copyright (C)
  27.  * 1998-1999 InterBase Software Corporation.
  28.  *
  29.  * Copyright (C) 2000 InterBase Software Corporation
  30.  * All Rights Reserved.
  31.  * Contributor(s): ______________________________________.
  32.  */
  33.  
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "example.h"
  38. #include <ibase.h>
  39.  
  40.  
  41. int main (ARG(int, argc), ARG(char **, argv))
  42. ARGLIST(int argc)
  43. ARGLIST(char **argv)
  44. {        
  45.     struct {
  46.         short    len;
  47.         char    data [9];
  48.     } po_number;
  49.     short               nullind = 0;
  50.     isc_stmt_handle     stmt = NULL;     /* statement handle */
  51.     isc_db_handle       DB = NULL;       /* database handle */
  52.     isc_tr_handle       trans = NULL;    /* transaction handle */
  53.     long                status[20];      /* status vector */
  54.     XSQLDA  ISC_FAR *   sqlda;
  55.     char                empdb[128];
  56.     char                *delete_str =
  57.         "DELETE FROM sales WHERE po_number LIKE 'VNEW%'";
  58.     char                *insert_str =
  59.         "INSERT INTO sales (po_number, cust_no, order_status, total_value) \
  60.         VALUES (?, 1015, 'new', 0)";
  61.  
  62.     if (argc > 1)
  63.         strcpy(empdb, argv[1]);
  64.     else
  65.         strcpy(empdb, "employee.gdb");
  66.  
  67.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  68.     {
  69.         ERREXIT(status, 1)
  70.     }
  71.  
  72.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  73.     {
  74.         ERREXIT(status, 1)
  75.     }
  76.  
  77.     /* Allocate an input SQLDA for po_number in insert string. */
  78.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(1));
  79.     sqlda->sqln = 1;
  80.     sqlda->sqld = 1;
  81.     sqlda->version = 1;
  82.  
  83.     /* Allocate a statement. */
  84.     if (isc_dsql_allocate_statement(status, &DB, &stmt))
  85.     {
  86.         ERREXIT(status, 1)
  87.     }
  88.  
  89.     /* Start out by deleting any existing records */
  90.  
  91.     if (isc_dsql_execute_immediate(status, &DB, &trans, 0, delete_str,
  92.                                    1, NULL))
  93.     {
  94.         ERREXIT(status, 2)
  95.     }
  96.  
  97.     if (isc_commit_transaction(status, &trans))
  98.     {
  99.         ERREXIT(status, 2)
  100.     }
  101.  
  102.  
  103.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  104.     {
  105.         ERREXIT(status, 3)
  106.     }
  107.  
  108.     /* Insert three records in one transaction */
  109.     if (isc_dsql_prepare(status, &trans, &stmt, 0, insert_str, 1, NULL))
  110.     {
  111.         ERREXIT(status, 4)
  112.     }
  113.     if (isc_dsql_describe_bind(status, &stmt, 1, sqlda))
  114.     {
  115.         ERREXIT(status, 5)
  116.     }
  117.  
  118.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  119.     sqlda->sqlvar[0].sqllen  = sizeof (po_number.data);
  120.     sqlda->sqlvar[0].sqldata = (char *) &po_number;
  121.     sqlda->sqlvar[0].sqlind  = &nullind;
  122.  
  123.     /* Add batch 1. */
  124.     po_number.len = strlen("VNEW1");
  125.     strncpy(po_number.data, "VNEW1", sizeof (po_number.data));
  126.     printf("api16t:  Adding %s\n", po_number.data);
  127.     if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  128.     {
  129.         ERREXIT(status, 6)
  130.     }
  131.  
  132.     po_number.len = strlen("VNEW2");
  133.     strncpy(po_number.data, "VNEW2", sizeof (po_number.data));
  134.     printf("api16t:  Adding %s\n", po_number.data);
  135.     if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  136.     {
  137.         ERREXIT(status, 6)
  138.     }
  139.  
  140.     po_number.len = strlen("VNEW3");
  141.     strncpy(po_number.data, "VNEW3", sizeof (po_number.data));
  142.     printf("api16t:  Adding %s\n", po_number.data);
  143.     if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  144.     {
  145.         ERREXIT(status, 6)
  146.     }
  147.  
  148.     /* This will fire the triggers for the first three records */
  149.     if (isc_commit_transaction(status, &trans))
  150.     {
  151.         ERREXIT(status, 7)
  152.     }
  153.      
  154.     /* Add batch 2. */
  155.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  156.     {
  157.         ERREXIT(status, 8)
  158.     }
  159.  
  160.     po_number.len = strlen("VNEW4");
  161.     strncpy(po_number.data, "VNEW4", sizeof (po_number.data));
  162.     printf("api16t:  Adding %s\n", po_number.data);
  163.     if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  164.     {
  165.         ERREXIT(status, 9)
  166.     }
  167.  
  168.     if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
  169.     {
  170.         ERREXIT(status, 10)
  171.     }
  172.  
  173.     /* This will fire the triggers for the fourth record */
  174.     if (isc_commit_transaction(status, &trans))
  175.     {
  176.         ERREXIT(status, 11)
  177.     }
  178.  
  179.     isc_detach_database(status, &DB);
  180.  
  181.     free(sqlda);
  182.  
  183.     return 0;
  184. }
  185.