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