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