TLA Line data Source code
1 : /* dynamic SQL test program
2 : */
3 :
4 : #include <stdio.h>
5 : #include <stdlib.h>
6 :
7 : exec sql include sql3types;
8 : exec sql include sqlca;
9 : exec sql include ../regression;
10 :
11 : static void
12 UBC 0 : error (void)
13 : {
14 0 : printf ("\n#%ld:%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
15 0 : exit (1);
16 : }
17 :
18 : int
19 CBC 1 : main ()
20 : {
21 : exec sql begin declare section;
22 : int COUNT;
23 : int INTVAR;
24 : int INDEX;
25 : int INDICATOR;
26 : int TYPE, LENGTH, OCTET_LENGTH, PRECISION, SCALE, RETURNED_OCTET_LENGTH;
27 : int DATETIME_INTERVAL_CODE;
28 : char NAME[120], BOOLVAR;
29 : char STRINGVAR[1024];
30 : double DOUBLEVAR;
31 : char *QUERY;
32 : exec sql end declare section;
33 1 : int done = 0;
34 :
35 : exec sql var BOOLVAR is bool;
36 :
37 1 : ECPGdebug (1, stderr);
38 :
39 1 : QUERY = "select * from dyntest";
40 :
41 : exec sql whenever sqlerror
42 : do
43 : error ();
44 :
45 1 : exec sql allocate descriptor MYDESC;
46 1 :
47 1 : exec sql connect to REGRESSDB1;
48 1 :
49 1 : exec sql set datestyle to german;
50 1 :
51 1 : exec sql create table dyntest (name char (14), d float8, i int,
52 : bignumber int8, b boolean, comment text,
53 : day date);
54 1 : exec sql insert into dyntest values ('first entry', 14.7, 14, 123045607890, true, 'The world''s most advanced open source database.', '1987-07-14');
55 1 : exec sql insert into dyntest values ('second entry', 1407.87, 1407, 987065403210, false, 'The elephant never forgets.', '1999-11-5');
56 1 :
57 1 : exec sql prepare MYQUERY from :QUERY;
58 1 : exec sql declare MYCURS cursor for MYQUERY;
59 :
60 1 : exec sql open MYCURS;
61 1 :
62 : while (1)
63 : {
64 3 : exec sql fetch in MYCURS into sql descriptor MYDESC;
65 3 :
66 3 : if (sqlca.sqlcode)
67 1 : break;
68 :
69 2 : exec sql get descriptor MYDESC:COUNT = count;
70 2 : if (!done)
71 : {
72 1 : printf ("Found %d columns\n", COUNT);
73 1 : done = 1;
74 : }
75 :
76 16 : for (INDEX = 1; INDEX <= COUNT; ++INDEX)
77 : {
78 14 : exec sql get descriptor MYDESC value :INDEX
79 : :TYPE = type,
80 : :LENGTH = length,
81 : :OCTET_LENGTH = octet_length,
82 : :RETURNED_OCTET_LENGTH = returned_octet_length,
83 : :PRECISION = precision,
84 : :SCALE = scale,
85 : :NAME = name,
86 : :INDICATOR = indicator;
87 14 : printf ("%2d\t%s (type: %d length: %d precision: %d scale: %d = " , INDEX, NAME, TYPE, LENGTH, PRECISION, SCALE);
88 14 : switch (TYPE)
89 : {
90 2 : case SQL3_BOOLEAN:
91 2 : printf ("bool");
92 2 : break;
93 UBC 0 : case SQL3_NUMERIC:
94 0 : printf ("numeric(%d,%d)", PRECISION, SCALE);
95 0 : break;
96 0 : case SQL3_DECIMAL:
97 0 : printf ("decimal(%d,%d)", PRECISION, SCALE);
98 0 : break;
99 CBC 2 : case SQL3_INTEGER:
100 2 : printf ("integer");
101 2 : break;
102 UBC 0 : case SQL3_SMALLINT:
103 0 : printf ("smallint");
104 0 : break;
105 0 : case SQL3_FLOAT:
106 0 : printf ("float(%d,%d)", PRECISION, SCALE);
107 0 : break;
108 0 : case SQL3_REAL:
109 0 : printf ("real");
110 0 : break;
111 CBC 2 : case SQL3_DOUBLE_PRECISION:
112 2 : printf ("double precision");
113 2 : break;
114 2 : case SQL3_DATE_TIME_TIMESTAMP:
115 2 : exec sql get descriptor MYDESC value :INDEX
116 : :DATETIME_INTERVAL_CODE = datetime_interval_code;
117 2 : switch (DATETIME_INTERVAL_CODE)
118 : {
119 2 : case SQL3_DDT_DATE:
120 2 : printf ("date");
121 2 : break;
122 UBC 0 : case SQL3_DDT_TIME:
123 0 : printf ("time");
124 0 : break;
125 0 : case SQL3_DDT_TIMESTAMP:
126 0 : printf ("timestamp");
127 0 : break;
128 0 : case SQL3_DDT_TIME_WITH_TIME_ZONE:
129 0 : printf ("time with time zone");
130 0 : break;
131 0 : case SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE:
132 0 : printf ("timestamp with time zone");
133 0 : break;
134 : }
135 CBC 2 : break;
136 UBC 0 : case SQL3_INTERVAL:
137 0 : printf ("interval");
138 0 : break;
139 CBC 4 : case SQL3_CHARACTER:
140 4 : if (LENGTH > 0)
141 2 : printf ("char(%d)", LENGTH);
142 : else
143 2 : printf ("text");
144 4 : break;
145 UBC 0 : case SQL3_CHARACTER_VARYING:
146 0 : if (LENGTH > 0)
147 0 : printf ("varchar(%d)", LENGTH);
148 : else
149 0 : printf ("varchar()");
150 0 : break;
151 CBC 2 : default:
152 2 : printf ("<SQL3 %d>", TYPE);
153 2 : break;
154 : }
155 14 : printf (")\n\toctet_length: %d returned_octet_length: %d)\n\t= ",
156 : OCTET_LENGTH, RETURNED_OCTET_LENGTH);
157 14 : if (INDICATOR == -1)
158 UBC 0 : printf ("NULL\n");
159 : else
160 CBC 14 : switch (TYPE)
161 : {
162 2 : case SQL3_BOOLEAN:
163 2 : exec sql get descriptor MYDESC value :INDEX :BOOLVAR = data;
164 2 : printf ("%s\n", BOOLVAR ? "true" : "false");
165 2 : break;
166 2 : case SQL3_INTEGER:
167 : case SQL3_SMALLINT:
168 2 : exec sql get descriptor MYDESC value :INDEX :INTVAR = data;
169 2 : printf ("%d\n", INTVAR);
170 2 : break;
171 2 : case SQL3_DOUBLE_PRECISION:
172 2 : exec sql get descriptor MYDESC value :INDEX :DOUBLEVAR = data;
173 2 : printf ("%.*f\n", PRECISION, DOUBLEVAR);
174 2 : break;
175 2 : case SQL3_DATE_TIME_TIMESTAMP:
176 2 : exec sql get descriptor MYDESC value :INDEX
177 : :DATETIME_INTERVAL_CODE = datetime_interval_code,
178 : :STRINGVAR = data;
179 2 : printf ("%d \"%s\"\n", DATETIME_INTERVAL_CODE, STRINGVAR);
180 2 : break;
181 4 : case SQL3_CHARACTER:
182 : case SQL3_CHARACTER_VARYING:
183 4 : exec sql get descriptor MYDESC value :INDEX :STRINGVAR = data;
184 4 : printf ("\"%s\"\n", STRINGVAR);
185 4 : break;
186 2 : default:
187 2 : exec sql get descriptor MYDESC value :INDEX :STRINGVAR = data;
188 2 : printf ("<\"%s\">\n", STRINGVAR);
189 2 : break;
190 : }
191 : }
192 : }
193 :
194 1 : exec sql close MYCURS;
195 1 :
196 1 : exec sql deallocate descriptor MYDESC;
197 1 :
198 1 : return 0;
199 : }
|