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