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);
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)
282  else
283  {
284  isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
287  {
288  /*
289  * arrays of character strings are not yet implemented
290  */
292  }
293  }
294  PQclear(query);
295  }
296  else
297  return ECPG_ARRAY_ERROR;
298 
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  else
1397  {
1398  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1399  return false;
1400 
1401  if (var->type == ECPGt_bytea)
1402  {
1403  binary_length = ((struct ECPGgeneric_bytea *) (var->value))->len;
1404  binary_format = true;
1405  }
1406  }
1407 
1408  /*
1409  * now tobeinserted points to an area that contains the next
1410  * parameter; now find the position in the string where it belongs
1411  */
1412  if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1413  {
1414  /*
1415  * We have an argument but we don't have the matched up
1416  * placeholder in the string
1417  */
1420  NULL);
1421  ecpg_free_params(stmt, false);
1422  ecpg_free(tobeinserted);
1423  return false;
1424  }
1425 
1426  /*
1427  * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1428  * to simulate a dynamic cursor because there is no backend
1429  * functionality for it
1430  */
1431  if (var->type == ECPGt_char_variable)
1432  {
1433  int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1434 
1435  if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1436  {
1437  ecpg_free_params(stmt, false);
1438  return false;
1439  }
1440  tobeinserted = NULL;
1441  }
1442 
1443  /*
1444  * if the placeholder is '$0' we have to replace it on the client side
1445  * this is for places we want to support variables at that are not
1446  * supported in the backend
1447  */
1448  else if (stmt->command[position] == '0')
1449  {
1450  if (stmt->statement_type == ECPGst_prepare ||
1452  {
1453  /* Need to double-quote the inserted statement name. */
1454  char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1455  stmt->lineno);
1456 
1457  if (!str)
1458  {
1459  ecpg_free(tobeinserted);
1460  ecpg_free_params(stmt, false);
1461  return false;
1462  }
1463  sprintf(str, "\"%s\"", tobeinserted);
1464  ecpg_free(tobeinserted);
1465  tobeinserted = str;
1466  }
1467 
1468  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1469  {
1470  ecpg_free_params(stmt, false);
1471  return false;
1472  }
1473  tobeinserted = NULL;
1474  }
1475  else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1476  {
1477  if (binary_format)
1478  {
1479  char *p = convert_bytea_to_string(tobeinserted,
1480  binary_length,
1481  stmt->lineno);
1482 
1483  ecpg_free(tobeinserted);
1484  if (!p)
1485  {
1486  ecpg_free_params(stmt, false);
1487  return false;
1488  }
1489  tobeinserted = p;
1490  }
1491 
1492  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1493  {
1494  ecpg_free_params(stmt, false);
1495  return false;
1496  }
1497  tobeinserted = NULL;
1498  }
1499  else
1500  {
1501  bool realloc_failed = false;
1502  char **newparamvalues;
1503  int *newparamlengths;
1504  int *newparamformats;
1505 
1506  /* enlarge all the param arrays */
1507  if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1508  stmt->paramvalues = newparamvalues;
1509  else
1510  realloc_failed = true;
1511 
1512  if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1513  stmt->paramlengths = newparamlengths;
1514  else
1515  realloc_failed = true;
1516 
1517  if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1518  stmt->paramformats = newparamformats;
1519  else
1520  realloc_failed = true;
1521 
1522  if (realloc_failed)
1523  {
1524  ecpg_free_params(stmt, false);
1525  ecpg_free(tobeinserted);
1526  return false;
1527  }
1528 
1529  /* only now can we assign ownership of "tobeinserted" to stmt */
1530  stmt->paramvalues[stmt->nparams] = tobeinserted;
1531  stmt->paramlengths[stmt->nparams] = binary_length;
1532  stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1533  stmt->nparams++;
1534 
1535  /* let's see if this was an old style placeholder */
1536  if (stmt->command[position] == '?')
1537  {
1538  /* yes, replace with new style */
1539  int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1540  * size we need */
1541 
1542  if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1543  {
1544  ecpg_free_params(stmt, false);
1545  return false;
1546  }
1547 
1548  snprintf(tobeinserted, buffersize, "$%d", counter++);
1549 
1550  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1551  {
1552  ecpg_free_params(stmt, false);
1553  return false;
1554  }
1555  tobeinserted = NULL;
1556  }
1557  }
1558 
1559  if (desc_counter == 0)
1560  var = var->next;
1561  }
1562 
1563  /*
1564  * Check if there are unmatched things left. PREPARE AS has no parameter.
1565  * Check other statement.
1566  */
1567  if (stmt->statement_type != ECPGst_prepare &&
1568  next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1569  {
1572  ecpg_free_params(stmt, false);
1573  return false;
1574  }
1575 
1576  return true;
1577 }
1578 
1579 /*
1580  * ecpg_autostart_transaction
1581  * If we are in non-autocommit mode, automatically start a transaction.
1582  */
1583 bool
1585 {
1587  {
1588  stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1589  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1590  {
1591  ecpg_free_params(stmt, false);
1592  return false;
1593  }
1594  PQclear(stmt->results);
1595  stmt->results = NULL;
1596  }
1597  return true;
1598 }
1599 
1600 /*
1601  * ecpg_execute
1602  * Execute the SQL statement.
1603  */
1604 bool
1606 {
1607  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);
1608  if (stmt->statement_type == ECPGst_execute)
1609  {
1611  stmt->name,
1612  stmt->nparams,
1613  (const char *const *) stmt->paramvalues,
1614  (const int *) stmt->paramlengths,
1615  (const int *) stmt->paramformats,
1616  0);
1617  ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1618  }
1619  else
1620  {
1621  if (stmt->nparams == 0)
1622  {
1623  stmt->results = PQexec(stmt->connection->connection, stmt->command);
1624  ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1625  }
1626  else
1627  {
1628  stmt->results = PQexecParams(stmt->connection->connection,
1629  stmt->command, stmt->nparams, NULL,
1630  (const char *const *) stmt->paramvalues,
1631  (const int *) stmt->paramlengths,
1632  (const int *) stmt->paramformats,
1633  0);
1634 
1635  ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1636  }
1637 
1638  if (stmt->statement_type == ECPGst_prepare)
1639  {
1640  if (!ecpg_register_prepared_stmt(stmt))
1641  {
1642  ecpg_free_params(stmt, true);
1643  return false;
1644  }
1645  }
1646  }
1647 
1648  ecpg_free_params(stmt, true);
1649 
1650  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1651  return false;
1652 
1653  return true;
1654 }
1655 
1656 /*-------
1657  * ecpg_process_output
1658  *
1659  * Process the statement result and store it into application variables. This
1660  * function can be called repeatedly during the same statement in case cursor
1661  * readahead is used and the application does FETCH N which overflows the
1662  * readahead window.
1663  *
1664  * Parameters
1665  * stmt statement structure holding the PGresult and
1666  * the list of output variables
1667  * clear_result
1668  * PQclear() the result upon returning from this function
1669  *
1670  * Returns success as boolean. Also an SQL error is raised in case of failure.
1671  *-------
1672  */
1673 bool
1674 ecpg_process_output(struct statement *stmt, bool clear_result)
1675 {
1676  struct variable *var;
1677  bool status = false;
1678  char *cmdstat;
1679  PGnotify *notify;
1680  struct sqlca_t *sqlca = ECPGget_sqlca();
1681  int nfields,
1682  ntuples,
1683  act_field;
1684 
1685  if (sqlca == NULL)
1686  {
1689  return false;
1690  }
1691 
1692  var = stmt->outlist;
1693  switch (PQresultStatus(stmt->results))
1694  {
1695  case PGRES_TUPLES_OK:
1696  nfields = PQnfields(stmt->results);
1697  sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1698 
1699  ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1700  status = true;
1701 
1702  if (ntuples < 1)
1703  {
1704  if (ntuples)
1705  ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1706  stmt->lineno, ntuples);
1708  status = false;
1709  break;
1710  }
1711 
1712  if (var != NULL && var->type == ECPGt_descriptor)
1713  {
1714  struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1715 
1716  if (desc == NULL)
1717  status = false;
1718  else
1719  {
1720  if (desc->result)
1721  PQclear(desc->result);
1722  desc->result = stmt->results;
1723  clear_result = false;
1724  ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1725  stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1726  }
1727  var = var->next;
1728  }
1729  else if (var != NULL && var->type == ECPGt_sqlda)
1730  {
1731  if (INFORMIX_MODE(stmt->compat))
1732  {
1733  struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1734  struct sqlda_compat *sqlda = *_sqlda;
1735  struct sqlda_compat *sqlda_new;
1736  int i;
1737 
1738  /*
1739  * If we are passed in a previously existing sqlda (chain)
1740  * then free it.
1741  */
1742  while (sqlda)
1743  {
1744  sqlda_new = sqlda->desc_next;
1745  free(sqlda);
1746  sqlda = sqlda_new;
1747  }
1748  *_sqlda = sqlda = sqlda_new = NULL;
1749  for (i = ntuples - 1; i >= 0; i--)
1750  {
1751  /*
1752  * Build a new sqlda structure. Note that only
1753  * fetching 1 record is supported
1754  */
1755  sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1756 
1757  if (!sqlda_new)
1758  {
1759  /* cleanup all SQLDAs we created up */
1760  while (sqlda)
1761  {
1762  sqlda_new = sqlda->desc_next;
1763  free(sqlda);
1764  sqlda = sqlda_new;
1765  }
1766  *_sqlda = NULL;
1767 
1768  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1769  status = false;
1770  break;
1771  }
1772  else
1773  {
1774  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1775 
1776  *_sqlda = sqlda_new;
1777 
1778  ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1779  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1780  stmt->lineno, PQnfields(stmt->results));
1781 
1782  sqlda_new->desc_next = sqlda;
1783  sqlda = sqlda_new;
1784  }
1785  }
1786  }
1787  else
1788  {
1789  struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1790  struct sqlda_struct *sqlda = *_sqlda;
1791  struct sqlda_struct *sqlda_new;
1792  int i;
1793 
1794  /*
1795  * If we are passed in a previously existing sqlda (chain)
1796  * then free it.
1797  */
1798  while (sqlda)
1799  {
1800  sqlda_new = sqlda->desc_next;
1801  free(sqlda);
1802  sqlda = sqlda_new;
1803  }
1804  *_sqlda = sqlda = sqlda_new = NULL;
1805  for (i = ntuples - 1; i >= 0; i--)
1806  {
1807  /*
1808  * Build a new sqlda structure. Note that only
1809  * fetching 1 record is supported
1810  */
1811  sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1812 
1813  if (!sqlda_new)
1814  {
1815  /* cleanup all SQLDAs we created up */
1816  while (sqlda)
1817  {
1818  sqlda_new = sqlda->desc_next;
1819  free(sqlda);
1820  sqlda = sqlda_new;
1821  }
1822  *_sqlda = NULL;
1823 
1824  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1825  status = false;
1826  break;
1827  }
1828  else
1829  {
1830  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1831 
1832  *_sqlda = sqlda_new;
1833 
1834  ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1835  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1836  stmt->lineno, PQnfields(stmt->results));
1837 
1838  sqlda_new->desc_next = sqlda;
1839  sqlda = sqlda_new;
1840  }
1841  }
1842  }
1843 
1844  var = var->next;
1845  }
1846  else
1847  for (act_field = 0; act_field < nfields && status; act_field++)
1848  {
1849  if (var != NULL)
1850  {
1851  status = ecpg_store_result(stmt->results, act_field, stmt, var);
1852  var = var->next;
1853  }
1854  else if (!INFORMIX_MODE(stmt->compat))
1855  {
1857  return false;
1858  }
1859  }
1860 
1861  if (status && var != NULL)
1862  {
1864  status = false;
1865  }
1866 
1867  break;
1868  case PGRES_COMMAND_OK:
1869  status = true;
1870  cmdstat = PQcmdStatus(stmt->results);
1871  sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1872  sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1873  ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1874  if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1875  !sqlca->sqlerrd[2] &&
1876  (strncmp(cmdstat, "UPDATE", 6) == 0
1877  || strncmp(cmdstat, "INSERT", 6) == 0
1878  || strncmp(cmdstat, "DELETE", 6) == 0))
1880  break;
1881  case PGRES_COPY_OUT:
1882  {
1883  char *buffer;
1884  int res;
1885 
1886  ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1887  while ((res = PQgetCopyData(stmt->connection->connection,
1888  &buffer, 0)) > 0)
1889  {
1890  printf("%s", buffer);
1891  PQfreemem(buffer);
1892  }
1893  if (res == -1)
1894  {
1895  /* COPY done */
1896  PQclear(stmt->results);
1897  stmt->results = PQgetResult(stmt->connection->connection);
1898  if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1899  ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1900  else
1901  ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1902  }
1903  break;
1904  }
1905  default:
1906 
1907  /*
1908  * execution should never reach this code because it is already
1909  * handled in ecpg_check_PQresult()
1910  */
1911  ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1912  stmt->lineno);
1913  ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1914  status = false;
1915  break;
1916  }
1917 
1918  if (clear_result)
1919  {
1920  PQclear(stmt->results);
1921  stmt->results = NULL;
1922  }
1923 
1924  /* check for asynchronous returns */
1926  while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1927  {
1928  ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1929  stmt->lineno, notify->relname, notify->be_pid);
1930  PQfreemem(notify);
1932  }
1933 
1934  return status;
1935 }
1936 
1937 /*
1938  * ecpg_do_prologue
1939  *
1940  * Initialize various infrastructure elements for executing the statement:
1941  *
1942  * - create the statement structure
1943  * - set the C numeric locale for communicating with the backend
1944  * - preprocess the variable list of input/output parameters into
1945  * linked lists
1946  */
1947 bool
1948 ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
1949  const char *connection_name, const bool questionmarks,
1950  enum ECPG_statement_type statement_type, const char *query,
1951  va_list args, struct statement **stmt_out)
1952 {
1953  struct statement *stmt = NULL;
1954  struct connection *con;
1955  enum ECPGttype type;
1956  struct variable **list;
1957  char *prepname;
1958  bool is_prepared_name_set;
1959 
1960  *stmt_out = NULL;
1961 
1962  if (!query)
1963  {
1965  return false;
1966  }
1967 
1968  stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1969 
1970  if (stmt == NULL)
1971  return false;
1972 
1973  /*
1974  * Make sure we do NOT honor the locale for numeric input/output since the
1975  * database wants the standard decimal point. If available, use
1976  * uselocale() for this because it's thread-safe. Windows doesn't have
1977  * that, but it usually does have _configthreadlocale(). In some versions
1978  * of MinGW, _configthreadlocale() exists but always returns -1 --- so
1979  * treat that situation as if the function doesn't exist.
1980  */
1981 #ifdef HAVE_USELOCALE
1982  stmt->clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
1983  if (stmt->clocale == (locale_t) 0)
1984  {
1985  ecpg_do_epilogue(stmt);
1986  return false;
1987  }
1988  stmt->oldlocale = uselocale(stmt->clocale);
1989  if (stmt->oldlocale == (locale_t) 0)
1990  {
1991  ecpg_do_epilogue(stmt);
1992  return false;
1993  }
1994 #else
1995 #ifdef HAVE__CONFIGTHREADLOCALE
1996  stmt->oldthreadlocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
1997 #endif
1998  stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
1999  if (stmt->oldlocale == NULL)
2000  {
2001  ecpg_do_epilogue(stmt);
2002  return false;
2003  }
2004  setlocale(LC_NUMERIC, "C");
2005 #endif
2006 
2007 #ifdef ENABLE_THREAD_SAFETY
2008  ecpg_pthreads_init();
2009 #endif
2010 
2011  con = ecpg_get_connection(connection_name);
2012 
2013  if (!ecpg_init(con, connection_name, lineno))
2014  {
2015  ecpg_do_epilogue(stmt);
2016  return false;
2017  }
2018 
2019  /*
2020  * If statement type is ECPGst_prepnormal we are supposed to prepare the
2021  * statement before executing them
2022  */
2024  {
2025  if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2026  {
2027  ecpg_do_epilogue(stmt);
2028  return false;
2029  }
2030 
2031  /*
2032  * statement is now prepared, so instead of the query we have to
2033  * execute the name
2034  */
2035  stmt->command = prepname;
2037  }
2038  else
2039  stmt->command = ecpg_strdup(query, lineno);
2040 
2041  stmt->name = NULL;
2042 
2044  {
2045  /* if we have an EXECUTE command, only the name is send */
2046  char *command = ecpg_prepared(stmt->command, con);
2047 
2048  if (command)
2049  {
2050  stmt->name = stmt->command;
2051  stmt->command = ecpg_strdup(command, lineno);
2052  }
2053  else
2054  {
2056  ecpg_do_epilogue(stmt);
2057  return false;
2058  }
2059  }
2060  /* name of PREPARE AS will be set in loop of inlist */
2061 
2062  stmt->connection = con;
2063  stmt->lineno = lineno;
2064  stmt->compat = compat;
2066  stmt->questionmarks = questionmarks;
2068 
2069  /*------
2070  * create a list of variables
2071  *
2072  * The variables are listed with input variables preceding output
2073  * variables. The end of each group is marked by an end marker.
2074  * Per variable we list:
2075  *
2076  * type - as defined in ecpgtype.h
2077  * value - where to store the data
2078  * varcharsize - length of string in case we have a stringvariable, else 0
2079  * arraysize - 0 for pointer (we don't know the size of the array), 1 for
2080  * simple variable, size for arrays
2081  * offset - offset between ith and (i+1)th entry in an array, normally
2082  * that means sizeof(type)
2083  * ind_type - type of indicator variable
2084  * ind_pointer - pointer to indicator variable
2085  * ind_varcharsize - empty
2086  * ind_arrsize - arraysize of indicator array
2087  * ind_offset - indicator offset
2088  *------
2089  */
2090 
2091  is_prepared_name_set = false;
2092 
2093  list = &(stmt->inlist);
2094 
2095  type = va_arg(args, enum ECPGttype);
2096 
2097  while (type != ECPGt_EORT)
2098  {
2099  if (type == ECPGt_EOIT)
2100  list = &(stmt->outlist);
2101  else
2102  {
2103  struct variable *var,
2104  *ptr;
2105 
2106  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2107  {
2108  ecpg_do_epilogue(stmt);
2109  return false;
2110  }
2111 
2112  var->type = type;
2113  var->pointer = va_arg(args, char *);
2114 
2115  var->varcharsize = va_arg(args, long);
2116  var->arrsize = va_arg(args, long);
2117  var->offset = va_arg(args, long);
2118 
2119  /*
2120  * Unknown array size means pointer to an array. Unknown
2121  * varcharsize usually also means pointer. But if the type is
2122  * character and the array size is known, it is an array of
2123  * pointers to char, so use var->pointer as it is.
2124  */
2125  if (var->arrsize == 0 ||
2126  (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
2127  var->value = *((char **) (var->pointer));
2128  else
2129  var->value = var->pointer;
2130 
2131  /*
2132  * negative values are used to indicate an array without given
2133  * bounds
2134  */
2135  /* reset to zero for us */
2136  if (var->arrsize < 0)
2137  var->arrsize = 0;
2138  if (var->varcharsize < 0)
2139  var->varcharsize = 0;
2140 
2141  var->next = NULL;
2142 
2143  var->ind_type = va_arg(args, enum ECPGttype);
2144  var->ind_pointer = va_arg(args, char *);
2145  var->ind_varcharsize = va_arg(args, long);
2146  var->ind_arrsize = va_arg(args, long);
2147  var->ind_offset = va_arg(args, long);
2148 
2149  if (var->ind_type != ECPGt_NO_INDICATOR
2150  && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2151  var->ind_value = *((char **) (var->ind_pointer));
2152  else
2153  var->ind_value = var->ind_pointer;
2154 
2155  /*
2156  * negative values are used to indicate an array without given
2157  * bounds
2158  */
2159  /* reset to zero for us */
2160  if (var->ind_arrsize < 0)
2161  var->ind_arrsize = 0;
2162  if (var->ind_varcharsize < 0)
2163  var->ind_varcharsize = 0;
2164 
2165  /* if variable is NULL, the statement hasn't been prepared */
2166  if (var->pointer == NULL)
2167  {
2169  ecpg_free(var);
2170  ecpg_do_epilogue(stmt);
2171  return false;
2172  }
2173 
2174  for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2175  ;
2176 
2177  if (ptr == NULL)
2178  *list = var;
2179  else
2180  ptr->next = var;
2181 
2182  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2183  {
2184  stmt->name = ecpg_strdup(var->value, lineno);
2185  is_prepared_name_set = true;
2186  }
2187  }
2188 
2189  type = va_arg(args, enum ECPGttype);
2190  }
2191 
2192  /* are we connected? */
2193  if (con == NULL || con->connection == NULL)
2194  {
2195  ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2196  ecpg_do_epilogue(stmt);
2197  return false;
2198  }
2199 
2200  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2201  {
2203  ecpg_do_epilogue(stmt);
2204  return false;
2205  }
2206 
2207  /* initialize auto_mem struct */
2209 
2210  *stmt_out = stmt;
2211 
2212  return true;
2213 }
2214 
2215 /*
2216  * ecpg_do_epilogue
2217  * Restore the application locale and free the statement structure.
2218  */
2219 void
2221 {
2222  if (stmt == NULL)
2223  return;
2224 
2225 #ifdef HAVE_USELOCALE
2226  if (stmt->oldlocale != (locale_t) 0)
2227  uselocale(stmt->oldlocale);
2228 #else
2229  if (stmt->oldlocale)
2230  setlocale(LC_NUMERIC, stmt->oldlocale);
2231 #ifdef HAVE__CONFIGTHREADLOCALE
2232 
2233  /*
2234  * This is a bit trickier than it looks: if we failed partway through
2235  * statement initialization, oldthreadlocale could still be 0. But that's
2236  * okay because a call with 0 is defined to be a no-op.
2237  */
2238  if (stmt->oldthreadlocale != -1)
2239  (void) _configthreadlocale(stmt->oldthreadlocale);
2240 #endif
2241 #endif
2242 
2243  free_statement(stmt);
2244 }
2245 
2246 /*
2247  * Execute SQL statements in the backend.
2248  * The input/output parameters (variable argument list) are passed
2249  * in a va_list, so other functions can use this interface.
2250  */
2251 bool
2252 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)
2253 {
2254  struct statement *stmt = NULL;
2255 
2256  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2258  query, args, &stmt))
2259  goto fail;
2260 
2261  if (!ecpg_build_params(stmt))
2262  goto fail;
2263 
2264  if (!ecpg_autostart_transaction(stmt))
2265  goto fail;
2266 
2267  if (!ecpg_execute(stmt))
2268  goto fail;
2269 
2270  if (!ecpg_process_output(stmt, true))
2271  goto fail;
2272 
2273  ecpg_do_epilogue(stmt);
2274  return true;
2275 
2276 fail:
2277  ecpg_do_epilogue(stmt);
2278  return false;
2279 }
2280 
2281 /*
2282  * Execute SQL statements in the backend.
2283  * The input/output parameters are passed as variable-length argument list.
2284  */
2285 bool
2286 ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
2287 {
2288  va_list args;
2289  bool ret;
2290 
2291  va_start(args, query);
2292  ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2293  questionmarks, st, query, args);
2294  va_end(args);
2295 
2296  return ret;
2297 }
2298 
2299 /* old descriptor interface */
2300 bool
2301 ECPGdo_descriptor(int line, const char *connection,
2302  const char *descriptor, const char *query)
2303 {
2304  return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2305  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2306  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
2307 }
void print(const void *obj)
Definition: print.c:36
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1164
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:74
unsigned ecpg_hex_enc_len(unsigned srclen)
Definition: data.c:128
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
unsigned ecpg_hex_encode(const char *src, unsigned len, char *dst)
Definition: data.c:191
enum COMPAT_MODE compat
Definition: ecpg.c:25
bool force_indicator
Definition: ecpg.c:18
bool questionmarks
Definition: ecpg.c:19
#define ECPG_EMPTY
Definition: ecpgerrno.h:30
#define ECPG_CONVERT_BOOL
Definition: ecpgerrno.h:29
#define ECPG_TOO_MANY_ARGUMENTS
Definition: ecpgerrno.h:19
#define ECPG_TOO_FEW_ARGUMENTS
Definition: ecpgerrno.h:20
#define ECPG_UNSUPPORTED
Definition: ecpgerrno.h:18
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
#define ECPG_INFORMIX_SUBSELECT_NOT_ONE
Definition: ecpgerrno.h:58
#define ECPG_TOO_MANY_MATCHES
Definition: ecpgerrno.h:21
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
#define ECPG_NOT_CONN
Definition: ecpgerrno.h:37
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
#define ECPG_NO_ARRAY
Definition: ecpgerrno.h:32
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:837
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:282
char * ecpg_alloc(long, int)
Definition: memory.c:19
#define ecpg_gettext(x)
#define ECPG_SQLSTATE_NO_DATA
char * ecpg_prepared(const char *, struct connection *)
Definition: prepare.c:357
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
@ ECPG_COMPAT_PGSQL
@ ECPG_COMPAT_INFORMIX_SE
char * ecpg_auto_alloc(long, int)
Definition: memory.c:108
bool ecpg_register_prepared_stmt(struct statement *)
Definition: prepare.c:59
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
int ecpg_dynamic_type(Oid)
Definition: typename.c:73
void ecpg_set_native_sqlda(int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:444
char * ecpg_realloc(void *, long, int)
Definition: memory.c:33
void ecpg_log(const char *format,...) pg_attribute_printf(1
void ecpg_clear_auto_mem(void)
Definition: memory.c:158
struct sqlda_compat * ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:205
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:412
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS
bool ecpg_init(const struct connection *, const char *, const int)
Definition: misc.c:104
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION
const char * ecpg_type_name(enum ECPGttype)
Definition: typename.c:17
#define INFORMIX_MODE(X)
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS
void ecpg_set_compat_sqlda(int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:255
#define ECPG_SQLSTATE_DATATYPE_MISMATCH
void ecpg_free(void *)
Definition: memory.c:13
#define ECPG_IS_ARRAY(X)
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
ARRAY_TYPE
@ ECPG_ARRAY_NOT_SET
@ ECPG_ARRAY_ARRAY
@ ECPG_ARRAY_VECTOR
@ ECPG_ARRAY_NONE
@ ECPG_ARRAY_ERROR
void ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
Definition: error.c:219
char * ecpg_strdup(const char *, int)
Definition: memory.c:47
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
void bool ecpg_auto_prepare(int, const char *, const int, char **, const char *)
Definition: prepare.c:553
ECPG_statement_type
Definition: ecpgtype.h:96
@ ECPGst_execute
Definition: ecpgtype.h:98
@ ECPGst_exec_with_exprlist
Definition: ecpgtype.h:102
@ ECPGst_prepare
Definition: ecpgtype.h:101
@ ECPGst_prepnormal
Definition: ecpgtype.h:100
ECPGttype
Definition: ecpgtype.h:42
@ ECPGt_float
Definition: ecpgtype.h:47
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_long_long
Definition: ecpgtype.h:45
@ ECPGt_sqlda
Definition: ecpgtype.h:66
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_decimal
Definition: ecpgtype.h:51
@ ECPGt_char_variable
Definition: ecpgtype.h:60
@ ECPGt_bytea
Definition: ecpgtype.h:67
@ ECPGt_numeric
Definition: ecpgtype.h:49
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_timestamp
Definition: ecpgtype.h:54
@ ECPGt_unsigned_short
Definition: ecpgtype.h:43
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_char
Definition: ecpgtype.h:43
@ ECPGt_double
Definition: ecpgtype.h:47
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
@ ECPGt_date
Definition: ecpgtype.h:53
@ ECPGt_interval
Definition: ecpgtype.h:55
@ ECPGt_unsigned_long
Definition: ecpgtype.h:44
@ ECPGt_const
Definition: ecpgtype.h:61
@ ECPGt_bool
Definition: ecpgtype.h:46
@ ECPGt_unsigned_long_long
Definition: ecpgtype.h:45
@ ECPGt_unsigned_int
Definition: ecpgtype.h:44
@ ECPGt_descriptor
Definition: ecpgtype.h:59
@ ECPGt_char
Definition: ecpgtype.h:43
@ ECPGt_string
Definition: ecpgtype.h:65
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
Definition: execute.c:151
bool ecpg_build_params(struct statement *stmt)
Definition: execute.c:1216
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:306
static void sprintf_double_value(char *ptr, double value, const char *delim)
Definition: execute.c:459
static void print_param_value(char *value, int len, int is_binary, int lineno, int nth)
Definition: execute.c:1079
static bool insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
Definition: execute.c:1130
static char * quote_postgres(char *arg, bool quote, int lineno)
Definition: execute.c:40
bool ecpg_execute(struct statement *stmt)
Definition: execute.c:1605
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1109
static enum ARRAY_TYPE ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
Definition: execute.c:167
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:509
static bool store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item, char **tobeinserted)
Definition: execute.c:1162
static void sprintf_float_value(char *ptr, float value, const char *delim)
Definition: execute.c:475
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:2220
static char * convert_bytea_to_string(char *from_data, int from_len, int lineno)
Definition: execute.c:491
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:2252
static void free_variable(struct variable *var)
Definition: execute.c:83
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:1948
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition: execute.c:1674
bool ecpg_autostart_transaction(struct statement *stmt)
Definition: execute.c:1584
bool ECPGdo_descriptor(int line, const char *connection, const char *descriptor, const char *query)
Definition: execute.c:2301
static void free_statement(struct statement *stmt)
Definition: execute.c:96
#define not_an_array_in_ecpg
static int next_insert(char *text, int pos, bool questionmarks, bool std_strings)
Definition: execute.c:114
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:2286
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6873
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6863
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3746
void PQfreemem(void *ptr)
Definition: fe-exec.c:3891
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2599
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3578
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:2287
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3270
void PQclear(PGresult *res)
Definition: fe-exec.c:718
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3681
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3286
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:3567
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3340
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:2334
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
size_t PQescapeString(char *to, const char *from, size_t length)
Definition: fe-exec.c:4027
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:2004
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3735
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3611
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3652
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3348
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2082
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2751
#define free(a)
Definition: header.h:65
static struct @151 value
bool ECPGis_noind_null(enum ECPGttype type, const void *ptr)
Definition: misc.c:387
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:141
return false
Definition: isn.c:131
int i
Definition: isn.c:73
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_COPY_OUT
Definition: libpq-fe.h:103
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
@ PQTRANS_IDLE
Definition: libpq-fe.h:118
va_end(args)
va_start(args, fmt)
void * arg
const void size_t len
while(p+4<=pend)
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
char * PGTYPESdate_to_asc(date)
Definition: datetime.c:101
long date
Definition: pgtypes_date.h:9
char * PGTYPESinterval_to_asc(interval *)
Definition: interval.c:1062
int PGTYPESnumeric_from_decimal(decimal *, numeric *)
Definition: numeric.c:1575
char * PGTYPESnumeric_to_asc(numeric *, int)
Definition: numeric.c:347
int PGTYPESnumeric_copy(numeric *, numeric *)
Definition: numeric.c:1393
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:46
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:389
int64 timestamp
char * PGTYPEStimestamp_to_asc(timestamp)
Definition: timestamp.c:272
#define sprintf
Definition: port.h:227
#define snprintf
Definition: port.h:225
#define printf(...)
Definition: port.h:231
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
@ SQL3_CHARACTER_VARYING
Definition: sql3types.h:20
@ SQL3_CHARACTER
Definition: sql3types.h:10
#define sqlca
Definition: sqlca.h:59
struct ECPGtype_information_cache * next
PGconn * connection
struct ECPGtype_information_cache * cache_head
struct descriptor_item * next
struct descriptor_item * items
PGresult * result
int be_pid
Definition: libpq-fe.h:190
char * relname
Definition: libpq-fe.h:189
Definition: sqlca.h:20
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
short * sqlind
Definition: sqlda-compat.h:13
char * sqldata
Definition: sqlda-compat.h:12
char * sqldata
Definition: sqlda-native.h:28
short * sqlind
Definition: sqlda-native.h:29
struct variable * inlist
bool questionmarks
char * command
char ** paramvalues
char * oldlocale
struct variable * outlist
struct connection * connection
bool force_indicator
int * paramformats
enum COMPAT_MODE compat
enum ECPG_statement_type statement_type
char * name
PGresult * results
int * paramlengths
void * value
long ind_arrsize
long ind_offset
void * ind_value
long ind_varcharsize
long varcharsize
struct variable * next
enum ECPGttype type
enum ECPGttype ind_type
void * ind_pointer
void * pointer
Definition: c.h:622
#define locale_t
Definition: win32_port.h:401
#define setlocale(a, b)
Definition: win32_port.h:446