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

Generated by: LCOV version 1.14