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