Branch data 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 *
43 : 1140 : 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 [ + - ]: 1140 : if (!quote)
55 : 1140 : return arg;
56 : : else
57 : : {
58 : 0 : length = strlen(arg);
59 : 0 : buffer_len = 2 * length + 1;
60 : 0 : res = ecpg_alloc(buffer_len + 3, lineno);
61 [ # # ]: 0 : if (!res)
62 : 0 : return res;
63 : 0 : escaped_len = PQescapeString(res + 1, arg, buffer_len);
64 [ # # ]: 0 : if (length == escaped_len)
65 : : {
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);
76 : 0 : res[0] = ESCAPE_STRING_SYNTAX;
77 : 0 : res[1] = res[escaped_len + 2] = '\'';
78 : 0 : res[escaped_len + 3] = '\0';
79 : : }
80 : 0 : ecpg_free(arg);
81 : 0 : return res;
82 : : }
83 : : }
84 : :
85 : : static void
86 : 10710 : free_variable(struct variable *var)
87 : : {
88 : : struct variable *var_next;
89 : :
90 [ + + ]: 17028 : while (var)
91 : : {
92 : 6318 : var_next = var->next;
93 : 6318 : ecpg_free(var);
94 : 6318 : var = var_next;
95 : : }
96 : 10710 : }
97 : :
98 : : static void
99 : 5355 : free_statement(struct statement *stmt)
100 : : {
101 [ - + ]: 5355 : if (stmt == NULL)
102 : 0 : return;
103 : 5355 : free_variable(stmt->inlist);
104 : 5355 : free_variable(stmt->outlist);
105 : 5355 : ecpg_free(stmt->command);
106 : 5355 : ecpg_free(stmt->name);
107 : : #ifndef HAVE_USELOCALE
108 : : ecpg_free(stmt->oldlocale);
109 : : #endif
110 : 5355 : ecpg_free(stmt);
111 : : }
112 : :
113 : : static int
114 : 9027 : next_insert(char *text, int pos, bool questionmarks, bool std_strings)
115 : : {
116 : 9027 : bool string = false;
117 : 9027 : int p = pos;
118 : :
119 [ + + ]: 243553 : for (; text[p] != '\0'; p++)
120 : : {
121 [ + + - + : 238208 : if (string && !std_strings && text[p] == '\\') /* escape character */
- - ]
122 : 0 : p++;
123 [ + + ]: 238208 : else if (text[p] == '\'')
124 : 3954 : string = string ? false : true;
125 [ + + ]: 234254 : else if (!string)
126 : : {
127 [ + + + + ]: 219177 : if (text[p] == '$' && isdigit((unsigned char) text[p + 1]))
128 : 0 : {
129 : : /* this can be either a dollar quote or a variable */
130 : : int i;
131 : :
132 [ + + ]: 7366 : for (i = p + 1; isdigit((unsigned char) text[i]); i++)
133 : : /* empty loop body */ ;
134 [ + - ]: 3682 : if (!isalpha((unsigned char) text[i]) &&
135 [ + - + - ]: 3682 : isascii((unsigned char) text[i]) && text[i] != '_')
136 : : /* not dollar delimited quote */
137 : 3682 : return p;
138 : : }
139 [ + + - + ]: 215495 : else if (questionmarks && text[p] == '?')
140 : : {
141 : : /* also allow old style placeholders */
142 : 0 : return p;
143 : : }
144 : : }
145 : : }
146 : :
147 : 5345 : return -1;
148 : : }
149 : :
150 : : static bool
151 : 5162 : 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
154 : 5162 : = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
155 : :
156 [ - + ]: 5162 : if (new_entry == NULL)
157 : 0 : return false;
158 : :
159 : 5162 : new_entry->oid = oid;
160 : 5162 : new_entry->isarray = isarray;
161 : 5162 : new_entry->next = *cache;
162 : 5162 : *cache = new_entry;
163 : 5162 : return true;
164 : : }
165 : :
166 : : static enum ARRAY_TYPE
167 : 2526 : ecpg_is_type_an_array(Oid type, const struct statement *stmt, const struct variable *var)
168 : : {
169 : : char *array_query;
170 : 2526 : enum ARRAY_TYPE isarray = ECPG_ARRAY_NOT_SET;
171 : : PGresult *query;
172 : : struct ECPGtype_information_cache *cache_entry;
173 : :
174 [ + + ]: 2526 : if ((stmt->connection->cache_head) == NULL)
175 : : {
176 : : /*
177 : : * Text like types are not an array for ecpg, but postgres counts them
178 : : * as an array. This define reminds you to not 'correct' these values.
179 : : */
180 : : #define not_an_array_in_ecpg ECPG_ARRAY_NONE
181 : :
182 : : /* populate cache with well known types to speed things up */
183 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOOLOID, ECPG_ARRAY_NONE, stmt->lineno))
184 : 0 : return ECPG_ARRAY_ERROR;
185 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BYTEAOID, ECPG_ARRAY_NONE, stmt->lineno))
186 : 0 : return ECPG_ARRAY_ERROR;
187 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CHAROID, ECPG_ARRAY_NONE, stmt->lineno))
188 : 0 : return ECPG_ARRAY_ERROR;
189 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NAMEOID, not_an_array_in_ecpg, stmt->lineno))
190 : 0 : return ECPG_ARRAY_ERROR;
191 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT8OID, ECPG_ARRAY_NONE, stmt->lineno))
192 : 0 : return ECPG_ARRAY_ERROR;
193 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2OID, ECPG_ARRAY_NONE, stmt->lineno))
194 : 0 : return ECPG_ARRAY_ERROR;
195 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2VECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
196 : 0 : return ECPG_ARRAY_ERROR;
197 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT4OID, ECPG_ARRAY_NONE, stmt->lineno))
198 : 0 : return ECPG_ARRAY_ERROR;
199 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), REGPROCOID, ECPG_ARRAY_NONE, stmt->lineno))
200 : 0 : return ECPG_ARRAY_ERROR;
201 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TEXTOID, ECPG_ARRAY_NONE, stmt->lineno))
202 : 0 : return ECPG_ARRAY_ERROR;
203 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDOID, ECPG_ARRAY_NONE, stmt->lineno))
204 : 0 : return ECPG_ARRAY_ERROR;
205 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIDOID, ECPG_ARRAY_NONE, stmt->lineno))
206 : 0 : return ECPG_ARRAY_ERROR;
207 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), XIDOID, ECPG_ARRAY_NONE, stmt->lineno))
208 : 0 : return ECPG_ARRAY_ERROR;
209 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDOID, ECPG_ARRAY_NONE, stmt->lineno))
210 : 0 : return ECPG_ARRAY_ERROR;
211 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDVECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
212 : 0 : return ECPG_ARRAY_ERROR;
213 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POINTOID, ECPG_ARRAY_VECTOR, stmt->lineno))
214 : 0 : return ECPG_ARRAY_ERROR;
215 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LSEGOID, ECPG_ARRAY_VECTOR, stmt->lineno))
216 : 0 : return ECPG_ARRAY_ERROR;
217 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), PATHOID, ECPG_ARRAY_NONE, stmt->lineno))
218 : 0 : return ECPG_ARRAY_ERROR;
219 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOXOID, ECPG_ARRAY_VECTOR, stmt->lineno))
220 : 0 : return ECPG_ARRAY_ERROR;
221 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POLYGONOID, ECPG_ARRAY_NONE, stmt->lineno))
222 : 0 : return ECPG_ARRAY_ERROR;
223 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LINEOID, ECPG_ARRAY_VECTOR, stmt->lineno))
224 : 0 : return ECPG_ARRAY_ERROR;
225 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT4OID, ECPG_ARRAY_NONE, stmt->lineno))
226 : 0 : return ECPG_ARRAY_ERROR;
227 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT8OID, ECPG_ARRAY_NONE, stmt->lineno))
228 : 0 : return ECPG_ARRAY_ERROR;
229 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), UNKNOWNOID, ECPG_ARRAY_NONE, stmt->lineno))
230 : 0 : return ECPG_ARRAY_ERROR;
231 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIRCLEOID, ECPG_ARRAY_NONE, stmt->lineno))
232 : 0 : return ECPG_ARRAY_ERROR;
233 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), MONEYOID, ECPG_ARRAY_NONE, stmt->lineno))
234 : 0 : return ECPG_ARRAY_ERROR;
235 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INETOID, ECPG_ARRAY_NONE, stmt->lineno))
236 : 0 : return ECPG_ARRAY_ERROR;
237 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDROID, ECPG_ARRAY_NONE, stmt->lineno))
238 : 0 : return ECPG_ARRAY_ERROR;
239 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BPCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
240 : 0 : return ECPG_ARRAY_ERROR;
241 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
242 : 0 : return ECPG_ARRAY_ERROR;
243 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), DATEOID, ECPG_ARRAY_NONE, stmt->lineno))
244 : 0 : return ECPG_ARRAY_ERROR;
245 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
246 : 0 : return ECPG_ARRAY_ERROR;
247 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPOID, ECPG_ARRAY_NONE, stmt->lineno))
248 : 0 : return ECPG_ARRAY_ERROR;
249 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPTZOID, ECPG_ARRAY_NONE, stmt->lineno))
250 : 0 : return ECPG_ARRAY_ERROR;
251 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INTERVALOID, ECPG_ARRAY_NONE, stmt->lineno))
252 : 0 : return ECPG_ARRAY_ERROR;
253 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMETZOID, ECPG_ARRAY_NONE, stmt->lineno))
254 : 0 : return ECPG_ARRAY_ERROR;
255 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BITOID, ECPG_ARRAY_NONE, stmt->lineno))
256 : 0 : return ECPG_ARRAY_ERROR;
257 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARBITOID, ECPG_ARRAY_NONE, stmt->lineno))
258 : 0 : return ECPG_ARRAY_ERROR;
259 [ - + ]: 132 : if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NUMERICOID, ECPG_ARRAY_NONE, stmt->lineno))
260 : 0 : return ECPG_ARRAY_ERROR;
261 : : }
262 : :
263 [ + + ]: 80242 : for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
264 : : {
265 [ + + ]: 80228 : if (cache_entry->oid == type)
266 : 2512 : return cache_entry->isarray;
267 : : }
268 : :
269 : 14 : array_query = ecpg_alloc(strlen("select typlen from pg_type where oid= and typelem<>0") + 11, stmt->lineno);
270 [ - + ]: 14 : if (array_query == NULL)
271 : 0 : return ECPG_ARRAY_ERROR;
272 : :
273 : 14 : sprintf(array_query, "select typlen from pg_type where oid=%u and typelem<>0", type);
274 : 14 : query = PQexec(stmt->connection->connection, array_query);
275 : 14 : ecpg_free(array_query);
276 [ - + ]: 14 : if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
277 : 0 : return ECPG_ARRAY_ERROR;
278 [ + - ]: 14 : else if (PQresultStatus(query) == PGRES_TUPLES_OK)
279 : : {
280 [ + + ]: 14 : if (PQntuples(query) == 0)
281 : 4 : isarray = ECPG_ARRAY_NONE;
282 : : else
283 : : {
284 [ + - ]: 10 : isarray = (atoi(PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
285 [ + - - + ]: 20 : if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
286 : 10 : ecpg_dynamic_type(type) == SQL3_CHARACTER_VARYING)
287 : : {
288 : : /*
289 : : * arrays of character strings are not yet implemented
290 : : */
291 : 0 : isarray = ECPG_ARRAY_NONE;
292 : : }
293 : : }
294 : 14 : PQclear(query);
295 : : }
296 : : else
297 : 0 : return ECPG_ARRAY_ERROR;
298 : :
299 : 14 : ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
300 [ + + ]: 18 : ecpg_log("ecpg_is_type_an_array on line %d: type (%u); C (%d); array (%s)\n",
301 [ - + ]: 18 : stmt->lineno, type, var->type, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
302 : 14 : return isarray;
303 : : }
304 : :
305 : :
306 : : bool
307 : 2526 : ecpg_store_result(const PGresult *results, int act_field,
308 : : const struct statement *stmt, struct variable *var)
309 : : {
310 : : enum ARRAY_TYPE isarray;
311 : : int act_tuple,
312 : 2526 : ntuples = PQntuples(results);
313 : 2526 : bool status = true;
314 : :
315 [ - + ]: 2526 : if ((isarray = ecpg_is_type_an_array(PQftype(results, act_field), stmt, var)) == ECPG_ARRAY_ERROR)
316 : : {
317 : 0 : ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
318 : 0 : return false;
319 : : }
320 : :
321 [ + + ]: 2526 : if (isarray == ECPG_ARRAY_NONE)
322 : : {
323 : : /*
324 : : * if we don't have enough space, we cannot read all tuples
325 : : */
326 [ + + + - : 2514 : if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
+ + - + ]
327 : : {
328 : 0 : ecpg_log("ecpg_store_result on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
329 : 0 : stmt->lineno, ntuples, var->arrsize);
330 [ # # # # ]: 0 : ecpg_raise(stmt->lineno, INFORMIX_MODE(stmt->compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_TOO_MANY_MATCHES, ECPG_SQLSTATE_CARDINALITY_VIOLATION, NULL);
331 : 0 : return false;
332 : : }
333 : : }
334 : : else
335 : : {
336 : : /*
337 : : * since we read an array, the variable has to be an array too
338 : : */
339 [ - + ]: 12 : if (var->arrsize == 0)
340 : : {
341 : 0 : ecpg_raise(stmt->lineno, ECPG_NO_ARRAY, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
342 : 0 : return false;
343 : : }
344 : : }
345 : :
346 : : /*
347 : : * allocate memory for NULL pointers
348 : : */
349 [ + + + + : 2526 : if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
+ + ]
350 : : {
351 : 1652 : int len = 0;
352 : :
353 [ + + ]: 1652 : if (!PQfformat(results, act_field))
354 : : {
355 [ + - + ]: 1650 : switch (var->type)
356 : : {
357 : 1642 : case ECPGt_char:
358 : : case ECPGt_unsigned_char:
359 : : case ECPGt_string:
360 [ + - + + ]: 1642 : if (!var->varcharsize && !var->arrsize)
361 : : {
362 : : /* special mode for handling char**foo=0 */
363 [ + + ]: 3250 : for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
364 : 1636 : len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
365 : 1614 : len *= var->offset; /* should be 1, but YMNK */
366 : 1614 : len += (ntuples + 1) * sizeof(char *);
367 : : }
368 : : else
369 : : {
370 : 28 : var->varcharsize = 0;
371 : : /* check strlen for each tuple */
372 [ + + ]: 56 : for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
373 : : {
374 : 28 : int slen = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
375 : :
376 [ + - ]: 28 : if (slen > var->varcharsize)
377 : 28 : var->varcharsize = slen;
378 : : }
379 : 28 : var->offset *= var->varcharsize;
380 : 28 : len = var->offset * ntuples;
381 : : }
382 : 1642 : break;
383 : 0 : case ECPGt_varchar:
384 : 0 : len = ntuples * (var->varcharsize + sizeof(int));
385 : 0 : break;
386 : 8 : default:
387 : 8 : len = var->offset * ntuples;
388 : 8 : break;
389 : : }
390 : : }
391 : : else
392 : : {
393 [ + + ]: 4 : for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
394 : 2 : len += PQgetlength(results, act_tuple, act_field);
395 : : }
396 : :
397 : 1652 : ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
398 : 1652 : var->value = ecpg_auto_alloc(len, stmt->lineno);
399 [ - + ]: 1652 : if (!var->value)
400 : 0 : return false;
401 : 1652 : *((char **) var->pointer) = var->value;
402 : : }
403 : :
404 : : /* allocate indicator variable if needed */
405 [ + + - + : 2526 : if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
+ + + + ]
406 : : {
407 : 20 : int len = var->ind_offset * ntuples;
408 : :
409 : 20 : var->ind_value = ecpg_auto_alloc(len, stmt->lineno);
410 [ - + ]: 20 : if (!var->ind_value)
411 : 0 : return false;
412 : 20 : *((char **) var->ind_pointer) = var->ind_value;
413 : : }
414 : :
415 : : /* fill the variable with the tuple(s) */
416 [ + + + + ]: 2526 : if (!var->varcharsize && !var->arrsize &&
417 [ - + - - : 1614 : (var->type == ECPGt_char || var->type == ECPGt_unsigned_char || var->type == ECPGt_string))
- - ]
418 : 1614 : {
419 : : /* special mode for handling char**foo=0 */
420 : :
421 : : /* filling the array of (char*)s */
422 : 1614 : char **current_string = (char **) var->value;
423 : :
424 : : /* storing the data (after the last array element) */
425 : 1614 : char *current_data_location = (char *) ¤t_string[ntuples + 1];
426 : :
427 [ + + + - ]: 3250 : for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
428 : : {
429 : 1636 : int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
430 : :
431 [ - + ]: 1636 : if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
432 : : var->type, var->ind_type, current_data_location,
433 : 1636 : var->ind_value, len, 0, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
434 : 0 : status = false;
435 : : else
436 : : {
437 : 1636 : *current_string = current_data_location;
438 : 1636 : current_data_location += len;
439 : 1636 : current_string++;
440 : : }
441 : : }
442 : :
443 : : /* terminate the list */
444 : 1614 : *current_string = NULL;
445 : : }
446 : : else
447 : : {
448 [ + + + - ]: 1992 : for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
449 : : {
450 [ + + ]: 1080 : if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
451 : 1080 : var->type, var->ind_type, var->value,
452 : 1080 : var->ind_value, var->varcharsize, var->offset, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
453 : 16 : status = false;
454 : : }
455 : : }
456 : 2526 : return status;
457 : : }
458 : :
459 : : static void
460 : 12 : sprintf_double_value(char *ptr, double value, const char *delim)
461 : : {
462 [ + + ]: 12 : if (isnan(value))
463 : 2 : sprintf(ptr, "%s%s", "NaN", delim);
464 [ + + ]: 10 : else if (isinf(value))
465 : : {
466 [ + + ]: 4 : if (value < 0)
467 : 2 : sprintf(ptr, "%s%s", "-Infinity", delim);
468 : : else
469 : 2 : sprintf(ptr, "%s%s", "Infinity", delim);
470 : : }
471 : : else
472 : 6 : sprintf(ptr, "%.15g%s", value, delim);
473 : 12 : }
474 : :
475 : : static void
476 : 2 : sprintf_float_value(char *ptr, float value, const char *delim)
477 : : {
478 [ - + ]: 2 : if (isnan(value))
479 : 0 : sprintf(ptr, "%s%s", "NaN", delim);
480 [ - + ]: 2 : else if (isinf(value))
481 : : {
482 [ # # ]: 0 : if (value < 0)
483 : 0 : sprintf(ptr, "%s%s", "-Infinity", delim);
484 : : else
485 : 0 : sprintf(ptr, "%s%s", "Infinity", delim);
486 : : }
487 : : else
488 : 2 : sprintf(ptr, "%.15g%s", value, delim);
489 : 2 : }
490 : :
491 : : static char *
492 : 0 : convert_bytea_to_string(char *from_data, int from_len, int lineno)
493 : : {
494 : : char *to_data;
495 : 0 : int to_len = ecpg_hex_enc_len(from_len) + 4 + 1; /* backslash + 'x' +
496 : : * quote + quote */
497 : :
498 : 0 : to_data = ecpg_alloc(to_len, lineno);
499 [ # # ]: 0 : if (!to_data)
500 : 0 : return NULL;
501 : :
502 : 0 : strcpy(to_data, "'\\x");
503 : 0 : ecpg_hex_encode(from_data, from_len, to_data + 3);
504 : 0 : strcpy(to_data + 3 + ecpg_hex_enc_len(from_len), "\'");
505 : :
506 : 0 : return to_data;
507 : : }
508 : :
509 : : bool
510 : 3704 : ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var,
511 : : char **tobeinserted_p, bool quote)
512 : : {
513 : 3704 : char *mallocedval = NULL;
514 : 3704 : 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 : 3704 : *tobeinserted_p = "";
528 : :
529 : : /* check for null value and set input buffer accordingly */
530 [ - + - - : 3704 : switch (var->ind_type)
+ + ]
531 : : {
532 : 0 : case ECPGt_short:
533 : : case ECPGt_unsigned_short:
534 [ # # ]: 0 : if (*(short *) var->ind_value < 0)
535 : 0 : *tobeinserted_p = NULL;
536 : 0 : break;
537 : 10 : case ECPGt_int:
538 : : case ECPGt_unsigned_int:
539 [ + + ]: 10 : if (*(int *) var->ind_value < 0)
540 : 6 : *tobeinserted_p = NULL;
541 : 10 : break;
542 : 0 : case ECPGt_long:
543 : : case ECPGt_unsigned_long:
544 [ # # ]: 0 : if (*(long *) var->ind_value < 0L)
545 : 0 : *tobeinserted_p = NULL;
546 : 0 : break;
547 : 0 : case ECPGt_long_long:
548 : : case ECPGt_unsigned_long_long:
549 [ # # ]: 0 : if (*(long long int *) var->ind_value < (long long) 0)
550 : 0 : *tobeinserted_p = NULL;
551 : 0 : break;
552 : 3668 : case ECPGt_NO_INDICATOR:
553 [ + + ]: 3668 : if (force_indicator == false)
554 : : {
555 [ + + ]: 34 : if (ECPGis_noind_null(var->type, var->value))
556 : 18 : *tobeinserted_p = NULL;
557 : : }
558 : 3668 : break;
559 : 26 : default:
560 : 26 : break;
561 : : }
562 [ + + ]: 3704 : if (*tobeinserted_p != NULL)
563 : : {
564 [ + + ]: 3680 : int asize = var->arrsize ? var->arrsize : 1;
565 : :
566 [ + + - - : 3680 : switch (var->type)
+ - - - +
+ + + + +
+ + + + +
- - ]
567 : : {
568 : : int element;
569 : :
570 : 8 : case ECPGt_short:
571 [ - + ]: 8 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
572 : 0 : return false;
573 : :
574 [ + + ]: 8 : if (asize > 1)
575 : : {
576 : 4 : strcpy(mallocedval, "{");
577 : :
578 [ + + ]: 44 : for (element = 0; element < asize; element++)
579 : 40 : sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
580 : :
581 : 4 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
582 : : }
583 : : else
584 : 4 : sprintf(mallocedval, "%hd", *((short *) var->value));
585 : :
586 : 8 : *tobeinserted_p = mallocedval;
587 : 8 : break;
588 : :
589 : 2552 : case ECPGt_int:
590 [ - + ]: 2552 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
591 : 0 : return false;
592 : :
593 [ - + ]: 2552 : if (asize > 1)
594 : : {
595 : 0 : strcpy(mallocedval, "{");
596 : :
597 [ # # ]: 0 : for (element = 0; element < asize; element++)
598 : 0 : sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
599 : :
600 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
601 : : }
602 : : else
603 : 2552 : sprintf(mallocedval, "%d", *((int *) var->value));
604 : :
605 : 2552 : *tobeinserted_p = mallocedval;
606 : 2552 : break;
607 : :
608 : 0 : case ECPGt_unsigned_short:
609 [ # # ]: 0 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
610 : 0 : return false;
611 : :
612 [ # # ]: 0 : if (asize > 1)
613 : : {
614 : 0 : strcpy(mallocedval, "{");
615 : :
616 [ # # ]: 0 : for (element = 0; element < asize; element++)
617 : 0 : sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
618 : :
619 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
620 : : }
621 : : else
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:
628 [ # # ]: 0 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
629 : 0 : return false;
630 : :
631 [ # # ]: 0 : if (asize > 1)
632 : : {
633 : 0 : strcpy(mallocedval, "{");
634 : :
635 [ # # ]: 0 : for (element = 0; element < asize; element++)
636 : 0 : sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
637 : :
638 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
639 : : }
640 : : else
641 : 0 : sprintf(mallocedval, "%u", *((unsigned int *) var->value));
642 : :
643 : 0 : *tobeinserted_p = mallocedval;
644 : 0 : break;
645 : :
646 : 10 : case ECPGt_long:
647 [ - + ]: 10 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
648 : 0 : return false;
649 : :
650 [ - + ]: 10 : if (asize > 1)
651 : : {
652 : 0 : strcpy(mallocedval, "{");
653 : :
654 [ # # ]: 0 : for (element = 0; element < asize; element++)
655 : 0 : sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
656 : :
657 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
658 : : }
659 : : else
660 : 10 : sprintf(mallocedval, "%ld", *((long *) var->value));
661 : :
662 : 10 : *tobeinserted_p = mallocedval;
663 : 10 : break;
664 : :
665 : 0 : case ECPGt_unsigned_long:
666 [ # # ]: 0 : if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
667 : 0 : return false;
668 : :
669 [ # # ]: 0 : if (asize > 1)
670 : : {
671 : 0 : strcpy(mallocedval, "{");
672 : :
673 [ # # ]: 0 : for (element = 0; element < asize; element++)
674 : 0 : sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
675 : :
676 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
677 : : }
678 : : else
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:
685 [ # # ]: 0 : if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
686 : 0 : return false;
687 : :
688 [ # # ]: 0 : if (asize > 1)
689 : : {
690 : 0 : strcpy(mallocedval, "{");
691 : :
692 [ # # ]: 0 : for (element = 0; element < asize; element++)
693 : 0 : sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
694 : :
695 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
696 : : }
697 : : else
698 : 0 : sprintf(mallocedval, "%lld", *((long long int *) var->value));
699 : :
700 : 0 : *tobeinserted_p = mallocedval;
701 : 0 : break;
702 : :
703 : 0 : case ECPGt_unsigned_long_long:
704 [ # # ]: 0 : if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
705 : 0 : return false;
706 : :
707 [ # # ]: 0 : if (asize > 1)
708 : : {
709 : 0 : strcpy(mallocedval, "{");
710 : :
711 [ # # ]: 0 : for (element = 0; element < asize; element++)
712 : 0 : sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
713 : :
714 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
715 : : }
716 : : else
717 : 0 : sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
718 : :
719 : 0 : *tobeinserted_p = mallocedval;
720 : 0 : break;
721 : :
722 : 2 : case ECPGt_float:
723 [ - + ]: 2 : if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
724 : 0 : return false;
725 : :
726 [ - + ]: 2 : if (asize > 1)
727 : : {
728 : 0 : strcpy(mallocedval, "{");
729 : :
730 [ # # ]: 0 : for (element = 0; element < asize; element++)
731 : 0 : sprintf_float_value(mallocedval + strlen(mallocedval), ((float *) var->value)[element], ",");
732 : :
733 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
734 : : }
735 : : else
736 : 2 : sprintf_float_value(mallocedval, *((float *) var->value), "");
737 : :
738 : 2 : *tobeinserted_p = mallocedval;
739 : 2 : break;
740 : :
741 : 12 : case ECPGt_double:
742 [ - + ]: 12 : if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
743 : 0 : return false;
744 : :
745 [ - + ]: 12 : if (asize > 1)
746 : : {
747 : 0 : strcpy(mallocedval, "{");
748 : :
749 [ # # ]: 0 : for (element = 0; element < asize; element++)
750 : 0 : sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
751 : :
752 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
753 : : }
754 : : else
755 : 12 : sprintf_double_value(mallocedval, *((double *) var->value), "");
756 : :
757 : 12 : *tobeinserted_p = mallocedval;
758 : 12 : break;
759 : :
760 : 4 : case ECPGt_bool:
761 [ - + ]: 4 : if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
762 : 0 : return false;
763 : :
764 [ - + ]: 4 : if (var->arrsize > 1)
765 : : {
766 : 0 : strcpy(mallocedval, "{");
767 : :
768 [ # # ]: 0 : for (element = 0; element < asize; element++)
769 [ # # ]: 0 : sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
770 : :
771 : 0 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
772 : : }
773 : : else
774 : : {
775 [ + - ]: 4 : if (var->offset == sizeof(char))
776 [ + - ]: 4 : sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
777 [ # # ]: 0 : else if (var->offset == sizeof(int))
778 [ # # ]: 0 : sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
779 : : else
780 : 0 : ecpg_raise(lineno, ECPG_CONVERT_BOOL, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
781 : : }
782 : :
783 : 4 : *tobeinserted_p = mallocedval;
784 : 4 : break;
785 : :
786 : 924 : case ECPGt_char:
787 : : case ECPGt_unsigned_char:
788 : : case ECPGt_string:
789 : : {
790 : : /* set slen to string length if type is char * */
791 [ + + ]: 924 : int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
792 : :
793 [ - + ]: 924 : if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
794 : 0 : return false;
795 : :
796 : 924 : strncpy(newcopy, (char *) var->value, slen);
797 : 924 : newcopy[slen] = '\0';
798 : :
799 : 924 : mallocedval = quote_postgres(newcopy, quote, lineno);
800 [ - + ]: 924 : if (!mallocedval)
801 : : {
802 : 0 : ecpg_free(newcopy);
803 : 0 : return false;
804 : : }
805 : :
806 : 924 : *tobeinserted_p = mallocedval;
807 : : }
808 : 924 : break;
809 : 74 : case ECPGt_const:
810 : : case ECPGt_char_variable:
811 : : {
812 : 74 : int slen = strlen((char *) var->value);
813 : :
814 [ - + ]: 74 : if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
815 : 0 : return false;
816 : :
817 : 74 : strncpy(mallocedval, (char *) var->value, slen);
818 : 74 : mallocedval[slen] = '\0';
819 : :
820 : 74 : *tobeinserted_p = mallocedval;
821 : : }
822 : 74 : break;
823 : :
824 : 26 : case ECPGt_bytea:
825 : : {
826 : 26 : struct ECPGgeneric_bytea *variable =
827 : : (struct ECPGgeneric_bytea *) (var->value);
828 : :
829 [ - + ]: 26 : if (!(mallocedval = ecpg_alloc(variable->len, lineno)))
830 : 0 : return false;
831 : :
832 : 26 : memcpy(mallocedval, variable->arr, variable->len);
833 : 26 : *tobeinserted_p = mallocedval;
834 : : }
835 : 26 : break;
836 : :
837 : 26 : case ECPGt_varchar:
838 : : {
839 : 26 : struct ECPGgeneric_varchar *variable =
840 : : (struct ECPGgeneric_varchar *) (var->value);
841 : :
842 [ - + ]: 26 : if (!(newcopy = ecpg_alloc(variable->len + 1, lineno)))
843 : 0 : return false;
844 : :
845 : 26 : strncpy(newcopy, variable->arr, variable->len);
846 : 26 : newcopy[variable->len] = '\0';
847 : :
848 : 26 : mallocedval = quote_postgres(newcopy, quote, lineno);
849 [ - + ]: 26 : if (!mallocedval)
850 : : {
851 : 0 : ecpg_free(newcopy);
852 : 0 : return false;
853 : : }
854 : :
855 : 26 : *tobeinserted_p = mallocedval;
856 : : }
857 : 26 : break;
858 : :
859 : 14 : case ECPGt_decimal:
860 : : case ECPGt_numeric:
861 : : {
862 : 14 : char *str = NULL;
863 : : int slen;
864 : : numeric *nval;
865 : :
866 [ + + ]: 14 : if (var->arrsize > 1)
867 : 6 : mallocedval = ecpg_strdup("{", lineno, NULL);
868 : : else
869 : 8 : mallocedval = ecpg_strdup("", lineno, NULL);
870 : :
871 [ - + ]: 14 : if (!mallocedval)
872 : 0 : return false;
873 : :
874 [ + + ]: 82 : for (element = 0; element < asize; element++)
875 : : {
876 : : int result;
877 : :
878 : 68 : nval = PGTYPESnumeric_new();
879 [ - + ]: 68 : if (!nval)
880 : : {
881 : 0 : ecpg_free(mallocedval);
882 : 0 : return false;
883 : : }
884 : :
885 [ + + ]: 68 : if (var->type == ECPGt_numeric)
886 : 64 : result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
887 : : else
888 : 4 : result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
889 : :
890 [ - + ]: 68 : if (result != 0)
891 : : {
892 : 0 : PGTYPESnumeric_free(nval);
893 : 0 : ecpg_free(mallocedval);
894 : 0 : return false;
895 : : }
896 : :
897 : 68 : str = PGTYPESnumeric_to_asc(nval, nval->dscale);
898 : 68 : PGTYPESnumeric_free(nval);
899 [ - + ]: 68 : if (!str)
900 : : {
901 : 0 : ecpg_free(mallocedval);
902 : 0 : return false;
903 : : }
904 : 68 : slen = strlen(str);
905 : :
906 [ - + ]: 68 : if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
907 : : {
908 : 0 : ecpg_free(mallocedval);
909 : 0 : ecpg_free(str);
910 : 0 : return false;
911 : : }
912 : 68 : mallocedval = newcopy;
913 : :
914 : : /* also copy trailing '\0' */
915 : 68 : memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
916 [ + + ]: 68 : if (var->arrsize > 1)
917 : 60 : strcpy(mallocedval + strlen(mallocedval), ",");
918 : :
919 : 68 : ecpg_free(str);
920 : : }
921 : :
922 [ + + ]: 14 : if (var->arrsize > 1)
923 : 6 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
924 : :
925 : 14 : *tobeinserted_p = mallocedval;
926 : : }
927 : 14 : break;
928 : :
929 : 6 : case ECPGt_interval:
930 : : {
931 : 6 : char *str = NULL;
932 : : int slen;
933 : :
934 [ + - ]: 6 : if (var->arrsize > 1)
935 : 6 : mallocedval = ecpg_strdup("{", lineno, NULL);
936 : : else
937 : 0 : mallocedval = ecpg_strdup("", lineno, NULL);
938 : :
939 [ - + ]: 6 : if (!mallocedval)
940 : 0 : return false;
941 : :
942 [ + + ]: 66 : for (element = 0; element < asize; element++)
943 : : {
944 : 60 : str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
945 [ - + ]: 60 : if (!str)
946 : : {
947 : 0 : ecpg_free(mallocedval);
948 : 0 : return false;
949 : : }
950 : :
951 : 60 : slen = strlen(str);
952 : :
953 [ - + ]: 60 : if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
954 : : {
955 : 0 : ecpg_free(mallocedval);
956 : 0 : ecpg_free(str);
957 : 0 : return false;
958 : : }
959 : 60 : mallocedval = newcopy;
960 : :
961 : : /* also copy trailing '\0' */
962 : 60 : memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
963 [ + - ]: 60 : if (var->arrsize > 1)
964 : 60 : strcpy(mallocedval + strlen(mallocedval), ",");
965 : :
966 : 60 : ecpg_free(str);
967 : : }
968 : :
969 [ + - ]: 6 : if (var->arrsize > 1)
970 : 6 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
971 : :
972 : 6 : *tobeinserted_p = mallocedval;
973 : : }
974 : 6 : break;
975 : :
976 : 10 : case ECPGt_date:
977 : : {
978 : 10 : char *str = NULL;
979 : : int slen;
980 : :
981 [ + + ]: 10 : if (var->arrsize > 1)
982 : 6 : mallocedval = ecpg_strdup("{", lineno, NULL);
983 : : else
984 : 4 : mallocedval = ecpg_strdup("", lineno, NULL);
985 : :
986 [ - + ]: 10 : if (!mallocedval)
987 : 0 : return false;
988 : :
989 [ + + ]: 74 : for (element = 0; element < asize; element++)
990 : : {
991 : 64 : str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
992 [ - + ]: 64 : if (!str)
993 : : {
994 : 0 : ecpg_free(mallocedval);
995 : 0 : return false;
996 : : }
997 : :
998 : 64 : slen = strlen(str);
999 : :
1000 [ - + ]: 64 : if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1001 : : {
1002 : 0 : ecpg_free(mallocedval);
1003 : 0 : ecpg_free(str);
1004 : 0 : return false;
1005 : : }
1006 : 64 : mallocedval = newcopy;
1007 : :
1008 : : /* also copy trailing '\0' */
1009 : 64 : memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1010 [ + + ]: 64 : if (var->arrsize > 1)
1011 : 60 : strcpy(mallocedval + strlen(mallocedval), ",");
1012 : :
1013 : 64 : ecpg_free(str);
1014 : : }
1015 : :
1016 [ + + ]: 10 : if (var->arrsize > 1)
1017 : 6 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1018 : :
1019 : 10 : *tobeinserted_p = mallocedval;
1020 : : }
1021 : 10 : break;
1022 : :
1023 : 12 : case ECPGt_timestamp:
1024 : : {
1025 : 12 : char *str = NULL;
1026 : : int slen;
1027 : :
1028 [ + + ]: 12 : if (var->arrsize > 1)
1029 : 6 : mallocedval = ecpg_strdup("{", lineno, NULL);
1030 : : else
1031 : 6 : mallocedval = ecpg_strdup("", lineno, NULL);
1032 : :
1033 [ - + ]: 12 : if (!mallocedval)
1034 : 0 : return false;
1035 : :
1036 [ + + ]: 78 : for (element = 0; element < asize; element++)
1037 : : {
1038 : 66 : str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1039 [ - + ]: 66 : if (!str)
1040 : : {
1041 : 0 : ecpg_free(mallocedval);
1042 : 0 : return false;
1043 : : }
1044 : :
1045 : 66 : slen = strlen(str);
1046 : :
1047 [ - + ]: 66 : if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1048 : : {
1049 : 0 : ecpg_free(mallocedval);
1050 : 0 : ecpg_free(str);
1051 : 0 : return false;
1052 : : }
1053 : 66 : mallocedval = newcopy;
1054 : :
1055 : : /* also copy trailing '\0' */
1056 : 66 : memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1057 [ + + ]: 66 : if (var->arrsize > 1)
1058 : 60 : strcpy(mallocedval + strlen(mallocedval), ",");
1059 : :
1060 : 66 : ecpg_free(str);
1061 : : }
1062 : :
1063 [ + + ]: 12 : if (var->arrsize > 1)
1064 : 6 : strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1065 : :
1066 : 12 : *tobeinserted_p = mallocedval;
1067 : : }
1068 : 12 : break;
1069 : :
1070 : 0 : case ECPGt_descriptor:
1071 : : case ECPGt_sqlda:
1072 : 0 : break;
1073 : :
1074 : 0 : default:
1075 : : /* Not implemented yet */
1076 : 0 : ecpg_raise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_type_name(var->type));
1077 : 0 : return false;
1078 : : break;
1079 : : }
1080 : : }
1081 : 3704 : return true;
1082 : : }
1083 : :
1084 : : static void
1085 : 3488 : print_param_value(char *value, int len, int is_binary, int lineno, int nth)
1086 : : {
1087 : : char *value_s;
1088 : 3488 : bool malloced = false;
1089 : :
1090 [ + + ]: 3488 : if (value == NULL)
1091 : 24 : value_s = "null";
1092 [ + + ]: 3464 : else if (!is_binary)
1093 : 3438 : value_s = value;
1094 : : else
1095 : : {
1096 : 26 : value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1097 [ + - ]: 26 : if (value_s != NULL)
1098 : : {
1099 : 26 : ecpg_hex_encode(value, len, value_s);
1100 : 26 : value_s[ecpg_hex_enc_len(len)] = '\0';
1101 : 26 : malloced = true;
1102 : : }
1103 : : else
1104 : 0 : value_s = "no memory for logging of parameter";
1105 : : }
1106 : :
1107 : 3488 : ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1108 : : lineno, nth, value_s);
1109 : :
1110 [ + + ]: 3488 : if (malloced)
1111 : 26 : ecpg_free(value_s);
1112 : 3488 : }
1113 : :
1114 : : void
1115 : 5355 : ecpg_free_params(struct statement *stmt, bool print)
1116 : : {
1117 : : int n;
1118 : :
1119 [ + + ]: 8843 : for (n = 0; n < stmt->nparams; n++)
1120 : : {
1121 [ + - ]: 3488 : if (print)
1122 : 3488 : print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1123 : 3488 : stmt->paramformats[n], stmt->lineno, n + 1);
1124 : 3488 : ecpg_free(stmt->paramvalues[n]);
1125 : : }
1126 : 5355 : ecpg_free(stmt->paramvalues);
1127 : 5355 : ecpg_free(stmt->paramlengths);
1128 : 5355 : ecpg_free(stmt->paramformats);
1129 : 5355 : stmt->paramvalues = NULL;
1130 : 5355 : stmt->paramlengths = NULL;
1131 : 5355 : stmt->paramformats = NULL;
1132 : 5355 : stmt->nparams = 0;
1133 : 5355 : }
1134 : :
1135 : : static bool
1136 : 194 : insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
1137 : : {
1138 : : char *newcopy;
1139 : :
1140 [ - + ]: 194 : if (!(newcopy = ecpg_alloc(strlen(stmt->command) + strlen(tobeinserted) + 1, stmt->lineno)))
1141 : : {
1142 : 0 : ecpg_free(tobeinserted);
1143 : 0 : return false;
1144 : : }
1145 : :
1146 : 194 : strcpy(newcopy, stmt->command);
1147 : 194 : 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 : 194 : strcat(newcopy,
1154 : 194 : stmt->command
1155 : : + position
1156 : 194 : + ph_len - 1);
1157 : :
1158 : 194 : ecpg_free(stmt->command);
1159 : 194 : stmt->command = newcopy;
1160 : :
1161 : 194 : ecpg_free(tobeinserted);
1162 : 194 : return true;
1163 : : }
1164 : :
1165 : : static bool
1166 : 30 : 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 [ + + ]: 30 : if (desc_item->is_binary)
1177 : : {
1178 [ - + ]: 4 : if (!(*tobeinserted = ecpg_alloc(desc_item->data_len, stmt->lineno)))
1179 : 0 : return false;
1180 : 4 : memcpy(*tobeinserted, desc_item->data, desc_item->data_len);
1181 : 4 : return true;
1182 : : }
1183 : :
1184 : 26 : var.type = ECPGt_char;
1185 : 26 : var.varcharsize = strlen(desc_item->data);
1186 : 26 : var.value = desc_item->data;
1187 : 26 : var.pointer = &(desc_item->data);
1188 : 26 : var.arrsize = 1;
1189 : 26 : var.offset = 0;
1190 : :
1191 [ + + ]: 26 : if (!desc_item->indicator)
1192 : : {
1193 : 22 : var.ind_type = ECPGt_NO_INDICATOR;
1194 : 22 : var.ind_value = var.ind_pointer = NULL;
1195 : 22 : var.ind_varcharsize = var.ind_arrsize = var.ind_offset = 0;
1196 : : }
1197 : : else
1198 : : {
1199 : 4 : var.ind_type = ECPGt_int;
1200 : 4 : var.ind_value = &(desc_item->indicator);
1201 : 4 : var.ind_pointer = &(var.ind_value);
1202 : 4 : var.ind_varcharsize = var.ind_arrsize = 1;
1203 : 4 : var.ind_offset = 0;
1204 : : }
1205 : :
1206 [ - + ]: 26 : if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &var, tobeinserted, false))
1207 : 0 : return false;
1208 : :
1209 : 26 : 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
1220 : 5355 : ecpg_build_params(struct statement *stmt)
1221 : : {
1222 : : struct variable *var;
1223 : 5355 : int desc_counter = 0;
1224 : 5355 : int position = 0;
1225 : : const char *value;
1226 : 5355 : bool std_strings = false;
1227 : :
1228 : : /* Get standard_conforming_strings setting. */
1229 : 5355 : value = PQparameterStatus(stmt->connection->connection, "standard_conforming_strings");
1230 [ + - + - ]: 5355 : if (value && strcmp(value, "on") == 0)
1231 : 5355 : 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 : : */
1238 : 5355 : var = stmt->inlist;
1239 [ + + ]: 9037 : while (var)
1240 : : {
1241 : : char *tobeinserted;
1242 : 3682 : int counter = 1;
1243 : : bool binary_format;
1244 : : int binary_length;
1245 : :
1246 : :
1247 : 3682 : tobeinserted = NULL;
1248 : 3682 : binary_length = 0;
1249 : 3682 : binary_format = false;
1250 : :
1251 : : /*
1252 : : * A descriptor is a special case since it contains many variables but
1253 : : * is listed only once.
1254 : : */
1255 [ + + ]: 3682 : 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 : :
1264 : 30 : desc = ecpg_find_desc(stmt->lineno, var->pointer);
1265 [ - + ]: 30 : if (desc == NULL)
1266 : 0 : return false;
1267 : :
1268 : 30 : desc_counter++;
1269 [ + - ]: 46 : for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1270 : : {
1271 [ + + ]: 46 : if (desc_item->num != desc_counter)
1272 : 16 : continue;
1273 : :
1274 [ - + ]: 30 : if (!store_input_from_desc(stmt, desc_item, &tobeinserted))
1275 : 0 : return false;
1276 : :
1277 [ + + ]: 30 : if (desc_item->is_binary)
1278 : : {
1279 : 4 : binary_length = desc_item->data_len;
1280 : 4 : binary_format = true;
1281 : : }
1282 : 30 : break;
1283 : : }
1284 [ + + ]: 30 : if (desc->count == desc_counter)
1285 : 16 : desc_counter = 0;
1286 : : }
1287 [ + + ]: 3652 : else if (var->type == ECPGt_sqlda)
1288 : : {
1289 [ + + - + ]: 8 : if (INFORMIX_MODE(stmt->compat))
1290 : 4 : {
1291 : 4 : struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1292 : : struct variable desc_inlist;
1293 : : int i;
1294 : :
1295 [ - + ]: 4 : if (sqlda == NULL)
1296 : 0 : return false;
1297 : :
1298 : 4 : desc_counter++;
1299 [ + - ]: 4 : for (i = 0; i < sqlda->sqld; i++)
1300 : : {
1301 [ + - ]: 4 : if (i + 1 == desc_counter)
1302 : : {
1303 : 4 : desc_inlist.type = sqlda->sqlvar[i].sqltype;
1304 : 4 : desc_inlist.value = sqlda->sqlvar[i].sqldata;
1305 : 4 : desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1306 [ - + ]: 4 : switch (desc_inlist.type)
1307 : : {
1308 : 0 : case ECPGt_char:
1309 : : case ECPGt_varchar:
1310 : 0 : desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1311 : 0 : break;
1312 : 4 : default:
1313 : 4 : desc_inlist.varcharsize = 0;
1314 : 4 : break;
1315 : : }
1316 : 4 : desc_inlist.arrsize = 1;
1317 : 4 : desc_inlist.offset = 0;
1318 [ - + ]: 4 : if (sqlda->sqlvar[i].sqlind)
1319 : : {
1320 : 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 : : {
1331 : 4 : desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1332 : 4 : desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1333 : 4 : desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1334 : : }
1335 [ - + ]: 4 : if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1336 : 0 : return false;
1337 : :
1338 : 4 : break;
1339 : : }
1340 : : }
1341 [ + - ]: 4 : if (sqlda->sqld == desc_counter)
1342 : 4 : desc_counter = 0;
1343 : : }
1344 : : else
1345 : : {
1346 : 4 : struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1347 : : struct variable desc_inlist;
1348 : : int i;
1349 : :
1350 [ - + ]: 4 : if (sqlda == NULL)
1351 : 0 : return false;
1352 : :
1353 : 4 : desc_counter++;
1354 [ + - ]: 4 : for (i = 0; i < sqlda->sqln; i++)
1355 : : {
1356 [ + - ]: 4 : if (i + 1 == desc_counter)
1357 : : {
1358 : 4 : desc_inlist.type = sqlda->sqlvar[i].sqltype;
1359 : 4 : desc_inlist.value = sqlda->sqlvar[i].sqldata;
1360 : 4 : desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1361 [ - + ]: 4 : switch (desc_inlist.type)
1362 : : {
1363 : 0 : case ECPGt_char:
1364 : : case ECPGt_varchar:
1365 : 0 : desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1366 : 0 : break;
1367 : 4 : default:
1368 : 4 : desc_inlist.varcharsize = 0;
1369 : 4 : break;
1370 : : }
1371 : 4 : desc_inlist.arrsize = 1;
1372 : 4 : desc_inlist.offset = 0;
1373 [ - + ]: 4 : if (sqlda->sqlvar[i].sqlind)
1374 : : {
1375 : 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 : : {
1386 : 4 : desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1387 : 4 : desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1388 : 4 : desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1389 : : }
1390 [ - + ]: 4 : if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1391 : 0 : return false;
1392 : :
1393 : 4 : break;
1394 : : }
1395 : : }
1396 [ + - ]: 4 : if (sqlda->sqln == desc_counter)
1397 : 4 : desc_counter = 0;
1398 : : }
1399 : : }
1400 : : else
1401 : : {
1402 [ - + ]: 3644 : if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1403 : 0 : return false;
1404 : :
1405 [ + + ]: 3644 : if (var->type == ECPGt_bytea)
1406 : : {
1407 : 22 : binary_length = ((struct ECPGgeneric_bytea *) (var->value))->len;
1408 : 22 : binary_format = true;
1409 : : }
1410 : : }
1411 : :
1412 : : /*
1413 : : * now tobeinserted points to an area that contains the next
1414 : : * parameter; now find the position in the string where it belongs
1415 : : */
1416 [ - + ]: 3682 : if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1417 : : {
1418 : : /*
1419 : : * We have an argument but we don't have the matched up
1420 : : * placeholder in the string
1421 : : */
1422 : 0 : ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS,
1423 : : ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS,
1424 : : NULL);
1425 : 0 : ecpg_free_params(stmt, false);
1426 : 0 : ecpg_free(tobeinserted);
1427 : 0 : return false;
1428 : : }
1429 : :
1430 : : /*
1431 : : * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1432 : : * to simulate a dynamic cursor because there is no backend
1433 : : * functionality for it
1434 : : */
1435 [ + + ]: 3682 : if (var->type == ECPGt_char_variable)
1436 : : {
1437 [ - + ]: 42 : int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1438 : :
1439 [ - + ]: 42 : if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1440 : : {
1441 : 0 : ecpg_free_params(stmt, false);
1442 : 0 : return false;
1443 : : }
1444 : 42 : tobeinserted = NULL;
1445 : : }
1446 : :
1447 : : /*
1448 : : * if the placeholder is '$0' we have to replace it on the client side
1449 : : * this is for places we want to support variables at that are not
1450 : : * supported in the backend
1451 : : */
1452 [ + + ]: 3640 : else if (stmt->command[position] == '0')
1453 : : {
1454 [ + + ]: 124 : if (stmt->statement_type == ECPGst_prepare ||
1455 [ + + ]: 114 : stmt->statement_type == ECPGst_exec_with_exprlist)
1456 : : {
1457 : : /* Need to double-quote the inserted statement name. */
1458 : 28 : char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1459 : : stmt->lineno);
1460 : :
1461 [ - + ]: 28 : if (!str)
1462 : : {
1463 : 0 : ecpg_free(tobeinserted);
1464 : 0 : ecpg_free_params(stmt, false);
1465 : 0 : return false;
1466 : : }
1467 : 28 : sprintf(str, "\"%s\"", tobeinserted);
1468 : 28 : ecpg_free(tobeinserted);
1469 : 28 : tobeinserted = str;
1470 : : }
1471 : :
1472 [ - + ]: 124 : if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1473 : : {
1474 : 0 : ecpg_free_params(stmt, false);
1475 : 0 : return false;
1476 : : }
1477 : 124 : tobeinserted = NULL;
1478 : : }
1479 [ + + ]: 3516 : else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1480 : : {
1481 [ - + ]: 28 : if (binary_format)
1482 : : {
1483 : 0 : char *p = convert_bytea_to_string(tobeinserted,
1484 : : binary_length,
1485 : : stmt->lineno);
1486 : :
1487 : 0 : ecpg_free(tobeinserted);
1488 [ # # ]: 0 : if (!p)
1489 : : {
1490 : 0 : ecpg_free_params(stmt, false);
1491 : 0 : return false;
1492 : : }
1493 : 0 : tobeinserted = p;
1494 : : }
1495 : :
1496 [ - + ]: 28 : if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1497 : : {
1498 : 0 : ecpg_free_params(stmt, false);
1499 : 0 : return false;
1500 : : }
1501 : 28 : tobeinserted = NULL;
1502 : : }
1503 : : else
1504 : : {
1505 : 3488 : bool realloc_failed = false;
1506 : : char **newparamvalues;
1507 : : int *newparamlengths;
1508 : : int *newparamformats;
1509 : :
1510 : : /* enlarge all the param arrays */
1511 [ + - ]: 3488 : if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1512 : 3488 : stmt->paramvalues = newparamvalues;
1513 : : else
1514 : 0 : realloc_failed = true;
1515 : :
1516 [ + - ]: 3488 : if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1517 : 3488 : stmt->paramlengths = newparamlengths;
1518 : : else
1519 : 0 : realloc_failed = true;
1520 : :
1521 [ + - ]: 3488 : if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1522 : 3488 : stmt->paramformats = newparamformats;
1523 : : else
1524 : 0 : realloc_failed = true;
1525 : :
1526 [ - + ]: 3488 : if (realloc_failed)
1527 : : {
1528 : 0 : ecpg_free_params(stmt, false);
1529 : 0 : ecpg_free(tobeinserted);
1530 : 0 : return false;
1531 : : }
1532 : :
1533 : : /* only now can we assign ownership of "tobeinserted" to stmt */
1534 : 3488 : stmt->paramvalues[stmt->nparams] = tobeinserted;
1535 : 3488 : stmt->paramlengths[stmt->nparams] = binary_length;
1536 : 3488 : stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1537 : 3488 : stmt->nparams++;
1538 : :
1539 : : /* let's see if this was an old style placeholder */
1540 [ - + ]: 3488 : if (stmt->command[position] == '?')
1541 : : {
1542 : : /* yes, replace with new style */
1543 : 0 : int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1544 : : * size we need */
1545 : :
1546 [ # # ]: 0 : if (!(tobeinserted = ecpg_alloc(buffersize, stmt->lineno)))
1547 : : {
1548 : 0 : ecpg_free_params(stmt, false);
1549 : 0 : return false;
1550 : : }
1551 : :
1552 : 0 : snprintf(tobeinserted, buffersize, "$%d", counter++);
1553 : :
1554 [ # # ]: 0 : if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1555 : : {
1556 : 0 : ecpg_free_params(stmt, false);
1557 : 0 : return false;
1558 : : }
1559 : 0 : tobeinserted = NULL;
1560 : : }
1561 : : }
1562 : :
1563 [ + + ]: 3682 : if (desc_counter == 0)
1564 : 3668 : var = var->next;
1565 : : }
1566 : :
1567 : : /*
1568 : : * Check if there are unmatched things left. PREPARE AS has no parameter.
1569 : : * Check other statement.
1570 : : */
1571 [ + + - + ]: 10700 : if (stmt->statement_type != ECPGst_prepare &&
1572 : 5345 : next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1573 : : {
1574 : 0 : ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS,
1575 : : ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS, NULL);
1576 : 0 : ecpg_free_params(stmt, false);
1577 : 0 : return false;
1578 : : }
1579 : :
1580 : 5355 : return true;
1581 : : }
1582 : :
1583 : : /*
1584 : : * ecpg_autostart_transaction
1585 : : * If we are in non-autocommit mode, automatically start a transaction.
1586 : : */
1587 : : bool
1588 : 5355 : ecpg_autostart_transaction(struct statement *stmt)
1589 : : {
1590 [ + + + + ]: 5355 : if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
1591 : : {
1592 : 191 : stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1593 [ - + ]: 191 : if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1594 : : {
1595 : 0 : ecpg_free_params(stmt, false);
1596 : 0 : return false;
1597 : : }
1598 : 191 : PQclear(stmt->results);
1599 : 191 : stmt->results = NULL;
1600 : : }
1601 : 5355 : return true;
1602 : : }
1603 : :
1604 : : /*
1605 : : * ecpg_execute
1606 : : * Execute the SQL statement.
1607 : : */
1608 : : bool
1609 : 5355 : ecpg_execute(struct statement *stmt)
1610 : : {
1611 : 5355 : 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);
1612 [ + + ]: 5355 : if (stmt->statement_type == ECPGst_execute)
1613 : : {
1614 : 3336 : stmt->results = PQexecPrepared(stmt->connection->connection,
1615 : 1668 : stmt->name,
1616 : : stmt->nparams,
1617 : 1668 : (const char *const *) stmt->paramvalues,
1618 : 1668 : (const int *) stmt->paramlengths,
1619 : 1668 : (const int *) stmt->paramformats,
1620 : : 0);
1621 : 1668 : ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1622 : : }
1623 : : else
1624 : : {
1625 [ + + ]: 3687 : if (stmt->nparams == 0)
1626 : : {
1627 : 2769 : stmt->results = PQexec(stmt->connection->connection, stmt->command);
1628 : 2769 : ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1629 : : }
1630 : : else
1631 : : {
1632 : 1836 : stmt->results = PQexecParams(stmt->connection->connection,
1633 : 918 : stmt->command, stmt->nparams, NULL,
1634 : 918 : (const char *const *) stmt->paramvalues,
1635 : 918 : (const int *) stmt->paramlengths,
1636 : 918 : (const int *) stmt->paramformats,
1637 : : 0);
1638 : :
1639 : 918 : ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1640 : : }
1641 : :
1642 [ + + ]: 3687 : if (stmt->statement_type == ECPGst_prepare)
1643 : : {
1644 [ - + ]: 10 : if (!ecpg_register_prepared_stmt(stmt))
1645 : : {
1646 : 0 : ecpg_free_params(stmt, true);
1647 : 0 : return false;
1648 : : }
1649 : : }
1650 : : }
1651 : :
1652 : 5355 : ecpg_free_params(stmt, true);
1653 : :
1654 [ + + ]: 5355 : if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1655 : 26 : return false;
1656 : :
1657 : 5329 : 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
1678 : 5329 : ecpg_process_output(struct statement *stmt, bool clear_result)
1679 : : {
1680 : : struct variable *var;
1681 : 5329 : bool status = false;
1682 : : char *cmdstat;
1683 : : PGnotify *notify;
1684 : 5329 : struct sqlca_t *sqlca = ECPGget_sqlca();
1685 : : int nfields,
1686 : : ntuples,
1687 : : act_field;
1688 : :
1689 [ - + ]: 5329 : if (sqlca == NULL)
1690 : : {
1691 : 0 : ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
1692 : : ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
1693 : 0 : return false;
1694 : : }
1695 : :
1696 : 5329 : var = stmt->outlist;
1697 [ + + + - ]: 5329 : switch (PQresultStatus(stmt->results))
1698 : : {
1699 : 2112 : case PGRES_TUPLES_OK:
1700 : 2112 : nfields = PQnfields(stmt->results);
1701 : 2112 : sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1702 : :
1703 : 2112 : ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1704 : 2112 : status = true;
1705 : :
1706 [ + + ]: 2112 : if (ntuples < 1)
1707 : : {
1708 [ - + ]: 42 : if (ntuples)
1709 : 0 : ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1710 : : stmt->lineno, ntuples);
1711 : 42 : ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1712 : 42 : status = false;
1713 : 42 : break;
1714 : : }
1715 : :
1716 [ + + + + ]: 2070 : if (var != NULL && var->type == ECPGt_descriptor)
1717 : 16 : {
1718 : 16 : struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1719 : :
1720 [ - + ]: 16 : if (desc == NULL)
1721 : 0 : status = false;
1722 : : else
1723 : : {
1724 : 16 : PQclear(desc->result);
1725 : 16 : desc->result = stmt->results;
1726 : 16 : clear_result = false;
1727 : 16 : ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1728 : 16 : stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1729 : : }
1730 : 16 : var = var->next;
1731 : : }
1732 [ + + + + ]: 2054 : else if (var != NULL && var->type == ECPGt_sqlda)
1733 : : {
1734 [ + + - + ]: 34 : if (INFORMIX_MODE(stmt->compat))
1735 : 16 : {
1736 : 16 : struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1737 : 16 : 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 : : */
1745 [ + + ]: 24 : while (sqlda)
1746 : : {
1747 : 8 : sqlda_new = sqlda->desc_next;
1748 : 8 : free(sqlda);
1749 : 8 : sqlda = sqlda_new;
1750 : : }
1751 : 16 : *_sqlda = sqlda = sqlda_new = NULL;
1752 [ + + ]: 32 : 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 : : */
1758 : 16 : sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1759 : :
1760 [ - + ]: 16 : if (!sqlda_new)
1761 : : {
1762 : : /* cleanup all SQLDAs we created up */
1763 [ # # ]: 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 : :
1771 : 0 : ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1772 : 0 : status = false;
1773 : 0 : break;
1774 : : }
1775 : : else
1776 : : {
1777 : 16 : ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1778 : :
1779 : 16 : *_sqlda = sqlda_new;
1780 : :
1781 : 16 : ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1782 : 16 : ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1783 : 16 : stmt->lineno, PQnfields(stmt->results));
1784 : :
1785 : 16 : sqlda_new->desc_next = sqlda;
1786 : 16 : sqlda = sqlda_new;
1787 : : }
1788 : : }
1789 : : }
1790 : : else
1791 : : {
1792 : 18 : struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1793 : 18 : 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 : : */
1801 [ + + ]: 26 : while (sqlda)
1802 : : {
1803 : 8 : sqlda_new = sqlda->desc_next;
1804 : 8 : free(sqlda);
1805 : 8 : sqlda = sqlda_new;
1806 : : }
1807 : 18 : *_sqlda = sqlda = sqlda_new = NULL;
1808 [ + + ]: 44 : 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 : : */
1814 : 26 : sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1815 : :
1816 [ - + ]: 26 : if (!sqlda_new)
1817 : : {
1818 : : /* cleanup all SQLDAs we created up */
1819 [ # # ]: 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 : :
1827 : 0 : ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1828 : 0 : status = false;
1829 : 0 : break;
1830 : : }
1831 : : else
1832 : : {
1833 : 26 : ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1834 : :
1835 : 26 : *_sqlda = sqlda_new;
1836 : :
1837 : 26 : ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1838 : 26 : ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1839 : 26 : stmt->lineno, PQnfields(stmt->results));
1840 : :
1841 : 26 : sqlda_new->desc_next = sqlda;
1842 : 26 : sqlda = sqlda_new;
1843 : : }
1844 : : }
1845 : : }
1846 : :
1847 : 34 : var = var->next;
1848 : : }
1849 : : else
1850 [ + + + - ]: 4494 : for (act_field = 0; act_field < nfields && status; act_field++)
1851 : : {
1852 [ + + ]: 2474 : if (var != NULL)
1853 : : {
1854 : 2470 : status = ecpg_store_result(stmt->results, act_field, stmt, var);
1855 : 2470 : var = var->next;
1856 : : }
1857 [ - + - - ]: 4 : else if (!INFORMIX_MODE(stmt->compat))
1858 : : {
1859 : 0 : ecpg_raise(stmt->lineno, ECPG_TOO_FEW_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1860 : 0 : return false;
1861 : : }
1862 : : }
1863 : :
1864 [ + + - + ]: 2070 : if (status && var != NULL)
1865 : : {
1866 : 0 : ecpg_raise(stmt->lineno, ECPG_TOO_MANY_ARGUMENTS, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, NULL);
1867 : 0 : status = false;
1868 : : }
1869 : :
1870 : 2070 : break;
1871 : 3215 : case PGRES_COMMAND_OK:
1872 : 3215 : status = true;
1873 : 3215 : cmdstat = PQcmdStatus(stmt->results);
1874 : 3215 : sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1875 : 3215 : sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1876 : 3215 : ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1877 [ + - ]: 3215 : if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1878 [ + + ]: 3215 : !sqlca->sqlerrd[2] &&
1879 [ + + ]: 493 : (strncmp(cmdstat, "UPDATE", 6) == 0
1880 [ + + ]: 491 : || strncmp(cmdstat, "INSERT", 6) == 0
1881 [ + + ]: 489 : || strncmp(cmdstat, "DELETE", 6) == 0))
1882 : 8 : ecpg_raise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL);
1883 : 3215 : break;
1884 : 2 : case PGRES_COPY_OUT:
1885 : : {
1886 : : char *buffer;
1887 : : int res;
1888 : :
1889 : 2 : ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1890 : 8 : while ((res = PQgetCopyData(stmt->connection->connection,
1891 [ + + ]: 8 : &buffer, 0)) > 0)
1892 : : {
1893 : 6 : printf("%s", buffer);
1894 : 6 : PQfreemem(buffer);
1895 : : }
1896 [ + - ]: 2 : if (res == -1)
1897 : : {
1898 : : /* COPY done */
1899 : 2 : PQclear(stmt->results);
1900 : 2 : stmt->results = PQgetResult(stmt->connection->connection);
1901 [ + - ]: 2 : if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1902 : 2 : ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1903 : : else
1904 : 0 : ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1905 : : }
1906 : 2 : break;
1907 : : }
1908 : 0 : default:
1909 : :
1910 : : /*
1911 : : * execution should never reach this code because it is already
1912 : : * handled in ecpg_check_PQresult()
1913 : : */
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);
1917 : 0 : status = false;
1918 : 0 : break;
1919 : : }
1920 : :
1921 [ + + ]: 5329 : if (clear_result)
1922 : : {
1923 : 5313 : PQclear(stmt->results);
1924 : 5313 : stmt->results = NULL;
1925 : : }
1926 : :
1927 : : /* check for asynchronous returns */
1928 : 5329 : PQconsumeInput(stmt->connection->connection);
1929 [ - + ]: 5329 : while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1930 : : {
1931 : 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);
1933 : 0 : PQfreemem(notify);
1934 : 0 : PQconsumeInput(stmt->connection->connection);
1935 : : }
1936 : :
1937 : 5329 : 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
1951 : 5369 : 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 : : {
1956 : 5369 : 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 : :
1963 : 5369 : *stmt_out = NULL;
1964 : :
1965 [ - + ]: 5369 : if (!query)
1966 : : {
1967 : 0 : ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
1968 : 0 : return false;
1969 : : }
1970 : :
1971 : 5369 : ecpg_pthreads_init();
1972 : :
1973 : 5369 : con = ecpg_get_connection(connection_name);
1974 : :
1975 [ + + ]: 5369 : if (!ecpg_init(con, connection_name, lineno))
1976 : 14 : return false;
1977 : :
1978 : 5355 : stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1979 : :
1980 [ - + ]: 5355 : if (stmt == NULL)
1981 : 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 : : */
1995 : : Assert(ecpg_clocale);
1996 : 5355 : stmt->oldlocale = uselocale(ecpg_clocale);
1997 [ - + ]: 5355 : if (stmt->oldlocale == (locale_t) 0)
1998 : : {
1999 : 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 : : */
2025 [ + + ]: 5355 : if (statement_type == ECPGst_prepnormal)
2026 : : {
2027 [ - + ]: 16 : if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2028 : : {
2029 : 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 : : */
2037 : 16 : stmt->command = prepname;
2038 : 16 : statement_type = ECPGst_execute;
2039 : : }
2040 : : else
2041 : : {
2042 : 5339 : stmt->command = ecpg_strdup(query, lineno, NULL);
2043 [ - + ]: 5339 : if (!stmt->command)
2044 : : {
2045 : 0 : ecpg_do_epilogue(stmt);
2046 : 0 : return false;
2047 : : }
2048 : : }
2049 : :
2050 : 5355 : stmt->name = NULL;
2051 : :
2052 [ + + ]: 5355 : if (statement_type == ECPGst_execute)
2053 : : {
2054 : : /* if we have an EXECUTE command, only the name is send */
2055 : 1668 : char *command = ecpg_prepared(stmt->command, con);
2056 : :
2057 [ + - ]: 1668 : if (command)
2058 : : {
2059 : 1668 : stmt->name = stmt->command;
2060 : 1668 : stmt->command = ecpg_strdup(command, lineno, NULL);
2061 [ - + ]: 1668 : if (!stmt->command)
2062 : : {
2063 : 0 : ecpg_do_epilogue(stmt);
2064 : 0 : return false;
2065 : : }
2066 : : }
2067 : : else
2068 : : {
2069 : 0 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, stmt->command);
2070 : 0 : ecpg_do_epilogue(stmt);
2071 : 0 : return false;
2072 : : }
2073 : : }
2074 : : /* name of PREPARE AS will be set in loop of inlist */
2075 : :
2076 : 5355 : stmt->connection = con;
2077 : 5355 : stmt->lineno = lineno;
2078 : 5355 : stmt->compat = compat;
2079 : 5355 : stmt->force_indicator = force_indicator;
2080 : 5355 : stmt->questionmarks = questionmarks;
2081 : 5355 : 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 : :
2105 : 5355 : is_prepared_name_set = false;
2106 : :
2107 : 5355 : list = &(stmt->inlist);
2108 : :
2109 : 5355 : type = va_arg(args, enum ECPGttype);
2110 : :
2111 [ + + ]: 17028 : while (type != ECPGt_EORT)
2112 : : {
2113 [ + + ]: 11673 : if (type == ECPGt_EOIT)
2114 : 5355 : list = &(stmt->outlist);
2115 : : else
2116 : : {
2117 : : struct variable *var,
2118 : : *ptr;
2119 : :
2120 [ - + ]: 6318 : if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2121 : : {
2122 : 0 : ecpg_do_epilogue(stmt);
2123 : 0 : return false;
2124 : : }
2125 : :
2126 : 6318 : var->type = type;
2127 : 6318 : var->pointer = va_arg(args, char *);
2128 : :
2129 : 6318 : var->varcharsize = va_arg(args, long);
2130 : 6318 : var->arrsize = va_arg(args, long);
2131 : 6318 : 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 : : */
2139 [ + + ]: 6318 : if (var->arrsize == 0 ||
2140 [ + + - + : 4650 : (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
- - + - ]
2141 : 1764 : var->value = *((char **) (var->pointer));
2142 : : else
2143 : 4554 : 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 [ + + ]: 6318 : if (var->arrsize < 0)
2151 : 28 : var->arrsize = 0;
2152 [ - + ]: 6318 : if (var->varcharsize < 0)
2153 : 0 : var->varcharsize = 0;
2154 : :
2155 : 6318 : var->next = NULL;
2156 : :
2157 : 6318 : var->ind_type = va_arg(args, enum ECPGttype);
2158 : 6318 : var->ind_pointer = va_arg(args, char *);
2159 : 6318 : var->ind_varcharsize = va_arg(args, long);
2160 : 6318 : var->ind_arrsize = va_arg(args, long);
2161 : 6318 : var->ind_offset = va_arg(args, long);
2162 : :
2163 [ + + ]: 6318 : if (var->ind_type != ECPGt_NO_INDICATOR
2164 [ + - - + ]: 224 : && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2165 : 0 : var->ind_value = *((char **) (var->ind_pointer));
2166 : : else
2167 : 6318 : 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 [ + + ]: 6318 : if (var->ind_arrsize < 0)
2175 : 28 : var->ind_arrsize = 0;
2176 [ - + ]: 6318 : if (var->ind_varcharsize < 0)
2177 : 0 : var->ind_varcharsize = 0;
2178 : :
2179 : : /* if variable is NULL, the statement hasn't been prepared */
2180 [ - + ]: 6318 : if (var->pointer == NULL)
2181 : : {
2182 : 0 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, NULL);
2183 : 0 : ecpg_free(var);
2184 : 0 : ecpg_do_epilogue(stmt);
2185 : 0 : return false;
2186 : : }
2187 : :
2188 [ + + + + ]: 6958 : for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2189 : : ;
2190 : :
2191 [ + + ]: 6318 : if (ptr == NULL)
2192 : 4832 : *list = var;
2193 : : else
2194 : 1486 : ptr->next = var;
2195 : :
2196 [ + - + + ]: 6318 : if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2197 : : {
2198 : 10 : stmt->name = ecpg_strdup(var->value, lineno, NULL);
2199 [ - + ]: 10 : if (!stmt->name)
2200 : : {
2201 : 0 : ecpg_do_epilogue(stmt);
2202 : 0 : return false;
2203 : : }
2204 : 10 : is_prepared_name_set = true;
2205 : : }
2206 : : }
2207 : :
2208 : 11673 : type = va_arg(args, enum ECPGttype);
2209 : : }
2210 : :
2211 : : /* are we connected? */
2212 [ + - - + ]: 5355 : if (con == NULL || con->connection == NULL)
2213 : : {
2214 [ # # ]: 0 : ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2215 : 0 : ecpg_do_epilogue(stmt);
2216 : 0 : return false;
2217 : : }
2218 : :
2219 [ + + - + ]: 5355 : if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2220 : : {
2221 [ # # ]: 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);
2223 : 0 : return false;
2224 : : }
2225 : :
2226 : : /* initialize auto_mem struct */
2227 : 5355 : ecpg_clear_auto_mem();
2228 : :
2229 : 5355 : *stmt_out = stmt;
2230 : :
2231 : 5355 : return true;
2232 : : }
2233 : :
2234 : : /*
2235 : : * ecpg_do_epilogue
2236 : : * Restore the application locale and free the statement structure.
2237 : : */
2238 : : void
2239 : 5369 : ecpg_do_epilogue(struct statement *stmt)
2240 : : {
2241 [ + + ]: 5369 : if (stmt == NULL)
2242 : 14 : return;
2243 : :
2244 : : #ifdef HAVE_USELOCALE
2245 [ + - ]: 5355 : if (stmt->oldlocale != (locale_t) 0)
2246 : 5355 : 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 : :
2257 : 5355 : 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
2266 : 5369 : 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 : 5369 : struct statement *stmt = NULL;
2269 : :
2270 [ + + ]: 5369 : if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2271 : : questionmarks, (enum ECPG_statement_type) st,
2272 : : query, args, &stmt))
2273 : 14 : goto fail;
2274 : :
2275 [ - + ]: 5355 : if (!ecpg_build_params(stmt))
2276 : 0 : goto fail;
2277 : :
2278 [ - + ]: 5355 : if (!ecpg_autostart_transaction(stmt))
2279 : 0 : goto fail;
2280 : :
2281 [ + + ]: 5355 : if (!ecpg_execute(stmt))
2282 : 26 : goto fail;
2283 : :
2284 [ + + ]: 5329 : if (!ecpg_process_output(stmt, true))
2285 : 60 : goto fail;
2286 : :
2287 : 5269 : ecpg_do_epilogue(stmt);
2288 : 5269 : return true;
2289 : :
2290 : 100 : fail:
2291 : 100 : ecpg_do_epilogue(stmt);
2292 : 100 : 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
2300 : 5369 : 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 : :
2305 : 5369 : va_start(args, query);
2306 : 5369 : ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2307 : : questionmarks, st, query, args);
2308 : 5369 : va_end(args);
2309 : :
2310 : 5369 : return ret;
2311 : : }
2312 : :
2313 : : /* old descriptor interface */
2314 : : bool
2315 : 0 : ECPGdo_descriptor(int line, const char *connection,
2316 : : const char *descriptor, const char *query)
2317 : : {
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 : : }
|