LCOV - differential code coverage report
Current view: top level - src/interfaces/ecpg/ecpglib - execute.c (source / functions) Coverage Total Hit UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 67.9 % 1124 763 32 329 6 99 4 654 26 104 5
Current Date: 2023-04-08 17:13:01 Functions: 91.7 % 24 22 1 1 4 2 16 1 4
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (180,240] days: 100.0 % 3 3 3
Legend: Lines: hit not hit (240..) days: 67.8 % 1121 760 32 329 6 99 1 654 26 104
Function coverage date bins:
(240..) days: 75.9 % 29 22 1 1 4 2 16 1 4

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

Generated by: LCOV version v1.16-55-g56c0a2a