summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/sql/describe.pgc
blob: 87d6bd9a29c50f6d21d57ec7f54f65d3fd457efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdlib.h>
#include <string.h>

exec sql include ../regression;
exec sql include sqlda.h;

exec sql whenever sqlerror stop;

sqlda_t	*sqlda1, *sqlda2, *sqlda3;

int
main (void)
{
exec sql begin declare section;
	char	*stmt1 = "SELECT id, t FROM descr_t2";
	char	*stmt2 = "SELECT id, t FROM descr_t2 WHERE id = -1";
	int	i, count1, count2;
	char	field_name1[30] = "not set";
	char	field_name2[30] = "not set";
exec sql end declare section;

	char msg[128];

	ECPGdebug(1, stderr);

	strcpy(msg, "connect");
	exec sql connect to REGRESSDB1;

	strcpy(msg, "set");
	exec sql set datestyle to iso;

	strcpy(msg, "create");
	exec sql create table descr_t2(id serial primary key, t text);

	strcpy(msg, "insert");
	exec sql insert into descr_t2(id, t) values (default, 'a');
	exec sql insert into descr_t2(id, t) values (default, 'b');
	exec sql insert into descr_t2(id, t) values (default, 'c');
	exec sql insert into descr_t2(id, t) values (default, 'd');

	strcpy(msg, "commit");
	exec sql commit;

	/*
	 * Test DESCRIBE with a query producing tuples.
	 * DESCRIPTOR and SQL DESCRIPTOR are NOT the same in
	 * Informix-compat mode.
	 */

	strcpy(msg, "allocate");
	exec sql allocate descriptor desc1;
	exec sql allocate descriptor desc2;

	strcpy(msg, "prepare");
	exec sql prepare st_id1 FROM :stmt1;

	sqlda1 = sqlda2 = sqlda3 = NULL;

	strcpy(msg, "describe");
	exec sql describe st_id1 into sql descriptor desc1;
	exec sql describe st_id1 using sql descriptor desc2;

	exec sql describe st_id1 into descriptor sqlda1;
	exec sql describe st_id1 using descriptor sqlda2;
	exec sql describe st_id1 into sqlda3;

	if (sqlda1 == NULL)
	{
		printf("sqlda1 NULL\n");
		exit(1);
	}

	if (sqlda2 == NULL)
	{
		printf("sqlda2 NULL\n");
		exit(1);
	}

	if (sqlda3 == NULL)
	{
		printf("sqlda3 NULL\n");
		exit(1);
	}

	strcpy(msg, "get descriptor");
	exec sql get descriptor desc1 :count1 = count;
	exec sql get descriptor desc1 :count2 = count;

	if (count1 != count2)
	{
		printf("count1 (%d) != count2 (%d)\n", count1, count2);
		exit(1);
	}

	if (count1 != sqlda1->sqld)
	{
		printf("count1 (%d) != sqlda1->sqld (%d)\n", count1, sqlda1->sqld);
		exit(1);
	}

	if (count1 != sqlda2->sqld)
	{
		printf("count1 (%d) != sqlda2->sqld (%d)\n", count1, sqlda2->sqld);
		exit(1);
	}

	if (count1 != sqlda3->sqld)
	{
		printf("count1 (%d) != sqlda3->sqld (%d)\n", count1, sqlda3->sqld);
		exit(1);
	}

	for (i = 1; i <= count1; i++)
	{
		exec sql get descriptor desc1 value :i :field_name1 = name;
		exec sql get descriptor desc2 value :i :field_name2 = name;
		printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
			"sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
			i, field_name1, field_name2,
			sqlda1->sqlvar[i-1].sqlname.data,
			sqlda2->sqlvar[i-1].sqlname.data,
			sqlda3->sqlvar[i-1].sqlname.data);
	}

	strcpy(msg, "deallocate");
	exec sql deallocate descriptor desc1;
	exec sql deallocate descriptor desc2;
	free(sqlda1);
	free(sqlda2);
	free(sqlda3);

	exec sql deallocate prepare st_id1;

	/* Test DESCRIBE with a query not producing tuples */

	strcpy(msg, "allocate");
	exec sql allocate descriptor desc1;
	exec sql allocate descriptor desc2;

	strcpy(msg, "prepare");
	exec sql prepare st_id2 FROM :stmt2;

	sqlda1 = sqlda2 = sqlda3 = NULL;

	strcpy(msg, "describe");
	exec sql describe st_id2 into sql descriptor desc1;
	exec sql describe st_id2 using sql descriptor desc2;

	exec sql describe st_id2 into descriptor sqlda1;
	exec sql describe st_id2 using descriptor sqlda2;
	exec sql describe st_id2 into sqlda3;

	if (sqlda1 == NULL || sqlda2 == NULL || sqlda3 == NULL)
		exit(1);

	strcpy(msg, "get descriptor");
	exec sql get descriptor desc1 :count1 = count;
	exec sql get descriptor desc1 :count2 = count;

	if (!(	count1 == count2 &&
		count1 == sqlda1->sqld &&
		count1 == sqlda2->sqld &&
		count1 == sqlda3->sqld))
		exit(1);

	for (i = 1; i <= count1; i++)
	{
		exec sql get descriptor desc1 value :i :field_name1 = name;
		exec sql get descriptor desc2 value :i :field_name2 = name;
		printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
			"sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
			i, field_name1, field_name2,
			sqlda1->sqlvar[i-1].sqlname.data,
			sqlda2->sqlvar[i-1].sqlname.data,
			sqlda3->sqlvar[i-1].sqlname.data);
	}

	strcpy(msg, "deallocate");
	exec sql deallocate descriptor desc1;
	exec sql deallocate descriptor desc2;
	free(sqlda1);
	free(sqlda2);
	free(sqlda3);

	exec sql deallocate prepare st_id2;

	/* End test */

	strcpy(msg, "drop");
	exec sql drop table descr_t2;

	strcpy(msg, "commit");
	exec sql commit;

	strcpy(msg, "disconnect");
	exec sql disconnect;

	return 0;
}