File: api5.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 (141 lines) | stat: -rw-r--r-- 3,830 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
/*
 *    Program type:  API Interface
 *
 *    Desription:
 *        This program demonstrates the reallocation of SQLDA
 *        and the 'isc_dsql_describe' statement.  After a query
 *        is examined with 'isc_dsql_describe', an SQLDA of correct
 *        size is reallocated, and some information is printed about
 *        the query:  its type (select, non-select), the number
 *        of columns, etc.
 * 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 <ibase.h>
#include <stdio.h>
#include "example.h"

char *sel_str =
     "SELECT department, mngr_no, location, head_dept \
      FROM department WHERE head_dept in ('100', '900', '600')";

isc_db_handle    DB = 0;                       /* database handle */
isc_tr_handle    trans = 0;                    /* transaction handle */
ISC_STATUS_ARRAY status;                       /* status vector */


int main (int argc, char** argv)
{
    int                num_cols, i;
    isc_stmt_handle    stmt = NULL;
    XSQLDA             *sqlda;
    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)
    }

    /* Allocate SQLDA of an arbitrary size. */
    sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
    sqlda->sqln = 3;
    sqlda->version = 1;

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

    /* Allocate a statement. */
    if (isc_dsql_allocate_statement(status, &DB, &stmt))
    {
        ERREXIT(status, 1)
    }

    /* Prepare the statement. */
    if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
    {
        ERREXIT(status, 1)
    }

    /* Describe the statement. */
    if (isc_dsql_describe(status, &stmt, 1, sqlda))
    {
        ERREXIT(status, 1)
    }
    
    /* This is a select statement, print more information about it. */

    printf("Query Type:  SELECT\n\n");

    num_cols = sqlda->sqld;

    printf("Number of columns selected:  %d\n", num_cols);

    /* Reallocate SQLDA if necessary. */
    if (sqlda->sqln < sqlda->sqld)
    {
        sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(num_cols));
        sqlda->sqln = num_cols;
        sqlda->version = 1;
    
        /* Re-describe the statement. */
        if (isc_dsql_describe(status, &stmt, 1, sqlda))
        {
            ERREXIT(status, 1)
        }

        num_cols = sqlda->sqld;
    }

    /* List column names, types, and lengths. */
    for (i = 0; i < num_cols; i++)
    {
        printf("\nColumn name:    %s\n", sqlda->sqlvar[i].sqlname);
        printf("Column type:    %d\n", sqlda->sqlvar[i].sqltype);
        printf("Column length:  %d\n", sqlda->sqlvar[i].sqllen);
    }


    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;
}