File: api9.c

package info (click to toggle)
firebird3.0 3.0.13.ds7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,632 kB
  • sloc: ansic: 374,403; cpp: 319,973; sql: 14,691; pascal: 14,532; yacc: 7,557; fortran: 5,645; sh: 5,336; makefile: 1,041; perl: 194; sed: 83; awk: 76; xml: 19; csh: 15
file content (179 lines) | stat: -rw-r--r-- 4,988 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *    Program type:  API Interface
 *
 *    Description:
 *        This program uses a filter to display a blob data type.
 *        Job descriptions are read through a filter, which formats
 *        the text into 40-character-wide paragraphs.
 *
 *    IMPORTANT NOTE!
 *        The server side file, api9f.c, must have been compiled
 *        and linked and must be running on the server before
 *        this example can be run.
 * The contents of this file are subject to the Interbase Public
 * License Version 1.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy
 * of the License at http://www.Inprise.com/IPL.html
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code was created by Inprise Corporation
 * and its predecessors. Portions created by Inprise Corporation are
 * Copyright (C) Inprise Corporation.
 *
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ibase.h>
#include "example.h"

#define COUNTRYLEN 15
#define CODELEN    5

char    *sel_str =
    "SELECT job_requirement, job_code, job_grade, job_country \
     FROM job WHERE job_code = 'SRep'";

/* Blob parameter buffer. */
char bpb[] = {1,2,2,-4,-1,1,2,1,0};

int main (int argc, char** argv)
{
    char                job_code[CODELEN + 2];
    short               job_grade;
    char                job_country[COUNTRYLEN + 2];
    short               flag0 = 0, flag1 = 0,
                        flag2 = 0, flag3 = 0;
    ISC_QUAD            blob_id;
    isc_blob_handle     blob_handle = NULL;
    short               blob_seg_len;
    char                blob_segment[401];
    isc_db_handle       DB = NULL;
    isc_tr_handle       trans = NULL;
    ISC_STATUS_ARRAY    status;
    isc_stmt_handle     stmt = NULL;
    XSQLDA *            sqlda;
    long                fetch_stat;
    char                empdb[128];

    if (argc > 1)
        strcpy(empdb, argv[1]);
    else
        strcpy(empdb, "employee.fdb");

    if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
    {
        ERREXIT(status, 1)
    }

    if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
    {
        ERREXIT(status, 1)
    }

    /*
     *    Allocate and prepare the select statement.
     */

    if (isc_dsql_allocate_statement(status, &DB, &stmt))
    {
        ERREXIT(status, 1)
    }

    sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(4));
    sqlda->sqln = 4;
    sqlda->version = 1;

    if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
    {
        ERREXIT(status, 1)
    }

    sqlda->sqlvar[0].sqldata = (char *) &blob_id;
    sqlda->sqlvar[0].sqltype = SQL_BLOB + 1;
    sqlda->sqlvar[0].sqlind  = &flag0;

    sqlda->sqlvar[1].sqldata = (char *) job_code;
    sqlda->sqlvar[1].sqltype = SQL_TEXT + 1;
    sqlda->sqlvar[1].sqlind  = &flag1;

    sqlda->sqlvar[2].sqldata = (char *) &job_grade;
    sqlda->sqlvar[2].sqltype = SQL_SHORT + 1;
    sqlda->sqlvar[2].sqlind  = &flag2;

    sqlda->sqlvar[3].sqldata = (char *) job_country;
    sqlda->sqlvar[3].sqltype = SQL_TEXT + 1;
    sqlda->sqlvar[3].sqlind  = &flag3;

    if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
    {
        ERREXIT(status, 1)
    }
            
    /*
     *  Display job descriptions.
     */

    while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
    {
        job_code[CODELEN] = '\0';
        job_country[COUNTRYLEN] = '\0';
        printf("\nJOB CODE: %5s  GRADE: %d", job_code, job_grade);
        printf("  COUNTRY:  %-20s\n\n", job_country);

        /* Open the blob with the fetched blob_id. */
        if (isc_open_blob2(status, &DB, &trans, &blob_handle, &blob_id, 9, bpb))
        {
            ERREXIT(status, 1)
        }

        /* Get blob segments and their lengths and print each segment. */
        while (isc_get_segment(status, &blob_handle,
               (unsigned short *) &blob_seg_len, sizeof(blob_segment),
               blob_segment) == 0)
            printf("  %*.*s", blob_seg_len, blob_seg_len, blob_segment);

        /* Close the blob.  */
        if (status[1] == isc_segstr_eof)
        {
            if (isc_close_blob(status, &blob_handle))
            {
                ERREXIT(status, 1)
            }
        }
        else
            isc_print_status(status);

        printf("\n");
    }

    if (fetch_stat != 100L)
        isc_print_status(status);

    if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
    {
        ERREXIT(status, 1)
    }

    if (isc_commit_transaction(status, &trans))
    {
        ERREXIT(status, 1)
    }

    if (isc_detach_database(status, &DB))
    {
        ERREXIT(status, 1)
    }

    free(sqlda);

    return 0;
}