Age Owner Branch data TLA Line data Source code
1 : : /* src/interfaces/ecpg/preproc/type.c */
2 : :
3 : : #include "postgres_fe.h"
4 : :
5 : : #include "preproc_extern.h"
6 : :
7 : : #define indicator_set ind_type != NULL && ind_type->type != ECPGt_NO_INDICATOR
8 : :
9 : : static struct ECPGstruct_member struct_no_indicator = {"no_indicator", &ecpg_no_indicator, NULL};
10 : :
11 : : /* duplicate memberlist */
12 : : struct ECPGstruct_member *
3265 tgl@sss.pgh.pa.us 13 :CBC 105 : ECPGstruct_member_dup(struct ECPGstruct_member *rm)
14 : : {
10133 bruce@momjian.us 15 : 105 : struct ECPGstruct_member *new = NULL;
16 : :
17 [ + + ]: 329 : while (rm)
18 : : {
19 : : struct ECPGtype *type;
20 : :
8924 meskes@postgresql.or 21 [ + + + ]: 224 : switch (rm->type->type)
22 : : {
10133 bruce@momjian.us 23 : 10 : case ECPGt_struct:
24 : : case ECPGt_union:
5968 meskes@postgresql.or 25 : 10 : type = ECPGmake_struct_type(rm->type->u.members, rm->type->type, rm->type->type_name, rm->type->struct_sizeof);
10133 bruce@momjian.us 26 : 10 : break;
10133 bruce@momjian.us 27 :GBC 2 : case ECPGt_array:
28 : :
29 : : /*
30 : : * if this array does contain a struct again, we have to
31 : : * create the struct too
32 : : */
5903 meskes@postgresql.or 33 [ - + - - ]: 2 : if (rm->type->u.element->type == ECPGt_struct || rm->type->u.element->type == ECPGt_union)
5968 34 : 2 : type = ECPGmake_struct_type(rm->type->u.element->u.members, rm->type->u.element->type, rm->type->u.element->type_name, rm->type->u.element->struct_sizeof);
35 : : else
5926 meskes@postgresql.or 36 :UBC 0 : type = ECPGmake_array_type(ECPGmake_simple_type(rm->type->u.element->type, rm->type->u.element->size, rm->type->u.element->counter), rm->type->size);
10133 bruce@momjian.us 37 :GBC 2 : break;
10133 bruce@momjian.us 38 :CBC 212 : default:
5926 meskes@postgresql.or 39 : 212 : type = ECPGmake_simple_type(rm->type->type, rm->type->size, rm->type->counter);
10133 bruce@momjian.us 40 : 212 : break;
41 : : }
42 : :
43 : 224 : ECPGmake_struct_member(rm->name, type, &new);
44 : :
45 : 224 : rm = rm->next;
46 : : }
47 : :
3208 peter_e@gmx.net 48 : 105 : return new;
49 : : }
50 : :
51 : : /* The NAME argument is copied. The type argument is preserved as a pointer. */
52 : : void
3133 53 : 280 : ECPGmake_struct_member(const char *name, struct ECPGtype *type, struct ECPGstruct_member **start)
54 : : {
55 : : struct ECPGstruct_member *ptr,
56 : : *ne =
1107 tgl@sss.pgh.pa.us 57 : 280 : (struct ECPGstruct_member *) mm_alloc(sizeof(struct ECPGstruct_member));
58 : :
6856 meskes@postgresql.or 59 : 280 : ne->name = mm_strdup(name);
8924 60 : 280 : ne->type = type;
10320 bruce@momjian.us 61 : 280 : ne->next = NULL;
62 : :
63 [ + + + + ]: 382 : for (ptr = *start; ptr && ptr->next; ptr = ptr->next);
64 : :
65 [ + + ]: 280 : if (ptr)
66 : 162 : ptr->next = ne;
67 : : else
68 : 118 : *start = ne;
10341 scrappy@hub.org 69 : 280 : }
70 : :
71 : : struct ECPGtype *
593 tgl@sss.pgh.pa.us 72 : 888 : ECPGmake_simple_type(enum ECPGttype type, const char *size, int counter)
73 : : {
10320 bruce@momjian.us 74 : 888 : struct ECPGtype *ne = (struct ECPGtype *) mm_alloc(sizeof(struct ECPGtype));
75 : :
8924 meskes@postgresql.or 76 : 888 : ne->type = type;
5968 77 : 888 : ne->type_name = NULL;
593 tgl@sss.pgh.pa.us 78 : 888 : ne->size = mm_strdup(size);
7902 neilc@samurai.com 79 : 888 : ne->u.element = NULL;
8924 meskes@postgresql.or 80 : 888 : ne->struct_sizeof = NULL;
2658 81 : 888 : ne->counter = counter; /* only needed for varchar and bytea */
82 : :
10320 bruce@momjian.us 83 : 888 : return ne;
84 : : }
85 : :
86 : : struct ECPGtype *
593 tgl@sss.pgh.pa.us 87 : 74 : ECPGmake_array_type(struct ECPGtype *type, const char *size)
88 : : {
6864 meskes@postgresql.or 89 : 74 : struct ECPGtype *ne = ECPGmake_simple_type(ECPGt_array, size, 0);
90 : :
8924 91 : 74 : ne->u.element = type;
92 : :
10320 bruce@momjian.us 93 : 74 : return ne;
94 : : }
95 : :
96 : : struct ECPGtype *
549 tgl@sss.pgh.pa.us 97 : 45 : ECPGmake_struct_type(struct ECPGstruct_member *rm, enum ECPGttype type,
98 : : const char *type_name, const char *struct_sizeof)
99 : : {
593 100 : 45 : struct ECPGtype *ne = ECPGmake_simple_type(type, "1", 0);
101 : :
5968 meskes@postgresql.or 102 : 45 : ne->type_name = mm_strdup(type_name);
9961 scrappy@hub.org 103 : 45 : ne->u.members = ECPGstruct_member_dup(rm);
549 tgl@sss.pgh.pa.us 104 : 45 : ne->struct_sizeof = mm_strdup(struct_sizeof);
105 : :
10320 bruce@momjian.us 106 : 45 : return ne;
107 : : }
108 : :
109 : : static const char *
8924 meskes@postgresql.or 110 : 628 : get_type(enum ECPGttype type)
111 : : {
112 : 628 : switch (type)
[ + - + -
+ - + - -
- + + + +
+ - + + +
+ + - - +
+ + - ]
113 : : {
8983 bruce@momjian.us 114 : 202 : case ECPGt_char:
3208 peter_e@gmx.net 115 : 202 : return "ECPGt_char";
116 : : break;
10133 bruce@momjian.us 117 :UBC 0 : case ECPGt_unsigned_char:
3208 peter_e@gmx.net 118 : 0 : return "ECPGt_unsigned_char";
119 : : break;
10133 bruce@momjian.us 120 :CBC 32 : case ECPGt_short:
3208 peter_e@gmx.net 121 : 32 : return "ECPGt_short";
122 : : break;
10133 bruce@momjian.us 123 :UBC 0 : case ECPGt_unsigned_short:
3208 peter_e@gmx.net 124 : 0 : return "ECPGt_unsigned_short";
125 : : break;
10133 bruce@momjian.us 126 :CBC 222 : case ECPGt_int:
3208 peter_e@gmx.net 127 : 222 : return "ECPGt_int";
128 : : break;
10133 bruce@momjian.us 129 :UBC 0 : case ECPGt_unsigned_int:
3208 peter_e@gmx.net 130 : 0 : return "ECPGt_unsigned_int";
131 : : break;
10133 bruce@momjian.us 132 :CBC 12 : case ECPGt_long:
3208 peter_e@gmx.net 133 : 12 : return "ECPGt_long";
134 : : break;
10133 bruce@momjian.us 135 :UBC 0 : case ECPGt_unsigned_long:
3208 peter_e@gmx.net 136 : 0 : return "ECPGt_unsigned_long";
137 : : break;
9384 meskes@postgresql.or 138 : 0 : case ECPGt_long_long:
3208 peter_e@gmx.net 139 : 0 : return "ECPGt_long_long";
140 : : break;
9384 meskes@postgresql.or 141 : 0 : case ECPGt_unsigned_long_long:
3208 peter_e@gmx.net 142 : 0 : return "ECPGt_unsigned_long_long";
143 : : break;
10133 bruce@momjian.us 144 :CBC 6 : case ECPGt_float:
3208 peter_e@gmx.net 145 : 6 : return "ECPGt_float";
146 : : break;
10133 bruce@momjian.us 147 : 16 : case ECPGt_double:
3208 peter_e@gmx.net 148 : 16 : return "ECPGt_double";
149 : : break;
10133 bruce@momjian.us 150 : 13 : case ECPGt_bool:
3208 peter_e@gmx.net 151 : 13 : return "ECPGt_bool";
152 : : break;
10133 bruce@momjian.us 153 : 24 : case ECPGt_varchar:
3208 peter_e@gmx.net 154 : 24 : return "ECPGt_varchar";
2658 meskes@postgresql.or 155 : 21 : case ECPGt_bytea:
156 : 21 : return "ECPGt_bytea";
3265 tgl@sss.pgh.pa.us 157 :UBC 0 : case ECPGt_NO_INDICATOR: /* no indicator */
3208 peter_e@gmx.net 158 : 0 : return "ECPGt_NO_INDICATOR";
159 : : break;
3265 tgl@sss.pgh.pa.us 160 :CBC 21 : case ECPGt_char_variable: /* string that should not be quoted */
3208 peter_e@gmx.net 161 : 21 : return "ECPGt_char_variable";
162 : : break;
8380 meskes@postgresql.or 163 : 16 : case ECPGt_const: /* constant string quoted */
3208 peter_e@gmx.net 164 : 16 : return "ECPGt_const";
165 : : break;
8369 meskes@postgresql.or 166 : 6 : case ECPGt_decimal:
3208 peter_e@gmx.net 167 : 6 : return "ECPGt_decimal";
168 : : break;
8476 meskes@postgresql.or 169 : 9 : case ECPGt_numeric:
3208 peter_e@gmx.net 170 : 9 : return "ECPGt_numeric";
171 : : break;
8465 meskes@postgresql.or 172 : 4 : case ECPGt_interval:
3208 peter_e@gmx.net 173 : 4 : return "ECPGt_interval";
174 : : break;
9019 meskes@postgresql.or 175 :UBC 0 : case ECPGt_descriptor:
3208 peter_e@gmx.net 176 : 0 : return "ECPGt_descriptor";
177 : : break;
5989 meskes@postgresql.or 178 : 0 : case ECPGt_sqlda:
3208 peter_e@gmx.net 179 : 0 : return "ECPGt_sqlda";
180 : : break;
8472 meskes@postgresql.or 181 :CBC 10 : case ECPGt_date:
3208 peter_e@gmx.net 182 : 10 : return "ECPGt_date";
183 : : break;
8472 meskes@postgresql.or 184 : 13 : case ECPGt_timestamp:
3208 peter_e@gmx.net 185 : 13 : return "ECPGt_timestamp";
186 : : break;
6140 meskes@postgresql.or 187 : 1 : case ECPGt_string:
3208 peter_e@gmx.net 188 : 1 : return "ECPGt_string";
189 : : break;
10133 bruce@momjian.us 190 :UBC 0 : default:
6336 peter_e@gmx.net 191 : 0 : mmerror(PARSE_ERROR, ET_ERROR, "unrecognized variable type code %d", type);
192 : : }
193 : :
9953 scrappy@hub.org 194 : 0 : return NULL;
195 : : }
196 : :
197 : : /*
198 : : * Dump a type.
199 : : * The type is dumped as:
200 : : * type-tag <comma> - enum ECPGttype
201 : : * reference-to-variable <comma> - char *
202 : : * size <comma> - long size of this field (if varchar)
203 : : * arrsize <comma> - long number of elements in the arr
204 : : * offset <comma> - offset to the next element
205 : : * Where:
206 : : * type-tag is one of the simple types or varchar.
207 : : * reference-to-variable can be a reference to a struct element.
208 : : * arrsize is the size of the array in case of array fetches. Otherwise 0.
209 : : * size is the maxsize in case it is a varchar. Otherwise it is the size of
210 : : * the variable (required to do array fetches of structs).
211 : : */
212 : : static void ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
213 : : char *varcharsize,
214 : : char *arrsize, const char *size, const char *prefix, int counter);
215 : : static void ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsize,
216 : : struct ECPGtype *type, struct ECPGtype *ind_type, const char *prefix, const char *ind_prefix);
217 : :
218 : : void
3265 tgl@sss.pgh.pa.us 219 :CBC 641 : ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype *type, const int brace_level,
220 : : const char *ind_name, struct ECPGtype *ind_type, const int ind_brace_level,
221 : : const char *prefix, const char *ind_prefix,
222 : : char *arr_str_size, const char *struct_sizeof,
223 : : const char *ind_struct_sizeof)
224 : : {
225 : : struct variable *var;
226 : :
5903 meskes@postgresql.or 227 [ + + + + ]: 641 : if (type->type != ECPGt_descriptor && type->type != ECPGt_sqlda &&
5559 heikki.linnakangas@i 228 [ + + + + : 592 : type->type != ECPGt_char_variable && type->type != ECPGt_const &&
+ + ]
229 : : brace_level >= 0)
230 : : {
231 : : char *str;
232 : :
5903 meskes@postgresql.or 233 : 519 : str = mm_strdup(name);
234 : 519 : var = find_variable(str);
235 : 519 : free(str);
236 : :
237 [ + - ]: 519 : if ((var->type->type != type->type) ||
238 [ + + + - ]: 519 : (var->type->type_name && !type->type_name) ||
239 [ + + + - ]: 519 : (!var->type->type_name && type->type_name) ||
5268 peter_e@gmx.net 240 [ + + + - : 519 : (var->type->type_name && type->type_name && strcmp(var->type->type_name, type->type_name) != 0))
- + ]
5901 peter_e@gmx.net 241 :UBC 0 : mmerror(PARSE_ERROR, ET_ERROR, "variable \"%s\" is hidden by a local variable of a different type", name);
5903 meskes@postgresql.or 242 [ - + ]:CBC 519 : else if (var->brace_level != brace_level)
5901 peter_e@gmx.net 243 :UBC 0 : mmerror(PARSE_ERROR, ET_WARNING, "variable \"%s\" is hidden by a local variable", name);
244 : :
5903 meskes@postgresql.or 245 [ + + + - :CBC 519 : if (ind_name && ind_type && ind_type->type != ECPGt_NO_INDICATOR && ind_brace_level >= 0)
+ + + - ]
246 : : {
247 : 32 : str = mm_strdup(ind_name);
248 : 32 : var = find_variable(str);
249 : 32 : free(str);
250 : :
251 [ + - ]: 32 : if ((var->type->type != ind_type->type) ||
252 [ + + + - ]: 32 : (var->type->type_name && !ind_type->type_name) ||
253 [ + + + - ]: 32 : (!var->type->type_name && ind_type->type_name) ||
5268 peter_e@gmx.net 254 [ + + + - : 32 : (var->type->type_name && ind_type->type_name && strcmp(var->type->type_name, ind_type->type_name) != 0))
- + ]
5901 peter_e@gmx.net 255 :UBC 0 : mmerror(PARSE_ERROR, ET_ERROR, "indicator variable \"%s\" is hidden by a local variable of a different type", ind_name);
5903 meskes@postgresql.or 256 [ - + ]:CBC 32 : else if (var->brace_level != ind_brace_level)
5901 peter_e@gmx.net 257 :UBC 0 : mmerror(PARSE_ERROR, ET_WARNING, "indicator variable \"%s\" is hidden by a local variable", ind_name);
258 : : }
259 : : }
260 : :
8924 meskes@postgresql.or 261 [ + + - + :CBC 641 : switch (type->type)
+ + ]
262 : : {
8983 bruce@momjian.us 263 : 83 : case ECPGt_array:
8924 meskes@postgresql.or 264 [ + + + + : 83 : if (indicator_set && ind_type->type != ECPGt_array)
- + ]
4582 peter_e@gmx.net 265 :UBC 0 : mmfatal(INDICATOR_NOT_ARRAY, "indicator for array/pointer has to be array/pointer");
8924 meskes@postgresql.or 266 [ - + + ]:CBC 83 : switch (type->u.element->type)
267 : : {
8983 bruce@momjian.us 268 :UBC 0 : case ECPGt_array:
6197 269 : 0 : mmerror(PARSE_ERROR, ET_ERROR, "nested arrays are not supported (except strings)"); /* array of array */
9953 scrappy@hub.org 270 : 0 : break;
9953 scrappy@hub.org 271 :CBC 8 : case ECPGt_struct:
272 : : case ECPGt_union:
8402 meskes@postgresql.or 273 [ + - ]: 16 : ECPGdump_a_struct(o, name,
274 : : ind_name,
275 : : type->size,
276 : : type->u.element,
7230 277 [ + - ]: 8 : (ind_type == NULL) ? NULL : ((ind_type->type == ECPGt_NO_INDICATOR) ? ind_type : ind_type->u.element),
278 : : prefix, ind_prefix);
9953 scrappy@hub.org 279 : 8 : break;
9867 bruce@momjian.us 280 : 75 : default:
8924 meskes@postgresql.or 281 [ + - + + : 75 : if (!IS_SIMPLE_TYPE(type->u.element->type))
+ - - + ]
2283 peter@eisentraut.org 282 :UBC 0 : base_yyerror("internal error: unknown datatype, please report this to <" PACKAGE_BUGREPORT ">");
283 : :
8402 meskes@postgresql.or 284 :CBC 75 : ECPGdump_a_simple(o, name,
8335 bruce@momjian.us 285 : 75 : type->u.element->type,
4434 meskes@postgresql.or 286 : 75 : type->u.element->size, type->size, struct_sizeof ? struct_sizeof : NULL,
287 : 75 : prefix, type->u.element->counter);
288 : :
8924 289 [ + + ]: 75 : if (ind_type != NULL)
290 : : {
291 [ + + ]: 55 : if (ind_type->type == ECPGt_NO_INDICATOR)
292 : : {
4407 bruce@momjian.us 293 : 53 : char *str_neg_one = mm_strdup("-1");
294 : :
4473 sfrost@snowman.net 295 : 53 : ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, str_neg_one, NULL, ind_prefix, 0);
296 : 53 : free(str_neg_one);
297 : : }
298 : : else
299 : : {
8402 meskes@postgresql.or 300 : 2 : ECPGdump_a_simple(o, ind_name, ind_type->u.element->type,
6864 301 : 2 : ind_type->u.element->size, ind_type->size, NULL, ind_prefix, 0);
302 : : }
303 : : }
304 : : }
10133 bruce@momjian.us 305 : 83 : break;
306 : 6 : case ECPGt_struct:
307 : : {
4407 308 : 6 : char *str_one = mm_strdup("1");
309 : :
4473 sfrost@snowman.net 310 [ + - + + : 6 : if (indicator_set && ind_type->type != ECPGt_struct)
- + ]
4473 sfrost@snowman.net 311 :UBC 0 : mmfatal(INDICATOR_NOT_STRUCT, "indicator for struct has to be a struct");
312 : :
4473 sfrost@snowman.net 313 :CBC 6 : ECPGdump_a_struct(o, name, ind_name, str_one, type, ind_type, prefix, ind_prefix);
314 : 6 : free(str_one);
315 : : }
10133 bruce@momjian.us 316 : 6 : break;
9867 bruce@momjian.us 317 :UBC 0 : case ECPGt_union: /* cannot dump a complete union */
6588 peter_e@gmx.net 318 : 0 : base_yyerror("type of union has to be specified");
9953 scrappy@hub.org 319 : 0 : break;
9953 scrappy@hub.org 320 :CBC 21 : case ECPGt_char_variable:
321 : : {
322 : : /*
323 : : * Allocate for each, as there are code-paths where the values
324 : : * get stomped on.
325 : : */
4407 bruce@momjian.us 326 : 21 : char *str_varchar_one = mm_strdup("1");
327 : 21 : char *str_arr_one = mm_strdup("1");
328 : 21 : char *str_neg_one = mm_strdup("-1");
329 : :
4473 sfrost@snowman.net 330 [ + - - + : 21 : if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
- - - - ]
4473 sfrost@snowman.net 331 :UBC 0 : mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
332 : :
3364 peter_e@gmx.net 333 [ + - - + ]:CBC 21 : ECPGdump_a_simple(o, name, type->type, str_varchar_one, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_arr_one, struct_sizeof, prefix, 0);
4473 sfrost@snowman.net 334 [ + - ]: 21 : if (ind_type != NULL)
3364 peter_e@gmx.net 335 [ + - - + ]: 21 : ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_neg_one, ind_struct_sizeof, ind_prefix, 0);
336 : :
4473 sfrost@snowman.net 337 : 21 : free(str_varchar_one);
338 : 21 : free(str_arr_one);
339 : 21 : free(str_neg_one);
340 : : }
9953 scrappy@hub.org 341 : 21 : break;
9019 meskes@postgresql.or 342 : 24 : case ECPGt_descriptor:
343 : : {
344 : : /*
345 : : * Allocate for each, as there are code-paths where the values
346 : : * get stomped on.
347 : : */
4407 bruce@momjian.us 348 : 24 : char *str_neg_one = mm_strdup("-1");
349 : 24 : char *ind_type_neg_one = mm_strdup("-1");
350 : :
4473 sfrost@snowman.net 351 [ + - - + : 24 : if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
- - - - ]
4473 sfrost@snowman.net 352 :UBC 0 : mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
353 : :
4473 sfrost@snowman.net 354 :CBC 24 : ECPGdump_a_simple(o, name, type->type, NULL, str_neg_one, NULL, prefix, 0);
355 [ + - ]: 24 : if (ind_type != NULL)
356 : 24 : ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, ind_type_neg_one, NULL, ind_prefix, 0);
357 : :
358 : 24 : free(str_neg_one);
359 : 24 : free(ind_type_neg_one);
360 : : }
9019 meskes@postgresql.or 361 : 24 : break;
10133 bruce@momjian.us 362 : 507 : default:
363 : : {
364 : : /*
365 : : * Allocate for each, as there are code-paths where the values
366 : : * get stomped on.
367 : : */
4407 368 : 507 : char *str_neg_one = mm_strdup("-1");
369 : 507 : char *ind_type_neg_one = mm_strdup("-1");
370 : :
4473 sfrost@snowman.net 371 [ + + + + : 507 : if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
+ - - + ]
4473 sfrost@snowman.net 372 :UBC 0 : mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
373 : :
3364 peter_e@gmx.net 374 [ + - + + ]:CBC 507 : ECPGdump_a_simple(o, name, type->type, type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_neg_one, struct_sizeof, prefix, type->counter);
4473 sfrost@snowman.net 375 [ + + ]: 507 : if (ind_type != NULL)
3364 peter_e@gmx.net 376 [ + - + + ]: 458 : ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : ind_type_neg_one, ind_struct_sizeof, ind_prefix, 0);
377 : :
4473 sfrost@snowman.net 378 : 507 : free(str_neg_one);
379 : 507 : free(ind_type_neg_one);
380 : : }
10133 bruce@momjian.us 381 : 507 : break;
382 : : }
10341 scrappy@hub.org 383 : 641 : }
384 : :
385 : :
386 : : /*
387 : : * If size is NULL, then the offset is 0, if not use size as a
388 : : * string, it represents the offset needed if we are in an array of structs.
389 : : */
390 : : static void
8924 meskes@postgresql.or 391 : 1185 : ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
392 : : char *varcharsize,
393 : : char *arrsize,
394 : : const char *size,
395 : : const char *prefix,
396 : : int counter)
397 : : {
398 [ + + ]: 1185 : if (type == ECPGt_NO_INDICATOR)
10133 bruce@momjian.us 399 : 508 : fprintf(o, "\n\tECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ");
8924 meskes@postgresql.or 400 [ + + ]: 677 : else if (type == ECPGt_descriptor)
401 : : /* remember that name here already contains quotes (if needed) */
4524 402 : 24 : fprintf(o, "\n\tECPGt_descriptor, %s, 1L, 1L, 1L, ", name);
5989 403 [ + + ]: 653 : else if (type == ECPGt_sqlda)
404 : 25 : fprintf(o, "\n\tECPGt_sqlda, &%s, 0L, 0L, 0L, ", name);
405 : : else
406 : : {
6197 bruce@momjian.us 407 [ + + ]: 628 : char *variable = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4);
408 : 628 : char *offset = (char *) mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1 + strlen(varcharsize) + sizeof(int) * CHAR_BIT * 10 / 3);
409 : : char *struct_name;
410 : :
8924 meskes@postgresql.or 411 [ + + + + : 628 : switch (type)
+ + + + ]
412 : : {
413 : : /*
414 : : * we have to use the & operator except for arrays and
415 : : * pointers
416 : : */
417 : :
10133 bruce@momjian.us 418 : 45 : case ECPGt_varchar:
419 : : case ECPGt_bytea:
420 : :
421 : : /*
422 : : * we have to use the pointer except for arrays with given
423 : : * bounds
424 : : */
8335 425 [ + + ]: 45 : if (((atoi(arrsize) > 0) ||
426 [ + + - + : 45 : (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
+ + ]
427 : : size == NULL)
9551 meskes@postgresql.or 428 [ - + ]: 2 : sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
429 : : else
430 [ + + ]: 43 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
431 : :
432 : : /*
433 : : * If we created a varchar structure automatically, counter is
434 : : * greater than 0.
435 : : */
2658 436 [ + + ]: 45 : if (type == ECPGt_varchar)
437 : 24 : struct_name = "struct varchar";
438 : : else
439 : 21 : struct_name = "struct bytea";
440 : :
5926 441 [ + + ]: 45 : if (counter)
2658 442 : 44 : sprintf(offset, "sizeof(%s_%d)", struct_name, counter);
443 : : else
444 : 1 : sprintf(offset, "sizeof(%s)", struct_name);
10133 bruce@momjian.us 445 : 45 : break;
446 : 224 : case ECPGt_char:
447 : : case ECPGt_unsigned_char:
448 : : case ECPGt_char_variable:
449 : : case ECPGt_string:
450 : : {
4407 451 : 224 : char *sizeof_name = "char";
452 : :
453 : : /*
454 : : * we have to use the pointer except for arrays with given
455 : : * bounds, ecpglib will distinguish between * and []
456 : : */
457 [ + + ]: 224 : if ((atoi(varcharsize) > 1 ||
458 [ + + ]: 86 : (atoi(arrsize) > 0) ||
459 [ + - + + ]: 59 : (atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) ||
460 [ + + - + ]: 51 : (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0))
3364 peter_e@gmx.net 461 [ + + ]: 173 : && size == NULL)
462 : : {
4407 bruce@momjian.us 463 [ - + ]: 166 : sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
464 [ + + - + ]: 166 : if ((type == ECPGt_char || type == ECPGt_unsigned_char) &&
465 [ - + ]: 144 : strcmp(varcharsize, "0") == 0)
466 : : {
467 : : /*
468 : : * If this is an array of char *, the offset would
469 : : * be sizeof(char *) and not sizeof(char).
470 : : */
4407 bruce@momjian.us 471 :UBC 0 : sizeof_name = "char *";
472 : : }
473 : : }
474 : : else
4407 bruce@momjian.us 475 [ + + ]:CBC 58 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
476 : :
477 [ + + ]: 224 : sprintf(offset, "(%s)*sizeof(%s)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize, sizeof_name);
478 : 224 : break;
479 : : }
8476 meskes@postgresql.or 480 : 9 : case ECPGt_numeric:
481 : :
482 : : /*
483 : : * we have to use a pointer here
484 : : */
485 [ - + ]: 9 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
8299 486 : 9 : sprintf(offset, "sizeof(numeric)");
8465 487 : 9 : break;
488 : 4 : case ECPGt_interval:
489 : :
490 : : /*
491 : : * we have to use a pointer here
492 : : */
493 [ - + ]: 4 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
8299 494 : 4 : sprintf(offset, "sizeof(interval)");
8476 495 : 4 : break;
8472 496 : 10 : case ECPGt_date:
497 : :
498 : : /*
499 : : * we have to use a pointer and translate the variable type
500 : : */
501 [ - + ]: 10 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
8299 502 : 10 : sprintf(offset, "sizeof(date)");
8472 503 : 10 : break;
504 : 13 : case ECPGt_timestamp:
505 : :
506 : : /*
507 : : * we have to use a pointer and translate the variable type
508 : : */
509 [ - + ]: 13 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
8299 510 : 13 : sprintf(offset, "sizeof(timestamp)");
8472 511 : 13 : break;
8380 512 : 16 : case ECPGt_const:
513 : :
514 : : /*
515 : : * just dump the const as string
516 : : */
517 : 16 : sprintf(variable, "\"%s\"", name);
518 : 16 : sprintf(offset, "strlen(\"%s\")", name);
519 : 16 : break;
10133 bruce@momjian.us 520 : 307 : default:
521 : :
522 : : /*
523 : : * we have to use the pointer except for arrays with given
524 : : * bounds
525 : : */
8335 526 [ + + ]: 307 : if (((atoi(arrsize) > 0) ||
527 [ + + + + : 307 : (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
+ + ]
528 : : size == NULL)
9551 meskes@postgresql.or 529 [ - + ]: 18 : sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
530 : : else
531 [ + + ]: 289 : sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
532 : :
6814 533 : 307 : sprintf(offset, "sizeof(%s)", ecpg_type_name(type));
10133 bruce@momjian.us 534 : 307 : break;
535 : : }
536 : :
537 : : /*
538 : : * Array size would be -1 for addresses of members within structure,
539 : : * when pointer to structure is being dumped.
540 : : */
3364 peter_e@gmx.net 541 [ + + + + ]: 628 : if (atoi(arrsize) < 0 && !size)
8417 meskes@postgresql.or 542 : 466 : strcpy(arrsize, "1");
543 : :
544 : : /*
545 : : * If size i.e. the size of structure of which this variable is part
546 : : * of, that gives the offset to the next element, if required
547 : : */
3364 peter_e@gmx.net 548 [ + + + + ]: 628 : if (size == NULL || strlen(size) == 0)
8417 meskes@postgresql.or 549 : 567 : fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, offset);
550 : : else
3364 peter_e@gmx.net 551 : 61 : fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, size);
552 : :
10133 bruce@momjian.us 553 : 628 : free(variable);
554 : 628 : free(offset);
555 : : }
10341 scrappy@hub.org 556 : 1185 : }
557 : :
558 : :
559 : : /* Penetrate a struct and dump the contents. */
560 : : static void
3265 tgl@sss.pgh.pa.us 561 : 14 : ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsize, struct ECPGtype *type, struct ECPGtype *ind_type, const char *prefix, const char *ind_prefix)
562 : : {
563 : : /*
564 : : * If offset is NULL, then this is the first recursive level. If not then
565 : : * we are in a struct and the offset is used as offset.
566 : : */
567 : : struct ECPGstruct_member *p,
10133 bruce@momjian.us 568 : 14 : *ind_p = NULL;
4749 569 [ + + ]: 14 : char *pbuf = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 3);
570 [ + + ]: 14 : char *ind_pbuf = (char *) mm_alloc(strlen(ind_name) + ((ind_prefix == NULL) ? 0 : strlen(ind_prefix)) + 3);
571 : :
3364 peter_e@gmx.net 572 [ + + ]: 14 : if (atoi(arrsize) == 1)
8939 meskes@postgresql.or 573 [ + + ]: 6 : sprintf(pbuf, "%s%s.", prefix ? prefix : "", name);
574 : : else
575 [ - + ]: 8 : sprintf(pbuf, "%s%s->", prefix ? prefix : "", name);
576 : :
10320 bruce@momjian.us 577 : 14 : prefix = pbuf;
578 : :
8924 meskes@postgresql.or 579 [ + + ]: 14 : if (ind_type == &ecpg_no_indicator)
8943 580 : 2 : ind_p = &struct_no_indicator;
8924 581 [ + - ]: 12 : else if (ind_type != NULL)
582 : : {
3364 peter_e@gmx.net 583 [ + + ]: 12 : if (atoi(arrsize) == 1)
8939 meskes@postgresql.or 584 [ + + ]: 4 : sprintf(ind_pbuf, "%s%s.", ind_prefix ? ind_prefix : "", ind_name);
585 : : else
586 [ - + ]: 8 : sprintf(ind_pbuf, "%s%s->", ind_prefix ? ind_prefix : "", ind_name);
587 : :
8943 588 : 12 : ind_prefix = ind_pbuf;
8924 589 : 12 : ind_p = ind_type->u.members;
590 : : }
591 : :
592 [ + + ]: 50 : for (p = type->u.members; p; p = p->next)
593 : : {
5903 594 [ + - + - ]: 72 : ECPGdump_a_type(o, p->name, p->type, -1,
595 : : (ind_p != NULL) ? ind_p->name : NULL,
596 : : (ind_p != NULL) ? ind_p->type : NULL,
597 : : -1,
3364 peter_e@gmx.net 598 [ + - ]: 36 : prefix, ind_prefix, arrsize, type->struct_sizeof,
599 : : (ind_p != NULL) ? ind_type->struct_sizeof : NULL);
8943 meskes@postgresql.or 600 [ + - + + ]: 36 : if (ind_p != NULL && ind_p != &struct_no_indicator)
601 : : {
10133 bruce@momjian.us 602 : 30 : ind_p = ind_p->next;
2956 tgl@sss.pgh.pa.us 603 [ + + - + ]: 30 : if (ind_p == NULL && p->next != NULL)
604 : : {
3059 meskes@postgresql.or 605 :UBC 0 : mmerror(PARSE_ERROR, ET_WARNING, "indicator struct \"%s\" has too few members", ind_name);
606 : 0 : ind_p = &struct_no_indicator;
607 : : }
608 : : }
609 : : }
610 : :
2956 tgl@sss.pgh.pa.us 611 [ + - + + :CBC 14 : if (ind_type != NULL && ind_p != NULL && ind_p != &struct_no_indicator)
- + ]
612 : : {
3059 meskes@postgresql.or 613 :UBC 0 : mmerror(PARSE_ERROR, ET_WARNING, "indicator struct \"%s\" has too many members", ind_name);
614 : : }
615 : :
4936 meskes@postgresql.or 616 :CBC 14 : free(pbuf);
617 : 14 : free(ind_pbuf);
10341 scrappy@hub.org 618 : 14 : }
619 : :
620 : : void
3265 tgl@sss.pgh.pa.us 621 : 133 : ECPGfree_struct_member(struct ECPGstruct_member *rm)
622 : : {
10133 bruce@momjian.us 623 [ + + ]: 330 : while (rm)
624 : : {
625 : 197 : struct ECPGstruct_member *p = rm;
626 : :
627 : 197 : rm = rm->next;
628 : 197 : free(p->name);
549 tgl@sss.pgh.pa.us 629 : 197 : ECPGfree_type(p->type);
10133 bruce@momjian.us 630 : 197 : free(p);
631 : : }
10251 scrappy@hub.org 632 : 133 : }
633 : :
634 : : void
3265 tgl@sss.pgh.pa.us 635 : 698 : ECPGfree_type(struct ECPGtype *type)
636 : : {
8924 meskes@postgresql.or 637 [ + - + + : 698 : if (!IS_SIMPLE_TYPE(type->type))
+ + + + ]
638 : : {
639 [ + + - ]: 109 : switch (type->type)
640 : : {
8983 bruce@momjian.us 641 : 70 : case ECPGt_array:
8924 meskes@postgresql.or 642 [ - + + ]: 70 : switch (type->u.element->type)
643 : : {
8983 bruce@momjian.us 644 :UBC 0 : case ECPGt_array:
6588 peter_e@gmx.net 645 : 0 : base_yyerror("internal error: found multidimensional array\n");
9953 scrappy@hub.org 646 : 0 : break;
9953 scrappy@hub.org 647 :CBC 15 : case ECPGt_struct:
648 : : case ECPGt_union:
649 : : /* Array of structs. */
549 tgl@sss.pgh.pa.us 650 : 15 : ECPGfree_type(type->u.element);
9953 scrappy@hub.org 651 : 15 : break;
9867 bruce@momjian.us 652 : 55 : default:
8924 meskes@postgresql.or 653 [ + - + + : 55 : if (!IS_SIMPLE_TYPE(type->u.element->type))
+ - - + ]
2283 peter@eisentraut.org 654 :UBC 0 : base_yyerror("internal error: unknown datatype, please report this to <" PACKAGE_BUGREPORT ">");
655 : :
549 tgl@sss.pgh.pa.us 656 :CBC 55 : ECPGfree_type(type->u.element);
657 : : }
9953 scrappy@hub.org 658 : 70 : break;
659 : 39 : case ECPGt_struct:
660 : : case ECPGt_union:
8924 meskes@postgresql.or 661 : 39 : ECPGfree_struct_member(type->u.members);
9953 scrappy@hub.org 662 : 39 : break;
9953 scrappy@hub.org 663 :UBC 0 : default:
6336 peter_e@gmx.net 664 : 0 : mmerror(PARSE_ERROR, ET_ERROR, "unrecognized variable type code %d", type->type);
9953 scrappy@hub.org 665 : 0 : break;
666 : : }
667 : : }
549 tgl@sss.pgh.pa.us 668 :CBC 698 : free(type->type_name);
669 : 698 : free(type->size);
670 : 698 : free(type->struct_sizeof);
8924 meskes@postgresql.or 671 : 698 : free(type);
10341 scrappy@hub.org 672 : 698 : }
673 : :
674 : : const char *
8924 meskes@postgresql.or 675 : 69 : get_dtype(enum ECPGdtype type)
676 : : {
677 : 69 : switch (type)
[ - + + -
+ - + + -
+ + - + +
+ - - ]
678 : : {
8983 bruce@momjian.us 679 :UBC 0 : case ECPGd_count:
878 michael@paquier.xyz 680 : 0 : return "ECPGd_count";
681 : : break;
9594 meskes@postgresql.or 682 :CBC 34 : case ECPGd_data:
3208 peter_e@gmx.net 683 : 34 : return "ECPGd_data";
684 : : break;
9594 meskes@postgresql.or 685 : 2 : case ECPGd_di_code:
3208 peter_e@gmx.net 686 : 2 : return "ECPGd_di_code";
687 : : break;
9594 meskes@postgresql.or 688 :UBC 0 : case ECPGd_di_precision:
3208 peter_e@gmx.net 689 : 0 : return "ECPGd_di_precision";
690 : : break;
9594 meskes@postgresql.or 691 :CBC 17 : case ECPGd_indicator:
3208 peter_e@gmx.net 692 : 17 : return "ECPGd_indicator";
693 : : break;
9594 meskes@postgresql.or 694 :UBC 0 : case ECPGd_key_member:
3208 peter_e@gmx.net 695 : 0 : return "ECPGd_key_member";
696 : : break;
9594 meskes@postgresql.or 697 :CBC 2 : case ECPGd_length:
3208 peter_e@gmx.net 698 : 2 : return "ECPGd_length";
699 : : break;
9594 meskes@postgresql.or 700 : 9 : case ECPGd_name:
3208 peter_e@gmx.net 701 : 9 : return "ECPGd_name";
702 : : break;
9594 meskes@postgresql.or 703 :UBC 0 : case ECPGd_nullable:
3208 peter_e@gmx.net 704 : 0 : return "ECPGd_nullable";
705 : : break;
9594 meskes@postgresql.or 706 :CBC 1 : case ECPGd_octet:
3208 peter_e@gmx.net 707 : 1 : return "ECPGd_octet";
708 : : break;
9594 meskes@postgresql.or 709 : 1 : case ECPGd_precision:
3208 peter_e@gmx.net 710 : 1 : return "ECPGd_precision";
711 : : break;
9594 meskes@postgresql.or 712 :UBC 0 : case ECPGd_ret_length:
3208 peter_e@gmx.net 713 : 0 : return "ECPGd_ret_length";
9594 meskes@postgresql.or 714 :CBC 1 : case ECPGd_ret_octet:
3208 peter_e@gmx.net 715 : 1 : return "ECPGd_ret_octet";
716 : : break;
9544 bruce@momjian.us 717 : 1 : case ECPGd_scale:
3208 peter_e@gmx.net 718 : 1 : return "ECPGd_scale";
719 : : break;
9544 bruce@momjian.us 720 : 1 : case ECPGd_type:
3208 peter_e@gmx.net 721 : 1 : return "ECPGd_type";
722 : : break;
9050 meskes@postgresql.or 723 :UBC 0 : case ECPGd_cardinality:
3208 peter_e@gmx.net 724 : 0 : return "ECPGd_cardinality";
9594 meskes@postgresql.or 725 : 0 : default:
6336 peter_e@gmx.net 726 : 0 : mmerror(PARSE_ERROR, ET_ERROR, "unrecognized descriptor item code %d", type);
727 : : }
728 : :
9594 meskes@postgresql.or 729 : 0 : return NULL;
730 : : }
|