home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / gpre / dyn5.e < prev    next >
Encoding:
Text File  |  2001-01-05  |  3.2 KB  |  128 lines

  1. /*
  2.  *  Program type:   Embedded Dynamic SQL
  3.  *
  4.  *    Description:
  5.  *        This program demonstrates the reallocation of SQLDA and
  6.  *        the 'describe' statement.  After a query is examined with
  7.  *        'describe', an SQLDA of correct size is reallocated, and some
  8.  *        information is printed about the query:  its type (select,
  9.  *        non-select), the number of columns, etc.
  10.  * The contents of this file are subject to the Interbase Public
  11.  * License Version 1.0 (the "License"); you may not use this file
  12.  * except in compliance with the License. You may obtain a copy
  13.  * of the License at http://www.Inprise.com/IPL.html
  14.  *
  15.  * Software distributed under the License is distributed on an
  16.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  17.  * or implied. See the License for the specific language governing
  18.  * rights and limitations under the License.
  19.  *
  20.  * The Original Code was created by Inprise Corporation
  21.  * and its predecessors. Portions created by Inprise Corporation are
  22.  *
  23.  * Copyright (C) 2000 Inprise Corporation
  24.  * All Rights Reserved.
  25.  * Contributor(s): ______________________________________.
  26.  */
  27.  
  28.  
  29. #include "example.h"
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. char *sel_str =
  34.     "SELECT department, mngr_no, location, head_dept \
  35.      FROM department WHERE head_dept in ('100', '900', '600')";
  36.  
  37. char Db_name[128];
  38.  
  39. EXEC SQL
  40.     SET DATABASE empdb = "employee.gdb" RUNTIME :Db_name;
  41.  
  42.  
  43. int main(ARG(int, argc), ARG(char **, argv))
  44. ARGLIST(int argc)
  45. ARGLIST(char **argv)
  46. {
  47.     short    num_cols, i;
  48.     XSQLDA    *sqlda;
  49.  
  50.         if (argc > 1)
  51.                 strcpy(Db_name, argv[1]);
  52.         else
  53.                 strcpy(Db_name, "employee.gdb");
  54.  
  55.     EXEC SQL
  56.         WHENEVER SQLERROR GO TO Error;
  57.  
  58.     EXEC SQL
  59.         CONNECT empdb;
  60.  
  61.     EXEC SQL
  62.         SET TRANSACTION;
  63.  
  64.     /* Allocate SQLDA of an arbitrary size. */
  65.     sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(4));
  66.     sqlda->sqln = 4;
  67.     sqlda->sqld = 4;
  68.     sqlda->version = 1;
  69.  
  70.     /* Prepare an unknown statement. */
  71.     EXEC SQL
  72.         PREPARE q INTO SQL DESCRIPTOR sqlda FROM :sel_str;
  73.  
  74.     /* Describe the statement. */
  75.     EXEC SQL
  76.         DESCRIBE q INTO SQL DESCRIPTOR sqlda;
  77.  
  78.     /* This is a non-select statement, which can now be executed. */
  79.     if (sqlda->sqld == 0)
  80.         return(0);
  81.     
  82.     /* If this is a select statement, print more information about it. */
  83.     else
  84.         printf("Query Type:  SELECT\n\n");
  85.     
  86.     num_cols = sqlda->sqld;
  87.  
  88.     printf("Number of columns selected:  %d\n", num_cols);
  89.  
  90.     /* Reallocate SQLDA if necessary. */
  91.     if (sqlda->sqln < sqlda->sqld)
  92.     {
  93.         free(sqlda);
  94.  
  95.         sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(num_cols));
  96.         sqlda->sqln = num_cols;
  97.         sqlda->sqld = num_cols;
  98.  
  99.         /* Re-describe the statement. */
  100.         EXEC SQL
  101.             DESCRIBE q INTO SQL DESCRIPTOR sqlda;
  102.  
  103.         num_cols = sqlda->sqld;
  104.     }
  105.  
  106.     /* List column names, types, and lengths. */
  107.     for (i = 0; i < num_cols; i++)
  108.     {
  109.         printf("\nColumn name:    %s\n", sqlda->sqlvar[i].sqlname);
  110.         printf("Column type:    %d\n", sqlda->sqlvar[i].sqltype);
  111.         printf("Column length:  %d\n", sqlda->sqlvar[i].sqllen);
  112.     }
  113.  
  114.     EXEC SQL
  115.         COMMIT;
  116.  
  117.     EXEC SQL
  118.         DISCONNECT empdb;
  119.  
  120.     free( sqlda);
  121.     return(0);
  122.  
  123. Error:
  124.     isc_print_status(gds__status);
  125.     printf("SQLCODE=%d\n", SQLCODE);
  126.     return(1);
  127. }
  128.