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"
26 #include "ecpgerrno.h"
29 #include "sqlda-native.h"
30 #include "sqlda-compat.h"
31 #include "sql3types.h"
32 #include "pgtypes_numeric.h"
33 #include "pgtypes_date.h"
34 #include "pgtypes_timestamp.h"
35 #include "pgtypes_interval.h"
38 * This function returns a newly malloced string that has ' and \
42 quote_postgres(char *arg, bool quote, int lineno)
50 * if quote is false we just need to store things in a descriptor they
51 * will be quoted once they are inserted in a statement
58 buffer_len = 2 * length + 1;
59 res = (char *) ecpg_alloc(buffer_len + 3, lineno);
62 escaped_len = PQescapeString(res + 1, arg, buffer_len);
63 if (length == escaped_len)
65 res[0] = res[escaped_len + 1] = '\'';
66 res[escaped_len + 2] = '\0';
71 * We don't know if the target database is using
72 * standard_conforming_strings, so we always use E'' strings.
74 memmove(res + 2, res + 1, escaped_len);
75 res[0] = ESCAPE_STRING_SYNTAX;
76 res[1] = res[escaped_len + 2] = '\'';
77 res[escaped_len + 3] = '\0';
85 free_variable(struct variable *var)
87 struct variable *var_next;
98 free_statement(struct statement *stmt)
102 free_variable(stmt->inlist);
103 free_variable(stmt->outlist);
104 ecpg_free(stmt->command);
105 ecpg_free(stmt->name);
106 ecpg_free(stmt->oldlocale);
111 next_insert(char *text, int pos, bool questionmarks)
116 for (; text[p] != '\0'; p++)
118 if (text[p] == '\\') /* escape character */
120 else if (text[p] == '\'')
121 string = string ? false : true;
124 if (text[p] == '$' && isdigit((unsigned char) text[p + 1]))
126 /* this can be either a dollar quote or a variable */
129 for (i = p + 1; isdigit((unsigned char) text[i]); i++)
130 /* empty loop body */ ;
131 if (!isalpha((unsigned char) text[i]) &&
132 isascii((unsigned char) text[i]) &&text[i] != '_')
133 /* not dollar delimited quote */
136 else if (questionmarks && text[p] == '?')
138 /* also allow old style placeholders */
148 ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
150 struct ECPGtype_information_cache *new_entry
151 = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
153 if (new_entry == NULL)
156 new_entry->oid = oid;
157 new_entry->isarray = isarray;
158 new_entry->next = *cache;
163 static enum ARRAY_TYPE
164 ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
167 enum ARRAY_TYPE isarray = ECPG_ARRAY_NOT_SET;
169 struct ECPGtype_information_cache *cache_entry;
171 if ((stmt->connection->cache_head) == NULL)
174 * Text like types are not an array for ecpg, but postgres counts them
175 * as an array. This define reminds you to not 'correct' these values.
177 #define not_an_array_in_ecpg ECPG_ARRAY_NONE
179 /* populate cache with well known types to speed things up */
180 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOOLOID, ECPG_ARRAY_NONE, stmt->lineno))
181 return (ECPG_ARRAY_ERROR);
182 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BYTEAOID, ECPG_ARRAY_NONE, stmt->lineno))
183 return (ECPG_ARRAY_ERROR);
184 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CHAROID, ECPG_ARRAY_NONE, stmt->lineno))
185 return (ECPG_ARRAY_ERROR);
186 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NAMEOID, not_an_array_in_ecpg, stmt->lineno))
187 return (ECPG_ARRAY_ERROR);
188 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT8OID, ECPG_ARRAY_NONE, stmt->lineno))
189 return (ECPG_ARRAY_ERROR);
190 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2OID, ECPG_ARRAY_NONE, stmt->lineno))
191 return (ECPG_ARRAY_ERROR);
192 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2VECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
193 return (ECPG_ARRAY_ERROR);
194 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT4OID, ECPG_ARRAY_NONE, stmt->lineno))
195 return (ECPG_ARRAY_ERROR);
196 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), REGPROCOID, ECPG_ARRAY_NONE, stmt->lineno))
197 return (ECPG_ARRAY_ERROR);
198 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TEXTOID, ECPG_ARRAY_NONE, stmt->lineno))
199 return (ECPG_ARRAY_ERROR);
200 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDOID, ECPG_ARRAY_NONE, stmt->lineno))
201 return (ECPG_ARRAY_ERROR);
202 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIDOID, ECPG_ARRAY_NONE, stmt->lineno))
203 return (ECPG_ARRAY_ERROR);
204 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), XIDOID, ECPG_ARRAY_NONE, stmt->lineno))
205 return (ECPG_ARRAY_ERROR);
206 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDOID, ECPG_ARRAY_NONE, stmt->lineno))
207 return (ECPG_ARRAY_ERROR);
208 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDVECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
209 return (ECPG_ARRAY_ERROR);
210 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POINTOID, ECPG_ARRAY_VECTOR, stmt->lineno))
211 return (ECPG_ARRAY_ERROR);
212 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LSEGOID, ECPG_ARRAY_VECTOR, stmt->lineno))
213 return (ECPG_ARRAY_ERROR);
214 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), PATHOID, ECPG_ARRAY_NONE, stmt->lineno))
215 return (ECPG_ARRAY_ERROR);
216 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOXOID, ECPG_ARRAY_VECTOR, stmt->lineno))
217 return (ECPG_ARRAY_ERROR);
218 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POLYGONOID, ECPG_ARRAY_NONE, stmt->lineno))
219 return (ECPG_ARRAY_ERROR);
220 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LINEOID, ECPG_ARRAY_VECTOR, stmt->lineno))
221 return (ECPG_ARRAY_ERROR);
222 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT4OID, ECPG_ARRAY_NONE, stmt->lineno))
223 return (ECPG_ARRAY_ERROR);
224 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT8OID, ECPG_ARRAY_NONE, stmt->lineno))
225 return (ECPG_ARRAY_ERROR);
226 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), ABSTIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
227 return (ECPG_ARRAY_ERROR);
228 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), RELTIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
229 return (ECPG_ARRAY_ERROR);
230 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TINTERVALOID, ECPG_ARRAY_NONE, stmt->lineno))
231 return (ECPG_ARRAY_ERROR);
232 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), UNKNOWNOID, ECPG_ARRAY_NONE, stmt->lineno))
233 return (ECPG_ARRAY_ERROR);
234 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIRCLEOID, ECPG_ARRAY_NONE, stmt->lineno))
235 return (ECPG_ARRAY_ERROR);
236 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CASHOID, ECPG_ARRAY_NONE, stmt->lineno))
237 return (ECPG_ARRAY_ERROR);
238 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INETOID, ECPG_ARRAY_NONE, stmt->lineno))
239 return (ECPG_ARRAY_ERROR);
240 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDROID, ECPG_ARRAY_NONE, stmt->lineno))
241 return (ECPG_ARRAY_ERROR);
242 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BPCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
243 return (ECPG_ARRAY_ERROR);
244 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
245 return (ECPG_ARRAY_ERROR);
246 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), DATEOID, ECPG_ARRAY_NONE, stmt->lineno))
247 return (ECPG_ARRAY_ERROR);
248 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
249 return (ECPG_ARRAY_ERROR);
250 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPOID, ECPG_ARRAY_NONE, stmt->lineno))
251 return (ECPG_ARRAY_ERROR);
252 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPTZOID, ECPG_ARRAY_NONE, stmt->lineno))
253 return (ECPG_ARRAY_ERROR);
254 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INTERVALOID, ECPG_ARRAY_NONE, stmt->lineno))
255 return (ECPG_ARRAY_ERROR);
256 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMETZOID, ECPG_ARRAY_NONE, stmt->lineno))
257 return (ECPG_ARRAY_ERROR);
258 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), ZPBITOID, ECPG_ARRAY_NONE, stmt->lineno))
259 return (ECPG_ARRAY_ERROR);
260 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARBITOID, ECPG_ARRAY_NONE, stmt->lineno))
261 return (ECPG_ARRAY_ERROR);
262 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NUMERICOID, ECPG_ARRAY_NONE, stmt->lineno))
263 return (ECPG_ARRAY_ERROR);
266 for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
268 if (cache_entry->oid == type)
269 return cache_entry->isarray;
272 array_query = (char *) ecpg_alloc(strlen("select typlen from pg_type where oid= and typelem<>0") + 11, stmt->lineno);
273 if (array_query == NULL)
274 return (ECPG_ARRAY_ERROR);
276 sprintf(array_query, "select typlen from pg_type where oid=%d and typelem<>0", type);
277 query = PQexec(stmt->connection->connection, array_query);
278 ecpg_free(array_query);
279 if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
280 return (ECPG_ARRAY_ERROR);
281 else if (PQresultStatus(query) == PGRES_TUPLES_OK)
283 if (PQntuples(query) == 0)
284 isarray = ECPG_ARRAY_NONE;
287 isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
288 if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
289 ecpg_dynamic_type(type) == SQL3_CHARACTER_VARYING)
292 * arrays of character strings are not yet implemented
294 isarray = ECPG_ARRAY_NONE;
300 return (ECPG_ARRAY_ERROR);
302 ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
303 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");
309 ecpg_store_result(const PGresult *results, int act_field,
310 const struct statement *stmt, struct variable *var)
312 enum ARRAY_TYPE isarray;
314 ntuples = PQntuples(results);
317 if ((isarray = ecpg_is_type_an_array(PQftype(results, act_field), stmt, var)) == ECPG_ARRAY_ERROR)
319 ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
323 if (isarray == ECPG_ARRAY_NONE)
326 * if we don't have enough space, we cannot read all tuples
328 if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
330 ecpg_log("ecpg_store_result on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
331 stmt->lineno, ntuples, var->arrsize);
332 ecpg_raise(stmt->lineno, INFORMIX_MODE(stmt->compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_TOO_MANY_MATCHES, ECPG_SQLSTATE_CARDINALITY_VIOLATION, NULL);
339 * since we read an array, the variable has to be an array too
341 if (var->arrsize == 0)
343 ecpg_raise(stmt->lineno, ECPG_NO_ARRAY, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
349 * allocate memory for NULL pointers
351 if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
355 if (!PQfformat(results, act_field))
360 case ECPGt_unsigned_char:
362 if (!var->varcharsize && !var->arrsize)
364 /* special mode for handling char**foo=0 */
365 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
366 len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
367 len *= var->offset; /* should be 1, but YMNK */
368 len += (ntuples + 1) * sizeof(char *);
372 var->varcharsize = 0;
373 /* check strlen for each tuple */
374 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
376 int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
378 if (len > var->varcharsize)
379 var->varcharsize = len;
381 var->offset *= var->varcharsize;
382 len = var->offset * ntuples;
386 len = ntuples * (var->varcharsize + sizeof(int));
389 len = var->offset * ntuples;
395 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
396 len += PQgetlength(results, act_tuple, act_field);
399 ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
400 var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
403 *((char **) var->pointer) = var->value;
406 /* allocate indicator variable if needed */
407 if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
409 int len = var->ind_offset * ntuples;
411 var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
414 *((char **) var->ind_pointer) = var->ind_value;
417 /* fill the variable with the tuple(s) */
418 if (!var->varcharsize && !var->arrsize &&
419 (var->type == ECPGt_char || var->type == ECPGt_unsigned_char || var->type == ECPGt_string))
421 /* special mode for handling char**foo=0 */
423 /* filling the array of (char*)s */
424 char **current_string = (char **) var->value;
426 /* storing the data (after the last array element) */
427 char *current_data_location = (char *) ¤t_string[ntuples + 1];
429 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
431 int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
433 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
434 var->type, var->ind_type, current_data_location,
435 var->ind_value, len, 0, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
439 *current_string = current_data_location;
440 current_data_location += len;
445 /* terminate the list */
446 *current_string = NULL;
450 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
452 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
453 var->type, var->ind_type, var->value,
454 var->ind_value, var->varcharsize, var->offset, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
462 sprintf_double_value(char *ptr, double value, const char *delim)
465 sprintf(ptr, "%s%s", "NaN", delim);
466 else if (isinf(value))
469 sprintf(ptr, "%s%s", "-Infinity", delim);
471 sprintf(ptr, "%s%s", "Infinity", delim);
474 sprintf(ptr, "%.15g%s", value, delim);
478 sprintf_float_value(char *ptr, float value, const char *delim)
481 sprintf(ptr, "%s%s", "NaN", delim);
482 else if (isinf(value))
485 sprintf(ptr, "%s%s", "-Infinity", delim);
487 sprintf(ptr, "%s%s", "Infinity", delim);
490 sprintf(ptr, "%.15g%s", value, delim);
494 ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var,
495 char **tobeinserted_p, bool quote)
497 char *mallocedval = NULL;
498 char *newcopy = NULL;
501 * arrays are not possible unless the column is an array, too FIXME: we do
502 * not know if the column is an array here array input to singleton column
503 * will result in a runtime error
507 * Some special treatment is needed for records since we want their
508 * contents to arrive in a comma-separated list on insert (I think).
511 *tobeinserted_p = "";
513 /* check for null value and set input buffer accordingly */
514 switch (var->ind_type)
517 case ECPGt_unsigned_short:
518 if (*(short *) var->ind_value < 0)
519 *tobeinserted_p = NULL;
522 case ECPGt_unsigned_int:
523 if (*(int *) var->ind_value < 0)
524 *tobeinserted_p = NULL;
527 case ECPGt_unsigned_long:
528 if (*(long *) var->ind_value < 0L)
529 *tobeinserted_p = NULL;
531 #ifdef HAVE_LONG_LONG_INT
532 case ECPGt_long_long:
533 case ECPGt_unsigned_long_long:
534 if (*(long long int *) var->ind_value < (long long) 0)
535 *tobeinserted_p = NULL;
537 #endif /* HAVE_LONG_LONG_INT */
538 case ECPGt_NO_INDICATOR:
539 if (force_indicator == false)
541 if (ECPGis_noind_null(var->type, var->value))
542 *tobeinserted_p = NULL;
548 if (*tobeinserted_p != NULL)
550 int asize = var->arrsize ? var->arrsize : 1;
557 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
562 strcpy(mallocedval, "{");
564 for (element = 0; element < asize; element++)
565 sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
567 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
570 sprintf(mallocedval, "%hd", *((short *) var->value));
572 *tobeinserted_p = mallocedval;
576 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
581 strcpy(mallocedval, "{");
583 for (element = 0; element < asize; element++)
584 sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
586 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
589 sprintf(mallocedval, "%d", *((int *) var->value));
591 *tobeinserted_p = mallocedval;
594 case ECPGt_unsigned_short:
595 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
600 strcpy(mallocedval, "{");
602 for (element = 0; element < asize; element++)
603 sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
605 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
608 sprintf(mallocedval, "%hu", *((unsigned short *) var->value));
610 *tobeinserted_p = mallocedval;
613 case ECPGt_unsigned_int:
614 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
619 strcpy(mallocedval, "{");
621 for (element = 0; element < asize; element++)
622 sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
624 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
627 sprintf(mallocedval, "%u", *((unsigned int *) var->value));
629 *tobeinserted_p = mallocedval;
633 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
638 strcpy(mallocedval, "{");
640 for (element = 0; element < asize; element++)
641 sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
643 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
646 sprintf(mallocedval, "%ld", *((long *) var->value));
648 *tobeinserted_p = mallocedval;
651 case ECPGt_unsigned_long:
652 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
657 strcpy(mallocedval, "{");
659 for (element = 0; element < asize; element++)
660 sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
662 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
665 sprintf(mallocedval, "%lu", *((unsigned long *) var->value));
667 *tobeinserted_p = mallocedval;
669 #ifdef HAVE_LONG_LONG_INT
670 case ECPGt_long_long:
671 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
676 strcpy(mallocedval, "{");
678 for (element = 0; element < asize; element++)
679 sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
681 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
684 sprintf(mallocedval, "%lld", *((long long int *) var->value));
686 *tobeinserted_p = mallocedval;
689 case ECPGt_unsigned_long_long:
690 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
695 strcpy(mallocedval, "{");
697 for (element = 0; element < asize; element++)
698 sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
700 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
703 sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
705 *tobeinserted_p = mallocedval;
707 #endif /* HAVE_LONG_LONG_INT */
709 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
714 strcpy(mallocedval, "{");
716 for (element = 0; element < asize; element++)
717 sprintf_float_value(mallocedval + strlen(mallocedval), ((float *) var->value)[element], ",");
719 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
722 sprintf_float_value(mallocedval, *((float *) var->value), "");
724 *tobeinserted_p = mallocedval;
728 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
733 strcpy(mallocedval, "{");
735 for (element = 0; element < asize; element++)
736 sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
738 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
741 sprintf_double_value(mallocedval, *((double *) var->value), "");
743 *tobeinserted_p = mallocedval;
747 if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
750 if (var->arrsize > 1)
752 strcpy(mallocedval, "{");
754 for (element = 0; element < asize; element++)
755 sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
757 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
761 if (var->offset == sizeof(char))
762 sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
763 else if (var->offset == sizeof(int))
764 sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
766 ecpg_raise(lineno, ECPG_CONVERT_BOOL, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
769 *tobeinserted_p = mallocedval;
773 case ECPGt_unsigned_char:
776 /* set slen to string length if type is char * */
777 int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
779 if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
782 strncpy(newcopy, (char *) var->value, slen);
783 newcopy[slen] = '\0';
785 mallocedval = quote_postgres(newcopy, quote, lineno);
792 *tobeinserted_p = mallocedval;
796 case ECPGt_char_variable:
798 int slen = strlen((char *) var->value);
800 if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
803 strncpy(mallocedval, (char *) var->value, slen);
804 mallocedval[slen] = '\0';
806 *tobeinserted_p = mallocedval;
811 struct ECPGgeneric_varchar *variable =
812 (struct ECPGgeneric_varchar *) (var->value);
814 if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, lineno)))
817 strncpy(newcopy, variable->arr, variable->len);
818 newcopy[variable->len] = '\0';
820 mallocedval = quote_postgres(newcopy, quote, lineno);
827 *tobeinserted_p = mallocedval;
838 if (var->arrsize > 1)
839 mallocedval = ecpg_strdup("{", lineno);
841 mallocedval = ecpg_strdup("", lineno);
846 for (element = 0; element < asize; element++)
850 nval = PGTYPESnumeric_new();
853 ecpg_free(mallocedval);
857 if (var->type == ECPGt_numeric)
858 result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
860 result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
864 PGTYPESnumeric_free(nval);
865 ecpg_free(mallocedval);
869 str = PGTYPESnumeric_to_asc(nval, nval->dscale);
871 PGTYPESnumeric_free(nval);
873 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
875 ecpg_free(mallocedval);
879 mallocedval = newcopy;
881 /* also copy trailing '\0' */
882 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
883 if (var->arrsize > 1)
884 strcpy(mallocedval + strlen(mallocedval), ",");
889 if (var->arrsize > 1)
890 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
892 *tobeinserted_p = mallocedval;
901 if (var->arrsize > 1)
902 mallocedval = ecpg_strdup("{", lineno);
904 mallocedval = ecpg_strdup("", lineno);
909 for (element = 0; element < asize; element++)
911 str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
914 ecpg_free(mallocedval);
920 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
922 ecpg_free(mallocedval);
926 mallocedval = newcopy;
928 /* also copy trailing '\0' */
929 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
930 if (var->arrsize > 1)
931 strcpy(mallocedval + strlen(mallocedval), ",");
936 if (var->arrsize > 1)
937 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
939 *tobeinserted_p = mallocedval;
948 if (var->arrsize > 1)
949 mallocedval = ecpg_strdup("{", lineno);
951 mallocedval = ecpg_strdup("", lineno);
956 for (element = 0; element < asize; element++)
958 str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
961 ecpg_free(mallocedval);
967 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
969 ecpg_free(mallocedval);
973 mallocedval = newcopy;
975 /* also copy trailing '\0' */
976 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
977 if (var->arrsize > 1)
978 strcpy(mallocedval + strlen(mallocedval), ",");
983 if (var->arrsize > 1)
984 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
986 *tobeinserted_p = mallocedval;
990 case ECPGt_timestamp:
995 if (var->arrsize > 1)
996 mallocedval = ecpg_strdup("{", lineno);
998 mallocedval = ecpg_strdup("", lineno);
1003 for (element = 0; element < asize; element++)
1005 str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1008 ecpg_free(mallocedval);
1014 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1016 ecpg_free(mallocedval);
1020 mallocedval = newcopy;
1022 /* also copy trailing '\0' */
1023 memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1024 if (var->arrsize > 1)
1025 strcpy(mallocedval + strlen(mallocedval), ",");
1030 if (var->arrsize > 1)
1031 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1033 *tobeinserted_p = mallocedval;
1037 case ECPGt_descriptor:
1042 /* Not implemented yet */
1043 ecpg_raise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_type_name(var->type));
1052 ecpg_free_params(struct statement *stmt, bool print)
1056 for (n = 0; n < stmt->nparams; n++)
1059 ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n", stmt->lineno, n + 1, stmt->paramvalues[n] ? stmt->paramvalues[n] : "null");
1060 ecpg_free(stmt->paramvalues[n]);
1062 ecpg_free(stmt->paramvalues);
1063 stmt->paramvalues = NULL;
1068 insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
1072 if (!(newcopy = (char *) ecpg_alloc(strlen(stmt->command)
1073 + strlen(tobeinserted)
1074 + 1, stmt->lineno)))
1076 ecpg_free(tobeinserted);
1080 strcpy(newcopy, stmt->command);
1081 strcpy(newcopy + position - 1, tobeinserted);
1084 * The strange thing in the second argument is the rest of the string from
1092 ecpg_free(stmt->command);
1093 stmt->command = newcopy;
1095 ecpg_free((char *) tobeinserted);
1101 * Build statement parameters
1103 * The input values are taken from user variables, and the results are stored
1104 * in arrays which can be used by PQexecParams().
1107 ecpg_build_params(struct statement *stmt)
1109 struct variable *var;
1110 int desc_counter = 0;
1114 * If the type is one of the fill in types then we take the argument and
1115 * enter it to our parameter array at the first position. Then if there
1116 * are any more fill in types we add more parameters.
1124 tobeinserted = NULL;
1127 * A descriptor is a special case since it contains many variables but
1128 * is listed only once.
1130 if (var->type == ECPGt_descriptor)
1133 * We create an additional variable list here, so the same logic
1136 struct variable desc_inlist;
1137 struct descriptor *desc;
1138 struct descriptor_item *desc_item;
1140 desc = ecpg_find_desc(stmt->lineno, var->pointer);
1145 for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1147 if (desc_item->num == desc_counter)
1149 desc_inlist.type = ECPGt_char;
1150 desc_inlist.value = desc_item->data;
1151 desc_inlist.pointer = &(desc_item->data);
1152 desc_inlist.varcharsize = strlen(desc_item->data);
1153 desc_inlist.arrsize = 1;
1154 desc_inlist.offset = 0;
1155 if (!desc_item->indicator)
1157 desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1158 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1159 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1163 desc_inlist.ind_type = ECPGt_int;
1164 desc_inlist.ind_value = &(desc_item->indicator);
1165 desc_inlist.ind_pointer = &(desc_inlist.ind_value);
1166 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1167 desc_inlist.ind_offset = 0;
1169 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1175 if (desc->count == desc_counter)
1178 else if (var->type == ECPGt_sqlda)
1180 if (INFORMIX_MODE(stmt->compat))
1182 struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1183 struct variable desc_inlist;
1190 for (i = 0; i < sqlda->sqld; i++)
1192 if (i + 1 == desc_counter)
1194 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1195 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1196 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1197 switch (desc_inlist.type)
1201 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1204 desc_inlist.varcharsize = 0;
1207 desc_inlist.arrsize = 1;
1208 desc_inlist.offset = 0;
1209 if (sqlda->sqlvar[i].sqlind)
1211 desc_inlist.ind_type = ECPGt_short;
1212 /* ECPG expects indicator value < 0 */
1213 if (*(sqlda->sqlvar[i].sqlind))
1214 *(sqlda->sqlvar[i].sqlind) = -1;
1215 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1216 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1217 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1218 desc_inlist.ind_offset = 0;
1222 desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1223 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1224 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1226 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1232 if (sqlda->sqld == desc_counter)
1237 struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1238 struct variable desc_inlist;
1245 for (i = 0; i < sqlda->sqln; i++)
1247 if (i + 1 == desc_counter)
1249 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1250 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1251 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1252 switch (desc_inlist.type)
1256 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1259 desc_inlist.varcharsize = 0;
1262 desc_inlist.arrsize = 1;
1263 desc_inlist.offset = 0;
1264 if (sqlda->sqlvar[i].sqlind)
1266 desc_inlist.ind_type = ECPGt_short;
1267 /* ECPG expects indicator value < 0 */
1268 if (*(sqlda->sqlvar[i].sqlind))
1269 *(sqlda->sqlvar[i].sqlind) = -1;
1270 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1271 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1272 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1273 desc_inlist.ind_offset = 0;
1277 desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1278 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1279 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1281 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1287 if (sqlda->sqln == desc_counter)
1294 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1299 * now tobeinserted points to an area that contains the next
1300 * parameter; now find the position in the string where it belongs
1302 if ((position = next_insert(stmt->command, position, stmt->questionmarks) + 1) == 0)
1305 * We have an argument but we dont have the matched up placeholder
1308 ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS,
1309 ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS,
1311 ecpg_free_params(stmt, false);
1316 * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1317 * to simulate a dynamic cursor because there is no backend
1318 * functionality for it
1320 if (var->type == ECPGt_char_variable)
1322 int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1324 if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1326 ecpg_free_params(stmt, false);
1329 tobeinserted = NULL;
1333 * if the placeholder is '$0' we have to replace it on the client side
1334 * this is for places we want to support variables at that are not
1335 * supported in the backend
1337 else if (stmt->command[position] == '0')
1339 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1341 ecpg_free_params(stmt, false);
1344 tobeinserted = NULL;
1350 if (!(paramvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1352 ecpg_free_params(stmt, false);
1357 stmt->paramvalues = paramvalues;
1358 stmt->paramvalues[stmt->nparams - 1] = tobeinserted;
1360 /* let's see if this was an old style placeholder */
1361 if (stmt->command[position] == '?')
1363 /* yes, replace with new style */
1364 int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1367 if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1369 ecpg_free_params(stmt, false);
1373 snprintf(tobeinserted, buffersize, "$%d", counter++);
1375 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1377 ecpg_free_params(stmt, false);
1380 tobeinserted = NULL;
1384 if (desc_counter == 0)
1388 /* Check if there are unmatched things left. */
1389 if (next_insert(stmt->command, position, stmt->questionmarks) >= 0)
1391 ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS,
1392 ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS, NULL);
1393 ecpg_free_params(stmt, false);
1401 * ecpg_autostart_transaction
1402 * If we are in non-autocommit mode, automatically start a transaction.
1405 ecpg_autostart_transaction(struct statement *stmt)
1407 if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
1409 stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1410 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1412 ecpg_free_params(stmt, false);
1415 PQclear(stmt->results);
1416 stmt->results = NULL;
1423 * Execute the SQL statement.
1426 ecpg_execute(struct statement *stmt)
1428 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);
1429 if (stmt->statement_type == ECPGst_execute)
1431 stmt->results = PQexecPrepared(stmt->connection->connection, stmt->name, stmt->nparams, (const char *const *) stmt->paramvalues, NULL, NULL, 0);
1432 ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1436 if (stmt->nparams == 0)
1438 stmt->results = PQexec(stmt->connection->connection, stmt->command);
1439 ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1443 stmt->results = PQexecParams(stmt->connection->connection, stmt->command, stmt->nparams, NULL, (const char *const *) stmt->paramvalues, NULL, NULL, 0);
1444 ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1448 ecpg_free_params(stmt, true);
1450 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1457 * ecpg_process_output
1459 * Process the statement result and store it into application variables. This
1460 * function can be called repeatedly during the same statement in case cursor
1461 * readahead is used and the application does FETCH N which overflows the
1465 * stmt statement structure holding the PGresult and
1466 * the list of output variables
1468 * PQclear() the result upon returning from this function
1470 * Returns success as boolean. Also an SQL error is raised in case of failure.
1474 ecpg_process_output(struct statement *stmt, bool clear_result)
1476 struct variable *var;
1477 bool status = false;
1480 struct sqlca_t *sqlca = ECPGget_sqlca();
1487 ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
1488 ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
1492 var = stmt->outlist;
1493 switch (PQresultStatus(stmt->results))
1495 case PGRES_TUPLES_OK:
1496 nfields = PQnfields(stmt->results);
1497 sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1499 ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1505 ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1506 stmt->lineno, ntuples);
1507 ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1512 if (var != NULL && var->type == ECPGt_descriptor)
1514 struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1521 PQclear(desc->result);
1522 desc->result = stmt->results;
1523 clear_result = false;
1524 ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1525 stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1529 else if (var != NULL && var->type == ECPGt_sqlda)
1531 if (INFORMIX_MODE(stmt->compat))
1533 struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1534 struct sqlda_compat *sqlda = *_sqlda;
1535 struct sqlda_compat *sqlda_new;
1539 * If we are passed in a previously existing sqlda (chain)
1544 sqlda_new = sqlda->desc_next;
1548 *_sqlda = sqlda = sqlda_new = NULL;
1549 for (i = ntuples - 1; i >= 0; i--)
1552 * Build a new sqlda structure. Note that only
1553 * fetching 1 record is supported
1555 sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1559 /* cleanup all SQLDAs we created up */
1562 sqlda_new = sqlda->desc_next;
1568 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1574 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1576 *_sqlda = sqlda_new;
1578 ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1579 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1580 stmt->lineno, PQnfields(stmt->results));
1582 sqlda_new->desc_next = sqlda;
1589 struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1590 struct sqlda_struct *sqlda = *_sqlda;
1591 struct sqlda_struct *sqlda_new;
1595 * If we are passed in a previously existing sqlda (chain)
1600 sqlda_new = sqlda->desc_next;
1604 *_sqlda = sqlda = sqlda_new = NULL;
1605 for (i = ntuples - 1; i >= 0; i--)
1608 * Build a new sqlda structure. Note that only
1609 * fetching 1 record is supported
1611 sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1615 /* cleanup all SQLDAs we created up */
1618 sqlda_new = sqlda->desc_next;
1624 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1630 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1632 *_sqlda = sqlda_new;
1634 ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1635 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1636 stmt->lineno, PQnfields(stmt->results));
1638 sqlda_new->desc_next = sqlda;
1647 for (act_field = 0; act_field < nfields && status; act_field++)
1651 status = ecpg_store_result(stmt->results, act_field, stmt, var);
1654 else if (!INFORMIX_MODE(stmt->compat))
1656 ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1661 if (status && var != NULL)
1663 ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1668 case PGRES_COMMAND_OK:
1670 cmdstat = PQcmdStatus(stmt->results);
1671 sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1672 sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1673 ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1674 if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1675 !sqlca->sqlerrd[2] &&
1676 (strncmp(cmdstat, "UPDATE", 6) == 0
1677 || strncmp(cmdstat, "INSERT", 6) == 0
1678 || strncmp(cmdstat, "DELETE", 6) == 0))
1679 ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1681 case PGRES_COPY_OUT:
1686 ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1687 while ((res = PQgetCopyData(stmt->connection->connection,
1690 printf("%s", buffer);
1696 PQclear(stmt->results);
1697 stmt->results = PQgetResult(stmt->connection->connection);
1698 if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1699 ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1701 ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1708 * execution should never reach this code because it is already
1709 * handled in ECPGcheck_PQresult()
1711 ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1713 ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1720 PQclear(stmt->results);
1721 stmt->results = NULL;
1724 /* check for asynchronous returns */
1725 notify = PQnotifies(stmt->connection->connection);
1728 ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1729 stmt->lineno, notify->relname, notify->be_pid);
1739 * Initialize various infrastructure elements for executing the statement:
1741 * - create the statement structure
1742 * - set the C numeric locale for communicating with the backend
1743 * - preprocess the variable list of input/output parameters into
1747 ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
1748 const char *connection_name, const bool questionmarks,
1749 enum ECPG_statement_type statement_type, const char *query,
1750 va_list args, struct statement **stmt_out)
1752 struct statement *stmt;
1753 struct connection *con;
1754 enum ECPGttype type;
1755 struct variable **list;
1762 ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
1766 stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1772 * Make sure we do NOT honor the locale for numeric input/output since the
1773 * database wants the standard decimal point
1775 stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
1776 if (stmt->oldlocale == NULL)
1778 ecpg_do_epilogue(stmt);
1781 setlocale(LC_NUMERIC, "C");
1783 #ifdef ENABLE_THREAD_SAFETY
1784 ecpg_pthreads_init();
1787 con = ecpg_get_connection(connection_name);
1789 if (!ecpg_init(con, connection_name, lineno))
1791 ecpg_do_epilogue(stmt);
1796 * If statement type is ECPGst_prepnormal we are supposed to prepare the
1797 * statement before executing them
1799 if (statement_type == ECPGst_prepnormal)
1801 if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
1803 ecpg_do_epilogue(stmt);
1808 * statement is now prepared, so instead of the query we have to
1811 stmt->command = prepname;
1812 statement_type = ECPGst_execute;
1815 stmt->command = ecpg_strdup(query, lineno);
1819 if (statement_type == ECPGst_execute)
1821 /* if we have an EXECUTE command, only the name is send */
1822 char *command = ecpg_prepared(stmt->command, con);
1826 stmt->name = stmt->command;
1827 stmt->command = ecpg_strdup(command, lineno);
1831 ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, stmt->command);
1832 ecpg_do_epilogue(stmt);
1837 stmt->connection = con;
1838 stmt->lineno = lineno;
1839 stmt->compat = compat;
1840 stmt->force_indicator = force_indicator;
1841 stmt->questionmarks = questionmarks;
1842 stmt->statement_type = statement_type;
1845 * create a list of variables
1847 * The variables are listed with input variables preceding outputvariables
1848 * The end of each group is marked by an end marker. per variable we list:
1850 * type - as defined in ecpgtype.h
1851 * value - where to store the data
1852 * varcharsize - length of string in case we have a stringvariable, else 0
1853 * arraysize - 0 for pointer (we don't know the size of the array), 1 for
1854 * simple variable, size for arrays
1855 * offset - offset between ith and (i+1)th entry in an array, normally
1856 * that means sizeof(type)
1857 * ind_type - type of indicator variable
1858 * ind_value - pointer to indicator variable
1859 * ind_varcharsize - empty
1860 * ind_arraysize - arraysize of indicator array
1861 * ind_offset - indicator offset
1865 list = &(stmt->inlist);
1867 type = va_arg(args, enum ECPGttype);
1869 while (type != ECPGt_EORT)
1871 if (type == ECPGt_EOIT)
1872 list = &(stmt->outlist);
1875 struct variable *var,
1878 if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
1880 ecpg_do_epilogue(stmt);
1885 var->pointer = va_arg(args, char *);
1887 var->varcharsize = va_arg(args, long);
1888 var->arrsize = va_arg(args, long);
1889 var->offset = va_arg(args, long);
1892 * Unknown array size means pointer to an array. Unknown
1893 * varcharsize usually also means pointer. But if the type is
1894 * character and the array size is known, it is an array of
1895 * pointers to char, so use var->pointer as it is.
1897 if (var->arrsize == 0 ||
1898 (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
1899 var->value = *((char **) (var->pointer));
1901 var->value = var->pointer;
1904 * negative values are used to indicate an array without given
1907 /* reset to zero for us */
1908 if (var->arrsize < 0)
1910 if (var->varcharsize < 0)
1911 var->varcharsize = 0;
1915 var->ind_type = va_arg(args, enum ECPGttype);
1916 var->ind_pointer = va_arg(args, char *);
1917 var->ind_varcharsize = va_arg(args, long);
1918 var->ind_arrsize = va_arg(args, long);
1919 var->ind_offset = va_arg(args, long);
1921 if (var->ind_type != ECPGt_NO_INDICATOR
1922 && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
1923 var->ind_value = *((char **) (var->ind_pointer));
1925 var->ind_value = var->ind_pointer;
1928 * negative values are used to indicate an array without given
1931 /* reset to zero for us */
1932 if (var->ind_arrsize < 0)
1933 var->ind_arrsize = 0;
1934 if (var->ind_varcharsize < 0)
1935 var->ind_varcharsize = 0;
1937 /* if variable is NULL, the statement hasn't been prepared */
1938 if (var->pointer == NULL)
1940 ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, NULL);
1942 ecpg_do_epilogue(stmt);
1946 for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
1955 type = va_arg(args, enum ECPGttype);
1958 /* are we connected? */
1959 if (con == NULL || con->connection == NULL)
1961 ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
1962 ecpg_do_epilogue(stmt);
1966 /* initialize auto_mem struct */
1967 ecpg_clear_auto_mem();
1976 * Restore the application locale and free the statement structure.
1979 ecpg_do_epilogue(struct statement *stmt)
1984 if (stmt->oldlocale)
1985 setlocale(LC_NUMERIC, stmt->oldlocale);
1987 free_statement(stmt);
1991 * Execute SQL statements in the backend.
1992 * The input/output parameters (variable argument list) are passed
1993 * in a va_list, so other functions can use this interface.
1996 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)
1998 struct statement *stmt = NULL;
2000 if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2001 questionmarks, (enum ECPG_statement_type) st,
2002 query, args, &stmt))
2005 if (!ecpg_build_params(stmt))
2008 if (!ecpg_autostart_transaction(stmt))
2011 if (!ecpg_execute(stmt))
2014 if (!ecpg_process_output(stmt, true))
2017 ecpg_do_epilogue(stmt);
2021 ecpg_do_epilogue(stmt);
2026 * Execute SQL statements in the backend.
2027 * The input/output parameters are passed as variable-length argument list.
2030 ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
2035 va_start(args, query);
2036 ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2037 questionmarks, st, query, args);
2043 /* old descriptor interface */
2045 ECPGdo_descriptor(int line, const char *connection,
2046 const char *descriptor, const char *query)
2048 return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2049 ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2050 ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);