home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api3.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  5KB  |  160 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.Inprise.com/IPL.html
  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 Inprise Corporation
  22.  * and its predecessors. Portions created by Inprise Corporation are
  23.  *
  24.  * Copyright (C) 2000 Inprise Corporation
  25.  * All Rights Reserved.
  26.  * Contributor(s): ______________________________________.
  27.  */
  28.  
  29.  
  30.  
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <ibase.h>
  35. #include "example.h"
  36.  
  37. #define    LASTLEN     20
  38. #define    FIRSTLEN    15
  39. #define    EXTLEN       4
  40.  
  41. /* This macro is used to declare structures representing SQL VARCHAR types */
  42. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  43.  
  44. int main (ARG(int, argc), ARG(char **, argv))
  45. ARGLIST(int argc)
  46. ARGLIST(char **argv)
  47. {
  48.     SQL_VARCHAR(LASTLEN)    last_name;
  49.     SQL_VARCHAR(FIRSTLEN)   first_name;
  50.     char                    phone_ext[EXTLEN + 2];
  51.     short                   flag0 = 0, flag1 = 0;
  52.     short                   flag2 = 0;
  53.     isc_stmt_handle         stmt = NULL;                /* statement handle */
  54.     isc_db_handle           DB = NULL;                  /* database handle */
  55.     isc_tr_handle           trans = NULL;               /* transaction handle */
  56.     long                    status[20];                 /* status vector */
  57.     XSQLDA  ISC_FAR *       sqlda;
  58.     long                    fetch_stat;
  59.     char                    empdb[128];
  60.     char                    *sel_str =
  61.         "SELECT last_name, first_name, phone_ext FROM phone_list \
  62.         WHERE location = 'Monterey' ORDER BY last_name, first_name;";
  63.  
  64.     if (argc > 1)
  65.         strcpy(empdb, argv[1]);
  66.     else
  67.         strcpy(empdb, "employee.gdb");
  68.  
  69.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  70.         isc_print_status(status);
  71.  
  72.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  73.     {
  74.         ERREXIT(status, 1)
  75.     }
  76.     
  77.     /* Allocate an output SQLDA. */
  78.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(3));
  79.     sqlda->sqln = 3;
  80.     sqlda->sqld = 3;
  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.     /* Prepare the statement. */
  90.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  91.     {
  92.         ERREXIT(status, 1)
  93.     }
  94.     
  95.     /*
  96.      *  Although all three selected columns are of type varchar, the
  97.      *  third field's type is changed and printed as type TEXT.
  98.      */
  99.  
  100.     sqlda->sqlvar[0].sqldata = (char *)&last_name;
  101.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  102.     sqlda->sqlvar[0].sqlind  = &flag0;
  103.  
  104.     sqlda->sqlvar[1].sqldata = (char *)&first_name;
  105.     sqlda->sqlvar[1].sqltype = SQL_VARYING + 1;
  106.     sqlda->sqlvar[1].sqlind  = &flag1;
  107.  
  108.     sqlda->sqlvar[2].sqldata = (char ISC_FAR *) phone_ext;
  109.     sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  110.     sqlda->sqlvar[2].sqlind  = &flag2;
  111.  
  112.     printf("\n%-20s %-15s %-10s\n\n", "LAST NAME", "FIRST NAME", "EXTENSION");
  113.  
  114.     /* Execute the statement. */
  115.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  116.     {
  117.         ERREXIT(status, 1)
  118.     }
  119.             
  120.     /*
  121.      *    Fetch and print the records.
  122.      *    Status is 100 after the last row is fetched.
  123.      */
  124.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  125.     {
  126.         printf("%-20.*s ", last_name.vary_length, last_name.vary_string);
  127.  
  128.         printf("%-15.*s ", first_name.vary_length, first_name.vary_string);
  129.  
  130.         phone_ext[sqlda->sqlvar[2].sqllen] = '\0';
  131.         printf("%s\n", phone_ext);
  132.     }
  133.  
  134.     if (fetch_stat != 100L)
  135.     {
  136.         ERREXIT(status, 1)
  137.     }
  138.  
  139.     /* Free statement handle. */
  140.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  141.     {
  142.         ERREXIT(status, 1)
  143.     }
  144.  
  145.  
  146.     if (isc_commit_transaction(status, &trans))
  147.     {
  148.         ERREXIT(status, 1)
  149.     }
  150.  
  151.     if (isc_detach_database(status, &DB))
  152.     {
  153.         ERREXIT(status, 1)
  154.     }
  155.  
  156.     free( sqlda);
  157.  
  158.     return 0;
  159. }            
  160.