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