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