home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api7.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  6KB  |  184 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.Inprise.com/IPL.html
  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 Inprise Corporation
  18.  * and its predecessors. Portions created by Inprise Corporation are
  19.  *
  20.  * Copyright (C) 2000 Inprise Corporation
  21.  * All Rights Reserved.
  22.  * Contributor(s): ______________________________________.
  23.  */
  24.  
  25.  
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ibase.h>
  29. #include <stdio.h>
  30. #include "example.h"
  31.  
  32. #define TYPELEN        12
  33. #define PROJLEN        20
  34. #define BUFLEN        512
  35.  
  36. /* This macro is used to declare structures representing SQL VARCHAR types */
  37. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  38.  
  39. int main (ARG(int, argc), ARG(char **, argv))
  40. ARGLIST(int argc)
  41. ARGLIST(char **argv)                         
  42. {
  43.     SQL_VARCHAR(PROJLEN + 2)    proj_name;
  44.     char                        prod_type[TYPELEN + 2];
  45.     char                        sel_str[BUFLEN + 1];
  46.     ISC_QUAD                    blob_id;
  47.     isc_blob_handle             blob_handle = NULL;
  48.     short                       blob_seg_len;
  49.     char                        blob_segment[11];
  50.     isc_db_handle               DB = NULL;        /* database handle */
  51.     isc_tr_handle               trans = NULL;     /* transaction handle */
  52.     long                        status[20];       /* status vector */
  53.     isc_stmt_handle             stmt = NULL;      /* statement handle */
  54.     XSQLDA ISC_FAR *            sqlda;
  55.     long                        fetch_stat, blob_stat;
  56.     short                       flag0 = 0,
  57.                                 flag1 = 0,
  58.                                 flag2 = 0;
  59.     char                        empdb[128];
  60.  
  61.     if (argc > 1)
  62.         strcpy(empdb, argv[1]);
  63.     else
  64.         strcpy(empdb, "employee.gdb");
  65.  
  66.  
  67.     strcpy(sel_str, "SELECT proj_name, proj_desc, product FROM project WHERE \
  68.            product IN ('software', 'hardware', 'other') ORDER BY proj_name");
  69.  
  70.     if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  71.     {
  72.         ERREXIT(status, 1)
  73.     }
  74.  
  75.     if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  76.     {
  77.         ERREXIT(status, 1)
  78.     }
  79.  
  80.     /*
  81.      *    Allocate and prepare the select statement.
  82.      */
  83.  
  84.     if (isc_dsql_allocate_statement(status, &DB, &stmt))
  85.     {
  86.         ERREXIT(status, 1)
  87.     }
  88.     
  89.     sqlda = (XSQLDA ISC_FAR *) malloc(XSQLDA_LENGTH(3));
  90.     sqlda->sqln = 3;
  91.     sqlda->version = 1;
  92.  
  93.     if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  94.     {
  95.         ERREXIT(status, 1)
  96.     }
  97.  
  98.     sqlda->sqlvar[0].sqldata = (char *)&proj_name;
  99.     sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  100.     sqlda->sqlvar[0].sqlind  = &flag0;
  101.  
  102.     sqlda->sqlvar[1].sqldata = (char ISC_FAR *) &blob_id;
  103.     sqlda->sqlvar[1].sqltype = SQL_BLOB + 1;
  104.     sqlda->sqlvar[1].sqlind  = &flag1;
  105.  
  106.     sqlda->sqlvar[2].sqldata = prod_type;
  107.     sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  108.     sqlda->sqlvar[2].sqlind  = &flag2;
  109.  
  110.     if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  111.     {
  112.         ERREXIT(status, 1)
  113.     }
  114.  
  115.     /*
  116.      *    For each project in the select statement, get and display
  117.      *    project descriptions.
  118.      */
  119.  
  120.     while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  121.     {
  122.         prod_type[TYPELEN] = '\0';
  123.         printf("\nPROJECT:  %-20.*s   TYPE:  %-15s\n\n",
  124.                proj_name.vary_length, proj_name.vary_string, prod_type);
  125.  
  126.         /* Open the blob with the fetched blob_id.   Notice that the
  127.         *  segment length is shorter than the average segment fetched.
  128.         *  Each partial fetch should return isc_segment.
  129.         */
  130.         if (isc_open_blob(status, &DB, &trans, &blob_handle, &blob_id))
  131.         {
  132.             ERREXIT(status, 1)
  133.         }
  134.  
  135.         /* Get blob segments and their lengths and print each segment. */
  136.         blob_stat = isc_get_segment(status, &blob_handle,
  137.                                     (unsigned short ISC_FAR *) &blob_seg_len,
  138.                                     sizeof(blob_segment), blob_segment);
  139.         while (blob_stat == 0 || status[1] == isc_segment)
  140.         {
  141.             printf("%*.*s", blob_seg_len, blob_seg_len, blob_segment);
  142.             blob_stat = isc_get_segment(status, &blob_handle,
  143.                                         (unsigned short ISC_FAR *)&blob_seg_len,
  144.                                         sizeof(blob_segment), blob_segment);
  145.         }
  146.         /* Close the blob.  Should be blob_stat to check */
  147.         if (status[1] == isc_segstr_eof)
  148.         {
  149.             if (isc_close_blob(status, &blob_handle))
  150.             {
  151.                 ERREXIT(status, 1)
  152.             }
  153.         }
  154.         else
  155.             isc_print_status(status);
  156.  
  157.         printf("\n");
  158.     }
  159.  
  160.     if (fetch_stat != 100L)
  161.     {
  162.         ERREXIT(status, 1)
  163.     }
  164.  
  165.     if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  166.     {
  167.         ERREXIT(status, 1)
  168.     }
  169.  
  170.     if (isc_commit_transaction (status, &trans))
  171.     {
  172.         ERREXIT(status, 1)
  173.     }
  174.  
  175.     if (isc_detach_database(status, &DB))
  176.     {
  177.         ERREXIT(status, 1)
  178.     }
  179.  
  180.     free(sqlda);
  181.  
  182.     return 0;
  183. }
  184.