PostgreSQL Source Code  git master
execute.c
Go to the documentation of this file.
1 /* src/interfaces/ecpg/ecpglib/execute.c */
2 
3 /*
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
6  * by this function.
7  */
8 /* Author: Linus Tolke
9  (actually most if the code is "borrowed" from the distribution and just
10  slightly modified)
11  */
12 
13 /* Taken over as part of PostgreSQL by Michael Meskes <meskes@postgresql.org>
14  on Feb. 5th, 1998 */
15 
16 #define POSTGRES_ECPG_INTERNAL
17 #include "postgres_fe.h"
18 
19 #include <math.h>
20 
21 #include "catalog/pg_type_d.h"
22 #include "ecpgerrno.h"
23 #include "ecpglib.h"
24 #include "ecpglib_extern.h"
25 #include "ecpgtype.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"
31 #include "sqlca.h"
32 #include "sqlda-compat.h"
33 #include "sqlda-native.h"
34 
35 /*
36  * This function returns a newly malloced string that has ' and \
37  * escaped.
38  */
39 static char *
40 quote_postgres(char *arg, bool quote, int lineno)
41 {
42  char *res;
43  size_t length;
44  size_t escaped_len;
45  size_t buffer_len;
46 
47  /*
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
50  */
51  if (!quote)
52  return arg;
53  else
54  {
55  length = strlen(arg);
56  buffer_len = 2 * length + 1;
57  res = (char *) ecpg_alloc(buffer_len + 3, lineno);
58  if (!res)
59  return res;
60  escaped_len = PQescapeString(res + 1, arg, buffer_len);
61  if (length == escaped_len)
62  {
63  res[0] = res[escaped_len + 1] = '\'';
64  res[escaped_len + 2] = '\0';
65  }
66  else
67  {
68  /*
69  * We don't know if the target database is using
70  * standard_conforming_strings, so we always use E'' strings.
71  */
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';
76  }
77  ecpg_free(arg);
78  return res;
79  }
80 }
81 
82 static void
83 free_variable(struct variable *var)
84 {
85  struct variable *var_next;
86 
87  while (var)
88  {
89  var_next = var->next;
90  ecpg_free(var);
91  var = var_next;
92  }
93 }
94 
95 static void
97 {
98  if (stmt == NULL)
99  return;
100  free_variable(stmt->inlist);
101  free_variable(stmt->outlist);
102  ecpg_free(stmt->command);
103  ecpg_free(stmt->name);
104 #ifdef HAVE_USELOCALE
105  if (stmt->clocale)
106  freelocale(stmt->clocale);
107 #else
108  ecpg_free(stmt->oldlocale);
109 #endif
110  ecpg_free(stmt);
111 }
112 
113 static int
114 next_insert(char *text, int pos, bool questionmarks, bool std_strings)
115 {
116  bool string = false;
117  int p = pos;
118 
119  for (; text[p] != '\0'; p++)
120  {
121  if (string && !std_strings && text[p] == '\\') /* escape character */
122  p++;
123  else if (text[p] == '\'')
124  string = string ? false : true;
125  else if (!string)
126  {
127  if (text[p] == '$' && isdigit((unsigned char) text[p + 1]))
128  {
129  /* this can be either a dollar quote or a variable */
130  int i;
131 
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 */
137  return p;
138  }
139  else if (questionmarks && text[p] == '?')
140  {
141  /* also allow old style placeholders */
142  return p;
143  }
144  }
145  }
146 
147  return -1;
148 }
149 
150 static bool
151 ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
152 {
153  struct ECPGtype_information_cache *new_entry
154  = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
155 
156  if (new_entry == NULL)
157  return false;
158 
159  new_entry->oid = oid;
160  new_entry->isarray = isarray;
161  new_entry->next = *cache;
162  *cache = new_entry;
163  return true;
164 }
165 
166 static enum ARRAY_TYPE
167 ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
168 {
169  char *array_query;
171  PGresult *query;
172  struct ECPGtype_information_cache *cache_entry;
173 
174  if ((stmt->connection->cache_head) == NULL)
175  {
176  /*
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.
179  */
180 #define not_an_array_in_ecpg ECPG_ARRAY_NONE
181 
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;
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;
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), MONEYOID, 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;
261  }
262 
263  for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
264  {
265  if (cache_entry->oid == type)
266  return cache_entry->isarray;
267  }
268 
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;
272 
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)
279  {
280  if (PQntuples(query) == 0)
281  isarray = ECPG_ARRAY_NONE;
282  else
283  {
284  isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
285  if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
287  {
288  /*
289  * arrays of character strings are not yet implemented
290  */
291  isarray = ECPG_ARRAY_NONE;
292  }
293  }
294  PQclear(query);
295  }
296  else
297  return ECPG_ARRAY_ERROR;
298 
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");
301  return isarray;
302 }
303 
304 
305 bool
306 ecpg_store_result(const PGresult *results, int act_field,
307  const struct statement *stmt, struct variable *var)
308 {
309  enum ARRAY_TYPE isarray;
310  int act_tuple,
311  ntuples = PQntuples(results);
312  bool status = true;
313 
314  if ((isarray = ecpg_is_type_an_array(PQftype(results, act_field), stmt, var)) == ECPG_ARRAY_ERROR)
315  {
317  return false;
318  }
319 
320  if (isarray == ECPG_ARRAY_NONE)
321  {
322  /*
323  * if we don't have enough space, we cannot read all tuples
324  */
325  if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
326  {
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);
330  return false;
331  }
332  }
333  else
334  {
335  /*
336  * since we read an array, the variable has to be an array too
337  */
338  if (var->arrsize == 0)
339  {
341  return false;
342  }
343  }
344 
345  /*
346  * allocate memory for NULL pointers
347  */
348  if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
349  {
350  int len = 0;
351 
352  if (!PQfformat(results, act_field))
353  {
354  switch (var->type)
355  {
356  case ECPGt_char:
357  case ECPGt_unsigned_char:
358  case ECPGt_string:
359  if (!var->varcharsize && !var->arrsize)
360  {
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 *);
366  }
367  else
368  {
369  var->varcharsize = 0;
370  /* check strlen for each tuple */
371  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
372  {
373  int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
374 
375  if (len > var->varcharsize)
376  var->varcharsize = len;
377  }
378  var->offset *= var->varcharsize;
379  len = var->offset * ntuples;
380  }
381  break;
382  case ECPGt_varchar:
383  len = ntuples * (var->varcharsize + sizeof(int));
384  break;
385  default:
386  len = var->offset * ntuples;
387  break;
388  }
389  }
390  else
391  {
392  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
393  len += PQgetlength(results, act_tuple, act_field);
394  }
395 
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);
398  if (!var->value)
399  return false;
400  *((char **) var->pointer) = var->value;
401  }
402 
403  /* allocate indicator variable if needed */
404  if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
405  {
406  int len = var->ind_offset * ntuples;
407 
408  var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
409  if (!var->ind_value)
410  return false;
411  *((char **) var->ind_pointer) = var->ind_value;
412  }
413 
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))
417  {
418  /* special mode for handling char**foo=0 */
419 
420  /* filling the array of (char*)s */
421  char **current_string = (char **) var->value;
422 
423  /* storing the data (after the last array element) */
424  char *current_data_location = (char *) &current_string[ntuples + 1];
425 
426  for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
427  {
428  int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
429 
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))
433  status = false;
434  else
435  {
436  *current_string = current_data_location;
437  current_data_location += len;
438  current_string++;
439  }
440  }
441 
442  /* terminate the list */
443  *current_string = NULL;
444  }
445  else
446  {
447  for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
448  {
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))
452  status = false;
453  }
454  }
455  return status;
456 }
457 
458 static void
459 sprintf_double_value(char *ptr, double value, const char *delim)
460 {
461  if (isnan(value))
462  sprintf(ptr, "%s%s", "NaN", delim);
463  else if (isinf(value))
464  {
465  if (value < 0)
466  sprintf(ptr, "%s%s", "-Infinity", delim);
467  else
468  sprintf(ptr, "%s%s", "Infinity", delim);
469  }
470  else
471  sprintf(ptr, "%.15g%s", value, delim);
472 }
473 
474 static void
475 sprintf_float_value(char *ptr, float value, const char *delim)
476 {
477  if (isnan(value))
478  sprintf(ptr, "%s%s", "NaN", delim);
479  else if (isinf(value))
480  {
481  if (value < 0)
482  sprintf(ptr, "%s%s", "-Infinity", delim);
483  else
484  sprintf(ptr, "%s%s", "Infinity", delim);
485  }
486  else
487  sprintf(ptr, "%.15g%s", value, delim);
488 }
489 
490 static char *
491 convert_bytea_to_string(char *from_data, int from_len, int lineno)
492 {
493  char *to_data;
494  int to_len = ecpg_hex_enc_len(from_len) + 4 + 1; /* backslash + 'x' +
495  * quote + quote */
496 
497  to_data = ecpg_alloc(to_len, lineno);
498  if (!to_data)
499  return NULL;
500 
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), "\'");
504 
505  return to_data;
506 }
507 
508 bool
509 ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var,
510  char **tobeinserted_p, bool quote)
511 {
512  char *mallocedval = NULL;
513  char *newcopy = NULL;
514 
515  /*
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
519  */
520 
521  /*
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).
524  */
525 
526  *tobeinserted_p = "";
527 
528  /* check for null value and set input buffer accordingly */
529  switch (var->ind_type)
530  {
531  case ECPGt_short:
533  if (*(short *) var->ind_value < 0)
534  *tobeinserted_p = NULL;
535  break;
536  case ECPGt_int:
537  case ECPGt_unsigned_int:
538  if (*(int *) var->ind_value < 0)
539  *tobeinserted_p = NULL;
540  break;
541  case ECPGt_long:
542  case ECPGt_unsigned_long:
543  if (*(long *) var->ind_value < 0L)
544  *tobeinserted_p = NULL;
545  break;
546  case ECPGt_long_long:
548  if (*(long long int *) var->ind_value < (long long) 0)
549  *tobeinserted_p = NULL;
550  break;
551  case ECPGt_NO_INDICATOR:
552  if (force_indicator == false)
553  {
554  if (ECPGis_noind_null(var->type, var->value))
555  *tobeinserted_p = NULL;
556  }
557  break;
558  default:
559  break;
560  }
561  if (*tobeinserted_p != NULL)
562  {
563  int asize = var->arrsize ? var->arrsize : 1;
564 
565  switch (var->type)
566  {
567  int element;
568 
569  case ECPGt_short:
570  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
571  return false;
572 
573  if (asize > 1)
574  {
575  strcpy(mallocedval, "{");
576 
577  for (element = 0; element < asize; element++)
578  sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
579 
580  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
581  }
582  else
583  sprintf(mallocedval, "%hd", *((short *) var->value));
584 
585  *tobeinserted_p = mallocedval;
586  break;
587 
588  case ECPGt_int:
589  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
590  return false;
591 
592  if (asize > 1)
593  {
594  strcpy(mallocedval, "{");
595 
596  for (element = 0; element < asize; element++)
597  sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
598 
599  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
600  }
601  else
602  sprintf(mallocedval, "%d", *((int *) var->value));
603 
604  *tobeinserted_p = mallocedval;
605  break;
606 
608  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
609  return false;
610 
611  if (asize > 1)
612  {
613  strcpy(mallocedval, "{");
614 
615  for (element = 0; element < asize; element++)
616  sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
617 
618  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
619  }
620  else
621  sprintf(mallocedval, "%hu", *((unsigned short *) var->value));
622 
623  *tobeinserted_p = mallocedval;
624  break;
625 
626  case ECPGt_unsigned_int:
627  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
628  return false;
629 
630  if (asize > 1)
631  {
632  strcpy(mallocedval, "{");
633 
634  for (element = 0; element < asize; element++)
635  sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
636 
637  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
638  }
639  else
640  sprintf(mallocedval, "%u", *((unsigned int *) var->value));
641 
642  *tobeinserted_p = mallocedval;
643  break;
644 
645  case ECPGt_long:
646  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
647  return false;
648 
649  if (asize > 1)
650  {
651  strcpy(mallocedval, "{");
652 
653  for (element = 0; element < asize; element++)
654  sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
655 
656  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
657  }
658  else
659  sprintf(mallocedval, "%ld", *((long *) var->value));
660 
661  *tobeinserted_p = mallocedval;
662  break;
663 
664  case ECPGt_unsigned_long:
665  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
666  return false;
667 
668  if (asize > 1)
669  {
670  strcpy(mallocedval, "{");
671 
672  for (element = 0; element < asize; element++)
673  sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
674 
675  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
676  }
677  else
678  sprintf(mallocedval, "%lu", *((unsigned long *) var->value));
679 
680  *tobeinserted_p = mallocedval;
681  break;
682 
683  case ECPGt_long_long:
684  if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
685  return false;
686 
687  if (asize > 1)
688  {
689  strcpy(mallocedval, "{");
690 
691  for (element = 0; element < asize; element++)
692  sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
693 
694  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
695  }
696  else
697  sprintf(mallocedval, "%lld", *((long long int *) var->value));
698 
699  *tobeinserted_p = mallocedval;
700  break;
701 
703  if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
704  return false;
705 
706  if (asize > 1)
707  {
708  strcpy(mallocedval, "{");
709 
710  for (element = 0; element < asize; element++)
711  sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
712 
713  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
714  }
715  else
716  sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
717 
718  *tobeinserted_p = mallocedval;
719  break;
720 
721  case ECPGt_float:
722  if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
723  return false;
724 
725  if (asize > 1)
726  {
727  strcpy(mallocedval, "{");
728 
729  for (element = 0; element < asize; element++)
730  sprintf_float_value(mallocedval + strlen(mallocedval), ((float *) var->value)[element], ",");
731 
732  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
733  }
734  else
735  sprintf_float_value(mallocedval, *((float *) var->value), "");
736 
737  *tobeinserted_p = mallocedval;
738  break;
739 
740  case ECPGt_double:
741  if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
742  return false;
743 
744  if (asize > 1)
745  {
746  strcpy(mallocedval, "{");
747 
748  for (element = 0; element < asize; element++)
749  sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
750 
751  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
752  }
753  else
754  sprintf_double_value(mallocedval, *((double *) var->value), "");
755 
756  *tobeinserted_p = mallocedval;
757  break;
758 
759  case ECPGt_bool:
760  if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
761  return false;
762 
763  if (var->arrsize > 1)
764  {
765  strcpy(mallocedval, "{");
766 
767  for (element = 0; element < asize; element++)
768  sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
769 
770  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
771  }
772  else
773  {
774  if (var->offset == sizeof(char))
775  sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
776  else if (var->offset == sizeof(int))
777  sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
778  else
780  }
781 
782  *tobeinserted_p = mallocedval;
783  break;
784 
785  case ECPGt_char:
786  case ECPGt_unsigned_char:
787  case ECPGt_string:
788  {
789  /* set slen to string length if type is char * */
790  int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
791 
792  if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
793  return false;
794 
795  strncpy(newcopy, (char *) var->value, slen);
796  newcopy[slen] = '\0';
797 
798  mallocedval = quote_postgres(newcopy, quote, lineno);
799  if (!mallocedval)
800  {
801  ecpg_free(newcopy);
802  return false;
803  }
804 
805  *tobeinserted_p = mallocedval;
806  }
807  break;
808  case ECPGt_const:
809  case ECPGt_char_variable:
810  {
811  int slen = strlen((char *) var->value);
812 
813  if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
814  return false;
815 
816  strncpy(mallocedval, (char *) var->value, slen);
817  mallocedval[slen] = '\0';
818 
819  *tobeinserted_p = mallocedval;
820  }
821  break;
822 
823  case ECPGt_bytea:
824  {
825  struct ECPGgeneric_bytea *variable =
826  (struct ECPGgeneric_bytea *) (var->value);
827 
828  if (!(mallocedval = (char *) ecpg_alloc(variable->len, lineno)))
829  return false;
830 
831  memcpy(mallocedval, variable->arr, variable->len);
832  *tobeinserted_p = mallocedval;
833  }
834  break;
835 
836  case ECPGt_varchar:
837  {
838  struct ECPGgeneric_varchar *variable =
839  (struct ECPGgeneric_varchar *) (var->value);
840 
841  if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, lineno)))
842  return false;
843 
844  strncpy(newcopy, variable->arr, variable->len);
845  newcopy[variable->len] = '\0';
846 
847  mallocedval = quote_postgres(newcopy, quote, lineno);
848  if (!mallocedval)
849  {
850  ecpg_free(newcopy);
851  return false;
852  }
853 
854  *tobeinserted_p = mallocedval;
855  }
856  break;
857 
858  case ECPGt_decimal:
859  case ECPGt_numeric:
860  {
861  char *str = NULL;
862  int slen;
863  numeric *nval;
864 
865  if (var->arrsize > 1)
866  mallocedval = ecpg_strdup("{", lineno);
867  else
868  mallocedval = ecpg_strdup("", lineno);
869 
870  if (!mallocedval)
871  return false;
872 
873  for (element = 0; element < asize; element++)
874  {
875  int result;
876 
877  nval = PGTYPESnumeric_new();
878  if (!nval)
879  {
880  ecpg_free(mallocedval);
881  return false;
882  }
883 
884  if (var->type == ECPGt_numeric)
885  result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
886  else
887  result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
888 
889  if (result != 0)
890  {
891  PGTYPESnumeric_free(nval);
892  ecpg_free(mallocedval);
893  return false;
894  }
895 
896  str = PGTYPESnumeric_to_asc(nval, nval->dscale);
897  slen = strlen(str);
898  PGTYPESnumeric_free(nval);
899 
900  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
901  {
902  ecpg_free(mallocedval);
903  ecpg_free(str);
904  return false;
905  }
906  mallocedval = newcopy;
907 
908  /* also copy trailing '\0' */
909  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
910  if (var->arrsize > 1)
911  strcpy(mallocedval + strlen(mallocedval), ",");
912 
913  ecpg_free(str);
914  }
915 
916  if (var->arrsize > 1)
917  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
918 
919  *tobeinserted_p = mallocedval;
920  }
921  break;
922 
923  case ECPGt_interval:
924  {
925  char *str = NULL;
926  int slen;
927 
928  if (var->arrsize > 1)
929  mallocedval = ecpg_strdup("{", lineno);
930  else
931  mallocedval = ecpg_strdup("", lineno);
932 
933  if (!mallocedval)
934  return false;
935 
936  for (element = 0; element < asize; element++)
937  {
938  str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
939  if (!str)
940  {
941  ecpg_free(mallocedval);
942  return false;
943  }
944 
945  slen = strlen(str);
946 
947  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
948  {
949  ecpg_free(mallocedval);
950  ecpg_free(str);
951  return false;
952  }
953  mallocedval = newcopy;
954 
955  /* also copy trailing '\0' */
956  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
957  if (var->arrsize > 1)
958  strcpy(mallocedval + strlen(mallocedval), ",");
959 
960  ecpg_free(str);
961  }
962 
963  if (var->arrsize > 1)
964  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
965 
966  *tobeinserted_p = mallocedval;
967  }
968  break;
969 
970  case ECPGt_date:
971  {
972  char *str = NULL;
973  int slen;
974 
975  if (var->arrsize > 1)
976  mallocedval = ecpg_strdup("{", lineno);
977  else
978  mallocedval = ecpg_strdup("", lineno);
979 
980  if (!mallocedval)
981  return false;
982 
983  for (element = 0; element < asize; element++)
984  {
985  str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
986  if (!str)
987  {
988  ecpg_free(mallocedval);
989  return false;
990  }
991 
992  slen = strlen(str);
993 
994  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
995  {
996  ecpg_free(mallocedval);
997  ecpg_free(str);
998  return false;
999  }
1000  mallocedval = newcopy;
1001 
1002  /* also copy trailing '\0' */
1003  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1004  if (var->arrsize > 1)
1005  strcpy(mallocedval + strlen(mallocedval), ",");
1006 
1007  ecpg_free(str);
1008  }
1009 
1010  if (var->arrsize > 1)
1011  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1012 
1013  *tobeinserted_p = mallocedval;
1014  }
1015  break;
1016 
1017  case ECPGt_timestamp:
1018  {
1019  char *str = NULL;
1020  int slen;
1021 
1022  if (var->arrsize > 1)
1023  mallocedval = ecpg_strdup("{", lineno);
1024  else
1025  mallocedval = ecpg_strdup("", lineno);
1026 
1027  if (!mallocedval)
1028  return false;
1029 
1030  for (element = 0; element < asize; element++)
1031  {
1032  str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1033  if (!str)
1034  {
1035  ecpg_free(mallocedval);
1036  return false;
1037  }
1038 
1039  slen = strlen(str);
1040 
1041  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1042  {
1043  ecpg_free(mallocedval);
1044  ecpg_free(str);
1045  return false;
1046  }
1047  mallocedval = newcopy;
1048 
1049  /* also copy trailing '\0' */
1050  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1051  if (var->arrsize > 1)
1052  strcpy(mallocedval + strlen(mallocedval), ",");
1053 
1054  ecpg_free(str);
1055  }
1056 
1057  if (var->arrsize > 1)
1058  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1059 
1060  *tobeinserted_p = mallocedval;
1061  }
1062  break;
1063 
1064  case ECPGt_descriptor:
1065  case ECPGt_sqlda:
1066  break;
1067 
1068  default:
1069  /* Not implemented yet */
1071  return false;
1072  break;
1073  }
1074  }
1075  return true;
1076 }
1077 
1078 static void
1079 print_param_value(char *value, int len, int is_binary, int lineno, int nth)
1080 {
1081  char *value_s;
1082  bool malloced = false;
1083 
1084  if (value == NULL)
1085  value_s = "null";
1086  else if (!is_binary)
1087  value_s = value;
1088  else
1089  {
1090  value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1091  if (value_s != NULL)
1092  {
1093  ecpg_hex_encode(value, len, value_s);
1094  value_s[ecpg_hex_enc_len(len)] = '\0';
1095  malloced = true;
1096  }
1097  else
1098  value_s = "no memory for logging of parameter";
1099  }
1100 
1101  ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1102  lineno, nth, value_s);
1103 
1104  if (malloced)
1105  ecpg_free(value_s);
1106 }
1107 
1108 void
1109 ecpg_free_params(struct statement *stmt, bool print)
1110 {
1111  int n;
1112 
1113  for (n = 0; n < stmt->nparams; n++)
1114  {
1115  if (print)
1116  print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1117  stmt->paramformats[n], stmt->lineno, n + 1);
1118  ecpg_free(stmt->paramvalues[n]);
1119  }
1120  ecpg_free(stmt->paramvalues);
1121  ecpg_free(stmt->paramlengths);
1122  ecpg_free(stmt->paramformats);
1123  stmt->paramvalues = NULL;
1124  stmt->paramlengths = NULL;
1125  stmt->paramformats = NULL;
1126  stmt->nparams = 0;
1127 }
1128 
1129 static bool
1130 insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
1131 {
1132  char *newcopy;
1133 
1134  if (!(newcopy = (char *) ecpg_alloc(strlen(stmt->command)
1135  + strlen(tobeinserted)
1136  + 1, stmt->lineno)))
1137  {
1138  ecpg_free(tobeinserted);
1139  return false;
1140  }
1141 
1142  strcpy(newcopy, stmt->command);
1143  strcpy(newcopy + position - 1, tobeinserted);
1144 
1145  /*
1146  * The strange thing in the second argument is the rest of the string from
1147  * the old string
1148  */
1149  strcat(newcopy,
1150  stmt->command
1151  + position
1152  + ph_len - 1);
1153 
1154  ecpg_free(stmt->command);
1155  stmt->command = newcopy;
1156 
1157  ecpg_free(tobeinserted);
1158  return true;
1159 }
1160 
1161 static bool
1162 store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item,
1163  char **tobeinserted)
1164 {
1165  struct variable var;
1166 
1167  /*
1168  * In case of binary data, only allocate memory and memcpy because binary
1169  * data have been already stored into desc_item->data with
1170  * ecpg_store_input() at ECPGset_desc().
1171  */
1172  if (desc_item->is_binary)
1173  {
1174  if (!(*tobeinserted = ecpg_alloc(desc_item->data_len, stmt->lineno)))
1175  return false;
1176  memcpy(*tobeinserted, desc_item->data, desc_item->data_len);
1177  return true;
1178  }
1179 
1180  var.type = ECPGt_char;
1181  var.varcharsize = strlen(desc_item->data);
1182  var.value = desc_item->data;
1183  var.pointer = &(desc_item->data);
1184  var.arrsize = 1;
1185  var.offset = 0;
1186 
1187  if (!desc_item->indicator)
1188  {
1190  var.ind_value = var.ind_pointer = NULL;
1191  var.ind_varcharsize = var.ind_arrsize = var.ind_offset = 0;
1192  }
1193  else
1194  {
1195  var.ind_type = ECPGt_int;
1196  var.ind_value = &(desc_item->indicator);
1197  var.ind_pointer = &(var.ind_value);
1198  var.ind_varcharsize = var.ind_arrsize = 1;
1199  var.ind_offset = 0;
1200  }
1201 
1202  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &var, tobeinserted, false))
1203  return false;
1204 
1205  return true;
1206 }
1207 
1208 /*
1209  * ecpg_build_params
1210  * Build statement parameters
1211  *
1212  * The input values are taken from user variables, and the results are stored
1213  * in arrays which can be used by PQexecParams().
1214  */
1215 bool
1217 {
1218  struct variable *var;
1219  int desc_counter = 0;
1220  int position = 0;
1221  const char *value;
1222  bool std_strings = false;
1223 
1224  /* Get standard_conforming_strings setting. */
1225  value = PQparameterStatus(stmt->connection->connection, "standard_conforming_strings");
1226  if (value && strcmp(value, "on") == 0)
1227  std_strings = true;
1228 
1229  /*
1230  * If the type is one of the fill in types then we take the argument and
1231  * enter it to our parameter array at the first position. Then if there
1232  * are any more fill in types we add more parameters.
1233  */
1234  var = stmt->inlist;
1235  while (var)
1236  {
1237  char *tobeinserted;
1238  int counter = 1;
1239  bool binary_format;
1240  int binary_length;
1241 
1242 
1243  tobeinserted = NULL;
1244  binary_length = 0;
1245  binary_format = false;
1246 
1247  /*
1248  * A descriptor is a special case since it contains many variables but
1249  * is listed only once.
1250  */
1251  if (var->type == ECPGt_descriptor)
1252  {
1253  /*
1254  * We create an additional variable list here, so the same logic
1255  * applies.
1256  */
1257  struct descriptor *desc;
1258  struct descriptor_item *desc_item;
1259 
1260  desc = ecpg_find_desc(stmt->lineno, var->pointer);
1261  if (desc == NULL)
1262  return false;
1263 
1264  desc_counter++;
1265  for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1266  {
1267  if (desc_item->num != desc_counter)
1268  continue;
1269 
1270  if (!store_input_from_desc(stmt, desc_item, &tobeinserted))
1271  return false;
1272 
1273  if (desc_item->is_binary)
1274  {
1275  binary_length = desc_item->data_len;
1276  binary_format = true;
1277  }
1278  break;
1279  }
1280  if (desc->count == desc_counter)
1281  desc_counter = 0;
1282  }
1283  else if (var->type == ECPGt_sqlda)
1284  {
1285  if (INFORMIX_MODE(stmt->compat))
1286  {
1287  struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1288  struct variable desc_inlist;
1289  int i;
1290 
1291  if (sqlda == NULL)
1292  return false;
1293 
1294  desc_counter++;
1295  for (i = 0; i < sqlda->sqld; i++)
1296  {
1297  if (i + 1 == desc_counter)
1298  {
1299  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1300  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1301  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1302  switch (desc_inlist.type)
1303  {
1304  case ECPGt_char:
1305  case ECPGt_varchar:
1306  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1307  break;
1308  default:
1309  desc_inlist.varcharsize = 0;
1310  break;
1311  }
1312  desc_inlist.arrsize = 1;
1313  desc_inlist.offset = 0;
1314  if (sqlda->sqlvar[i].sqlind)
1315  {
1316  desc_inlist.ind_type = ECPGt_short;
1317  /* ECPG expects indicator value < 0 */
1318  if (*(sqlda->sqlvar[i].sqlind))
1319  *(sqlda->sqlvar[i].sqlind) = -1;
1320  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1321  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1322  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1323  desc_inlist.ind_offset = 0;
1324  }
1325  else
1326  {
1327  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1328  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1329  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1330  }
1331  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1332  return false;
1333 
1334  break;
1335  }
1336  }
1337  if (sqlda->sqld == desc_counter)
1338  desc_counter = 0;
1339  }
1340  else
1341  {
1342  struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1343  struct variable desc_inlist;
1344  int i;
1345 
1346  if (sqlda == NULL)
1347  return false;
1348 
1349  desc_counter++;
1350  for (i = 0; i < sqlda->sqln; i++)
1351  {
1352  if (i + 1 == desc_counter)
1353  {
1354  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1355  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1356  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1357  switch (desc_inlist.type)
1358  {
1359  case ECPGt_char:
1360  case ECPGt_varchar:
1361  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1362  break;
1363  default:
1364  desc_inlist.varcharsize = 0;
1365  break;
1366  }
1367  desc_inlist.arrsize = 1;
1368  desc_inlist.offset = 0;
1369  if (sqlda->sqlvar[i].sqlind)
1370  {
1371  desc_inlist.ind_type = ECPGt_short;
1372  /* ECPG expects indicator value < 0 */
1373  if (*(sqlda->sqlvar[i].sqlind))
1374  *(sqlda->sqlvar[i].sqlind) = -1;
1375  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1376  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1377  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1378  desc_inlist.ind_offset = 0;
1379  }
1380  else
1381  {
1382  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1383  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1384  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1385  }
1386  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1387  return false;
1388 
1389  break;
1390  }
1391  }
1392  if (sqlda->sqln == desc_counter)
1393  desc_counter = 0;
1394  }
1395 
1396  }
1397  else
1398  {
1399  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1400  return false;
1401 
1402  if (var->type == ECPGt_bytea)
1403  {
1404  binary_length = ((struct ECPGgeneric_bytea *) (var->value))->len;
1405  binary_format = true;
1406  }
1407  }
1408 
1409  /*
1410  * now tobeinserted points to an area that contains the next
1411  * parameter; now find the position in the string where it belongs
1412  */
1413  if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1414  {
1415  /*
1416  * We have an argument but we don't have the matched up
1417  * placeholder in the string
1418  */
1421  NULL);
1422  ecpg_free_params(stmt, false);
1423  ecpg_free(tobeinserted);
1424  return false;
1425  }
1426 
1427  /*
1428  * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1429  * to simulate a dynamic cursor because there is no backend
1430  * functionality for it
1431  */
1432  if (var->type == ECPGt_char_variable)
1433  {
1434  int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1435 
1436  if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1437  {
1438  ecpg_free_params(stmt, false);
1439  return false;
1440  }
1441  tobeinserted = NULL;
1442  }
1443 
1444  /*
1445  * if the placeholder is '$0' we have to replace it on the client side
1446  * this is for places we want to support variables at that are not
1447  * supported in the backend
1448  */
1449  else if (stmt->command[position] == '0')
1450  {
1451  if (stmt->statement_type == ECPGst_prepare ||
1453  {
1454  /* Need to double-quote the inserted statement name. */
1455  char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1456  stmt->lineno);
1457 
1458  if (!str)
1459  {
1460  ecpg_free(tobeinserted);
1461  ecpg_free_params(stmt, false);
1462  return false;
1463  }
1464  sprintf(str, "\"%s\"", tobeinserted);
1465  ecpg_free(tobeinserted);
1466  tobeinserted = str;
1467  }
1468 
1469  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1470  {
1471  ecpg_free_params(stmt, false);
1472  return false;
1473  }
1474  tobeinserted = NULL;
1475  }
1476  else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1477  {
1478  if (binary_format)
1479  {
1480  char *p = convert_bytea_to_string(tobeinserted,
1481  binary_length,
1482  stmt->lineno);
1483 
1484  ecpg_free(tobeinserted);
1485  if (!p)
1486  {
1487  ecpg_free_params(stmt, false);
1488  return false;
1489  }
1490  tobeinserted = p;
1491  }
1492 
1493  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1494  {
1495  ecpg_free_params(stmt, false);
1496  return false;
1497  }
1498  tobeinserted = NULL;
1499  }
1500  else
1501  {
1502  bool realloc_failed = false;
1503  char **newparamvalues;
1504  int *newparamlengths;
1505  int *newparamformats;
1506 
1507  /* enlarge all the param arrays */
1508  if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1509  stmt->paramvalues = newparamvalues;
1510  else
1511  realloc_failed = true;
1512 
1513  if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1514  stmt->paramlengths = newparamlengths;
1515  else
1516  realloc_failed = true;
1517 
1518  if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1519  stmt->paramformats = newparamformats;
1520  else
1521  realloc_failed = true;
1522 
1523  if (realloc_failed)
1524  {
1525  ecpg_free_params(stmt, false);
1526  ecpg_free(tobeinserted);
1527  return false;
1528  }
1529 
1530  /* only now can we assign ownership of "tobeinserted" to stmt */
1531  stmt->paramvalues[stmt->nparams] = tobeinserted;
1532  stmt->paramlengths[stmt->nparams] = binary_length;
1533  stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1534  stmt->nparams++;
1535 
1536  /* let's see if this was an old style placeholder */
1537  if (stmt->command[position] == '?')
1538  {
1539  /* yes, replace with new style */
1540  int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1541  * size we need */
1542 
1543  if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1544  {
1545  ecpg_free_params(stmt, false);
1546  return false;
1547  }
1548 
1549  snprintf(tobeinserted, buffersize, "$%d", counter++);
1550 
1551  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1552  {
1553  ecpg_free_params(stmt, false);
1554  return false;
1555  }
1556  tobeinserted = NULL;
1557  }
1558  }
1559 
1560  if (desc_counter == 0)
1561  var = var->next;
1562  }
1563 
1564  /*
1565  * Check if there are unmatched things left. PREPARE AS has no parameter.
1566  * Check other statement.
1567  */
1568  if (stmt->statement_type != ECPGst_prepare &&
1569  next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1570  {
1573  ecpg_free_params(stmt, false);
1574  return false;
1575  }
1576 
1577  return true;
1578 }
1579 
1580 /*
1581  * ecpg_autostart_transaction
1582  * If we are in non-autocommit mode, automatically start a transaction.
1583  */
1584 bool
1586 {
1588  {
1589  stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1590  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1591  {
1592  ecpg_free_params(stmt, false);
1593  return false;
1594  }
1595  PQclear(stmt->results);
1596  stmt->results = NULL;
1597  }
1598  return true;
1599 }
1600 
1601 /*
1602  * ecpg_execute
1603  * Execute the SQL statement.
1604  */
1605 bool
1607 {
1608  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);
1609  if (stmt->statement_type == ECPGst_execute)
1610  {
1612  stmt->name,
1613  stmt->nparams,
1614  (const char *const *) stmt->paramvalues,
1615  (const int *) stmt->paramlengths,
1616  (const int *) stmt->paramformats,
1617  0);
1618  ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1619  }
1620  else
1621  {
1622  if (stmt->nparams == 0)
1623  {
1624  stmt->results = PQexec(stmt->connection->connection, stmt->command);
1625  ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1626  }
1627  else
1628  {
1629  stmt->results = PQexecParams(stmt->connection->connection,
1630  stmt->command, stmt->nparams, NULL,
1631  (const char *const *) stmt->paramvalues,
1632  (const int *) stmt->paramlengths,
1633  (const int *) stmt->paramformats,
1634  0);
1635 
1636  ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1637  }
1638 
1639  if (stmt->statement_type == ECPGst_prepare)
1640  {
1641  if (!ecpg_register_prepared_stmt(stmt))
1642  {
1643  ecpg_free_params(stmt, true);
1644  return false;
1645  }
1646  }
1647  }
1648 
1649  ecpg_free_params(stmt, true);
1650 
1651  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1652  return false;
1653 
1654  return true;
1655 }
1656 
1657 /*-------
1658  * ecpg_process_output
1659  *
1660  * Process the statement result and store it into application variables. This
1661  * function can be called repeatedly during the same statement in case cursor
1662  * readahead is used and the application does FETCH N which overflows the
1663  * readahead window.
1664  *
1665  * Parameters
1666  * stmt statement structure holding the PGresult and
1667  * the list of output variables
1668  * clear_result
1669  * PQclear() the result upon returning from this function
1670  *
1671  * Returns success as boolean. Also an SQL error is raised in case of failure.
1672  *-------
1673  */
1674 bool
1675 ecpg_process_output(struct statement *stmt, bool clear_result)
1676 {
1677  struct variable *var;
1678  bool status = false;
1679  char *cmdstat;
1680  PGnotify *notify;
1681  struct sqlca_t *sqlca = ECPGget_sqlca();
1682  int nfields,
1683  ntuples,
1684  act_field;
1685 
1686  if (sqlca == NULL)
1687  {
1690  return false;
1691  }
1692 
1693  var = stmt->outlist;
1694  switch (PQresultStatus(stmt->results))
1695  {
1696  case PGRES_TUPLES_OK:
1697  nfields = PQnfields(stmt->results);
1698  sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1699 
1700  ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1701  status = true;
1702 
1703  if (ntuples < 1)
1704  {
1705  if (ntuples)
1706  ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1707  stmt->lineno, ntuples);
1709  status = false;
1710  break;
1711  }
1712 
1713  if (var != NULL && var->type == ECPGt_descriptor)
1714  {
1715  struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1716 
1717  if (desc == NULL)
1718  status = false;
1719  else
1720  {
1721  if (desc->result)
1722  PQclear(desc->result);
1723  desc->result = stmt->results;
1724  clear_result = false;
1725  ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1726  stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1727  }
1728  var = var->next;
1729  }
1730  else if (var != NULL && var->type == ECPGt_sqlda)
1731  {
1732  if (INFORMIX_MODE(stmt->compat))
1733  {
1734  struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1735  struct sqlda_compat *sqlda = *_sqlda;
1736  struct sqlda_compat *sqlda_new;
1737  int i;
1738 
1739  /*
1740  * If we are passed in a previously existing sqlda (chain)
1741  * then free it.
1742  */
1743  while (sqlda)
1744  {
1745  sqlda_new = sqlda->desc_next;
1746  free(sqlda);
1747  sqlda = sqlda_new;
1748  }
1749  *_sqlda = sqlda = sqlda_new = NULL;
1750  for (i = ntuples - 1; i >= 0; i--)
1751  {
1752  /*
1753  * Build a new sqlda structure. Note that only
1754  * fetching 1 record is supported
1755  */
1756  sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1757 
1758  if (!sqlda_new)
1759  {
1760  /* cleanup all SQLDAs we created up */
1761  while (sqlda)
1762  {
1763  sqlda_new = sqlda->desc_next;
1764  free(sqlda);
1765  sqlda = sqlda_new;
1766  }
1767  *_sqlda = NULL;
1768 
1769  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1770  status = false;
1771  break;
1772  }
1773  else
1774  {
1775  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1776 
1777  *_sqlda = sqlda_new;
1778 
1779  ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1780  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1781  stmt->lineno, PQnfields(stmt->results));
1782 
1783  sqlda_new->desc_next = sqlda;
1784  sqlda = sqlda_new;
1785  }
1786  }
1787  }
1788  else
1789  {
1790  struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1791  struct sqlda_struct *sqlda = *_sqlda;
1792  struct sqlda_struct *sqlda_new;
1793  int i;
1794 
1795  /*
1796  * If we are passed in a previously existing sqlda (chain)
1797  * then free it.
1798  */
1799  while (sqlda)
1800  {
1801  sqlda_new = sqlda->desc_next;
1802  free(sqlda);
1803  sqlda = sqlda_new;
1804  }
1805  *_sqlda = sqlda = sqlda_new = NULL;
1806  for (i = ntuples - 1; i >= 0; i--)
1807  {
1808  /*
1809  * Build a new sqlda structure. Note that only
1810  * fetching 1 record is supported
1811  */
1812  sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1813 
1814  if (!sqlda_new)
1815  {
1816  /* cleanup all SQLDAs we created up */
1817  while (sqlda)
1818  {
1819  sqlda_new = sqlda->desc_next;
1820  free(sqlda);
1821  sqlda = sqlda_new;
1822  }
1823  *_sqlda = NULL;
1824 
1825  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1826  status = false;
1827  break;
1828  }
1829  else
1830  {
1831  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1832 
1833  *_sqlda = sqlda_new;
1834 
1835  ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1836  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1837  stmt->lineno, PQnfields(stmt->results));
1838 
1839  sqlda_new->desc_next = sqlda;
1840  sqlda = sqlda_new;
1841  }
1842  }
1843  }
1844 
1845  var = var->next;
1846  }
1847  else
1848  for (act_field = 0; act_field < nfields && status; act_field++)
1849  {
1850  if (var != NULL)
1851  {
1852  status = ecpg_store_result(stmt->results, act_field, stmt, var);
1853  var = var->next;
1854  }
1855  else if (!INFORMIX_MODE(stmt->compat))
1856  {
1858  return false;
1859  }
1860  }
1861 
1862  if (status && var != NULL)
1863  {
1865  status = false;
1866  }
1867 
1868  break;
1869  case PGRES_COMMAND_OK:
1870  status = true;
1871  cmdstat = PQcmdStatus(stmt->results);
1872  sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1873  sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1874  ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1875  if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1876  !sqlca->sqlerrd[2] &&
1877  (strncmp(cmdstat, "UPDATE", 6) == 0
1878  || strncmp(cmdstat, "INSERT", 6) == 0
1879  || strncmp(cmdstat, "DELETE", 6) == 0))
1881  break;
1882  case PGRES_COPY_OUT:
1883  {
1884  char *buffer;
1885  int res;
1886 
1887  ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1888  while ((res = PQgetCopyData(stmt->connection->connection,
1889  &buffer, 0)) > 0)
1890  {
1891  printf("%s", buffer);
1892  PQfreemem(buffer);
1893  }
1894  if (res == -1)
1895  {
1896  /* COPY done */
1897  PQclear(stmt->results);
1898  stmt->results = PQgetResult(stmt->connection->connection);
1899  if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1900  ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1901  else
1902  ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1903  }
1904  break;
1905  }
1906  default:
1907 
1908  /*
1909  * execution should never reach this code because it is already
1910  * handled in ecpg_check_PQresult()
1911  */
1912  ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1913  stmt->lineno);
1914  ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1915  status = false;
1916  break;
1917  }
1918 
1919  if (clear_result)
1920  {
1921  PQclear(stmt->results);
1922  stmt->results = NULL;
1923  }
1924 
1925  /* check for asynchronous returns */
1927  while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1928  {
1929  ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1930  stmt->lineno, notify->relname, notify->be_pid);
1931  PQfreemem(notify);
1933  }
1934 
1935  return status;
1936 }
1937 
1938 /*
1939  * ecpg_do_prologue
1940  *
1941  * Initialize various infrastructure elements for executing the statement:
1942  *
1943  * - create the statement structure
1944  * - set the C numeric locale for communicating with the backend
1945  * - preprocess the variable list of input/output parameters into
1946  * linked lists
1947  */
1948 bool
1949 ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
1950  const char *connection_name, const bool questionmarks,
1951  enum ECPG_statement_type statement_type, const char *query,
1952  va_list args, struct statement **stmt_out)
1953 {
1954  struct statement *stmt = NULL;
1955  struct connection *con;
1956  enum ECPGttype type;
1957  struct variable **list;
1958  char *prepname;
1959  bool is_prepared_name_set;
1960 
1961  *stmt_out = NULL;
1962 
1963  if (!query)
1964  {
1966  return false;
1967  }
1968 
1969  stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1970 
1971  if (stmt == NULL)
1972  return false;
1973 
1974  /*
1975  * Make sure we do NOT honor the locale for numeric input/output since the
1976  * database wants the standard decimal point. If available, use
1977  * uselocale() for this because it's thread-safe. Windows doesn't have
1978  * that, but it usually does have _configthreadlocale(). In some versions
1979  * of MinGW, _configthreadlocale() exists but always returns -1 --- so
1980  * treat that situation as if the function doesn't exist.
1981  */
1982 #ifdef HAVE_USELOCALE
1983  stmt->clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
1984  if (stmt->clocale == (locale_t) 0)
1985  {
1986  ecpg_do_epilogue(stmt);
1987  return false;
1988  }
1989  stmt->oldlocale = uselocale(stmt->clocale);
1990  if (stmt->oldlocale == (locale_t) 0)
1991  {
1992  ecpg_do_epilogue(stmt);
1993  return false;
1994  }
1995 #else
1996 #ifdef HAVE__CONFIGTHREADLOCALE
1997  stmt->oldthreadlocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
1998 #endif
1999  stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
2000  if (stmt->oldlocale == NULL)
2001  {
2002  ecpg_do_epilogue(stmt);
2003  return false;
2004  }
2005  setlocale(LC_NUMERIC, "C");
2006 #endif
2007 
2008 #ifdef ENABLE_THREAD_SAFETY
2009  ecpg_pthreads_init();
2010 #endif
2011 
2012  con = ecpg_get_connection(connection_name);
2013 
2014  if (!ecpg_init(con, connection_name, lineno))
2015  {
2016  ecpg_do_epilogue(stmt);
2017  return false;
2018  }
2019 
2020  /*
2021  * If statement type is ECPGst_prepnormal we are supposed to prepare the
2022  * statement before executing them
2023  */
2024  if (statement_type == ECPGst_prepnormal)
2025  {
2026  if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2027  {
2028  ecpg_do_epilogue(stmt);
2029  return false;
2030  }
2031 
2032  /*
2033  * statement is now prepared, so instead of the query we have to
2034  * execute the name
2035  */
2036  stmt->command = prepname;
2037  statement_type = ECPGst_execute;
2038  }
2039  else
2040  stmt->command = ecpg_strdup(query, lineno);
2041 
2042  stmt->name = NULL;
2043 
2044  if (statement_type == ECPGst_execute)
2045  {
2046  /* if we have an EXECUTE command, only the name is send */
2047  char *command = ecpg_prepared(stmt->command, con);
2048 
2049  if (command)
2050  {
2051  stmt->name = stmt->command;
2052  stmt->command = ecpg_strdup(command, lineno);
2053  }
2054  else
2055  {
2057  ecpg_do_epilogue(stmt);
2058  return false;
2059  }
2060  }
2061  /* name of PREPARE AS will be set in loop of inlist */
2062 
2063  stmt->connection = con;
2064  stmt->lineno = lineno;
2065  stmt->compat = compat;
2067  stmt->questionmarks = questionmarks;
2069 
2070  /*------
2071  * create a list of variables
2072  *
2073  * The variables are listed with input variables preceding output
2074  * variables. The end of each group is marked by an end marker.
2075  * Per variable we list:
2076  *
2077  * type - as defined in ecpgtype.h
2078  * value - where to store the data
2079  * varcharsize - length of string in case we have a stringvariable, else 0
2080  * arraysize - 0 for pointer (we don't know the size of the array), 1 for
2081  * simple variable, size for arrays
2082  * offset - offset between ith and (i+1)th entry in an array, normally
2083  * that means sizeof(type)
2084  * ind_type - type of indicator variable
2085  * ind_pointer - pointer to indicator variable
2086  * ind_varcharsize - empty
2087  * ind_arrsize - arraysize of indicator array
2088  * ind_offset - indicator offset
2089  *------
2090  */
2091 
2092  is_prepared_name_set = false;
2093 
2094  list = &(stmt->inlist);
2095 
2096  type = va_arg(args, enum ECPGttype);
2097 
2098  while (type != ECPGt_EORT)
2099  {
2100  if (type == ECPGt_EOIT)
2101  list = &(stmt->outlist);
2102  else
2103  {
2104  struct variable *var,
2105  *ptr;
2106 
2107  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2108  {
2109  ecpg_do_epilogue(stmt);
2110  return false;
2111  }
2112 
2113  var->type = type;
2114  var->pointer = va_arg(args, char *);
2115 
2116  var->varcharsize = va_arg(args, long);
2117  var->arrsize = va_arg(args, long);
2118  var->offset = va_arg(args, long);
2119 
2120  /*
2121  * Unknown array size means pointer to an array. Unknown
2122  * varcharsize usually also means pointer. But if the type is
2123  * character and the array size is known, it is an array of
2124  * pointers to char, so use var->pointer as it is.
2125  */
2126  if (var->arrsize == 0 ||
2127  (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
2128  var->value = *((char **) (var->pointer));
2129  else
2130  var->value = var->pointer;
2131 
2132  /*
2133  * negative values are used to indicate an array without given
2134  * bounds
2135  */
2136  /* reset to zero for us */
2137  if (var->arrsize < 0)
2138  var->arrsize = 0;
2139  if (var->varcharsize < 0)
2140  var->varcharsize = 0;
2141 
2142  var->next = NULL;
2143 
2144  var->ind_type = va_arg(args, enum ECPGttype);
2145  var->ind_pointer = va_arg(args, char *);
2146  var->ind_varcharsize = va_arg(args, long);
2147  var->ind_arrsize = va_arg(args, long);
2148  var->ind_offset = va_arg(args, long);
2149 
2150  if (var->ind_type != ECPGt_NO_INDICATOR
2151  && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2152  var->ind_value = *((char **) (var->ind_pointer));
2153  else
2154  var->ind_value = var->ind_pointer;
2155 
2156  /*
2157  * negative values are used to indicate an array without given
2158  * bounds
2159  */
2160  /* reset to zero for us */
2161  if (var->ind_arrsize < 0)
2162  var->ind_arrsize = 0;
2163  if (var->ind_varcharsize < 0)
2164  var->ind_varcharsize = 0;
2165 
2166  /* if variable is NULL, the statement hasn't been prepared */
2167  if (var->pointer == NULL)
2168  {
2170  ecpg_free(var);
2171  ecpg_do_epilogue(stmt);
2172  return false;
2173  }
2174 
2175  for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2176  ;
2177 
2178  if (ptr == NULL)
2179  *list = var;
2180  else
2181  ptr->next = var;
2182 
2183  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2184  {
2185  stmt->name = ecpg_strdup(var->value, lineno);
2186  is_prepared_name_set = true;
2187  }
2188  }
2189 
2190  type = va_arg(args, enum ECPGttype);
2191  }
2192 
2193  /* are we connected? */
2194  if (con == NULL || con->connection == NULL)
2195  {
2196  ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2197  ecpg_do_epilogue(stmt);
2198  return false;
2199  }
2200 
2201  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2202  {
2204  ecpg_do_epilogue(stmt);
2205  return false;
2206  }
2207 
2208  /* initialize auto_mem struct */
2210 
2211  *stmt_out = stmt;
2212 
2213  return true;
2214 }
2215 
2216 /*
2217  * ecpg_do_epilogue
2218  * Restore the application locale and free the statement structure.
2219  */
2220 void
2222 {
2223  if (stmt == NULL)
2224  return;
2225 
2226 #ifdef HAVE_USELOCALE
2227  if (stmt->oldlocale != (locale_t) 0)
2228  uselocale(stmt->oldlocale);
2229 #else
2230  if (stmt->oldlocale)
2231  setlocale(LC_NUMERIC, stmt->oldlocale);
2232 #ifdef HAVE__CONFIGTHREADLOCALE
2233 
2234  /*
2235  * This is a bit trickier than it looks: if we failed partway through
2236  * statement initialization, oldthreadlocale could still be 0. But that's
2237  * okay because a call with 0 is defined to be a no-op.
2238  */
2239  if (stmt->oldthreadlocale != -1)
2240  (void) _configthreadlocale(stmt->oldthreadlocale);
2241 #endif
2242 #endif
2243 
2244  free_statement(stmt);
2245 }
2246 
2247 /*
2248  * Execute SQL statements in the backend.
2249  * The input/output parameters (variable argument list) are passed
2250  * in a va_list, so other functions can use this interface.
2251  */
2252 bool
2253 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)
2254 {
2255  struct statement *stmt = NULL;
2256 
2257  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2258  questionmarks, (enum ECPG_statement_type) st,
2259  query, args, &stmt))
2260  goto fail;
2261 
2262  if (!ecpg_build_params(stmt))
2263  goto fail;
2264 
2265  if (!ecpg_autostart_transaction(stmt))
2266  goto fail;
2267 
2268  if (!ecpg_execute(stmt))
2269  goto fail;
2270 
2271  if (!ecpg_process_output(stmt, true))
2272  goto fail;
2273 
2274  ecpg_do_epilogue(stmt);
2275  return true;
2276 
2277 fail:
2278  ecpg_do_epilogue(stmt);
2279  return false;
2280 }
2281 
2282 /*
2283  * Execute SQL statements in the backend.
2284  * The input/output parameters are passed as variable-length argument list.
2285  */
2286 bool
2287 ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
2288 {
2289  va_list args;
2290  bool ret;
2291 
2292  va_start(args, query);
2293  ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2294  questionmarks, st, query, args);
2295  va_end(args);
2296 
2297  return ret;
2298 }
2299 
2300 /* old descriptor interface */
2301 bool
2302 ECPGdo_descriptor(int line, const char *connection,
2303  const char *descriptor, const char *query)
2304 {
2305  return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2306  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2307  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
2308 }
long ind_arrsize
bool ecpg_register_prepared_stmt(struct statement *)
Definition: prepare.c:59
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3174
static bool insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
Definition: execute.c:1130
PGresult * PQexecPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:2000
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:306
static void sprintf_float_value(char *ptr, float value, const char *delim)
Definition: execute.c:475
int PGTYPESnumeric_copy(numeric *, numeric *)
Definition: numeric.c:1394
char arr[FLEXIBLE_ARRAY_MEMBER]
#define ECPG_NOT_CONN
Definition: ecpgerrno.h:37
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1109
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition: execute.c:1675
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
Definition: execute.c:151
void print(const void *obj)
Definition: print.c:36
size_t PQescapeString(char *to, const char *from, size_t length)
Definition: fe-exec.c:3428
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
#define setlocale(a, b)
Definition: win32_port.h:436
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6634
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2289
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
int ecpg_dynamic_type(Oid)
Definition: typename.c:73
ARRAY_TYPE
#define sqlca
Definition: sqlca.h:59
int64 timestamp
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2287
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3110
char * ecpg_realloc(void *, long, int)
Definition: memory.c:33
void ecpg_free(void *)
Definition: memory.c:13
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
unsigned ecpg_hex_enc_len(unsigned srclen)
Definition: data.c:128
#define ECPG_SQLSTATE_NO_DATA
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3081
enum ECPG_statement_type statement_type
char * ecpg_strdup(const char *, int)
Definition: memory.c:47
int PGTYPESnumeric_from_decimal(decimal *, numeric *)
Definition: numeric.c:1571
#define INFORMIX_MODE(X)
void * pointer
#define ECPG_CONVERT_BOOL
Definition: ecpgerrno.h:29
enum ECPGttype type
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
PGresult * results
#define ECPG_TOO_MANY_ARGUMENTS
Definition: ecpgerrno.h:19
static void free_statement(struct statement *stmt)
Definition: execute.c:96
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS
bool ecpg_autostart_transaction(struct statement *stmt)
Definition: execute.c:1585
#define printf(...)
Definition: port.h:221
long date
Definition: pgtypes_date.h:9
#define false
Definition: c.h:333
void ecpg_set_compat_sqlda(int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:255
char * PGTYPESdate_to_asc(date)
Definition: datetime.c:101
char * sqldata
Definition: sqlda-native.h:28
struct variable * next
static void free_variable(struct variable *var)
Definition: execute.c:83
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1109
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static enum ARRAY_TYPE ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
Definition: execute.c:167
bool ECPGdo_descriptor(int line, const char *connection, const char *descriptor, const char *query)
Definition: execute.c:2302
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2473
#define ECPG_EMPTY
Definition: ecpgerrno.h:30
char * oldlocale
static char * quote_postgres(char *arg, bool quote, int lineno)
Definition: execute.c:40
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:389
ECPG_statement_type
Definition: ecpgtype.h:95
bool ecpg_build_params(struct statement *stmt)
Definition: execute.c:1216
#define ecpg_gettext(x)
bool ecpg_execute(struct statement *stmt)
Definition: execute.c:1606
#define ECPG_TOO_MANY_MATCHES
Definition: ecpgerrno.h:21
#define sprintf
Definition: port.h:217
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:46
static void print_param_value(char *value, int len, int is_binary, int lineno, int nth)
Definition: execute.c:1079
struct ECPGtype_information_cache * next
int * paramformats
char * ecpg_auto_alloc(long, int)
Definition: memory.c:108
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:141
#define ECPG_NO_ARRAY
Definition: ecpgerrno.h:32
char * sqldata
Definition: sqlda-compat.h:12
struct connection * connection
int be_pid
Definition: libpq-fe.h:167
struct ECPGtype_information_cache * cache_head
enum COMPAT_MODE compat
static int next_insert(char *text, int pos, bool questionmarks, bool std_strings)
Definition: execute.c:114
Definition: sqlca.h:19
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3007
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:837
char * ecpg_prepared(const char *, struct connection *)
Definition: prepare.c:357
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:412
void * value
#define ECPG_IS_ARRAY(X)
PGresult * result
char * relname
Definition: libpq-fe.h:166
long sqlerrd[6]
Definition: sqlca.h:30
#define ECPG_UNSUPPORTED
Definition: ecpgerrno.h:18
bool questionmarks
Definition: ecpg.c:19
#define locale_t
Definition: win32_port.h:391
void ecpg_log(const char *format,...) pg_attribute_printf(1
struct sqlda_compat * ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:205
#define not_an_array_in_ecpg
enum COMPAT_MODE compat
Definition: ecpg.c:25
char * ecpg_alloc(long, int)
Definition: memory.c:19
void bool ecpg_auto_prepare(int, const char *, const int, char **, const char *)
Definition: prepare.c:553
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6624
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:380
void ecpg_clear_auto_mem(void)
Definition: memory.c:158
const char * ecpg_type_name(enum ECPGttype)
Definition: typename.c:17
char * PGTYPESnumeric_to_asc(numeric *, int)
Definition: numeric.c:347
bool ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno, enum ECPGttype type, enum ECPGttype ind_type, char *var, char *ind, long varcharsize, long offset, long ind_offset, enum ARRAY_TYPE isarray, enum COMPAT_MODE compat, bool force_indicator)
Definition: data.c:206
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
bool ecpg_do_prologue(int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, enum ECPG_statement_type statement_type, const char *query, va_list args, struct statement **stmt_out)
Definition: execute.c:1949
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3040
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
static char * convert_bytea_to_string(char *from_data, int from_len, int lineno)
Definition: execute.c:491
void * ind_pointer
static bool store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item, char **tobeinserted)
Definition: execute.c:1162
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1704
char * command
static struct @143 value
void PQclear(PGresult *res)
Definition: fe-exec.c:694
struct variable * inlist
#define free(a)
Definition: header.h:65
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:282
PGconn * connection
unsigned ecpg_hex_encode(const char *src, unsigned len, char *dst)
Definition: data.c:191
#define ECPG_TOO_FEW_ARGUMENTS
Definition: ecpgerrno.h:20
long ind_varcharsize
bool force_indicator
char * PGTYPEStimestamp_to_asc(timestamp)
Definition: timestamp.c:272
void ecpg_set_native_sqlda(int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:444
enum ECPGttype ind_type
char * name
struct descriptor_item * items
char * PGTYPESinterval_to_asc(interval *)
Definition: interval.c:1066
bool ECPGis_noind_null(enum ECPGttype type, const void *ptr)
Definition: misc.c:388
#define ECPG_INFORMIX_SUBSELECT_NOT_ONE
Definition: ecpgerrno.h:58
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:2221
char arr[FLEXIBLE_ARRAY_MEMBER]
long ind_offset
char ** paramvalues
short * sqlind
Definition: sqlda-native.h:29
struct descriptor_item * next
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2708
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1953
bool questionmarks
static void sprintf_double_value(char *ptr, double value, const char *delim)
Definition: execute.c:459
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:74
#define ECPG_SQLSTATE_DATATYPE_MISMATCH
bool force_indicator
Definition: ecpg.c:18
void * ind_value
int i
int * paramlengths
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:509
short * sqlind
Definition: sqlda-compat.h:13
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
void * arg
Definition: c.h:563
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
bool 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)
Definition: execute.c:2253
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:2996
#define snprintf
Definition: port.h:215
void PQfreemem(void *ptr)
Definition: fe-exec.c:3296
bool ecpg_init(const struct connection *, const char *, const int)
Definition: misc.c:104
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1778
void ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
Definition: error.c:219
long varcharsize
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40
struct variable * outlist
ECPGttype
Definition: ecpgtype.h:41