LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/ecpglib - execute.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 67.5 % 1131 763
Test Date: 2026-05-25 20:16:17 Functions: 91.7 % 24 22
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.0-1