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

Generated by: LCOV version 1.13