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