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