1
#include "postgres_fe.h"
5
#define indicator_set ind_type != NULL && ind_type->type != ECPGt_NO_INDICATOR
7
struct ECPGstruct_member struct_no_indicator = {"no_indicator", &ecpg_no_indicator, NULL};
9
/* malloc + error check */
13
void *ptr = malloc(size);
16
mmerror(OUT_OF_MEMORY, ET_FATAL, "Out of memory\n");
21
/* strdup + error check */
23
mm_strdup(const char *string)
25
char *new = strdup(string);
28
mmerror(OUT_OF_MEMORY, ET_FATAL, "Out of memory\n");
33
/* duplicate memberlist */
34
struct ECPGstruct_member *
35
ECPGstruct_member_dup(struct ECPGstruct_member * rm)
37
struct ECPGstruct_member *new = NULL;
41
struct ECPGtype *type;
43
switch (rm->type->type)
47
type = ECPGmake_struct_type(rm->type->u.members, rm->type->type, rm->type->struct_sizeof);
52
* if this array does contain a struct again, we have to
53
* create the struct too
55
if (rm->type->u.element->type == ECPGt_struct)
56
type = ECPGmake_struct_type(rm->type->u.element->u.members, rm->type->u.element->type, rm->type->u.element->struct_sizeof);
58
type = ECPGmake_array_type(ECPGmake_simple_type(rm->type->u.element->type, rm->type->u.element->size), rm->type->size);
61
type = ECPGmake_simple_type(rm->type->type, rm->type->size);
65
ECPGmake_struct_member(rm->name, type, &new);
73
/* The NAME argument is copied. The type argument is preserved as a pointer. */
75
ECPGmake_struct_member(char *name, struct ECPGtype * type, struct ECPGstruct_member ** start)
77
struct ECPGstruct_member *ptr,
79
(struct ECPGstruct_member *) mm_alloc(sizeof(struct ECPGstruct_member));
81
ne->name = strdup(name);
85
for (ptr = *start; ptr && ptr->next; ptr = ptr->next);
94
ECPGmake_simple_type(enum ECPGttype type, char *size)
96
struct ECPGtype *ne = (struct ECPGtype *) mm_alloc(sizeof(struct ECPGtype));
100
ne->u.element = NULL;
101
ne->struct_sizeof = NULL;
107
ECPGmake_array_type(struct ECPGtype * type, char *size)
109
struct ECPGtype *ne = ECPGmake_simple_type(ECPGt_array, size);
111
ne->u.element = type;
117
ECPGmake_struct_type(struct ECPGstruct_member * rm, enum ECPGttype type, char *struct_sizeof)
119
struct ECPGtype *ne = ECPGmake_simple_type(type, make_str("1"));
121
ne->u.members = ECPGstruct_member_dup(rm);
122
ne->struct_sizeof = struct_sizeof;
128
get_type(enum ECPGttype type)
133
return ("ECPGt_char");
135
case ECPGt_unsigned_char:
136
return ("ECPGt_unsigned_char");
139
return ("ECPGt_short");
141
case ECPGt_unsigned_short:
142
return ("ECPGt_unsigned_short");
145
return ("ECPGt_int");
147
case ECPGt_unsigned_int:
148
return ("ECPGt_unsigned_int");
151
return ("ECPGt_long");
153
case ECPGt_unsigned_long:
154
return ("ECPGt_unsigned_long");
156
case ECPGt_long_long:
157
return ("ECPGt_long_long");
159
case ECPGt_unsigned_long_long:
160
return ("ECPGt_unsigned_long_long");
163
return ("ECPGt_float");
166
return ("ECPGt_double");
169
return ("ECPGt_bool");
172
return ("ECPGt_varchar");
173
case ECPGt_NO_INDICATOR: /* no indicator */
174
return ("ECPGt_NO_INDICATOR");
176
case ECPGt_char_variable: /* string that should not be
178
return ("ECPGt_char_variable");
180
case ECPGt_const: /* constant string quoted */
181
return ("ECPGt_const");
184
return ("ECPGt_decimal");
187
return ("ECPGt_numeric");
190
return ("ECPGt_interval");
192
case ECPGt_descriptor:
193
return ("ECPGt_descriptor");
196
return ("ECPGt_date");
198
case ECPGt_timestamp:
199
return ("ECPGt_timestamp");
202
mmerror(PARSE_ERROR, ET_ERROR, "illegal variable type %d\n", type);
209
The type is dumped as:
210
type-tag <comma> - enum ECPGttype
211
reference-to-variable <comma> - char *
212
size <comma> - long size of this field (if varchar)
213
arrsize <comma> - long number of elements in the arr
214
offset <comma> - offset to the next element
216
type-tag is one of the simple types or varchar.
217
reference-to-variable can be a reference to a struct element.
218
arrsize is the size of the array in case of array fetches. Otherwise 0.
219
size is the maxsize in case it is a varchar. Otherwise it is the size of
220
the variable (required to do array fetches of structs).
222
static void ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
224
char *arrsiz, const char *siz, const char *prefix);
225
static void ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsiz,
226
struct ECPGtype * type, struct ECPGtype * ind_type, const char *offset, const char *prefix, const char *ind_prefix);
229
ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type,
230
const char *ind_name, struct ECPGtype * ind_type,
231
const char *prefix, const char *ind_prefix,
232
char *arr_str_siz, const char *struct_sizeof,
233
const char *ind_struct_sizeof)
238
if (indicator_set && ind_type->type != ECPGt_array)
239
mmerror(INDICATOR_NOT_ARRAY, ET_FATAL, "Indicator for array/pointer has to be array/pointer.\n");
240
switch (type->u.element->type)
243
mmerror(PARSE_ERROR, ET_ERROR, "No nested arrays allowed (except strings)"); /* array of array */
247
ECPGdump_a_struct(o, name,
251
(ind_type->type == ECPGt_NO_INDICATOR) ? ind_type : ind_type->u.element,
252
NULL, prefix, ind_prefix);
255
if (!IS_SIMPLE_TYPE(type->u.element->type))
256
yyerror("Internal error: unknown datatype, please inform pgsql-bugs@postgresql.org");
258
ECPGdump_a_simple(o, name,
259
type->u.element->type,
260
type->u.element->size, type->size, NULL, prefix);
262
if (ind_type != NULL)
264
if (ind_type->type == ECPGt_NO_INDICATOR)
265
ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, make_str("-1"), NULL, ind_prefix);
268
ECPGdump_a_simple(o, ind_name, ind_type->u.element->type,
269
ind_type->u.element->size, ind_type->size, NULL, ind_prefix);
275
if (indicator_set && ind_type->type != ECPGt_struct)
276
mmerror(INDICATOR_NOT_STRUCT, ET_FATAL, "Indicator for struct has to be struct.\n");
278
ECPGdump_a_struct(o, name, ind_name, make_str("1"), type, ind_type, NULL, prefix, ind_prefix);
280
case ECPGt_union: /* cannot dump a complete union */
281
yyerror("Type of union has to be specified");
283
case ECPGt_char_variable:
284
if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
285
mmerror(INDICATOR_NOT_SIMPLE, ET_FATAL, "Indicator for simple datatype has to be simple.\n");
287
ECPGdump_a_simple(o, name, type->type, make_str("1"), (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : make_str("1"), struct_sizeof, prefix);
288
ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : make_str("-1"), ind_struct_sizeof, ind_prefix);
290
case ECPGt_descriptor:
291
if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
292
mmerror(INDICATOR_NOT_SIMPLE, ET_FATAL, "Indicator for simple datatype has to be simple.\n");
294
ECPGdump_a_simple(o, name, type->type, NULL, make_str("-1"), NULL, prefix);
295
ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, make_str("-1"), NULL, ind_prefix);
298
if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
299
mmerror(INDICATOR_NOT_SIMPLE, ET_FATAL, "Indicator for simple datatype has to be simple.\n");
301
ECPGdump_a_simple(o, name, type->type, type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : make_str("-1"), struct_sizeof, prefix);
302
if (ind_type != NULL)
303
ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : make_str("-1"), ind_struct_sizeof, ind_prefix);
309
/* If siz is NULL, then the offset is 0, if not use siz as a
310
string, it represents the offset needed if we are in an array of structs. */
312
ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
319
if (type == ECPGt_NO_INDICATOR)
320
fprintf(o, "\n\tECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ");
321
else if (type == ECPGt_descriptor)
322
/* remember that name here already contains quotes (if needed) */
323
fprintf(o, "\n\tECPGt_descriptor, %s, 0L, 0L, 0L, ", name);
326
char *variable = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4);
327
char *offset = (char *) mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1 + strlen(varcharsize));
332
* we have to use the & operator except for arrays and
339
* we have to use the pointer except for arrays with given
342
if (((atoi(arrsize) > 0) ||
343
(atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
345
sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
347
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
349
sprintf(offset, "sizeof(struct varchar_%s)", name);
352
case ECPGt_unsigned_char:
353
case ECPGt_char_variable:
356
* we have to use the pointer except for arrays with given
357
* bounds, ecpglib will distinguish between * and []
359
if ((atoi(varcharsize) > 1 ||
360
(atoi(arrsize) > 0) ||
361
(atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) ||
362
(atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0))
364
sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
366
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
368
sprintf(offset, "(%s)*sizeof(char)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize);
373
* we have to use a pointer here
375
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
376
sprintf(offset, "sizeof(numeric)");
381
* we have to use a pointer here
383
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
384
sprintf(offset, "sizeof(interval)");
389
* we have to use a pointer and translate the variable
392
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
393
sprintf(offset, "sizeof(date)");
395
case ECPGt_timestamp:
398
* we have to use a pointer and translate the variable
401
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
402
sprintf(offset, "sizeof(timestamp)");
407
* just dump the const as string
409
sprintf(variable, "\"%s\"", name);
410
sprintf(offset, "strlen(\"%s\")", name);
415
* we have to use the pointer except for arrays with given
418
if (((atoi(arrsize) > 0) ||
419
(atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
421
sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
423
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
425
sprintf(offset, "sizeof(%s)", ECPGtype_name(type));
429
if (atoi(arrsize) < 0)
430
strcpy(arrsize, "1");
432
if (siz == NULL || strcmp(arrsize, "0") == 0 || strcmp(arrsize, "1") == 0)
433
fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, offset);
435
fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, siz);
443
/* Penetrate a struct and dump the contents. */
445
ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsiz, struct ECPGtype * type, struct ECPGtype * ind_type, const char *offsetarg, const char *prefix, const char *ind_prefix)
448
* If offset is NULL, then this is the first recursive level. If not
449
* then we are in a struct in a struct and the offset is used as
452
struct ECPGstruct_member *p,
459
if (offsetarg == NULL)
461
sprintf(obuf, "sizeof(%s)", name);
467
if (atoi(arrsiz) == 1)
468
sprintf(pbuf, "%s%s.", prefix ? prefix : "", name);
470
sprintf(pbuf, "%s%s->", prefix ? prefix : "", name);
474
if (ind_type == &ecpg_no_indicator)
475
ind_p = &struct_no_indicator;
476
else if (ind_type != NULL)
478
if (atoi(arrsiz) == 1)
479
sprintf(ind_pbuf, "%s%s.", ind_prefix ? ind_prefix : "", ind_name);
481
sprintf(ind_pbuf, "%s%s->", ind_prefix ? ind_prefix : "", ind_name);
483
ind_prefix = ind_pbuf;
484
ind_p = ind_type->u.members;
487
for (p = type->u.members; p; p = p->next)
489
ECPGdump_a_type(o, p->name, p->type,
490
(ind_p != NULL) ? ind_p->name : NULL,
491
(ind_p != NULL) ? ind_p->type : NULL,
492
prefix, ind_prefix, arrsiz, type->struct_sizeof,
493
(ind_p != NULL) ? ind_type->struct_sizeof : NULL);
494
if (ind_p != NULL && ind_p != &struct_no_indicator)
500
ECPGfree_struct_member(struct ECPGstruct_member * rm)
504
struct ECPGstruct_member *p = rm;
514
ECPGfree_type(struct ECPGtype * type)
516
if (!IS_SIMPLE_TYPE(type->type))
521
switch (type->u.element->type)
524
yyerror("internal error, found multidimensional array\n");
528
/* Array of structs. */
529
ECPGfree_struct_member(type->u.element->u.members);
530
free(type->u.element);
533
if (!IS_SIMPLE_TYPE(type->u.element->type))
534
yyerror("Internal error: unknown datatype, please inform pgsql-bugs@postgresql.org");
536
free(type->u.element);
541
ECPGfree_struct_member(type->u.members);
544
mmerror(PARSE_ERROR, ET_ERROR, "illegal variable type %d\n", type->type);
552
get_dtype(enum ECPGdtype type)
557
return ("ECPGd_countr");
560
return ("ECPGd_data");
563
return ("ECPGd_di_code");
565
case ECPGd_di_precision:
566
return ("ECPGd_di_precision");
568
case ECPGd_indicator:
569
return ("ECPGd_indicator");
571
case ECPGd_key_member:
572
return ("ECPGd_key_member");
575
return ("ECPGd_length");
578
return ("ECPGd_name");
581
return ("ECPGd_nullable");
584
return ("ECPGd_octet");
586
case ECPGd_precision:
587
return ("ECPGd_precision");
589
case ECPGd_ret_length:
590
return ("ECPGd_ret_length");
591
case ECPGd_ret_octet:
592
return ("ECPGd_ret_octet");
595
return ("ECPGd_scale");
598
return ("ECPGd_type");
600
case ECPGd_cardinality:
601
return ("ECPGd_cardinality");
603
mmerror(PARSE_ERROR, ET_ERROR, "illegal descriptor item %d\n", type);