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

« back to all changes in this revision

Viewing changes to examples/dyn/dyn5.e

  • 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:   Embedded Dynamic SQL
 
3
 *
 
4
 *      Description:
 
5
 *              This program demonstrates the reallocation of SQLDA and
 
6
 *              the 'describe' statement.  After a query is examined with
 
7
 *              'describe', an SQLDA of correct size is reallocated, and some
 
8
 *              information is printed about the query:  its type (select,
 
9
 *              non-select), the number of columns, etc.
 
10
 * The contents of this file are subject to the Interbase Public
 
11
 * License Version 1.0 (the "License"); you may not use this file
 
12
 * except in compliance with the License. You may obtain a copy
 
13
 * of the License at http://www.Inprise.com/IPL.html
 
14
 *
 
15
 * Software distributed under the License is distributed on an
 
16
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 
17
 * or implied. See the License for the specific language governing
 
18
 * rights and limitations under the License.
 
19
 *
 
20
 * The Original Code was created by Inprise Corporation
 
21
 * and its predecessors. Portions created by Inprise Corporation are
 
22
 * Copyright (C) Inprise Corporation.
 
23
 *
 
24
 * All Rights Reserved.
 
25
 * Contributor(s): ______________________________________.
 
26
 */
 
27
 
 
28
 
 
29
#include "example.h"
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
 
 
33
char *sel_str =
 
34
        "SELECT department, mngr_no, location, head_dept \
 
35
         FROM department WHERE head_dept in ('100', '900', '600')";
 
36
 
 
37
char Db_name[128];
 
38
 
 
39
EXEC SQL
 
40
        SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
 
41
 
 
42
 
 
43
int main(int argc, char** argv)
 
44
{
 
45
        short   num_cols, i;
 
46
        XSQLDA  *sqlda;
 
47
 
 
48
        if (argc > 1)
 
49
                strcpy(Db_name, argv[1]);
 
50
        else
 
51
                strcpy(Db_name, "employee.fdb");
 
52
 
 
53
        EXEC SQL
 
54
                WHENEVER SQLERROR GO TO Error;
 
55
 
 
56
        EXEC SQL
 
57
                CONNECT empdb;
 
58
 
 
59
        EXEC SQL
 
60
                SET TRANSACTION;
 
61
 
 
62
        /* Allocate SQLDA of an arbitrary size. */
 
63
        sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(4));
 
64
        sqlda->sqln = 4;
 
65
        sqlda->sqld = 4;
 
66
        sqlda->version = 1;
 
67
 
 
68
        /* Prepare an unknown statement. */
 
69
        EXEC SQL
 
70
                PREPARE q INTO SQL DESCRIPTOR sqlda FROM :sel_str;
 
71
 
 
72
        /* Describe the statement. */
 
73
        EXEC SQL
 
74
                DESCRIBE q INTO SQL DESCRIPTOR sqlda;
 
75
 
 
76
        /* This is a non-select statement, which can now be executed. */
 
77
        if (sqlda->sqld == 0)
 
78
                return(0);
 
79
        
 
80
        /* If this is a select statement, print more information about it. */
 
81
        else
 
82
                printf("Query Type:  SELECT\n\n");
 
83
        
 
84
        num_cols = sqlda->sqld;
 
85
 
 
86
        printf("Number of columns selected:  %d\n", num_cols);
 
87
 
 
88
        /* Reallocate SQLDA if necessary. */
 
89
        if (sqlda->sqln < sqlda->sqld)
 
90
        {
 
91
                free(sqlda);
 
92
 
 
93
                sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(num_cols));
 
94
                sqlda->sqln = num_cols;
 
95
                sqlda->sqld = num_cols;
 
96
 
 
97
                /* Re-describe the statement. */
 
98
                EXEC SQL
 
99
                        DESCRIBE q INTO SQL DESCRIPTOR sqlda;
 
100
 
 
101
                num_cols = sqlda->sqld;
 
102
        }
 
103
 
 
104
        /* List column names, types, and lengths. */
 
105
        for (i = 0; i < num_cols; i++)
 
106
        {
 
107
                printf("\nColumn name:    %s\n", sqlda->sqlvar[i].sqlname);
 
108
                printf("Column type:    %d\n", sqlda->sqlvar[i].sqltype);
 
109
                printf("Column length:  %d\n", sqlda->sqlvar[i].sqllen);
 
110
        }
 
111
 
 
112
        EXEC SQL
 
113
                COMMIT;
 
114
 
 
115
        EXEC SQL
 
116
                DISCONNECT empdb;
 
117
 
 
118
        free( sqlda);
 
119
        return(0);
 
120
 
 
121
Error:
 
122
        isc_print_status(gds__status);
 
123
        printf("SQLCODE=%d\n", SQLCODE);
 
124
        return(1);
 
125
}
 
126