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 "dyntest.pgc"
10 /* dynamic SQL test program
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16
17 #line 1 "sql3types.h"
18 #ifndef _ECPG_SQL3TYPES_H
19 #define _ECPG_SQL3TYPES_H
20
21 /* SQL3 dynamic type codes */
22
23 /* chapter 13.1 table 2: Codes used for SQL data types in Dynamic SQL */
24
25 enum
26 {
27 SQL3_CHARACTER = 1,
28 SQL3_NUMERIC,
29 SQL3_DECIMAL,
30 SQL3_INTEGER,
31 SQL3_SMALLINT,
32 SQL3_FLOAT,
33 SQL3_REAL,
34 SQL3_DOUBLE_PRECISION,
35 SQL3_DATE_TIME_TIMESTAMP,
36 SQL3_INTERVAL, /* 10 */
37 SQL3_CHARACTER_VARYING = 12,
38 SQL3_ENUMERATED,
39 SQL3_BIT,
40 SQL3_BIT_VARYING,
41 SQL3_BOOLEAN,
42 SQL3_abstract
43 /* the rest is xLOB stuff */
44 };
45
46 /* chapter 13.1 table 3: Codes associated with datetime data types in Dynamic SQL */
47
48 enum
49 {
50 SQL3_DDT_DATE = 1,
51 SQL3_DDT_TIME,
52 SQL3_DDT_TIMESTAMP,
53 SQL3_DDT_TIME_WITH_TIME_ZONE,
54 SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE,
55
56 SQL3_DDT_ILLEGAL /* not a datetime data type (not part of
57 * standard) */
58 };
59
60 #endif /* !_ECPG_SQL3TYPES_H */
61
62 #line 7 "dyntest.pgc"
63
64
65 #line 1 "sqlca.h"
66 #ifndef POSTGRES_SQLCA_H
67 #define POSTGRES_SQLCA_H
68
69 #ifndef PGDLLIMPORT
70 #if defined(WIN32) || defined(__CYGWIN__)
71 #define PGDLLIMPORT __declspec (dllimport)
72 #else
73 #define PGDLLIMPORT
74 #endif /* __CYGWIN__ */
75 #endif /* PGDLLIMPORT */
76
77 #define SQLERRMC_LEN 150
78
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #endif
83
84 struct sqlca_t
85 {
86 char sqlcaid[8];
87 long sqlabc;
88 long sqlcode;
89 struct
90 {
91 int sqlerrml;
92 char sqlerrmc[SQLERRMC_LEN];
93 } sqlerrm;
94 char sqlerrp[8];
95 long sqlerrd[6];
96 /* Element 0: empty */
97 /* 1: OID of processed tuple if applicable */
98 /* 2: number of rows processed */
99 /* after an INSERT, UPDATE or */
100 /* DELETE statement */
101 /* 3: empty */
102 /* 4: empty */
103 /* 5: empty */
104 char sqlwarn[8];
105 /* Element 0: set to 'W' if at least one other is 'W' */
106 /* 1: if 'W' at least one character string */
107 /* value was truncated when it was */
108 /* stored into a host variable. */
109
110 /*
111 * 2: if 'W' a (hopefully) non-fatal notice occurred
112 */ /* 3: empty */
113 /* 4: empty */
114 /* 5: empty */
115 /* 6: empty */
116 /* 7: empty */
117
118 char sqlstate[5];
119 };
120
121 struct sqlca_t *ECPGget_sqlca(void);
122
123 #ifndef POSTGRES_ECPG_INTERNAL
124 #define sqlca (*ECPGget_sqlca())
125 #endif
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif
132
133 #line 8 "dyntest.pgc"
134
135
136 #line 1 "regression.h"
137
138
139
140
141
142
143 #line 9 "dyntest.pgc"
144
145
146 static void
error(void)147 error (void)
148 {
149 printf ("\n#%ld:%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
150 exit (1);
151 }
152
153 int
main()154 main ()
155 {
156 /* exec sql begin declare section */
157
158
159
160
161
162
163
164
165
166
167
168 #line 22 "dyntest.pgc"
169 int COUNT ;
170
171 #line 23 "dyntest.pgc"
172 int INTVAR ;
173
174 #line 24 "dyntest.pgc"
175 int INDEX ;
176
177 #line 25 "dyntest.pgc"
178 int INDICATOR ;
179
180 #line 26 "dyntest.pgc"
181 int TYPE , LENGTH , OCTET_LENGTH , PRECISION , SCALE , RETURNED_OCTET_LENGTH ;
182
183 #line 27 "dyntest.pgc"
184 int DATETIME_INTERVAL_CODE ;
185
186 #line 28 "dyntest.pgc"
187 char NAME [ 120 ] , BOOLVAR ;
188
189 #line 29 "dyntest.pgc"
190 char STRINGVAR [ 1024 ] ;
191
192 #line 30 "dyntest.pgc"
193 double DOUBLEVAR ;
194
195 #line 31 "dyntest.pgc"
196 char * QUERY ;
197 /* exec sql end declare section */
198 #line 32 "dyntest.pgc"
199
200 int done = 0;
201
202 /* exec sql var BOOLVAR is bool */
203 #line 35 "dyntest.pgc"
204
205
206 ECPGdebug (1, stderr);
207
208 QUERY = "select * from dyntest";
209
210 /* exec sql whenever sqlerror do error ( ) ; */
211 #line 43 "dyntest.pgc"
212
213
214 ECPGallocate_desc(__LINE__, "MYDESC");
215 #line 45 "dyntest.pgc"
216
217 if (sqlca.sqlcode < 0) error ( );
218 #line 45 "dyntest.pgc"
219
220
221 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
222 #line 47 "dyntest.pgc"
223
224 if (sqlca.sqlcode < 0) error ( );}
225 #line 47 "dyntest.pgc"
226
227
228 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to german", ECPGt_EOIT, ECPGt_EORT);
229 #line 49 "dyntest.pgc"
230
231 if (sqlca.sqlcode < 0) error ( );}
232 #line 49 "dyntest.pgc"
233
234
235 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table dyntest ( name char ( 14 ) , d float8 , i int , bignumber int8 , b boolean , comment text , day date )", ECPGt_EOIT, ECPGt_EORT);
236 #line 53 "dyntest.pgc"
237
238 if (sqlca.sqlcode < 0) error ( );}
239 #line 53 "dyntest.pgc"
240
241 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into dyntest values ( 'first entry' , 14.7 , 14 , 123045607890 , true , 'The world''s most advanced open source database.' , '1987-07-14' )", ECPGt_EOIT, ECPGt_EORT);
242 #line 54 "dyntest.pgc"
243
244 if (sqlca.sqlcode < 0) error ( );}
245 #line 54 "dyntest.pgc"
246
247 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into dyntest values ( 'second entry' , 1407.87 , 1407 , 987065403210 , false , 'The elephant never forgets.' , '1999-11-5' )", ECPGt_EOIT, ECPGt_EORT);
248 #line 55 "dyntest.pgc"
249
250 if (sqlca.sqlcode < 0) error ( );}
251 #line 55 "dyntest.pgc"
252
253
254 { ECPGprepare(__LINE__, NULL, 0, "myquery", QUERY);
255 #line 57 "dyntest.pgc"
256
257 if (sqlca.sqlcode < 0) error ( );}
258 #line 57 "dyntest.pgc"
259
260 /* declare MYCURS cursor for $1 */
261 #line 58 "dyntest.pgc"
262
263
264 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare MYCURS cursor for $1",
265 ECPGt_char_variable,(ECPGprepared_statement(NULL, "myquery", __LINE__)),(long)1,(long)1,(1)*sizeof(char),
266 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
267 #line 60 "dyntest.pgc"
268
269 if (sqlca.sqlcode < 0) error ( );}
270 #line 60 "dyntest.pgc"
271
272
273 while (1)
274 {
275 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch in MYCURS", ECPGt_EOIT,
276 ECPGt_descriptor, "MYDESC", 1L, 1L, 1L,
277 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
278 #line 64 "dyntest.pgc"
279
280 if (sqlca.sqlcode < 0) error ( );}
281 #line 64 "dyntest.pgc"
282
283
284 if (sqlca.sqlcode)
285 break;
286
287 { ECPGget_desc_header(__LINE__, "MYDESC", &(COUNT));
288
289 #line 69 "dyntest.pgc"
290
291 if (sqlca.sqlcode < 0) error ( );}
292 #line 69 "dyntest.pgc"
293
294 if (!done)
295 {
296 printf ("Found %d columns\n", COUNT);
297 done = 1;
298 }
299
300 for (INDEX = 1; INDEX <= COUNT; ++INDEX)
301 {
302 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_indicator,
303 ECPGt_int,&(INDICATOR),(long)1,(long)1,sizeof(int), ECPGd_name,
304 ECPGt_char,(NAME),(long)120,(long)1,(120)*sizeof(char), ECPGd_scale,
305 ECPGt_int,&(SCALE),(long)1,(long)1,sizeof(int), ECPGd_precision,
306 ECPGt_int,&(PRECISION),(long)1,(long)1,sizeof(int), ECPGd_ret_octet,
307 ECPGt_int,&(RETURNED_OCTET_LENGTH),(long)1,(long)1,sizeof(int), ECPGd_octet,
308 ECPGt_int,&(OCTET_LENGTH),(long)1,(long)1,sizeof(int), ECPGd_length,
309 ECPGt_int,&(LENGTH),(long)1,(long)1,sizeof(int), ECPGd_type,
310 ECPGt_int,&(TYPE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
311
312 #line 86 "dyntest.pgc"
313
314 if (sqlca.sqlcode < 0) error ( );}
315 #line 86 "dyntest.pgc"
316
317 printf ("%2d\t%s (type: %d length: %d precision: %d scale: %d = " , INDEX, NAME, TYPE, LENGTH, PRECISION, SCALE);
318 switch (TYPE)
319 {
320 case SQL3_BOOLEAN:
321 printf ("bool");
322 break;
323 case SQL3_NUMERIC:
324 printf ("numeric(%d,%d)", PRECISION, SCALE);
325 break;
326 case SQL3_DECIMAL:
327 printf ("decimal(%d,%d)", PRECISION, SCALE);
328 break;
329 case SQL3_INTEGER:
330 printf ("integer");
331 break;
332 case SQL3_SMALLINT:
333 printf ("smallint");
334 break;
335 case SQL3_FLOAT:
336 printf ("float(%d,%d)", PRECISION, SCALE);
337 break;
338 case SQL3_REAL:
339 printf ("real");
340 break;
341 case SQL3_DOUBLE_PRECISION:
342 printf ("double precision");
343 break;
344 case SQL3_DATE_TIME_TIMESTAMP:
345 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_di_code,
346 ECPGt_int,&(DATETIME_INTERVAL_CODE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
347
348 #line 116 "dyntest.pgc"
349
350 if (sqlca.sqlcode < 0) error ( );}
351 #line 116 "dyntest.pgc"
352
353 switch (DATETIME_INTERVAL_CODE)
354 {
355 case SQL3_DDT_DATE:
356 printf ("date");
357 break;
358 case SQL3_DDT_TIME:
359 printf ("time");
360 break;
361 case SQL3_DDT_TIMESTAMP:
362 printf ("timestamp");
363 break;
364 case SQL3_DDT_TIME_WITH_TIME_ZONE:
365 printf ("time with time zone");
366 break;
367 case SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE:
368 printf ("timestamp with time zone");
369 break;
370 }
371 break;
372 case SQL3_INTERVAL:
373 printf ("interval");
374 break;
375 case SQL3_CHARACTER:
376 if (LENGTH > 0)
377 printf ("char(%d)", LENGTH);
378 else
379 printf ("text");
380 break;
381 case SQL3_CHARACTER_VARYING:
382 if (LENGTH > 0)
383 printf ("varchar(%d)", LENGTH);
384 else
385 printf ("varchar()");
386 break;
387 default:
388 printf ("<SQL3 %d>", TYPE);
389 break;
390 }
391 printf (")\n\toctet_length: %d returned_octet_length: %d)\n\t= ",
392 OCTET_LENGTH, RETURNED_OCTET_LENGTH);
393 if (INDICATOR == -1)
394 printf ("NULL\n");
395 else
396 switch (TYPE)
397 {
398 case SQL3_BOOLEAN:
399 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
400 ECPGt_bool,&(BOOLVAR),(long)1,(long)1,sizeof(bool), ECPGd_EODT);
401
402 #line 163 "dyntest.pgc"
403
404 if (sqlca.sqlcode < 0) error ( );}
405 #line 163 "dyntest.pgc"
406
407 printf ("%s\n", BOOLVAR ? "true" : "false");
408 break;
409 case SQL3_INTEGER:
410 case SQL3_SMALLINT:
411 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
412 ECPGt_int,&(INTVAR),(long)1,(long)1,sizeof(int), ECPGd_EODT);
413
414 #line 168 "dyntest.pgc"
415
416 if (sqlca.sqlcode < 0) error ( );}
417 #line 168 "dyntest.pgc"
418
419 printf ("%d\n", INTVAR);
420 break;
421 case SQL3_DOUBLE_PRECISION:
422 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
423 ECPGt_double,&(DOUBLEVAR),(long)1,(long)1,sizeof(double), ECPGd_EODT);
424
425 #line 172 "dyntest.pgc"
426
427 if (sqlca.sqlcode < 0) error ( );}
428 #line 172 "dyntest.pgc"
429
430 printf ("%.*f\n", PRECISION, DOUBLEVAR);
431 break;
432 case SQL3_DATE_TIME_TIMESTAMP:
433 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
434 ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_di_code,
435 ECPGt_int,&(DATETIME_INTERVAL_CODE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
436
437 #line 178 "dyntest.pgc"
438
439 if (sqlca.sqlcode < 0) error ( );}
440 #line 178 "dyntest.pgc"
441
442 printf ("%d \"%s\"\n", DATETIME_INTERVAL_CODE, STRINGVAR);
443 break;
444 case SQL3_CHARACTER:
445 case SQL3_CHARACTER_VARYING:
446 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
447 ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_EODT);
448
449 #line 183 "dyntest.pgc"
450
451 if (sqlca.sqlcode < 0) error ( );}
452 #line 183 "dyntest.pgc"
453
454 printf ("\"%s\"\n", STRINGVAR);
455 break;
456 default:
457 { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
458 ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_EODT);
459
460 #line 187 "dyntest.pgc"
461
462 if (sqlca.sqlcode < 0) error ( );}
463 #line 187 "dyntest.pgc"
464
465 printf ("<\"%s\">\n", STRINGVAR);
466 break;
467 }
468 }
469 }
470
471 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close MYCURS", ECPGt_EOIT, ECPGt_EORT);
472 #line 194 "dyntest.pgc"
473
474 if (sqlca.sqlcode < 0) error ( );}
475 #line 194 "dyntest.pgc"
476
477
478 ECPGdeallocate_desc(__LINE__, "MYDESC");
479 #line 196 "dyntest.pgc"
480
481 if (sqlca.sqlcode < 0) error ( );
482 #line 196 "dyntest.pgc"
483
484
485 return 0;
486 }
487