PostgreSQL Source Code git master
preproc-pointer_to_struct.c
Go to the documentation of this file.
1/* Processed by ecpg (regression mode) */
2/* These include files are added by the preprocessor */
3#include <ecpglib.h>
4#include <ecpgerrno.h>
5#include <sqlca.h>
6/* End of automatic include section */
7#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
8
9#line 1 "pointer_to_struct.pgc"
10#include <stdio.h>
11#include <stdlib.h>
12
13
14#line 1 "regression.h"
15
16
17
18
19
20
21#line 4 "pointer_to_struct.pgc"
22
23
24/* exec sql whenever sqlerror sqlprint ; */
25#line 6 "pointer_to_struct.pgc"
26
27/* exec sql whenever sql_warning sqlprint ; */
28#line 7 "pointer_to_struct.pgc"
29
30/* exec sql whenever not found sqlprint ; */
31#line 8 "pointer_to_struct.pgc"
32
33
34typedef struct {
35#line 13 "pointer_to_struct.pgc"
36 struct varchar_1 { int len; char arr[ 50 ]; } name ;
37
38#line 14 "pointer_to_struct.pgc"
39 int phone ;
40 } customer ;
41#line 15 "pointer_to_struct.pgc"
42
43
44typedef struct ind {
45#line 20 "pointer_to_struct.pgc"
46 short name_ind ;
47
48#line 21 "pointer_to_struct.pgc"
49 short phone_ind ;
51#line 22 "pointer_to_struct.pgc"
52
53
54typedef struct {
55#line 27 "pointer_to_struct.pgc"
57 } company ;
58#line 28 "pointer_to_struct.pgc"
59
60
61int main()
62{
63 /* exec sql begin declare section */
64
65
66
67
68
69
70 typedef struct {
71#line 37 "pointer_to_struct.pgc"
72 struct varchar_2 { int len; char arr[ 50 ]; } name ;
73
74#line 38 "pointer_to_struct.pgc"
75 int phone ;
76 } customer2 ;
77
78#line 39 "pointer_to_struct.pgc"
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99#line 33 "pointer_to_struct.pgc"
100 customer * custs1 = ( customer * ) malloc ( sizeof ( customer ) * 10 ) ;
101
102#line 34 "pointer_to_struct.pgc"
103 cust_ind * inds = ( cust_ind * ) malloc ( sizeof ( cust_ind ) * 10 ) ;
104
105#line 40 "pointer_to_struct.pgc"
106 customer2 * custs2 = ( customer2 * ) malloc ( sizeof ( customer2 ) * 10 ) ;
107
108#line 46 "pointer_to_struct.pgc"
109 struct customer3 {
110#line 44 "pointer_to_struct.pgc"
111 char name [ 50 ] ;
112
113#line 45 "pointer_to_struct.pgc"
114 int phone ;
115 } * custs3 = ( struct customer3 * ) malloc ( sizeof ( struct customer3 ) * 10 ) ;
116
117#line 52 "pointer_to_struct.pgc"
118 struct customer4 {
119#line 50 "pointer_to_struct.pgc"
120 struct varchar_3 { int len; char arr[ 50 ]; } name ;
121
122#line 51 "pointer_to_struct.pgc"
123 int phone ;
124 } * custs4 = ( struct customer4 * ) malloc ( sizeof ( struct customer4 ) ) ;
125
126#line 54 "pointer_to_struct.pgc"
127 company acme ;
128
129#line 56 "pointer_to_struct.pgc"
130 int r ;
131
132#line 57 "pointer_to_struct.pgc"
133 struct varchar_4 { int len; char arr[ 50 ]; } onlyname [ 2 ] ;
134/* exec sql end declare section */
135#line 58 "pointer_to_struct.pgc"
136
137
138 ECPGdebug(1, stderr);
139
140 acme.customers = (customer *) malloc(sizeof(customer) * 10);
141
142 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
143#line 64 "pointer_to_struct.pgc"
144
145if (sqlca.sqlwarn[0] == 'W') sqlprint();
146#line 64 "pointer_to_struct.pgc"
147
148if (sqlca.sqlcode < 0) sqlprint();}
149#line 64 "pointer_to_struct.pgc"
150
151
152 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table customers ( c varchar ( 50 ) , p int )", ECPGt_EOIT, ECPGt_EORT);
153#line 66 "pointer_to_struct.pgc"
154
155if (sqlca.sqlwarn[0] == 'W') sqlprint();
156#line 66 "pointer_to_struct.pgc"
157
158if (sqlca.sqlcode < 0) sqlprint();}
159#line 66 "pointer_to_struct.pgc"
160
161
162 /* First we'll insert some data using C variable references */
163 strcpy(custs1[0].name.arr, "John Doe");
164 custs1[0].name.len = strlen(custs1[0].name.arr);
165 custs1[0].phone = 12345;
166
167 strcpy(acme.customers[1].name.arr, "Jane Doe");
168 acme.customers[1].name.len = strlen(acme.customers[1].name.arr);
169 acme.customers[1].phone = 67890;
170
171 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into customers values ( $1 , $2 )",
172 ECPGt_varchar,&(custs1->name),(long)50,(long)1,sizeof(struct varchar_1),
173 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
174 ECPGt_int,&(custs1[0].phone),(long)1,(long)1,sizeof(int),
175 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
176#line 78 "pointer_to_struct.pgc"
177
178if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
179#line 78 "pointer_to_struct.pgc"
180
181if (sqlca.sqlwarn[0] == 'W') sqlprint();
182#line 78 "pointer_to_struct.pgc"
183
184if (sqlca.sqlcode < 0) sqlprint();}
185#line 78 "pointer_to_struct.pgc"
186
187 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into customers values ( $1 , $2 )",
188 ECPGt_varchar,&(acme.customers[1].name),(long)50,(long)1,sizeof(struct varchar_1),
189 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
190 ECPGt_int,&(acme.customers[1].phone),(long)1,(long)1,sizeof(int),
191 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
192#line 80 "pointer_to_struct.pgc"
193
194if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
195#line 80 "pointer_to_struct.pgc"
196
197if (sqlca.sqlwarn[0] == 'W') sqlprint();
198#line 80 "pointer_to_struct.pgc"
199
200if (sqlca.sqlcode < 0) sqlprint();}
201#line 80 "pointer_to_struct.pgc"
202
203
204 /* Clear the array, to be sure reading back into it actually gets data */
205 memset(custs1, 0, sizeof(customer) * 10);
206
207 /* Now read back the data */
208 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from customers limit 2", ECPGt_EOIT,
209 ECPGt_varchar,&(custs1->name),(long)50,(long)-1,sizeof( customer ),
210 ECPGt_short,&(inds->name_ind),(long)1,(long)-1,sizeof( struct ind ),
211 ECPGt_int,&(custs1->phone),(long)1,(long)-1,sizeof( customer ),
212 ECPGt_short,&(inds->phone_ind),(long)1,(long)-1,sizeof( struct ind ), ECPGt_EORT);
213#line 86 "pointer_to_struct.pgc"
214
215if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
216#line 86 "pointer_to_struct.pgc"
217
218if (sqlca.sqlwarn[0] == 'W') sqlprint();
219#line 86 "pointer_to_struct.pgc"
220
221if (sqlca.sqlcode < 0) sqlprint();}
222#line 86 "pointer_to_struct.pgc"
223
224 printf("custs1:\n");
225 for (r = 0; r < 2; r++)
226 {
227 printf( "name - %s\n", custs1[r].name.arr );
228 printf( "phone - %d\n", custs1[r].phone );
229 }
230
231 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from customers limit 2", ECPGt_EOIT,
232 ECPGt_varchar,&(custs2->name),(long)50,(long)-1,sizeof( customer2 ),
233 ECPGt_short,&(inds->name_ind),(long)1,(long)-1,sizeof( struct ind ),
234 ECPGt_int,&(custs2->phone),(long)1,(long)-1,sizeof( customer2 ),
235 ECPGt_short,&(inds->phone_ind),(long)1,(long)-1,sizeof( struct ind ), ECPGt_EORT);
236#line 94 "pointer_to_struct.pgc"
237
238if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
239#line 94 "pointer_to_struct.pgc"
240
241if (sqlca.sqlwarn[0] == 'W') sqlprint();
242#line 94 "pointer_to_struct.pgc"
243
244if (sqlca.sqlcode < 0) sqlprint();}
245#line 94 "pointer_to_struct.pgc"
246
247 printf("\ncusts2:\n");
248 for (r = 0; r < 2; r++)
249 {
250 printf( "name - %s\n", custs2[r].name.arr );
251 printf( "phone - %d\n", custs2[r].phone );
252 }
253
254 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from customers limit 2", ECPGt_EOIT,
255 ECPGt_char,&(custs3->name),(long)50,(long)-1,sizeof( struct customer3 ),
256 ECPGt_short,&(inds->name_ind),(long)1,(long)-1,sizeof( struct ind ),
257 ECPGt_int,&(custs3->phone),(long)1,(long)-1,sizeof( struct customer3 ),
258 ECPGt_short,&(inds->phone_ind),(long)1,(long)-1,sizeof( struct ind ), ECPGt_EORT);
259#line 102 "pointer_to_struct.pgc"
260
261if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
262#line 102 "pointer_to_struct.pgc"
263
264if (sqlca.sqlwarn[0] == 'W') sqlprint();
265#line 102 "pointer_to_struct.pgc"
266
267if (sqlca.sqlcode < 0) sqlprint();}
268#line 102 "pointer_to_struct.pgc"
269
270 printf("\ncusts3:\n");
271 for (r = 0; r < 2; r++)
272 {
273 printf( "name - %s\n", custs3[r].name );
274 printf( "phone - %d\n", custs3[r].phone );
275 }
276
277 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from customers limit 1", ECPGt_EOIT,
278 ECPGt_varchar,&(custs4->name),(long)50,(long)-1,sizeof( struct customer4 ),
279 ECPGt_short,&(inds->name_ind),(long)1,(long)-1,sizeof( struct ind ),
280 ECPGt_int,&(custs4->phone),(long)1,(long)-1,sizeof( struct customer4 ),
281 ECPGt_short,&(inds->phone_ind),(long)1,(long)-1,sizeof( struct ind ), ECPGt_EORT);
282#line 110 "pointer_to_struct.pgc"
283
284if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
285#line 110 "pointer_to_struct.pgc"
286
287if (sqlca.sqlwarn[0] == 'W') sqlprint();
288#line 110 "pointer_to_struct.pgc"
289
290if (sqlca.sqlcode < 0) sqlprint();}
291#line 110 "pointer_to_struct.pgc"
292
293 printf("\ncusts4:\n");
294 printf( "name - %s\n", custs4->name.arr );
295 printf( "phone - %d\n", custs4->phone );
296
297 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select c from customers limit 2", ECPGt_EOIT,
298 ECPGt_varchar,(onlyname),(long)50,(long)2,sizeof(struct varchar_4),
299 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
300#line 115 "pointer_to_struct.pgc"
301
302if (sqlca.sqlcode == ECPG_NOT_FOUND) sqlprint();
303#line 115 "pointer_to_struct.pgc"
304
305if (sqlca.sqlwarn[0] == 'W') sqlprint();
306#line 115 "pointer_to_struct.pgc"
307
308if (sqlca.sqlcode < 0) sqlprint();}
309#line 115 "pointer_to_struct.pgc"
310
311 printf("\nname:\n");
312 for (r = 0; r < 2; r++)
313 {
314 printf( "name - %s\n", onlyname[r].arr );
315 }
316
317 { ECPGdisconnect(__LINE__, "ALL");
318#line 122 "pointer_to_struct.pgc"
319
320if (sqlca.sqlwarn[0] == 'W') sqlprint();
321#line 122 "pointer_to_struct.pgc"
322
323if (sqlca.sqlcode < 0) sqlprint();}
324#line 122 "pointer_to_struct.pgc"
325
326
327 /* All the memory will anyway be freed at the end */
328 return 0;
329}
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:693
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:260
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
@ ECPGt_char
Definition: ecpgtype.h:43
void sqlprint(void)
Definition: error.c:334
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2291
#define malloc(a)
Definition: header.h:50
const void size_t len
#define printf(...)
Definition: port.h:266
struct ind cust_ind
#define ECPGdebug(X, Y)
#define sqlca
Definition: sqlca.h:59
customer * customers
customer customers[10]
struct customer::varchar_1 name
const char * name