1 /* src/interfaces/ecpg/ecpglib/execute.c */
4 * The aim is to get a simpler interface to the database routines.
5 * All the tedious messing around with tuples is supposed to be hidden
9 (actually most if the code is "borrowed" from the distribution and just
13 /* Taken over as part of PostgreSQL by Michael Meskes <meskes@postgresql.org>
16 #define POSTGRES_ECPG_INTERNAL
17 #include "postgres_fe.h"
21 #include "catalog/pg_type_d.h"
22 #include "ecpgerrno.h"
24 #include "ecpglib_extern.h"
26 #include "pgtypes_date.h"
27 #include "pgtypes_interval.h"
28 #include "pgtypes_numeric.h"
29 #include "pgtypes_timestamp.h"
30 #include "sql3types.h"
32 #include "sqlda-compat.h"
33 #include "sqlda-native.h"
36 * This function returns a newly malloced string that has ' and \
40 quote_postgres(char *arg, bool quote, int lineno)
48 * if quote is false we just need to store things in a descriptor they
49 * will be quoted once they are inserted in a statement
56 buffer_len = 2 * length + 1;
57 res = (char *) ecpg_alloc(buffer_len + 3, lineno);
60 escaped_len = PQescapeString(res + 1, arg, buffer_len);
61 if (length == escaped_len)
63 res[0] = res[escaped_len + 1] = '\'';
64 res[escaped_len + 2] = '\0';
69 * We don't know if the target database is using
70 * standard_conforming_strings, so we always use E'' strings.
72 memmove(res + 2, res + 1, escaped_len);
73 res[0] = ESCAPE_STRING_SYNTAX;
74 res[1] = res[escaped_len + 2] = '\'';
75 res[escaped_len + 3] = '\0';
83 free_variable(struct variable *var)
85 struct variable *var_next;
96 free_statement(struct statement *stmt)
100 free_variable(stmt->inlist);
101 free_variable(stmt->outlist);
102 ecpg_free(stmt->command);
103 ecpg_free(stmt->name);
104 #ifdef HAVE_USELOCALE
106 freelocale(stmt->clocale);
108 ecpg_free(stmt->oldlocale);
114 next_insert(char *text, int pos, bool questionmarks, bool std_strings)
119 for (; text[p] != '\0'; p++)
121 if (string && !std_strings && text[p] == '\\') /* escape character */
123 else if (text[p] == '\'')
124 string = string ? false : true;
127 if (text[p] == '$' && isdigit((unsigned char) text[p + 1]))
129 /* this can be either a dollar quote or a variable */
132 for (i = p + 1; isdigit((unsigned char) text[i]); i++)
133 /* empty loop body */ ;
134 if (!isalpha((unsigned char) text[i]) &&
135 isascii((unsigned char) text[i]) &&text[i] != '_')
136 /* not dollar delimited quote */
139 else if (questionmarks && text[p] == '?')
141 /* also allow old style placeholders */
151 ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
153 struct ECPGtype_information_cache *new_entry
154 = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
156 if (new_entry == NULL)
159 new_entry->oid = oid;
160 new_entry->isarray = isarray;
161 new_entry->next = *cache;
166 static enum ARRAY_TYPE
167 ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
170 enum ARRAY_TYPE isarray = ECPG_ARRAY_NOT_SET;
172 struct ECPGtype_information_cache *cache_entry;
174 if ((stmt->connection->cache_head) == NULL)
177 * Text like types are not an array for ecpg, but postgres counts them
178 * as an array. This define reminds you to not 'correct' these values.
180 #define not_an_array_in_ecpg ECPG_ARRAY_NONE
182 /* populate cache with well known types to speed things up */
183 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOOLOID, ECPG_ARRAY_NONE, stmt->lineno))
184 return ECPG_ARRAY_ERROR;
185 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BYTEAOID, ECPG_ARRAY_NONE, stmt->lineno))
186 return ECPG_ARRAY_ERROR;
187 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CHAROID, ECPG_ARRAY_NONE, stmt->lineno))
188 return ECPG_ARRAY_ERROR;
189 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NAMEOID, not_an_array_in_ecpg, stmt->lineno))
190 return ECPG_ARRAY_ERROR;
191 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT8OID, ECPG_ARRAY_NONE, stmt->lineno))
192 return ECPG_ARRAY_ERROR;
193 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2OID, ECPG_ARRAY_NONE, stmt->lineno))
194 return ECPG_ARRAY_ERROR;
195 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2VECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
196 return ECPG_ARRAY_ERROR;
197 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT4OID, ECPG_ARRAY_NONE, stmt->lineno))
198 return ECPG_ARRAY_ERROR;
199 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), REGPROCOID, ECPG_ARRAY_NONE, stmt->lineno))
200 return ECPG_ARRAY_ERROR;
201 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TEXTOID, ECPG_ARRAY_NONE, stmt->lineno))
202 return ECPG_ARRAY_ERROR;
203 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDOID, ECPG_ARRAY_NONE, stmt->lineno))
204 return ECPG_ARRAY_ERROR;
205 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIDOID, ECPG_ARRAY_NONE, stmt->lineno))
206 return ECPG_ARRAY_ERROR;
207 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), XIDOID, ECPG_ARRAY_NONE, stmt->lineno))
208 return ECPG_ARRAY_ERROR;
209 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDOID, ECPG_ARRAY_NONE, stmt->lineno))
210 return ECPG_ARRAY_ERROR;
211 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDVECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
212 return ECPG_ARRAY_ERROR;
213 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POINTOID, ECPG_ARRAY_VECTOR, stmt->lineno))
214 return ECPG_ARRAY_ERROR;
215 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LSEGOID, ECPG_ARRAY_VECTOR, stmt->lineno))
216 return ECPG_ARRAY_ERROR;
217 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), PATHOID, ECPG_ARRAY_NONE, stmt->lineno))
218 return ECPG_ARRAY_ERROR;
219 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOXOID, ECPG_ARRAY_VECTOR, stmt->lineno))
220 return ECPG_ARRAY_ERROR;
221 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POLYGONOID, ECPG_ARRAY_NONE, stmt->lineno))
222 return ECPG_ARRAY_ERROR;
223 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LINEOID, ECPG_ARRAY_VECTOR, stmt->lineno))
224 return ECPG_ARRAY_ERROR;
225 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT4OID, ECPG_ARRAY_NONE, stmt->lineno))
226 return ECPG_ARRAY_ERROR;
227 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT8OID, ECPG_ARRAY_NONE, stmt->lineno))
228 return ECPG_ARRAY_ERROR;
229 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), UNKNOWNOID, ECPG_ARRAY_NONE, stmt->lineno))
230 return ECPG_ARRAY_ERROR;
231 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIRCLEOID, ECPG_ARRAY_NONE, stmt->lineno))
232 return ECPG_ARRAY_ERROR;
233 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CASHOID, ECPG_ARRAY_NONE, stmt->lineno))
234 return ECPG_ARRAY_ERROR;
235 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INETOID, ECPG_ARRAY_NONE, stmt->lineno))
236 return ECPG_ARRAY_ERROR;
237 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDROID, ECPG_ARRAY_NONE, stmt->lineno))
238 return ECPG_ARRAY_ERROR;
239 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BPCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
240 return ECPG_ARRAY_ERROR;
241 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
242 return ECPG_ARRAY_ERROR;
243 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), DATEOID, ECPG_ARRAY_NONE, stmt->lineno))
244 return ECPG_ARRAY_ERROR;
245 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
246 return ECPG_ARRAY_ERROR;
247 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPOID, ECPG_ARRAY_NONE, stmt->lineno))
248 return ECPG_ARRAY_ERROR;
249 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPTZOID, ECPG_ARRAY_NONE, stmt->lineno))
250 return ECPG_ARRAY_ERROR;
251 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INTERVALOID, ECPG_ARRAY_NONE, stmt->lineno))
252 return ECPG_ARRAY_ERROR;
253 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMETZOID, ECPG_ARRAY_NONE, stmt->lineno))
254 return ECPG_ARRAY_ERROR;
255 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BITOID, ECPG_ARRAY_NONE, stmt->lineno))
256 return ECPG_ARRAY_ERROR;
257 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARBITOID, ECPG_ARRAY_NONE, stmt->lineno))
258 return ECPG_ARRAY_ERROR;
259 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NUMERICOID, ECPG_ARRAY_NONE, stmt->lineno))
260 return ECPG_ARRAY_ERROR;
263 for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
265 if (cache_entry->oid == type)
266 return cache_entry->isarray;
269 array_query = (char *) ecpg_alloc(strlen("select typlen from pg_type where oid= and typelem<>0") + 11, stmt->lineno);
270 if (array_query == NULL)
271 return ECPG_ARRAY_ERROR;
273 sprintf(array_query, "select typlen from pg_type where oid=%d and typelem<>0", type);
274 query = PQexec(stmt->connection->connection, array_query);
275 ecpg_free(array_query);
276 if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
277 return ECPG_ARRAY_ERROR;
278 else if (PQresultStatus(query) == PGRES_TUPLES_OK)
280 if (PQntuples(query) == 0)
281 isarray = ECPG_ARRAY_NONE;
284 isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
285 if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
286 ecpg_dynamic_type(type) == SQL3_CHARACTER_VARYING)
289 * arrays of character strings are not yet implemented
291 isarray = ECPG_ARRAY_NONE;
297 return ECPG_ARRAY_ERROR;
299 ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
300 ecpg_log("ecpg_is_type_an_array on line %d: type (%d); C (%d); array (%s)\n", stmt->lineno, type, var->type, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
306 ecpg_store_result(const PGresult *results, int act_field,
307 const struct statement *stmt, struct variable *var)
309 enum ARRAY_TYPE isarray;
311 ntuples = PQntuples(results);
314 if ((isarray = ecpg_is_type_an_array(PQftype(results, act_field), stmt, var)) == ECPG_ARRAY_ERROR)
316 ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
320 if (isarray == ECPG_ARRAY_NONE)
323 * if we don't have enough space, we cannot read all tuples
325 if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
327 ecpg_log("ecpg_store_result on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
328 stmt->lineno, ntuples, var->arrsize);
329 ecpg_raise(stmt->lineno, INFORMIX_MODE(stmt->compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_TOO_MANY_MATCHES, ECPG_SQLSTATE_CARDINALITY_VIOLATION, NULL);
336 * since we read an array, the variable has to be an array too
338 if (var->arrsize == 0)
340 ecpg_raise(stmt->lineno, ECPG_NO_ARRAY, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
346 * allocate memory for NULL pointers
348 if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
352 if (!PQfformat(results, act_field))
357 case ECPGt_unsigned_char:
359 if (!var->varcharsize && !var->arrsize)
361 /* special mode for handling char**foo=0 */
362 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
363 len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
364 len *= var->offset; /* should be 1, but YMNK */
365 len += (ntuples + 1) * sizeof(char *);
369 var->varcharsize = 0;
370 /* check strlen for each tuple */
371 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
373 int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
375 if (len > var->varcharsize)
376 var->varcharsize = len;
378 var->offset *= var->varcharsize;
379 len = var->offset * ntuples;
383 len = ntuples * (var->varcharsize + sizeof(int));
386 len = var->offset * ntuples;
392 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
393 len += PQgetlength(results, act_tuple, act_field);
396 ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
397 var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
400 *((char **) var->pointer) = var->value;
403 /* allocate indicator variable if needed */
404 if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
406 int len = var->ind_offset * ntuples;
408 var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
411 *((char **) var->ind_pointer) = var->ind_value;
414 /* fill the variable with the tuple(s) */
415 if (!var->varcharsize && !var->arrsize &&
416 (var->type == ECPGt_char || var->type == ECPGt_unsigned_char || var->type == ECPGt_string))
418 /* special mode for handling char**foo=0 */
420 /* filling the array of (char*)s */
421 char **current_string = (char **) var->value;
423 /* storing the data (after the last array element) */
424 char *current_data_location = (char *) ¤t_string[ntuples + 1];
426 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
428 int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
430 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
431 var->type, var->ind_type, current_data_location,
432 var->ind_value, len, 0, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
436 *current_string = current_data_location;
437 current_data_location += len;
442 /* terminate the list */
443 *current_string = NULL;
447 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
449 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
450 var->type, var->ind_type, var->value,
451 var->ind_value, var->varcharsize, var->offset, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
459 sprintf_double_value(char *ptr, double value, const char *delim)
462 sprintf(ptr, "%s%s", "NaN", delim);
463 else if (isinf(value))
466 sprintf(ptr, "%s%s", "-Infinity", delim);
468 sprintf(ptr, "%s%s", "Infinity", delim);
471 sprintf(ptr, "%.15g%s", value, delim);
475 sprintf_float_value(char *ptr, float value, const char *delim)
478 sprintf(ptr, "%s%s", "NaN", delim);
479 else if (isinf(value))
482 sprintf(ptr, "%s%s", "-Infinity", delim);
484 sprintf(ptr, "%s%s", "Infinity", delim);
487 sprintf(ptr, "%.15g%s", value, delim);
491 convert_bytea_to_string(char *from_data, int from_len, int lineno)
494 int to_len = ecpg_hex_enc_len(from_len) + 4 + 1; /* backslash + 'x' +
497 to_data = ecpg_alloc(to_len, lineno);
501 strcpy(to_data, "'\\x");
502 ecpg_hex_encode(from_data, from_len, to_data + 3);
503 strcpy(to_data + 3 + ecpg_hex_enc_len(from_len), "\'");
509 ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var,
510 char **tobeinserted_p, bool quote)
512 char *mallocedval = NULL;
513 char *newcopy = NULL;
516 * arrays are not possible unless the column is an array, too FIXME: we do
517 * not know if the column is an array here array input to singleton column
518 * will result in a runtime error
522 * Some special treatment is needed for records since we want their
523 * contents to arrive in a comma-separated list on insert (I think).
526 *tobeinserted_p = "";
528 /* check for null value and set input buffer accordingly */
529 switch (var->ind_type)
532 case ECPGt_unsigned_short:
533 if (*(short *) var->ind_value < 0)
534 *tobeinserted_p = NULL;
537 case ECPGt_unsigned_int:
538 if (*(int *) var->ind_value < 0)
539 *tobeinserted_p = NULL;
542 case ECPGt_unsigned_long:
543 if (*(long *) var->ind_value < 0L)
544 *tobeinserted_p = NULL;
546 #ifdef HAVE_LONG_LONG_INT
547 case ECPGt_long_long:
548 case ECPGt_unsigned_long_long:
549 if (*(long long int *) var->ind_value < (long long) 0)
550 *tobeinserted_p = NULL;
552 #endif /* HAVE_LONG_LONG_INT */
553 case ECPGt_NO_INDICATOR:
554 if (force_indicator == false)
556 if (ECPGis_noind_null(var->type, var->value))
557 *tobeinserted_p = NULL;
563 if (*tobeinserted_p != NULL)
565 int asize = var->arrsize ? var->arrsize : 1;
572 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
577 strcpy(mallocedval, "{");
579 for (element = 0; element < asize; element++)
580 sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
582 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
585 sprintf(mallocedval, "%hd", *((short *) var->value));
587 *tobeinserted_p = mallocedval;
591 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
596 strcpy(mallocedval, "{");
598 for (element = 0; element < asize; element++)
599 sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
601 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
604 sprintf(mallocedval, "%d", *((int *) var->value));
606 *tobeinserted_p = mallocedval;
609 case ECPGt_unsigned_short:
610 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
615 strcpy(mallocedval, "{");
617 for (element = 0; element < asize; element++)
618 sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
620 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
623 sprintf(mallocedval, "%hu", *((unsigned short *) var->value));
625 *tobeinserted_p = mallocedval;
628 case ECPGt_unsigned_int:
629 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
634 strcpy(mallocedval, "{");
636 for (element = 0; element < asize; element++)
637 sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
639 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
642 sprintf(mallocedval, "%u", *((unsigned int *) var->value));
644 *tobeinserted_p = mallocedval;
648 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
653 strcpy(mallocedval, "{");
655 for (element = 0; element < asize; element++)
656 sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
658 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
661 sprintf(mallocedval, "%ld", *((long *) var->value));
663 *tobeinserted_p = mallocedval;
666 case ECPGt_unsigned_long:
667 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
672 strcpy(mallocedval, "{");
674 for (element = 0; element < asize; element++)
675 sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
677 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
680 sprintf(mallocedval, "%lu", *((unsigned long *) var->value));
682 *tobeinserted_p = mallocedval;
684 #ifdef HAVE_LONG_LONG_INT
685 case ECPGt_long_long:
686 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
691 strcpy(mallocedval, "{");
693 for (element = 0; element < asize; element++)
694 sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
696 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
699 sprintf(mallocedval, "%lld", *((long long int *) var->value));
701 *tobeinserted_p = mallocedval;
704 case ECPGt_unsigned_long_long:
705 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
710 strcpy(mallocedval, "{");
712 for (element = 0; element < asize; element++)
713 sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
715 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
718 sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
720 *tobeinserted_p = mallocedval;
722 #endif /* HAVE_LONG_LONG_INT */
724 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
729 strcpy(mallocedval, "{");
731 for (element = 0; element < asize; element++)
732 sprintf_float_value(mallocedval + strlen(mallocedval), ((float *) var->value)[element], ",");
734 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
737 sprintf_float_value(mallocedval, *((float *) var->value), "");
739 *tobeinserted_p = mallocedval;
743 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
748 strcpy(mallocedval, "{");
750 for (element = 0; element < asize; element++)
751 sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
753 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
756 sprintf_double_value(mallocedval, *((double *) var->value), "");
758 *tobeinserted_p = mallocedval;
762 if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
765 if (var->arrsize > 1)
767 strcpy(mallocedval, "{");
769 for (element = 0; element < asize; element++)
770 sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
772 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
776 if (var->offset == sizeof(char))
777 sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
778 else if (var->offset == sizeof(int))
779 sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
781 ecpg_raise(lineno, ECPG_CONVERT_BOOL, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
784 *tobeinserted_p = mallocedval;
788 case ECPGt_unsigned_char:
791 /* set slen to string length if type is char * */
792 int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
794 if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
797 strncpy(newcopy, (char *) var->value, slen);
798 newcopy[slen] = '\0';
800 mallocedval = quote_postgres(newcopy, quote, lineno);
807 *tobeinserted_p = mallocedval;
811 case ECPGt_char_variable:
813 int slen = strlen((char *) var->value);
815 if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
818 strncpy(mallocedval, (char *) var->value, slen);
819 mallocedval[slen] = '\0';
821 *tobeinserted_p = mallocedval;
827 struct ECPGgeneric_varchar *variable =
828 (struct ECPGgeneric_varchar *) (var->value);
830 if (!(mallocedval = (char *) ecpg_alloc(variable->len, lineno)))
833 memcpy(mallocedval, variable->arr, variable->len);
834 *tobeinserted_p = mallocedval;
840 struct ECPGgeneric_varchar *variable =
841 (struct ECPGgeneric_varchar *) (var->value);
843 if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, lineno)))
846 strncpy(newcopy, variable->arr, variable->len);
847 newcopy[variable->len] = '\0';
849 mallocedval = quote_postgres(newcopy, quote, lineno);
856 *tobeinserted_p = mallocedval;
867 if (var->arrsize > 1)
868 mallocedval = ecpg_strdup("{", lineno);
870 mallocedval = ecpg_strdup("", lineno);
875 for (element = 0; element < asize; element++)
879 nval = PGTYPESnumeric_new();
882 ecpg_free(mallocedval);
886 if (var->type == ECPGt_numeric)
887 result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
889 result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
893 PGTYPESnumeric_free(nval);
894 ecpg_free(mallocedval);
898 str = PGTYPESnumeric_to_asc(nval, nval->dscale);
900 PGTYPESnumeric_free(nval);
902 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
904 ecpg_free(mallocedval);
908 mallocedval = newcopy;
910 /* also copy trailing '\0' */
911 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
912 if (var->arrsize > 1)
913 strcpy(mallocedval + strlen(mallocedval), ",");
918 if (var->arrsize > 1)
919 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
921 *tobeinserted_p = mallocedval;
930 if (var->arrsize > 1)
931 mallocedval = ecpg_strdup("{", lineno);
933 mallocedval = ecpg_strdup("", lineno);
938 for (element = 0; element < asize; element++)
940 str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
943 ecpg_free(mallocedval);
949 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
951 ecpg_free(mallocedval);
955 mallocedval = newcopy;
957 /* also copy trailing '\0' */
958 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
959 if (var->arrsize > 1)
960 strcpy(mallocedval + strlen(mallocedval), ",");
965 if (var->arrsize > 1)
966 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
968 *tobeinserted_p = mallocedval;
977 if (var->arrsize > 1)
978 mallocedval = ecpg_strdup("{", lineno);
980 mallocedval = ecpg_strdup("", lineno);
985 for (element = 0; element < asize; element++)
987 str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
990 ecpg_free(mallocedval);
996 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
998 ecpg_free(mallocedval);
1002 mallocedval = newcopy;
1004 /* also copy trailing '\0' */
1005 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1006 if (var->arrsize > 1)
1007 strcpy(mallocedval + strlen(mallocedval), ",");
1012 if (var->arrsize > 1)
1013 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1015 *tobeinserted_p = mallocedval;
1019 case ECPGt_timestamp:
1024 if (var->arrsize > 1)
1025 mallocedval = ecpg_strdup("{", lineno);
1027 mallocedval = ecpg_strdup("", lineno);
1032 for (element = 0; element < asize; element++)
1034 str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1037 ecpg_free(mallocedval);
1043 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1045 ecpg_free(mallocedval);
1049 mallocedval = newcopy;
1051 /* also copy trailing '\0' */
1052 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1053 if (var->arrsize > 1)
1054 strcpy(mallocedval + strlen(mallocedval), ",");
1059 if (var->arrsize > 1)
1060 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1062 *tobeinserted_p = mallocedval;
1066 case ECPGt_descriptor:
1071 /* Not implemented yet */
1072 ecpg_raise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_type_name(var->type));
1081 print_param_value(char *value, int len, int is_binary, int lineno, int nth)
1084 bool malloced = false;
1088 else if (!is_binary)
1092 value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1093 if (value_s != NULL)
1095 ecpg_hex_encode(value, len, value_s);
1096 value_s[ecpg_hex_enc_len(len)] = '\0';
1100 value_s = "no memory for logging of parameter";
1103 ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1104 lineno, nth, value_s);
1111 ecpg_free_params(struct statement *stmt, bool print)
1115 for (n = 0; n < stmt->nparams; n++)
1118 print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1119 stmt->paramformats[n], stmt->lineno, n + 1);
1120 ecpg_free(stmt->paramvalues[n]);
1122 ecpg_free(stmt->paramvalues);
1123 ecpg_free(stmt->paramlengths);
1124 ecpg_free(stmt->paramformats);
1125 stmt->paramvalues = NULL;
1126 stmt->paramlengths = NULL;
1127 stmt->paramformats = NULL;
1132 insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
1136 if (!(newcopy = (char *) ecpg_alloc(strlen(stmt->command)
1137 + strlen(tobeinserted)
1138 + 1, stmt->lineno)))
1140 ecpg_free(tobeinserted);
1144 strcpy(newcopy, stmt->command);
1145 strcpy(newcopy + position - 1, tobeinserted);
1148 * The strange thing in the second argument is the rest of the string from
1156 ecpg_free(stmt->command);
1157 stmt->command = newcopy;
1159 ecpg_free(tobeinserted);
1164 store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item,
1165 char **tobeinserted)
1167 struct variable var;
1170 * In case of binary data, only allocate memory and memcpy because binary
1171 * data have been already stored into desc_item->data with
1172 * ecpg_store_input() at ECPGset_desc().
1174 if (desc_item->is_binary)
1176 if (!(*tobeinserted = ecpg_alloc(desc_item->data_len, stmt->lineno)))
1178 memcpy(*tobeinserted, desc_item->data, desc_item->data_len);
1182 var.type = ECPGt_char;
1183 var.varcharsize = strlen(desc_item->data);
1184 var.value = desc_item->data;
1185 var.pointer = &(desc_item->data);
1189 if (!desc_item->indicator)
1191 var.ind_type = ECPGt_NO_INDICATOR;
1192 var.ind_value = var.ind_pointer = NULL;
1193 var.ind_varcharsize = var.ind_arrsize = var.ind_offset = 0;
1197 var.ind_type = ECPGt_int;
1198 var.ind_value = &(desc_item->indicator);
1199 var.ind_pointer = &(var.ind_value);
1200 var.ind_varcharsize = var.ind_arrsize = 1;
1204 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &var, tobeinserted, false))
1212 * Build statement parameters
1214 * The input values are taken from user variables, and the results are stored
1215 * in arrays which can be used by PQexecParams().
1218 ecpg_build_params(struct statement *stmt)
1220 struct variable *var;
1221 int desc_counter = 0;
1224 bool std_strings = false;
1226 /* Get standard_conforming_strings setting. */
1227 value = PQparameterStatus(stmt->connection->connection, "standard_conforming_strings");
1228 if (value && strcmp(value, "on") == 0)
1232 * If the type is one of the fill in types then we take the argument and
1233 * enter it to our parameter array at the first position. Then if there
1234 * are any more fill in types we add more parameters.
1245 tobeinserted = NULL;
1247 binary_format = false;
1250 * A descriptor is a special case since it contains many variables but
1251 * is listed only once.
1253 if (var->type == ECPGt_descriptor)
1256 * We create an additional variable list here, so the same logic
1259 struct descriptor *desc;
1260 struct descriptor_item *desc_item;
1262 desc = ecpg_find_desc(stmt->lineno, var->pointer);
1267 for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1269 if (desc_item->num != desc_counter)
1272 if (!store_input_from_desc(stmt, desc_item, &tobeinserted))
1275 if (desc_item->is_binary)
1277 binary_length = desc_item->data_len;
1278 binary_format = true;
1282 if (desc->count == desc_counter)
1285 else if (var->type == ECPGt_sqlda)
1287 if (INFORMIX_MODE(stmt->compat))
1289 struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1290 struct variable desc_inlist;
1297 for (i = 0; i < sqlda->sqld; i++)
1299 if (i + 1 == desc_counter)
1301 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1302 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1303 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1304 switch (desc_inlist.type)
1308 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1311 desc_inlist.varcharsize = 0;
1314 desc_inlist.arrsize = 1;
1315 desc_inlist.offset = 0;
1316 if (sqlda->sqlvar[i].sqlind)
1318 desc_inlist.ind_type = ECPGt_short;
1319 /* ECPG expects indicator value < 0 */
1320 if (*(sqlda->sqlvar[i].sqlind))
1321 *(sqlda->sqlvar[i].sqlind) = -1;
1322 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1323 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1324 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1325 desc_inlist.ind_offset = 0;
1329 desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1330 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1331 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1333 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1339 if (sqlda->sqld == desc_counter)
1344 struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1345 struct variable desc_inlist;
1352 for (i = 0; i < sqlda->sqln; i++)
1354 if (i + 1 == desc_counter)
1356 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1357 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1358 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1359 switch (desc_inlist.type)
1363 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1366 desc_inlist.varcharsize = 0;
1369 desc_inlist.arrsize = 1;
1370 desc_inlist.offset = 0;
1371 if (sqlda->sqlvar[i].sqlind)
1373 desc_inlist.ind_type = ECPGt_short;
1374 /* ECPG expects indicator value < 0 */
1375 if (*(sqlda->sqlvar[i].sqlind))
1376 *(sqlda->sqlvar[i].sqlind) = -1;
1377 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1378 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1379 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1380 desc_inlist.ind_offset = 0;
1384 desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1385 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1386 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1388 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1394 if (sqlda->sqln == desc_counter)
1401 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1404 if (var->type == ECPGt_bytea)
1406 binary_length = ((struct ECPGgeneric_varchar *) (var->value))->len;
1407 binary_format = true;
1412 * now tobeinserted points to an area that contains the next
1413 * parameter; now find the position in the string where it belongs
1415 if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1418 * We have an argument but we don't have the matched up
1419 * placeholder in the string
1421 ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS,
1422 ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS,
1424 ecpg_free_params(stmt, false);
1425 ecpg_free(tobeinserted);
1430 * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1431 * to simulate a dynamic cursor because there is no backend
1432 * functionality for it
1434 if (var->type == ECPGt_char_variable)
1436 int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1438 if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1440 ecpg_free_params(stmt, false);
1443 tobeinserted = NULL;
1447 * if the placeholder is '$0' we have to replace it on the client side
1448 * this is for places we want to support variables at that are not
1449 * supported in the backend
1451 else if (stmt->command[position] == '0')
1453 if (stmt->statement_type == ECPGst_prepare ||
1454 stmt->statement_type == ECPGst_exec_with_exprlist)
1456 /* Need to double-quote the inserted statement name. */
1457 char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1462 ecpg_free(tobeinserted);
1463 ecpg_free_params(stmt, false);
1466 sprintf(str, "\"%s\"", tobeinserted);
1467 ecpg_free(tobeinserted);
1471 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1473 ecpg_free_params(stmt, false);
1476 tobeinserted = NULL;
1478 else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1482 char *p = convert_bytea_to_string(tobeinserted,
1486 ecpg_free(tobeinserted);
1489 ecpg_free_params(stmt, false);
1495 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1497 ecpg_free_params(stmt, false);
1500 tobeinserted = NULL;
1504 if (!(stmt->paramvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1506 ecpg_free_params(stmt, false);
1507 ecpg_free(tobeinserted);
1510 stmt->paramvalues[stmt->nparams] = tobeinserted;
1512 if (!(stmt->paramlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1514 ecpg_free_params(stmt, false);
1517 stmt->paramlengths[stmt->nparams] = binary_length;
1519 if (!(stmt->paramformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1521 ecpg_free_params(stmt, false);
1524 stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1527 /* let's see if this was an old style placeholder */
1528 if (stmt->command[position] == '?')
1530 /* yes, replace with new style */
1531 int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1534 if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1536 ecpg_free_params(stmt, false);
1540 snprintf(tobeinserted, buffersize, "$%d", counter++);
1542 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1544 ecpg_free_params(stmt, false);
1547 tobeinserted = NULL;
1551 if (desc_counter == 0)
1556 * Check if there are unmatched things left. PREPARE AS has no parameter.
1557 * Check other statement.
1559 if (stmt->statement_type != ECPGst_prepare &&
1560 next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1562 ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS,
1563 ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS, NULL);
1564 ecpg_free_params(stmt, false);
1572 * ecpg_autostart_transaction
1573 * If we are in non-autocommit mode, automatically start a transaction.
1576 ecpg_autostart_transaction(struct statement *stmt)
1578 if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
1580 stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1581 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1583 ecpg_free_params(stmt, false);
1586 PQclear(stmt->results);
1587 stmt->results = NULL;
1594 * Execute the SQL statement.
1597 ecpg_execute(struct statement *stmt)
1599 ecpg_log("ecpg_execute on line %d: query: %s; with %d parameter(s) on connection %s\n", stmt->lineno, stmt->command, stmt->nparams, stmt->connection->name);
1600 if (stmt->statement_type == ECPGst_execute)
1602 stmt->results = PQexecPrepared(stmt->connection->connection,
1605 (const char *const *) stmt->paramvalues,
1606 (const int *) stmt->paramlengths,
1607 (const int *) stmt->paramformats,
1609 ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1613 if (stmt->nparams == 0)
1615 stmt->results = PQexec(stmt->connection->connection, stmt->command);
1616 ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1620 stmt->results = PQexecParams(stmt->connection->connection,
1621 stmt->command, stmt->nparams, NULL,
1622 (const char *const *) stmt->paramvalues,
1623 (const int *) stmt->paramlengths,
1624 (const int *) stmt->paramformats,
1627 ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1630 if (stmt->statement_type == ECPGst_prepare)
1632 if (!ecpg_register_prepared_stmt(stmt))
1634 ecpg_free_params(stmt, true);
1640 ecpg_free_params(stmt, true);
1642 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1649 * ecpg_process_output
1651 * Process the statement result and store it into application variables. This
1652 * function can be called repeatedly during the same statement in case cursor
1653 * readahead is used and the application does FETCH N which overflows the
1657 * stmt statement structure holding the PGresult and
1658 * the list of output variables
1660 * PQclear() the result upon returning from this function
1662 * Returns success as boolean. Also an SQL error is raised in case of failure.
1666 ecpg_process_output(struct statement *stmt, bool clear_result)
1668 struct variable *var;
1669 bool status = false;
1672 struct sqlca_t *sqlca = ECPGget_sqlca();
1679 ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
1680 ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
1684 var = stmt->outlist;
1685 switch (PQresultStatus(stmt->results))
1687 case PGRES_TUPLES_OK:
1688 nfields = PQnfields(stmt->results);
1689 sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1691 ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1697 ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1698 stmt->lineno, ntuples);
1699 ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1704 if (var != NULL && var->type == ECPGt_descriptor)
1706 struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1713 PQclear(desc->result);
1714 desc->result = stmt->results;
1715 clear_result = false;
1716 ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1717 stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1721 else if (var != NULL && var->type == ECPGt_sqlda)
1723 if (INFORMIX_MODE(stmt->compat))
1725 struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1726 struct sqlda_compat *sqlda = *_sqlda;
1727 struct sqlda_compat *sqlda_new;
1731 * If we are passed in a previously existing sqlda (chain)
1736 sqlda_new = sqlda->desc_next;
1740 *_sqlda = sqlda = sqlda_new = NULL;
1741 for (i = ntuples - 1; i >= 0; i--)
1744 * Build a new sqlda structure. Note that only
1745 * fetching 1 record is supported
1747 sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1751 /* cleanup all SQLDAs we created up */
1754 sqlda_new = sqlda->desc_next;
1760 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1766 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1768 *_sqlda = sqlda_new;
1770 ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1771 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1772 stmt->lineno, PQnfields(stmt->results));
1774 sqlda_new->desc_next = sqlda;
1781 struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1782 struct sqlda_struct *sqlda = *_sqlda;
1783 struct sqlda_struct *sqlda_new;
1787 * If we are passed in a previously existing sqlda (chain)
1792 sqlda_new = sqlda->desc_next;
1796 *_sqlda = sqlda = sqlda_new = NULL;
1797 for (i = ntuples - 1; i >= 0; i--)
1800 * Build a new sqlda structure. Note that only
1801 * fetching 1 record is supported
1803 sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1807 /* cleanup all SQLDAs we created up */
1810 sqlda_new = sqlda->desc_next;
1816 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1822 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1824 *_sqlda = sqlda_new;
1826 ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1827 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1828 stmt->lineno, PQnfields(stmt->results));
1830 sqlda_new->desc_next = sqlda;
1839 for (act_field = 0; act_field < nfields && status; act_field++)
1843 status = ecpg_store_result(stmt->results, act_field, stmt, var);
1846 else if (!INFORMIX_MODE(stmt->compat))
1848 ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1853 if (status && var != NULL)
1855 ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1860 case PGRES_COMMAND_OK:
1862 cmdstat = PQcmdStatus(stmt->results);
1863 sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1864 sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1865 ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1866 if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1867 !sqlca->sqlerrd[2] &&
1868 (strncmp(cmdstat, "UPDATE", 6) == 0
1869 || strncmp(cmdstat, "INSERT", 6) == 0
1870 || strncmp(cmdstat, "DELETE", 6) == 0))
1871 ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1873 case PGRES_COPY_OUT:
1878 ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1879 while ((res = PQgetCopyData(stmt->connection->connection,
1882 printf("%s", buffer);
1888 PQclear(stmt->results);
1889 stmt->results = PQgetResult(stmt->connection->connection);
1890 if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1891 ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1893 ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1900 * execution should never reach this code because it is already
1901 * handled in ecpg_check_PQresult()
1903 ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1905 ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1912 PQclear(stmt->results);
1913 stmt->results = NULL;
1916 /* check for asynchronous returns */
1917 PQconsumeInput(stmt->connection->connection);
1918 while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1920 ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1921 stmt->lineno, notify->relname, notify->be_pid);
1923 PQconsumeInput(stmt->connection->connection);
1932 * Initialize various infrastructure elements for executing the statement:
1934 * - create the statement structure
1935 * - set the C numeric locale for communicating with the backend
1936 * - preprocess the variable list of input/output parameters into
1940 ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
1941 const char *connection_name, const bool questionmarks,
1942 enum ECPG_statement_type statement_type, const char *query,
1943 va_list args, struct statement **stmt_out)
1945 struct statement *stmt = NULL;
1946 struct connection *con;
1947 enum ECPGttype type;
1948 struct variable **list;
1950 bool is_prepared_name_set;
1956 ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
1960 stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1966 * Make sure we do NOT honor the locale for numeric input/output since the
1967 * database wants the standard decimal point. If available, use
1968 * uselocale() for this because it's thread-safe. Windows doesn't have
1969 * that, but it usually does have _configthreadlocale(). In some versions
1970 * of MinGW, _configthreadlocale() exists but always returns -1 --- so
1971 * treat that situation as if the function doesn't exist.
1973 #ifdef HAVE_USELOCALE
1974 stmt->clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
1975 if (stmt->clocale == (locale_t) 0)
1977 ecpg_do_epilogue(stmt);
1980 stmt->oldlocale = uselocale(stmt->clocale);
1981 if (stmt->oldlocale == (locale_t) 0)
1983 ecpg_do_epilogue(stmt);
1987 #ifdef HAVE__CONFIGTHREADLOCALE
1988 stmt->oldthreadlocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
1990 stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
1991 if (stmt->oldlocale == NULL)
1993 ecpg_do_epilogue(stmt);
1996 setlocale(LC_NUMERIC, "C");
1999 #ifdef ENABLE_THREAD_SAFETY
2000 ecpg_pthreads_init();
2003 con = ecpg_get_connection(connection_name);
2005 if (!ecpg_init(con, connection_name, lineno))
2007 ecpg_do_epilogue(stmt);
2012 * If statement type is ECPGst_prepnormal we are supposed to prepare the
2013 * statement before executing them
2015 if (statement_type == ECPGst_prepnormal)
2017 if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2019 ecpg_do_epilogue(stmt);
2024 * statement is now prepared, so instead of the query we have to
2027 stmt->command = prepname;
2028 statement_type = ECPGst_execute;
2031 stmt->command = ecpg_strdup(query, lineno);
2035 if (statement_type == ECPGst_execute)
2037 /* if we have an EXECUTE command, only the name is send */
2038 char *command = ecpg_prepared(stmt->command, con);
2042 stmt->name = stmt->command;
2043 stmt->command = ecpg_strdup(command, lineno);
2047 ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, stmt->command);
2048 ecpg_do_epilogue(stmt);
2052 /* name of PREPARE AS will be set in loop of inlist */
2054 stmt->connection = con;
2055 stmt->lineno = lineno;
2056 stmt->compat = compat;
2057 stmt->force_indicator = force_indicator;
2058 stmt->questionmarks = questionmarks;
2059 stmt->statement_type = statement_type;
2062 * create a list of variables
2064 * The variables are listed with input variables preceding output
2065 * variables. The end of each group is marked by an end marker.
2066 * Per variable we list:
2068 * type - as defined in ecpgtype.h
2069 * value - where to store the data
2070 * varcharsize - length of string in case we have a stringvariable, else 0
2071 * arraysize - 0 for pointer (we don't know the size of the array), 1 for
2072 * simple variable, size for arrays
2073 * offset - offset between ith and (i+1)th entry in an array, normally
2074 * that means sizeof(type)
2075 * ind_type - type of indicator variable
2076 * ind_pointer - pointer to indicator variable
2077 * ind_varcharsize - empty
2078 * ind_arrsize - arraysize of indicator array
2079 * ind_offset - indicator offset
2083 is_prepared_name_set = false;
2085 list = &(stmt->inlist);
2087 type = va_arg(args, enum ECPGttype);
2089 while (type != ECPGt_EORT)
2091 if (type == ECPGt_EOIT)
2092 list = &(stmt->outlist);
2095 struct variable *var,
2098 if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2100 ecpg_do_epilogue(stmt);
2105 var->pointer = va_arg(args, char *);
2107 var->varcharsize = va_arg(args, long);
2108 var->arrsize = va_arg(args, long);
2109 var->offset = va_arg(args, long);
2112 * Unknown array size means pointer to an array. Unknown
2113 * varcharsize usually also means pointer. But if the type is
2114 * character and the array size is known, it is an array of
2115 * pointers to char, so use var->pointer as it is.
2117 if (var->arrsize == 0 ||
2118 (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
2119 var->value = *((char **) (var->pointer));
2121 var->value = var->pointer;
2124 * negative values are used to indicate an array without given
2127 /* reset to zero for us */
2128 if (var->arrsize < 0)
2130 if (var->varcharsize < 0)
2131 var->varcharsize = 0;
2135 var->ind_type = va_arg(args, enum ECPGttype);
2136 var->ind_pointer = va_arg(args, char *);
2137 var->ind_varcharsize = va_arg(args, long);
2138 var->ind_arrsize = va_arg(args, long);
2139 var->ind_offset = va_arg(args, long);
2141 if (var->ind_type != ECPGt_NO_INDICATOR
2142 && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2143 var->ind_value = *((char **) (var->ind_pointer));
2145 var->ind_value = var->ind_pointer;
2148 * negative values are used to indicate an array without given
2151 /* reset to zero for us */
2152 if (var->ind_arrsize < 0)
2153 var->ind_arrsize = 0;
2154 if (var->ind_varcharsize < 0)
2155 var->ind_varcharsize = 0;
2157 /* if variable is NULL, the statement hasn't been prepared */
2158 if (var->pointer == NULL)
2160 ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, NULL);
2162 ecpg_do_epilogue(stmt);
2166 for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2174 if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2176 stmt->name = ecpg_strdup(var->value, lineno);
2177 is_prepared_name_set = true;
2181 type = va_arg(args, enum ECPGttype);
2184 /* are we connected? */
2185 if (con == NULL || con->connection == NULL)
2187 ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2188 ecpg_do_epilogue(stmt);
2192 if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2194 ecpg_raise(lineno, ECPG_TOO_FEW_ARGUMENTS, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2195 ecpg_do_epilogue(stmt);
2199 /* initialize auto_mem struct */
2200 ecpg_clear_auto_mem();
2209 * Restore the application locale and free the statement structure.
2212 ecpg_do_epilogue(struct statement *stmt)
2217 #ifdef HAVE_USELOCALE
2218 if (stmt->oldlocale != (locale_t) 0)
2219 uselocale(stmt->oldlocale);
2221 if (stmt->oldlocale)
2222 setlocale(LC_NUMERIC, stmt->oldlocale);
2223 #ifdef HAVE__CONFIGTHREADLOCALE
2226 * This is a bit trickier than it looks: if we failed partway through
2227 * statement initialization, oldthreadlocale could still be 0. But that's
2228 * okay because a call with 0 is defined to be a no-op.
2230 if (stmt->oldthreadlocale != -1)
2231 (void) _configthreadlocale(stmt->oldthreadlocale);
2235 free_statement(stmt);
2239 * Execute SQL statements in the backend.
2240 * The input/output parameters (variable argument list) are passed
2241 * in a va_list, so other functions can use this interface.
2244 ecpg_do(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query, va_list args)
2246 struct statement *stmt = NULL;
2248 if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2249 questionmarks, (enum ECPG_statement_type) st,
2250 query, args, &stmt))
2253 if (!ecpg_build_params(stmt))
2256 if (!ecpg_autostart_transaction(stmt))
2259 if (!ecpg_execute(stmt))
2262 if (!ecpg_process_output(stmt, true))
2265 ecpg_do_epilogue(stmt);
2269 ecpg_do_epilogue(stmt);
2274 * Execute SQL statements in the backend.
2275 * The input/output parameters are passed as variable-length argument list.
2278 ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
2283 va_start(args, query);
2284 ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2285 questionmarks, st, query, args);
2291 /* old descriptor interface */
2293 ECPGdo_descriptor(int line, const char *connection,
2294 const char *descriptor, const char *query)
2296 return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2297 ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2298 ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);