summaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/test/test3.pgc
blob: fe6fb56f0336f4564acd40d36ee05f4f912acb7e (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>

exec sql include header_test;

exec sql type str is varchar[10];

int
main ()
{
	typedef struct { long born; short age; } birthinfo;
	exec sql type birthinfo is struct { long born; short age; };
exec sql begin declare section;
	struct personal_struct	{	str name;
					birthinfo birth;
				} personal;
	struct personal_indicator {	int ind_name;
					birthinfo ind_birth;
				  } ind_personal;
	int *ind_married = NULL;
	int children;
	int ind_children;
	str *married = NULL;
	char *testname="Petra";
	char *query="select name, born, age, married, children from meskes where name = :var1";
exec sql end declare section;

	exec sql declare cur cursor for
	       select name, born, age, married, children from meskes;

	char msg[128], command[128];
	FILE *dbgs;

	if ((dbgs = fopen("log", "w")) != NULL)
                ECPGdebug(1, dbgs);

	strcpy(msg, "connect");
	exec sql connect to unix:postgresql://localhost:5432/mm; 

	strcpy(msg, "create");
	exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer);

	strcpy(msg, "insert");
	exec sql insert into meskes(name, married, children) values ('Petra', '19900404', 3);
	exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 33, '19900404', 3);
	exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 8);
	exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 5);
	exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 1);

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

	strcpy(msg, "open");
	exec sql open cur;

	exec sql whenever not found do break;

	while (1) {
		strcpy(msg, "fetch");
		exec sql fetch in cur into :personal:ind_personal, :married:ind_married, :children:ind_children;
		printf("%8.8s", personal.name.arr);
		if (ind_personal.ind_birth.born >= 0)
			printf(", born %d", personal.birth.born);
		if (ind_personal.ind_birth.age >= 0)
			printf(", age = %d", personal.birth.age);
		if (ind_married >= 0)
			printf(", married %10.10s", married->arr);
		if (ind_children >= 0)
			printf(", children = %d", children);
		putchar('\n');

		free(married);
		married = NULL;
	}

	strcpy(msg, "close");
	exec sql close cur;

	/* and now the same query with prepare */
	exec sql prepare MM from :query;
	exec sql declare prep cursor for MM;

	strcpy(msg, "open");
	exec sql open prep using :testname;

	exec sql whenever not found do break;

	while (1) {
		strcpy(msg, "fetch");
		exec sql fetch in prep into :personal:ind_personal, :married:ind_married, :children:ind_children;
		printf("%8.8s", personal.name.arr);
		if (ind_personal.ind_birth.born >= 0)
			printf(", born %d", personal.birth.born);
		if (ind_personal.ind_birth.age >= 0)
			printf(", age = %d", personal.birth.age);
		if (ind_married >= 0)
			printf(", married %10.10s", married->arr);
		if (ind_children >= 0)
			printf(", children = %d", children);
		putchar('\n');
	}

	free(married);

	strcpy(msg, "close");
	exec sql close prep;

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

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

	strcpy(msg, "disconnect"); 

	exec sql disconnect;
	if (dbgs != NULL)
                fclose(dbgs);

	return (0);
}
#include <stdio.h>

exec sql include header_test;

exec sql type str is varchar[10];

int
main ()
{
	typedef struct { long born; short age; } birthinfo;
	exec sql type birthinfo is struct { long born; short age; };
exec sql begin declare section;
	struct personal_struct	{	str name;
					birthinfo birth;
				} personal;
	struct personal_indicator {	int ind_name;
					birthinfo ind_birth;
				  } ind_personal;
	int *ind_married = NULL;
	int children;
	int ind_children;
	str *married = NULL;
	char *testname="Petra";
	char *query="select name, born, age, married, children from meskes where name = :var1";
exec sql end declare section;

	exec sql declare cur cursor for
	       select name, born, age, married, children from meskes;

	char msg[128], command[128];
	FILE *dbgs;

	if ((dbgs = fopen("log", "w")) != NULL)
                ECPGdebug(1, dbgs);

	strcpy(msg, "connect");
	exec sql connect to unix:postgresql://localhost:5432/mm; 

	strcpy(msg, "create");
	exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer);

	strcpy(msg, "insert");
	exec sql insert into meskes(name, married, children) values ('Petra', '19900404', 3);
	exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 33, '19900404', 3);
	exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 8);
	exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 5);
	exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 1);

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

	strcpy(msg, "open");
	exec sql open cur;

	exec sql whenever not found do break;

	while (1) {
		strcpy(msg, "fetch");
		exec sql fetch in cur into :personal:ind_personal, :married:ind_married, :children:ind_children;
		printf("%8.8s", personal.name.arr);
		if (ind_personal.ind_birth.born >= 0)
			printf(", born %d", personal.birth.born);
		if (ind_personal.ind_birth.age >= 0)
			printf(", age = %d", personal.birth.age);
		if (ind_married >= 0)
			printf(", married %10.10s", married->arr);
		if (ind_children >= 0)
			printf(", children = %d", children);
		putchar('\n');

		free(married);
		married = NULL;
	}

	strcpy(msg, "close");
	exec sql close cur;

	/* and now the same query with prepare */
	exec sql prepare MM from :query;
	exec sql declare prep cursor for MM;

	strcpy(msg, "open");
	exec sql open prep using :testname;

	exec sql whenever not found do break;

	while (1) {
		strcpy(msg, "fetch");
		exec sql fetch in prep into :personal:ind_personal, :married:ind_married, :children:ind_children;
		printf("%8.8s", personal.name.arr);
		if (ind_personal.ind_birth.born >= 0)
			printf(", born %d", personal.birth.born);
		if (ind_personal.ind_birth.age >= 0)
			printf(", age = %d", personal.birth.age);
		if (ind_married >= 0)
			printf(", married %10.10s", married->arr);
		if (ind_children >= 0)
			printf(", children = %d", children);
		putchar('\n');
	}

	free(married);

	strcpy(msg, "close");
	exec sql close prep;

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

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

	strcpy(msg, "disconnect"); 

	exec sql disconnect;
	if (dbgs != NULL)
                fclose(dbgs);

	return (0);
}