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 / api3.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  5KB  |  163 lines

  1. /*
  2.  *    Program type:  API Interface
  3.  *
  4.  *    Description:
  5.  *        This program displays employee names and phone extensions.
  6.  *
  7.  *        It allocates an output SQLDA, prepares and executes a statement,
  8.  *        and loops fetching multiple rows.
  9.  *
  10.  *        The SQLCODE returned by fetch is checked.
  11.  * The contents of this file are subject to the Interbase Public
  12.  * License Version 1.0 (the "License"); you may not use this file
  13.  * except in compliance with the License. You may obtain a copy
  14.  * of the License at http://www.Interbase.com/IPL/
  15.  *
  16.  * Software distributed under the License is distributed on an
  17.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  18.  * or implied. See the License for the specific language governing
  19.  * rights and limitations under the License.
  20.  *
  21.  * The Original Code was created by Interbase Software Corporation
  22.  * and its successors. Portions created by Borland/Inprise are
  23.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  24.  * created by InterBase Software Corporation are Copyright (C)
  25.  * 1998-1999 InterBase Software Corporation.
  26.  *
  27.  * Copyright (C) 2000 InterBase Software Corporation
  28.  * All Rights Reserved.
  29.  * Contributor(s): ______________________________________.
  30.  */
  31.  
  32.  
  33.  
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <ibase.h>
  38. #include "example.h"
  39.  
  40. #define    LASTLEN     20
  41. #define    FIRSTLEN    15
  42. #define    EXTLEN       4
  43.  
  44. /* This macro is used to declare structures representing SQL VARCHAR types */
  45. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  46.  
  47. int main (ARG(int, argc), ARG(char **, argv))
  48. ARGLIST(int argc)
  49. ARGLIST(char **argv)
  50. {
  51.     SQL_VARCHAR(LASTLEN)    last_name;
  52.     SQL_VARCHAR(FIRSTLEN)   first_name;
  53.     char                    phone_ext[EXTLEN + 2];
  54.     short                   flag0 = 0, flag1 = 0;
  55.     short                   flag2 = 0;
  56.     isc_stmt_handle         stmt = NULL;                /* statement handle */
  57.     isc_db_handle           DB = NULL;                  /* database handle */
  58.     isc_tr_handle           trans = NULL;               /* transaction handle */
  59.     long                    status[20];                 /* status vector */
  60.     XSQLDA  ISC_FAR *       sqlda;
  61.     long                    fetch_stat;
  62.     char                    empdb[128];
  63.     char                    *sel_str =
  64.         "SELECT last_name, first_name, phone_ext FROM phone_list \
  65.         WHERE location = 'Monterey' ORDER BY last_name, first_name;";
  66.  
  67.     if (argc > 1)
  68.         strcpy(empdb, argv[1]);
  69.     else
  70.         strcpy(empdb, "employee.gdb");
  71.  
  72.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  73.         isc_print_status(status);
  74.  
  75.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  76.     {
  77.         ERREXIT(status, 1)
  78.     }
  79.     
  80.     /* Allocate an output SQLDA. */
  81.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(3));
  82.     sqlda->sqln = 3;
  83.     sqlda->sqld = 3;
  84.     sqlda->version = 1;
  85.  
  86.     /* Allocate a statement. */
  87.     if (isc_dsql_allocate_statement(status, &DB, &stmt))
  88.     {
  89.         ERREXIT(status, 1)
  90.     }
  91.     
  92.     /* Prepare the statement. */
  93.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  94.     {
  95.         ERREXIT(status, 1)
  96.     }
  97.     
  98.     /*
  99.      *  Although all three selected columns are of type varchar, the
  100.      *  third field's type is changed and printed as type TEXT.
  101.      */
  102.  
  103.     sqlda->sqlvar[0].sqldata = (char *)&last_name;
  104.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  105.     sqlda->sqlvar[0].sqlind  = &flag0;
  106.  
  107.     sqlda->sqlvar[1].sqldata = (char *)&first_name;
  108.     sqlda->sqlvar[1].sqltype = SQL_VARYING + 1;
  109.     sqlda->sqlvar[1].sqlind  = &flag1;
  110.  
  111.     sqlda->sqlvar[2].sqldata = (char ISC_FAR *) phone_ext;
  112.     sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  113.     sqlda->sqlvar[2].sqlind  = &flag2;
  114.  
  115.     printf("\n%-20s %-15s %-10s\n\n", "LAST NAME", "FIRST NAME", "EXTENSION");
  116.  
  117.     /* Execute the statement. */
  118.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  119.     {
  120.         ERREXIT(status, 1)
  121.     }
  122.             
  123.     /*
  124.      *    Fetch and print the records.
  125.      *    Status is 100 after the last row is fetched.
  126.      */
  127.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  128.     {
  129.         printf("%-20.*s ", last_name.vary_length, last_name.vary_string);
  130.  
  131.         printf("%-15.*s ", first_name.vary_length, first_name.vary_string);
  132.  
  133.         phone_ext[sqlda->sqlvar[2].sqllen] = '\0';
  134.         printf("%s\n", phone_ext);
  135.     }
  136.  
  137.     if (fetch_stat != 100L)
  138.     {
  139.         ERREXIT(status, 1)
  140.     }
  141.  
  142.     /* Free statement handle. */
  143.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  144.     {
  145.         ERREXIT(status, 1)
  146.     }
  147.  
  148.  
  149.     if (isc_commit_transaction(status, &trans))
  150.     {
  151.         ERREXIT(status, 1)
  152.     }
  153.  
  154.     if (isc_detach_database(status, &DB))
  155.     {
  156.         ERREXIT(status, 1)
  157.     }
  158.  
  159.     free( sqlda);
  160.  
  161.     return 0;
  162. }            
  163.