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 / api7.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  6KB  |  187 lines

  1. /*
  2.  *  Program type:  API Interface
  3.  *
  4.  *    Description:
  5.  *      This program selects a blob data type.
  6.  *      A set of project descriptions is printed.
  7.  * The contents of this file are subject to the Interbase Public
  8.  * License Version 1.0 (the "License"); you may not use this file
  9.  * except in compliance with the License. You may obtain a copy
  10.  * of the License at http://www.Interbase.com/IPL/
  11.  *
  12.  * Software distributed under the License is distributed on an
  13.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  14.  * or implied. See the License for the specific language governing
  15.  * rights and limitations under the License.
  16.  *
  17.  * The Original Code was created by Interbase Software Corporation
  18.  * and its successors. Portions created by Borland/Inprise are
  19.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  20.  * created by InterBase Software Corporation are Copyright (C)
  21.  * 1998-1999 InterBase Software Corporation.
  22.  *
  23.  * Copyright (C) 2000 InterBase Software Corporation
  24.  * All Rights Reserved.
  25.  * Contributor(s): ______________________________________.
  26.  */
  27.  
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ibase.h>
  32. #include <stdio.h>
  33. #include "example.h"
  34.  
  35. #define TYPELEN        12
  36. #define PROJLEN        20
  37. #define BUFLEN        512
  38.  
  39. /* This macro is used to declare structures representing SQL VARCHAR types */
  40. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  41.  
  42. int main (ARG(int, argc), ARG(char **, argv))
  43. ARGLIST(int argc)
  44. ARGLIST(char **argv)                         
  45. {
  46.     SQL_VARCHAR(PROJLEN + 2)    proj_name;
  47.     char                        prod_type[TYPELEN + 2];
  48.     char                        sel_str[BUFLEN + 1];
  49.     ISC_QUAD                    blob_id;
  50.     isc_blob_handle             blob_handle = NULL;
  51.     short                       blob_seg_len;
  52.     char                        blob_segment[11];
  53.     isc_db_handle               DB = NULL;        /* database handle */
  54.     isc_tr_handle               trans = NULL;     /* transaction handle */
  55.     long                        status[20];       /* status vector */
  56.     isc_stmt_handle             stmt = NULL;      /* statement handle */
  57.     XSQLDA ISC_FAR *            sqlda;
  58.     long                        fetch_stat, blob_stat;
  59.     short                       flag0 = 0,
  60.                                 flag1 = 0,
  61.                                 flag2 = 0;
  62.     char                        empdb[128];
  63.  
  64.     if (argc > 1)
  65.         strcpy(empdb, argv[1]);
  66.     else
  67.         strcpy(empdb, "employee.gdb");
  68.  
  69.  
  70.     strcpy(sel_str, "SELECT proj_name, proj_desc, product FROM project WHERE \
  71.            product IN ('software', 'hardware', 'other') ORDER BY proj_name");
  72.  
  73.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  74.     {
  75.         ERREXIT(status, 1)
  76.     }
  77.  
  78.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  79.     {
  80.         ERREXIT(status, 1)
  81.     }
  82.  
  83.     /*
  84.      *    Allocate and prepare the select statement.
  85.      */
  86.  
  87.     if (isc_dsql_allocate_statement(status, &DB, &stmt))
  88.     {
  89.         ERREXIT(status, 1)
  90.     }
  91.     
  92.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(3));
  93.     sqlda->sqln = 3;
  94.     sqlda->version = 1;
  95.  
  96.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  97.     {
  98.         ERREXIT(status, 1)
  99.     }
  100.  
  101.     sqlda->sqlvar[0].sqldata = (char *)&proj_name;
  102.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  103.     sqlda->sqlvar[0].sqlind  = &flag0;
  104.  
  105.     sqlda->sqlvar[1].sqldata = (char ISC_FAR *) &blob_id;
  106.     sqlda->sqlvar[1].sqltype = SQL_BLOB + 1;
  107.     sqlda->sqlvar[1].sqlind  = &flag1;
  108.  
  109.     sqlda->sqlvar[2].sqldata = prod_type;
  110.     sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  111.     sqlda->sqlvar[2].sqlind  = &flag2;
  112.  
  113.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  114.     {
  115.         ERREXIT(status, 1)
  116.     }
  117.  
  118.     /*
  119.      *    For each project in the select statement, get and display
  120.      *    project descriptions.
  121.      */
  122.  
  123.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  124.     {
  125.         prod_type[TYPELEN] = '\0';
  126.         printf("\nPROJECT:  %-20.*s   TYPE:  %-15s\n\n",
  127.                proj_name.vary_length, proj_name.vary_string, prod_type);
  128.  
  129.         /* Open the blob with the fetched blob_id.   Notice that the
  130.         *  segment length is shorter than the average segment fetched.
  131.         *  Each partial fetch should return isc_segment.
  132.         */
  133.         if (isc_open_blob(status, &DB, &trans, &blob_handle, &blob_id))
  134.         {
  135.             ERREXIT(status, 1)
  136.         }
  137.  
  138.         /* Get blob segments and their lengths and print each segment. */
  139.         blob_stat = isc_get_segment(status, &blob_handle,
  140.                                     (unsigned short ISC_FAR *) &blob_seg_len,
  141.                                     sizeof(blob_segment), blob_segment);
  142.         while (blob_stat == 0 || status[1] == isc_segment)
  143.         {
  144.             printf("%*.*s", blob_seg_len, blob_seg_len, blob_segment);
  145.             blob_stat = isc_get_segment(status, &blob_handle,
  146.                                         (unsigned short ISC_FAR *)&blob_seg_len,
  147.                                         sizeof(blob_segment), blob_segment);
  148.         }
  149.         /* Close the blob.  Should be blob_stat to check */
  150.         if (status[1] == isc_segstr_eof)
  151.         {
  152.             if (isc_close_blob(status, &blob_handle))
  153.             {
  154.                 ERREXIT(status, 1)
  155.             }
  156.         }
  157.         else
  158.             isc_print_status(status);
  159.  
  160.         printf("\n");
  161.     }
  162.  
  163.     if (fetch_stat != 100L)
  164.     {
  165.         ERREXIT(status, 1)
  166.     }
  167.  
  168.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  169.     {
  170.         ERREXIT(status, 1)
  171.     }
  172.  
  173.     if (isc_commit_transaction (status, &trans))
  174.     {
  175.         ERREXIT(status, 1)
  176.     }
  177.  
  178.     if (isc_detach_database(status, &DB))
  179.     {
  180.         ERREXIT(status, 1)
  181.     }
  182.  
  183.     free(sqlda);
  184.  
  185.     return 0;
  186. }
  187.