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

Generated by: LCOV version 1.13