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 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 4 : 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 4 : int done = 0;
34 :
35 : exec sql var BOOLVAR is bool;
36 :
37 4 : ECPGdebug (1, stderr);
38 :
39 4 : QUERY = "select * from dyntest";
40 :
41 : exec sql whenever sqlerror
42 : do
43 : error ();
44 :
45 4 : exec sql allocate descriptor MYDESC;
46 4 :
47 4 : exec sql connect to REGRESSDB1;
48 4 :
49 4 : exec sql set datestyle to german;
50 4 :
51 4 : exec sql create table dyntest (name char (14), d float8, i int,
52 : bignumber int8, b boolean, comment text,
53 : day date);
54 4 : 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 4 : exec sql insert into dyntest values ('second entry', 1407.87, 1407, 987065403210, false, 'The elephant never forgets.', '1999-11-5');
56 4 :
57 4 : exec sql prepare MYQUERY from :QUERY;
58 4 : exec sql declare MYCURS cursor for MYQUERY;
59 :
60 4 : exec sql open MYCURS;
61 4 :
62 : while (1)
63 : {
64 12 : exec sql fetch in MYCURS into sql descriptor MYDESC;
65 12 :
66 12 : if (sqlca.sqlcode)
67 4 : break;
68 :
69 8 : exec sql get descriptor MYDESC:COUNT = count;
70 8 : if (!done)
71 : {
72 4 : printf ("Found %d columns\n", COUNT);
73 4 : done = 1;
74 : }
75 :
76 64 : for (INDEX = 1; INDEX <= COUNT; ++INDEX)
77 : {
78 56 : 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 56 : printf ("%2d\t%s (type: %d length: %d precision: %d scale: %d = " , INDEX, NAME, TYPE, LENGTH, PRECISION, SCALE);
88 56 : switch (TYPE)
89 : {
90 8 : case SQL3_BOOLEAN:
91 8 : printf ("bool");
92 8 : break;
93 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 8 : case SQL3_INTEGER:
100 8 : printf ("integer");
101 8 : break;
102 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 8 : case SQL3_DOUBLE_PRECISION:
112 8 : printf ("double precision");
113 8 : break;
114 8 : case SQL3_DATE_TIME_TIMESTAMP:
115 8 : exec sql get descriptor MYDESC value :INDEX
116 : :DATETIME_INTERVAL_CODE = datetime_interval_code;
117 8 : switch (DATETIME_INTERVAL_CODE)
118 : {
119 8 : case SQL3_DDT_DATE:
120 8 : printf ("date");
121 8 : break;
122 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 8 : break;
136 0 : case SQL3_INTERVAL:
137 0 : printf ("interval");
138 0 : break;
139 16 : case SQL3_CHARACTER:
140 16 : if (LENGTH > 0)
141 8 : printf ("char(%d)", LENGTH);
142 : else
143 8 : printf ("text");
144 16 : break;
145 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 8 : default:
152 8 : printf ("<SQL3 %d>", TYPE);
153 8 : break;
154 : }
155 56 : printf (")\n\toctet_length: %d returned_octet_length: %d)\n\t= ",
156 : OCTET_LENGTH, RETURNED_OCTET_LENGTH);
157 56 : if (INDICATOR == -1)
158 0 : printf ("NULL\n");
159 : else
160 56 : switch (TYPE)
161 : {
162 8 : case SQL3_BOOLEAN:
163 8 : exec sql get descriptor MYDESC value :INDEX :BOOLVAR = data;
164 8 : printf ("%s\n", BOOLVAR ? "true" : "false");
165 8 : break;
166 8 : case SQL3_INTEGER:
167 : case SQL3_SMALLINT:
168 8 : exec sql get descriptor MYDESC value :INDEX :INTVAR = data;
169 8 : printf ("%d\n", INTVAR);
170 8 : break;
171 8 : case SQL3_DOUBLE_PRECISION:
172 8 : exec sql get descriptor MYDESC value :INDEX :DOUBLEVAR = data;
173 8 : printf ("%.*f\n", PRECISION, DOUBLEVAR);
174 8 : break;
175 8 : case SQL3_DATE_TIME_TIMESTAMP:
176 8 : exec sql get descriptor MYDESC value :INDEX
177 : :DATETIME_INTERVAL_CODE = datetime_interval_code,
178 : :STRINGVAR = data;
179 8 : printf ("%d \"%s\"\n", DATETIME_INTERVAL_CODE, STRINGVAR);
180 8 : break;
181 16 : case SQL3_CHARACTER:
182 : case SQL3_CHARACTER_VARYING:
183 16 : exec sql get descriptor MYDESC value :INDEX :STRINGVAR = data;
184 16 : printf ("\"%s\"\n", STRINGVAR);
185 16 : break;
186 8 : default:
187 8 : exec sql get descriptor MYDESC value :INDEX :STRINGVAR = data;
188 8 : printf ("<\"%s\">\n", STRINGVAR);
189 8 : break;
190 : }
191 : }
192 : }
193 :
194 4 : exec sql close MYCURS;
195 4 :
196 4 : exec sql deallocate descriptor MYDESC;
197 4 :
198 4 : return 0;
199 : }
|