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 / api5.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  4KB  |  146 lines

  1. /*
  2.  *    Program type:  API Interface
  3.  *
  4.  *    Desription:
  5.  *        This program demonstrates the reallocation of SQLDA
  6.  *        and the 'isc_dsql_describe' statement.  After a query
  7.  *        is examined with 'isc_dsql_describe', an SQLDA of correct
  8.  *        size is reallocated, and some information is printed about
  9.  *        the query:  its type (select, non-select), the number
  10.  *        of columns, etc.
  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. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ibase.h>
  35. #include <stdio.h>
  36. #include "example.h"
  37.  
  38. char *sel_str =
  39.      "SELECT department, mngr_no, location, head_dept \
  40.       FROM department WHERE head_dept in ('100', '900', '600')";
  41.  
  42. isc_db_handle    DB = 0;                       /* database handle */
  43. isc_tr_handle    trans = 0;                    /* transaction handle */
  44. long             status[20];                   /* status vector */
  45.  
  46.  
  47. int main (ARG(int, argc), ARG(char **, argv))
  48. ARGLIST(int argc)
  49. ARGLIST(char **argv)
  50. {
  51.     int                num_cols, i;
  52.     isc_stmt_handle    stmt = NULL;
  53.     XSQLDA    ISC_FAR  *sqlda;
  54.     char               empdb[128];
  55.  
  56.     if (argc > 1)
  57.          strcpy(empdb, argv[1]);
  58.     else
  59.          strcpy(empdb, "employee.gdb");
  60.  
  61.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  62.     {
  63.         ERREXIT(status, 1)
  64.     }
  65.  
  66.     /* Allocate SQLDA of an arbitrary size. */
  67.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(3));
  68.     sqlda->sqln = 3;
  69.     sqlda->version = 1;
  70.  
  71.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  72.     {
  73.         ERREXIT(status, 1)
  74.     }
  75.  
  76.     /* Allocate a statement. */
  77.     if (isc_dsql_allocate_statement(status, &DB, &stmt))
  78.     {
  79.         ERREXIT(status, 1)
  80.     }
  81.  
  82.     /* Prepare the statement. */
  83.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  84.     {
  85.         ERREXIT(status, 1)
  86.     }
  87.  
  88.     /* Describe the statement. */
  89.     if (isc_dsql_describe(status, &stmt, 1, sqlda))
  90.     {
  91.         ERREXIT(status, 1)
  92.     }
  93.     
  94.     /* This is a select statement, print more information about it. */
  95.  
  96.     printf("Query Type:  SELECT\n\n");
  97.  
  98.     num_cols = sqlda->sqld;
  99.  
  100.     printf("Number of columns selected:  %d\n", num_cols);
  101.  
  102.     /* Reallocate SQLDA if necessary. */
  103.     if (sqlda->sqln < sqlda->sqld)
  104.     {
  105.         sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(num_cols));
  106.         sqlda->sqln = num_cols;
  107.         sqlda->version = 1;
  108.     
  109.         /* Re-describe the statement. */
  110.         if (isc_dsql_describe(status, &stmt, 1, sqlda))
  111.         {
  112.             ERREXIT(status, 1)
  113.         }
  114.  
  115.         num_cols = sqlda->sqld;
  116.     }
  117.  
  118.     /* List column names, types, and lengths. */
  119.     for (i = 0; i < num_cols; i++)
  120.     {
  121.         printf("\nColumn name:    %s\n", sqlda->sqlvar[i].sqlname);
  122.         printf("Column type:    %d\n", sqlda->sqlvar[i].sqltype);
  123.         printf("Column length:  %d\n", sqlda->sqlvar[i].sqllen);
  124.     }
  125.  
  126.  
  127.     if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
  128.     {
  129.         ERREXIT(status, 1)
  130.     }
  131.  
  132.     if (isc_commit_transaction(status, &trans))
  133.     {
  134.         ERREXIT(status, 1)
  135.     }
  136.  
  137.     if (isc_detach_database(status, &DB))
  138.     {
  139.         ERREXIT (status, 1)
  140.     }
  141.  
  142.     free(sqlda);
  143.  
  144.     return 0;
  145. }
  146.