Line data Source code
1 : #include <stdlib.h>
2 : #include <string.h>
3 : #include <limits.h>
4 :
5 : exec sql include ../regression;
6 :
7 : exec sql include sqlda.h;
8 : exec sql include sqltypes.h;
9 :
10 : exec sql whenever sqlerror stop;
11 :
12 : /* These shouldn't be under DECLARE SECTION */
13 : sqlda_t *inp_sqlda, *outp_sqlda;
14 :
15 : static void
16 32 : dump_sqlda(sqlda_t *sqlda)
17 : {
18 : int i;
19 :
20 32 : if (sqlda == NULL)
21 : {
22 0 : printf("dump_sqlda called with NULL sqlda\n");
23 0 : return;
24 : }
25 :
26 192 : for (i = 0; i < sqlda->sqld; i++)
27 : {
28 160 : if (sqlda->sqlvar[i].sqlind && *(sqlda->sqlvar[i].sqlind) == -1)
29 32 : printf("name sqlda descriptor: '%s' value NULL'\n", sqlda->sqlvar[i].sqlname);
30 : else
31 128 : switch (sqlda->sqlvar[i].sqltype)
32 : {
33 48 : case SQLCHAR:
34 48 : printf("name sqlda descriptor: '%s' value '%s'\n", sqlda->sqlvar[i].sqlname, sqlda->sqlvar[i].sqldata);
35 48 : break;
36 32 : case SQLINT:
37 32 : printf("name sqlda descriptor: '%s' value %d\n", sqlda->sqlvar[i].sqlname, *(int *)sqlda->sqlvar[i].sqldata);
38 32 : break;
39 24 : case SQLFLOAT:
40 24 : printf("name sqlda descriptor: '%s' value %f\n", sqlda->sqlvar[i].sqlname, *(double *)sqlda->sqlvar[i].sqldata);
41 24 : break;
42 24 : case SQLDECIMAL:
43 : {
44 : char val[64];
45 24 : dectoasc((dec_t *)sqlda->sqlvar[i].sqldata, val, 64, -1);
46 24 : printf("name sqlda descriptor: '%s' value DECIMAL '%s'\n", sqlda->sqlvar[i].sqlname, val);
47 24 : break;
48 : }
49 : }
50 160 : }
51 : }
52 :
53 : int
54 4 : main (void)
55 : {
56 : exec sql begin declare section;
57 4 : char *stmt1 = "SELECT * FROM t1";
58 4 : char *stmt2 = "SELECT * FROM t1 WHERE id = ?";
59 : int rec;
60 : int id;
61 : exec sql end declare section;
62 :
63 : char msg[128];
64 :
65 4 : ECPGdebug(1, stderr);
66 :
67 4 : strcpy(msg, "connect");
68 4 : exec sql connect to REGRESSDB1 as regress1;
69 4 :
70 4 : strcpy(msg, "set");
71 4 : exec sql set datestyle to iso;
72 4 :
73 4 : strcpy(msg, "create");
74 4 : exec sql create table t1(
75 : id integer,
76 : t text,
77 : d1 numeric,
78 : d2 float8,
79 : c char(10));
80 4 :
81 4 : strcpy(msg, "insert");
82 4 : exec sql insert into t1 values
83 : (1, 'a', 1.0, 1, 'a'),
84 : (2, null, null, null, null),
85 : (4, 'd', 4.0, 4, 'd');
86 4 :
87 4 : strcpy(msg, "commit");
88 4 : exec sql commit;
89 4 :
90 : /* SQLDA test for getting all records from a table */
91 :
92 4 : outp_sqlda = NULL;
93 :
94 4 : strcpy(msg, "prepare");
95 4 : exec sql prepare st_id1 from :stmt1;
96 4 :
97 4 : strcpy(msg, "declare");
98 : exec sql declare mycur1 cursor for st_id1;
99 :
100 4 : strcpy(msg, "open");
101 4 : exec sql open mycur1;
102 4 :
103 : exec sql whenever not found do break;
104 :
105 4 : rec = 0;
106 : while (1)
107 : {
108 16 : strcpy(msg, "fetch");
109 16 : exec sql fetch 1 from mycur1 into descriptor outp_sqlda;
110 16 :
111 12 : printf("FETCH RECORD %d\n", ++rec);
112 12 : dump_sqlda(outp_sqlda);
113 : }
114 :
115 : exec sql whenever not found continue;
116 :
117 4 : strcpy(msg, "close");
118 4 : exec sql close mycur1;
119 4 :
120 4 : strcpy(msg, "deallocate");
121 4 : exec sql deallocate prepare st_id1;
122 4 :
123 4 : free(outp_sqlda);
124 :
125 : /* SQLDA test for getting all records from a table
126 : using the Informix-specific FETCH ... USING DESCRIPTOR
127 : */
128 :
129 4 : outp_sqlda = NULL;
130 :
131 4 : strcpy(msg, "prepare");
132 4 : exec sql prepare st_id2 from :stmt1;
133 4 :
134 4 : strcpy(msg, "declare");
135 : exec sql declare mycur2 cursor for st_id2;
136 :
137 4 : strcpy(msg, "open");
138 4 : exec sql open mycur2;
139 4 :
140 : exec sql whenever not found do break;
141 :
142 4 : rec = 0;
143 : while (1)
144 : {
145 16 : strcpy(msg, "fetch");
146 16 : exec sql fetch from mycur2 using descriptor outp_sqlda;
147 16 :
148 12 : printf("FETCH RECORD %d\n", ++rec);
149 12 : dump_sqlda(outp_sqlda);
150 : }
151 :
152 : exec sql whenever not found continue;
153 :
154 4 : strcpy(msg, "close");
155 4 : exec sql close mycur2;
156 4 :
157 4 : strcpy(msg, "deallocate");
158 4 : exec sql deallocate prepare st_id2;
159 4 :
160 4 : free(outp_sqlda);
161 :
162 : /* SQLDA test for getting one record using an input descriptor */
163 :
164 : /* Input sqlda has to be built manually */
165 4 : inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t));
166 4 : memset(inp_sqlda, 0, sizeof(sqlda_t));
167 4 : inp_sqlda->sqld = 1;
168 4 : inp_sqlda->sqlvar = malloc(sizeof(sqlvar_t));
169 4 : memset(inp_sqlda->sqlvar, 0, sizeof(sqlvar_t));
170 :
171 4 : inp_sqlda->sqlvar[0].sqltype = SQLINT;
172 4 : inp_sqlda->sqlvar[0].sqldata = (char *)&id;
173 :
174 4 : printf("EXECUTE RECORD 4\n");
175 :
176 4 : id = 4;
177 :
178 4 : outp_sqlda = NULL;
179 :
180 4 : strcpy(msg, "prepare");
181 4 : exec sql prepare st_id3 FROM :stmt2;
182 4 :
183 4 : strcpy(msg, "execute");
184 4 : exec sql execute st_id3 using descriptor inp_sqlda into descriptor outp_sqlda;
185 4 :
186 4 : dump_sqlda(outp_sqlda);
187 :
188 4 : strcpy(msg, "deallocate");
189 4 : exec sql deallocate prepare st_id3;
190 4 :
191 4 : free(inp_sqlda->sqlvar);
192 4 : free(inp_sqlda);
193 4 : free(outp_sqlda);
194 :
195 : /* SQLDA test for getting one record using an input descriptor
196 : * on a named connection
197 : */
198 :
199 4 : exec sql connect to REGRESSDB1 as con2;
200 4 :
201 : /* Input sqlda has to be built manually */
202 4 : inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t));
203 4 : memset(inp_sqlda, 0, sizeof(sqlda_t));
204 4 : inp_sqlda->sqld = 1;
205 4 : inp_sqlda->sqlvar = malloc(sizeof(sqlvar_t));
206 4 : memset(inp_sqlda->sqlvar, 0, sizeof(sqlvar_t));
207 :
208 4 : inp_sqlda->sqlvar[0].sqltype = SQLINT;
209 4 : inp_sqlda->sqlvar[0].sqldata = (char *)&id;
210 :
211 4 : printf("EXECUTE RECORD 4\n");
212 :
213 4 : id = 4;
214 :
215 4 : outp_sqlda = NULL;
216 :
217 4 : strcpy(msg, "prepare");
218 4 : exec sql at con2 prepare st_id4 FROM :stmt2;
219 4 :
220 4 : strcpy(msg, "execute");
221 4 : exec sql at con2 execute st_id4 using descriptor inp_sqlda into descriptor outp_sqlda;
222 4 :
223 4 : dump_sqlda(outp_sqlda);
224 :
225 4 : strcpy(msg, "commit");
226 4 : exec sql at con2 commit;
227 4 :
228 4 : strcpy(msg, "deallocate");
229 4 : exec sql deallocate prepare st_id4;
230 4 :
231 4 : free(inp_sqlda->sqlvar);
232 4 : free(inp_sqlda);
233 4 : free(outp_sqlda);
234 :
235 4 : strcpy(msg, "disconnect");
236 4 : exec sql disconnect con2;
237 4 :
238 : /* End test */
239 :
240 4 : strcpy(msg, "drop");
241 4 : exec sql drop table t1;
242 4 :
243 4 : strcpy(msg, "commit");
244 4 : exec sql commit;
245 4 :
246 4 : strcpy(msg, "disconnect");
247 4 : exec sql disconnect;
248 4 :
249 4 : return 0;
250 : }
|