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

  1. /*
  2.  *    Program type:   Embedded Dynamic SQL
  3.  *
  4.  *    Description:
  5.  *        This program displays employee names and phone extensions.
  6.  *
  7.  *        It allocates an output SQLDA, declares and opens a cursor,
  8.  *        and loops fetching multiple rows.
  9.  * The contents of this file are subject to the Interbase Public
  10.  * License Version 1.0 (the "License"); you may not use this file
  11.  * except in compliance with the License. You may obtain a copy
  12.  * of the License at http://www.Inprise.com/IPL.html
  13.  *
  14.  * Software distributed under the License is distributed on an
  15.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16.  * or implied. See the License for the specific language governing
  17.  * rights and limitations under the License.
  18.  *
  19.  * The Original Code was created by Inprise Corporation
  20.  * and its predecessors. Portions created by Inprise Corporation are
  21.  *
  22.  * Copyright (C) 2000 Inprise Corporation
  23.  * All Rights Reserved.
  24.  * Contributor(s): ______________________________________.
  25.  */
  26.  
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. void print_error (void);
  32.  
  33. char    *sel_str =
  34.     "SELECT last_name, first_name, phone_ext FROM phone_list \
  35.     WHERE location = 'Monterey' ORDER BY last_name, first_name;";
  36.  
  37. /* This macro is used to declare structures representing SQL VARCHAR types */
  38. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  39.  
  40. char Db_name[128];
  41.  
  42. EXEC SQL
  43.     SET DATABASE empdb = "employee.gdb" RUNTIME :Db_name;
  44.  
  45.  
  46. int main(ARG(int, argc), ARG(char **, argv))
  47. ARGLIST(int argc)
  48. ARGLIST(char **argv)
  49. {
  50.  
  51.     SQL_VARCHAR(15) first_name;
  52.     SQL_VARCHAR(20) last_name;
  53.     char phone_ext[6];
  54.  
  55.     XSQLDA    *sqlda;
  56.     short    flag0 = 0, flag1 = 0, flag2 = 0;
  57.  
  58.     if (argc > 1)
  59.                 strcpy(Db_name, argv[1]);
  60.         else
  61.                 strcpy(Db_name, "employee.gdb");
  62.  
  63.     EXEC SQL
  64.         WHENEVER SQLERROR GO TO Error;
  65.  
  66.     EXEC SQL
  67.         CONNECT empdb;
  68.  
  69.     EXEC SQL
  70.         SET TRANSACTION;
  71.  
  72.     /* Allocate an output SQLDA. */
  73.     sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
  74.     sqlda->sqln = 3;
  75.     sqlda->version = 1;
  76.  
  77.     /* Prepare the query. */
  78.     EXEC SQL
  79.         PREPARE q INTO SQL DESCRIPTOR sqlda FROM :sel_str;
  80.  
  81.     /*
  82.      *  Although, all three selected columns are of type varchar, the
  83.      *  third field's type is changed and printed as type TEXT.
  84.      */
  85.  
  86.     sqlda->sqlvar[0].sqldata = (char *)&last_name;
  87.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  88.     sqlda->sqlvar[0].sqlind = &flag0;
  89.  
  90.     sqlda->sqlvar[1].sqldata = (char *)&first_name;
  91.     sqlda->sqlvar[1].sqltype = SQL_VARYING + 1;
  92.     sqlda->sqlvar[1].sqlind = &flag1;
  93.  
  94.     sqlda->sqlvar[2].sqldata = phone_ext;
  95.     sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  96.     sqlda->sqlvar[2].sqlind = &flag2;
  97.  
  98.     /* Declare the cursor for the prepared query. */
  99.     EXEC SQL
  100.         DECLARE s CURSOR FOR q;
  101.  
  102.     EXEC SQL
  103.         OPEN s;
  104.  
  105.     printf("\n%-20s %-15s %-10s\n\n", "LAST NAME", "FIRST NAME", "EXTENSION");
  106.  
  107.     /*
  108.      *  Fetch and print the records.
  109.      */
  110.     while (SQLCODE == 0)
  111.     {
  112.         EXEC SQL
  113.             FETCH s USING SQL DESCRIPTOR sqlda;
  114.  
  115.         if (SQLCODE == 100)
  116.             break;
  117.  
  118.         printf("%-20.*s ", last_name.vary_length, last_name.vary_string);
  119.  
  120.         printf("%-15.*s ", first_name.vary_length, first_name.vary_string);
  121.  
  122.         phone_ext[sqlda->sqlvar[2].sqllen] = '\0';
  123.         printf("%-10s\n", phone_ext);
  124.     }
  125.  
  126.     EXEC SQL
  127.         CLOSE s;
  128.  
  129.     EXEC SQL
  130.         COMMIT;
  131.  
  132.     EXEC SQL
  133.         DISCONNECT empdb;
  134.  
  135.     free( sqlda);
  136.     return(0);
  137.         
  138. Error:
  139.     print_error();
  140. }
  141.  
  142. void print_error (void)
  143. {
  144.     isc_print_status(gds__status);
  145.     printf("SQLCODE=%d\n", SQLCODE);
  146. }
  147.