~ubuntu-branches/ubuntu/karmic/firebird2.1/karmic

« back to all changes in this revision

Viewing changes to examples/api/api7.c

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2008-05-26 23:59:25 UTC
  • Revision ID: james.westby@ubuntu.com-20080526235925-2pnqj6nxpppoeaer
Tags: upstream-2.1.0.17798-0.ds2
ImportĀ upstreamĀ versionĀ 2.1.0.17798-0.ds2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * Copyright (C) Inprise Corporation.
 
20
 *
 
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 (int argc, char** argv)
 
40
{
 
41
    SQL_VARCHAR(PROJLEN + 2)    proj_name;
 
42
    char                        prod_type[TYPELEN + 2];
 
43
    char                        sel_str[BUFLEN + 1];
 
44
    ISC_QUAD                    blob_id;
 
45
    isc_blob_handle             blob_handle = NULL;
 
46
    short                       blob_seg_len;
 
47
    char                        blob_segment[11];
 
48
    isc_db_handle               DB = NULL;        /* database handle */
 
49
    isc_tr_handle               trans = NULL;     /* transaction handle */
 
50
    ISC_STATUS_ARRAY            status;           /* status vector */
 
51
    isc_stmt_handle             stmt = NULL;      /* statement handle */
 
52
    XSQLDA *                    sqlda;
 
53
    long                        fetch_stat, blob_stat;
 
54
    short                       flag0 = 0,
 
55
                                flag1 = 0,
 
56
                                flag2 = 0;
 
57
    char                        empdb[128];
 
58
 
 
59
    if (argc > 1)
 
60
        strcpy(empdb, argv[1]);
 
61
    else
 
62
        strcpy(empdb, "employee.fdb");
 
63
 
 
64
 
 
65
    strcpy(sel_str, "SELECT proj_name, proj_desc, product FROM project WHERE \
 
66
           product IN ('software', 'hardware', 'other') ORDER BY proj_name");
 
67
 
 
68
    if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
 
69
    {
 
70
        ERREXIT(status, 1)
 
71
    }
 
72
 
 
73
    if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
 
74
    {
 
75
        ERREXIT(status, 1)
 
76
    }
 
77
 
 
78
    /*
 
79
     *    Allocate and prepare the select statement.
 
80
     */
 
81
 
 
82
    if (isc_dsql_allocate_statement(status, &DB, &stmt))
 
83
    {
 
84
        ERREXIT(status, 1)
 
85
    }
 
86
    
 
87
    sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
 
88
    sqlda->sqln = 3;
 
89
    sqlda->version = 1;
 
90
 
 
91
    if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
 
92
    {
 
93
        ERREXIT(status, 1)
 
94
    }
 
95
 
 
96
    sqlda->sqlvar[0].sqldata = (char *)&proj_name;
 
97
    sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
 
98
    sqlda->sqlvar[0].sqlind  = &flag0;
 
99
 
 
100
    sqlda->sqlvar[1].sqldata = (char *) &blob_id;
 
101
    sqlda->sqlvar[1].sqltype = SQL_BLOB + 1;
 
102
    sqlda->sqlvar[1].sqlind  = &flag1;
 
103
 
 
104
    sqlda->sqlvar[2].sqldata = prod_type;
 
105
    sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
 
106
    sqlda->sqlvar[2].sqlind  = &flag2;
 
107
 
 
108
    if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
 
109
    {
 
110
        ERREXIT(status, 1)
 
111
    }
 
112
 
 
113
    /*
 
114
     *    For each project in the select statement, get and display
 
115
     *    project descriptions.
 
116
     */
 
117
 
 
118
    while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
 
119
    {
 
120
        prod_type[TYPELEN] = '\0';
 
121
        printf("\nPROJECT:  %-20.*s   TYPE:  %-15s\n\n",
 
122
               proj_name.vary_length, proj_name.vary_string, prod_type);
 
123
 
 
124
        /* Open the blob with the fetched blob_id.   Notice that the
 
125
        *  segment length is shorter than the average segment fetched.
 
126
        *  Each partial fetch should return isc_segment.
 
127
        */
 
128
        if (isc_open_blob(status, &DB, &trans, &blob_handle, &blob_id))
 
129
        {
 
130
            ERREXIT(status, 1)
 
131
        }
 
132
 
 
133
        /* Get blob segments and their lengths and print each segment. */
 
134
        blob_stat = isc_get_segment(status, &blob_handle,
 
135
                                    (unsigned short *) &blob_seg_len,
 
136
                                    sizeof(blob_segment), blob_segment);
 
137
        while (blob_stat == 0 || status[1] == isc_segment)
 
138
        {
 
139
            printf("%*.*s", blob_seg_len, blob_seg_len, blob_segment);
 
140
            blob_stat = isc_get_segment(status, &blob_handle,
 
141
                                        (unsigned short *)&blob_seg_len,
 
142
                                        sizeof(blob_segment), blob_segment);
 
143
        }
 
144
        /* Close the blob.  Should be blob_stat to check */
 
145
        if (status[1] == isc_segstr_eof)
 
146
        {
 
147
            if (isc_close_blob(status, &blob_handle))
 
148
            {
 
149
                ERREXIT(status, 1)
 
150
            }
 
151
        }
 
152
        else
 
153
            isc_print_status(status);
 
154
 
 
155
        printf("\n");
 
156
    }
 
157
 
 
158
    if (fetch_stat != 100L)
 
159
    {
 
160
        ERREXIT(status, 1)
 
161
    }
 
162
 
 
163
    if (isc_dsql_free_statement(status, &stmt, DSQL_close))
 
164
    {
 
165
        ERREXIT(status, 1)
 
166
    }
 
167
 
 
168
    if (isc_commit_transaction (status, &trans))
 
169
    {
 
170
        ERREXIT(status, 1)
 
171
    }
 
172
 
 
173
    if (isc_detach_database(status, &DB))
 
174
    {
 
175
        ERREXIT(status, 1)
 
176
    }
 
177
 
 
178
    free(sqlda);
 
179
 
 
180
    return 0;
 
181
}
 
182