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/* 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;
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, Oid oid, enum ARRAY_TYPE isarray, int lineno)
149{
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(Oid type, const struct statement *stmt, const struct variable *var)
165{
166 char *array_query;
168 PGresult *query;
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=%u and typelem<>0", type);
271 query = PQexec(stmt->connection->connection, 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 (%u); C (%d); array (%s)\n",
298 stmt->lineno, type, var->type, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
299 return isarray;
300}
301
302
303bool
305 const struct statement *stmt, struct variable *var)
306{
307 enum ARRAY_TYPE isarray;
308 int act_tuple,
309 ntuples = PQntuples(results);
310 bool status = true;
311
313 {
315 return false;
316 }
317
319 {
320 /*
321 * if we don't have enough space, we cannot read all tuples
322 */
323 if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
324 {
325 ecpg_log("ecpg_store_result on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
326 stmt->lineno, ntuples, var->arrsize);
328 return false;
329 }
330 }
331 else
332 {
333 /*
334 * since we read an array, the variable has to be an array too
335 */
336 if (var->arrsize == 0)
337 {
339 return false;
340 }
341 }
342
343 /*
344 * allocate memory for NULL pointers
345 */
346 if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
347 {
348 int len = 0;
349
350 if (!PQfformat(results, act_field))
351 {
352 switch (var->type)
353 {
354 case ECPGt_char:
356 case ECPGt_string:
357 if (!var->varcharsize && !var->arrsize)
358 {
359 /* special mode for handling char**foo=0 */
360 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
361 len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
362 len *= var->offset; /* should be 1, but YMNK */
363 len += (ntuples + 1) * sizeof(char *);
364 }
365 else
366 {
367 var->varcharsize = 0;
368 /* check strlen for each tuple */
369 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
370 {
371 int slen = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
372
373 if (slen > var->varcharsize)
374 var->varcharsize = slen;
375 }
376 var->offset *= var->varcharsize;
377 len = var->offset * ntuples;
378 }
379 break;
380 case ECPGt_varchar:
381 len = ntuples * (var->varcharsize + sizeof(int));
382 break;
383 default:
384 len = var->offset * ntuples;
385 break;
386 }
387 }
388 else
389 {
390 for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
391 len += PQgetlength(results, act_tuple, act_field);
392 }
393
394 ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
395 var->value = ecpg_auto_alloc(len, stmt->lineno);
396 if (!var->value)
397 return false;
398 *((char **) var->pointer) = var->value;
399 }
400
401 /* allocate indicator variable if needed */
402 if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
403 {
404 int len = var->ind_offset * ntuples;
405
406 var->ind_value = ecpg_auto_alloc(len, stmt->lineno);
407 if (!var->ind_value)
408 return false;
409 *((char **) var->ind_pointer) = var->ind_value;
410 }
411
412 /* fill the variable with the tuple(s) */
413 if (!var->varcharsize && !var->arrsize &&
414 (var->type == ECPGt_char || var->type == ECPGt_unsigned_char || var->type == ECPGt_string))
415 {
416 /* special mode for handling char**foo=0 */
417
418 /* filling the array of (char*)s */
419 char **current_string = (char **) var->value;
420
421 /* storing the data (after the last array element) */
422 char *current_data_location = (char *) &current_string[ntuples + 1];
423
424 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
425 {
426 int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
427
428 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
430 var->ind_value, len, 0, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
431 status = false;
432 else
433 {
437 }
438 }
439
440 /* terminate the list */
442 }
443 else
444 {
445 for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
446 {
447 if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
448 var->type, var->ind_type, var->value,
449 var->ind_value, var->varcharsize, var->offset, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
450 status = false;
451 }
452 }
453 return status;
454}
455
456static void
457sprintf_double_value(char *ptr, double value, const char *delim)
458{
459 if (isnan(value))
460 sprintf(ptr, "%s%s", "NaN", delim);
461 else if (isinf(value))
462 {
463 if (value < 0)
464 sprintf(ptr, "%s%s", "-Infinity", delim);
465 else
466 sprintf(ptr, "%s%s", "Infinity", delim);
467 }
468 else
469 sprintf(ptr, "%.15g%s", value, delim);
470}
471
472static void
473sprintf_float_value(char *ptr, float value, const char *delim)
474{
475 if (isnan(value))
476 sprintf(ptr, "%s%s", "NaN", delim);
477 else if (isinf(value))
478 {
479 if (value < 0)
480 sprintf(ptr, "%s%s", "-Infinity", delim);
481 else
482 sprintf(ptr, "%s%s", "Infinity", delim);
483 }
484 else
485 sprintf(ptr, "%.15g%s", value, delim);
486}
487
488static char *
490{
491 char *to_data;
492 int to_len = ecpg_hex_enc_len(from_len) + 4 + 1; /* backslash + 'x' +
493 * quote + quote */
494
495 to_data = ecpg_alloc(to_len, lineno);
496 if (!to_data)
497 return NULL;
498
499 strcpy(to_data, "'\\x");
502
503 return to_data;
504}
505
506bool
507ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var,
508 char **tobeinserted_p, bool quote)
509{
510 char *mallocedval = NULL;
511 char *newcopy = NULL;
512
513 /*
514 * arrays are not possible unless the column is an array, too FIXME: we do
515 * not know if the column is an array here array input to singleton column
516 * will result in a runtime error
517 */
518
519 /*
520 * Some special treatment is needed for records since we want their
521 * contents to arrive in a comma-separated list on insert (I think).
522 */
523
524 *tobeinserted_p = "";
525
526 /* check for null value and set input buffer accordingly */
527 switch (var->ind_type)
528 {
529 case ECPGt_short:
531 if (*(short *) var->ind_value < 0)
533 break;
534 case ECPGt_int:
536 if (*(int *) var->ind_value < 0)
538 break;
539 case ECPGt_long:
541 if (*(long *) var->ind_value < 0L)
543 break;
544 case ECPGt_long_long:
546 if (*(long long int *) var->ind_value < (long long) 0)
548 break;
550 if (force_indicator == false)
551 {
552 if (ECPGis_noind_null(var->type, var->value))
554 }
555 break;
556 default:
557 break;
558 }
559 if (*tobeinserted_p != NULL)
560 {
561 int asize = var->arrsize ? var->arrsize : 1;
562
563 switch (var->type)
564 {
565 int element;
566
567 case ECPGt_short:
568 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
569 return false;
570
571 if (asize > 1)
572 {
573 strcpy(mallocedval, "{");
574
575 for (element = 0; element < asize; element++)
576 sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
577
579 }
580 else
581 sprintf(mallocedval, "%hd", *((short *) var->value));
582
584 break;
585
586 case ECPGt_int:
587 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
588 return false;
589
590 if (asize > 1)
591 {
592 strcpy(mallocedval, "{");
593
594 for (element = 0; element < asize; element++)
595 sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
596
598 }
599 else
600 sprintf(mallocedval, "%d", *((int *) var->value));
601
603 break;
604
606 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
607 return false;
608
609 if (asize > 1)
610 {
611 strcpy(mallocedval, "{");
612
613 for (element = 0; element < asize; element++)
614 sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
615
617 }
618 else
619 sprintf(mallocedval, "%hu", *((unsigned short *) var->value));
620
622 break;
623
625 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
626 return false;
627
628 if (asize > 1)
629 {
630 strcpy(mallocedval, "{");
631
632 for (element = 0; element < asize; element++)
633 sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
634
636 }
637 else
638 sprintf(mallocedval, "%u", *((unsigned int *) var->value));
639
641 break;
642
643 case ECPGt_long:
644 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
645 return false;
646
647 if (asize > 1)
648 {
649 strcpy(mallocedval, "{");
650
651 for (element = 0; element < asize; element++)
652 sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
653
655 }
656 else
657 sprintf(mallocedval, "%ld", *((long *) var->value));
658
660 break;
661
663 if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
664 return false;
665
666 if (asize > 1)
667 {
668 strcpy(mallocedval, "{");
669
670 for (element = 0; element < asize; element++)
671 sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
672
674 }
675 else
676 sprintf(mallocedval, "%lu", *((unsigned long *) var->value));
677
679 break;
680
681 case ECPGt_long_long:
682 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
683 return false;
684
685 if (asize > 1)
686 {
687 strcpy(mallocedval, "{");
688
689 for (element = 0; element < asize; element++)
690 sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
691
693 }
694 else
695 sprintf(mallocedval, "%lld", *((long long int *) var->value));
696
698 break;
699
701 if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
702 return false;
703
704 if (asize > 1)
705 {
706 strcpy(mallocedval, "{");
707
708 for (element = 0; element < asize; element++)
709 sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
710
712 }
713 else
714 sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
715
717 break;
718
719 case ECPGt_float:
720 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
721 return false;
722
723 if (asize > 1)
724 {
725 strcpy(mallocedval, "{");
726
727 for (element = 0; element < asize; element++)
729
731 }
732 else
733 sprintf_float_value(mallocedval, *((float *) var->value), "");
734
736 break;
737
738 case ECPGt_double:
739 if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
740 return false;
741
742 if (asize > 1)
743 {
744 strcpy(mallocedval, "{");
745
746 for (element = 0; element < asize; element++)
747 sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
748
750 }
751 else
752 sprintf_double_value(mallocedval, *((double *) var->value), "");
753
755 break;
756
757 case ECPGt_bool:
758 if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
759 return false;
760
761 if (var->arrsize > 1)
762 {
763 strcpy(mallocedval, "{");
764
765 for (element = 0; element < asize; element++)
766 sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
767
769 }
770 else
771 {
772 if (var->offset == sizeof(char))
773 sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
774 else if (var->offset == sizeof(int))
775 sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
776 else
778 }
779
781 break;
782
783 case ECPGt_char:
785 case ECPGt_string:
786 {
787 /* set slen to string length if type is char * */
788 int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
789
790 if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
791 return false;
792
793 strncpy(newcopy, (char *) var->value, slen);
794 newcopy[slen] = '\0';
795
796 mallocedval = quote_postgres(newcopy, quote, lineno);
797 if (!mallocedval)
798 {
800 return false;
801 }
802
804 }
805 break;
806 case ECPGt_const:
808 {
809 int slen = strlen((char *) var->value);
810
811 if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
812 return false;
813
814 strncpy(mallocedval, (char *) var->value, slen);
815 mallocedval[slen] = '\0';
816
818 }
819 break;
820
821 case ECPGt_bytea:
822 {
824 (struct ECPGgeneric_bytea *) (var->value);
825
826 if (!(mallocedval = ecpg_alloc(variable->len, lineno)))
827 return false;
828
829 memcpy(mallocedval, variable->arr, variable->len);
831 }
832 break;
833
834 case ECPGt_varchar:
835 {
837 (struct ECPGgeneric_varchar *) (var->value);
838
839 if (!(newcopy = ecpg_alloc(variable->len + 1, lineno)))
840 return false;
841
842 strncpy(newcopy, variable->arr, variable->len);
843 newcopy[variable->len] = '\0';
844
845 mallocedval = quote_postgres(newcopy, quote, lineno);
846 if (!mallocedval)
847 {
849 return false;
850 }
851
853 }
854 break;
855
856 case ECPGt_decimal:
857 case ECPGt_numeric:
858 {
859 char *str = NULL;
860 int slen;
861 numeric *nval;
862
863 if (var->arrsize > 1)
864 mallocedval = ecpg_strdup("{", lineno, NULL);
865 else
866 mallocedval = ecpg_strdup("", lineno, NULL);
867
868 if (!mallocedval)
869 return false;
870
871 for (element = 0; element < asize; element++)
872 {
873 int result;
874
876 if (!nval)
877 {
879 return false;
880 }
881
882 if (var->type == ECPGt_numeric)
883 result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
884 else
885 result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
886
887 if (result != 0)
888 {
891 return false;
892 }
893
895 slen = strlen(str);
897
898 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
899 {
901 ecpg_free(str);
902 return false;
903 }
905
906 /* also copy trailing '\0' */
908 if (var->arrsize > 1)
910
911 ecpg_free(str);
912 }
913
914 if (var->arrsize > 1)
916
918 }
919 break;
920
921 case ECPGt_interval:
922 {
923 char *str = NULL;
924 int slen;
925
926 if (var->arrsize > 1)
927 mallocedval = ecpg_strdup("{", lineno, NULL);
928 else
929 mallocedval = ecpg_strdup("", lineno, NULL);
930
931 if (!mallocedval)
932 return false;
933
934 for (element = 0; element < asize; element++)
935 {
936 str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
937 if (!str)
938 {
940 return false;
941 }
942
943 slen = strlen(str);
944
945 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
946 {
948 ecpg_free(str);
949 return false;
950 }
952
953 /* also copy trailing '\0' */
955 if (var->arrsize > 1)
957
958 ecpg_free(str);
959 }
960
961 if (var->arrsize > 1)
963
965 }
966 break;
967
968 case ECPGt_date:
969 {
970 char *str = NULL;
971 int slen;
972
973 if (var->arrsize > 1)
974 mallocedval = ecpg_strdup("{", lineno, NULL);
975 else
976 mallocedval = ecpg_strdup("", lineno, NULL);
977
978 if (!mallocedval)
979 return false;
980
981 for (element = 0; element < asize; element++)
982 {
983 str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
984 if (!str)
985 {
987 return false;
988 }
989
990 slen = strlen(str);
991
992 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
993 {
995 ecpg_free(str);
996 return false;
997 }
999
1000 /* also copy trailing '\0' */
1002 if (var->arrsize > 1)
1004
1005 ecpg_free(str);
1006 }
1007
1008 if (var->arrsize > 1)
1009 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1010
1012 }
1013 break;
1014
1015 case ECPGt_timestamp:
1016 {
1017 char *str = NULL;
1018 int slen;
1019
1020 if (var->arrsize > 1)
1021 mallocedval = ecpg_strdup("{", lineno, NULL);
1022 else
1023 mallocedval = ecpg_strdup("", lineno, NULL);
1024
1025 if (!mallocedval)
1026 return false;
1027
1028 for (element = 0; element < asize; element++)
1029 {
1030 str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1031 if (!str)
1032 {
1034 return false;
1035 }
1036
1037 slen = strlen(str);
1038
1039 if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1040 {
1042 ecpg_free(str);
1043 return false;
1044 }
1046
1047 /* also copy trailing '\0' */
1049 if (var->arrsize > 1)
1051
1052 ecpg_free(str);
1053 }
1054
1055 if (var->arrsize > 1)
1056 strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1057
1059 }
1060 break;
1061
1062 case ECPGt_descriptor:
1063 case ECPGt_sqlda:
1064 break;
1065
1066 default:
1067 /* Not implemented yet */
1069 return false;
1070 break;
1071 }
1072 }
1073 return true;
1074}
1075
1076static void
1077print_param_value(char *value, int len, int is_binary, int lineno, int nth)
1078{
1079 char *value_s;
1080 bool malloced = false;
1081
1082 if (value == NULL)
1083 value_s = "null";
1084 else if (!is_binary)
1085 value_s = value;
1086 else
1087 {
1088 value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1089 if (value_s != NULL)
1090 {
1092 value_s[ecpg_hex_enc_len(len)] = '\0';
1093 malloced = true;
1094 }
1095 else
1096 value_s = "no memory for logging of parameter";
1097 }
1098
1099 ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1100 lineno, nth, value_s);
1101
1102 if (malloced)
1104}
1105
1106void
1108{
1109 int n;
1110
1111 for (n = 0; n < stmt->nparams; n++)
1112 {
1113 if (print)
1114 print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1115 stmt->paramformats[n], stmt->lineno, n + 1);
1116 ecpg_free(stmt->paramvalues[n]);
1117 }
1118 ecpg_free(stmt->paramvalues);
1119 ecpg_free(stmt->paramlengths);
1120 ecpg_free(stmt->paramformats);
1121 stmt->paramvalues = NULL;
1122 stmt->paramlengths = NULL;
1123 stmt->paramformats = NULL;
1124 stmt->nparams = 0;
1125}
1126
1127static bool
1128insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
1129{
1130 char *newcopy;
1131
1132 if (!(newcopy = ecpg_alloc(strlen(stmt->command) + strlen(tobeinserted) + 1, stmt->lineno)))
1133 {
1135 return false;
1136 }
1137
1138 strcpy(newcopy, stmt->command);
1139 strcpy(newcopy + position - 1, tobeinserted);
1140
1141 /*
1142 * The strange thing in the second argument is the rest of the string from
1143 * the old string
1144 */
1146 stmt->command
1147 + position
1148 + ph_len - 1);
1149
1150 ecpg_free(stmt->command);
1151 stmt->command = newcopy;
1152
1154 return true;
1155}
1156
1157static bool
1159 char **tobeinserted)
1160{
1161 struct variable var;
1162
1163 /*
1164 * In case of binary data, only allocate memory and memcpy because binary
1165 * data have been already stored into desc_item->data with
1166 * ecpg_store_input() at ECPGset_desc().
1167 */
1168 if (desc_item->is_binary)
1169 {
1170 if (!(*tobeinserted = ecpg_alloc(desc_item->data_len, stmt->lineno)))
1171 return false;
1172 memcpy(*tobeinserted, desc_item->data, desc_item->data_len);
1173 return true;
1174 }
1175
1176 var.type = ECPGt_char;
1177 var.varcharsize = strlen(desc_item->data);
1178 var.value = desc_item->data;
1179 var.pointer = &(desc_item->data);
1180 var.arrsize = 1;
1181 var.offset = 0;
1182
1183 if (!desc_item->indicator)
1184 {
1186 var.ind_value = var.ind_pointer = NULL;
1187 var.ind_varcharsize = var.ind_arrsize = var.ind_offset = 0;
1188 }
1189 else
1190 {
1191 var.ind_type = ECPGt_int;
1192 var.ind_value = &(desc_item->indicator);
1193 var.ind_pointer = &(var.ind_value);
1194 var.ind_varcharsize = var.ind_arrsize = 1;
1195 var.ind_offset = 0;
1196 }
1197
1198 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &var, tobeinserted, false))
1199 return false;
1200
1201 return true;
1202}
1203
1204/*
1205 * ecpg_build_params
1206 * Build statement parameters
1207 *
1208 * The input values are taken from user variables, and the results are stored
1209 * in arrays which can be used by PQexecParams().
1210 */
1211bool
1213{
1214 struct variable *var;
1215 int desc_counter = 0;
1216 int position = 0;
1217 const char *value;
1218 bool std_strings = false;
1219
1220 /* Get standard_conforming_strings setting. */
1221 value = PQparameterStatus(stmt->connection->connection, "standard_conforming_strings");
1222 if (value && strcmp(value, "on") == 0)
1223 std_strings = true;
1224
1225 /*
1226 * If the type is one of the fill in types then we take the argument and
1227 * enter it to our parameter array at the first position. Then if there
1228 * are any more fill in types we add more parameters.
1229 */
1230 var = stmt->inlist;
1231 while (var)
1232 {
1233 char *tobeinserted;
1234 int counter = 1;
1235 bool binary_format;
1236 int binary_length;
1237
1238
1240 binary_length = 0;
1241 binary_format = false;
1242
1243 /*
1244 * A descriptor is a special case since it contains many variables but
1245 * is listed only once.
1246 */
1247 if (var->type == ECPGt_descriptor)
1248 {
1249 /*
1250 * We create an additional variable list here, so the same logic
1251 * applies.
1252 */
1253 struct descriptor *desc;
1254 struct descriptor_item *desc_item;
1255
1256 desc = ecpg_find_desc(stmt->lineno, var->pointer);
1257 if (desc == NULL)
1258 return false;
1259
1260 desc_counter++;
1261 for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1262 {
1263 if (desc_item->num != desc_counter)
1264 continue;
1265
1267 return false;
1268
1269 if (desc_item->is_binary)
1270 {
1271 binary_length = desc_item->data_len;
1272 binary_format = true;
1273 }
1274 break;
1275 }
1276 if (desc->count == desc_counter)
1277 desc_counter = 0;
1278 }
1279 else if (var->type == ECPGt_sqlda)
1280 {
1281 if (INFORMIX_MODE(stmt->compat))
1282 {
1283 struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1284 struct variable desc_inlist;
1285 int i;
1286
1287 if (sqlda == NULL)
1288 return false;
1289
1290 desc_counter++;
1291 for (i = 0; i < sqlda->sqld; i++)
1292 {
1293 if (i + 1 == desc_counter)
1294 {
1295 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1296 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1297 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1298 switch (desc_inlist.type)
1299 {
1300 case ECPGt_char:
1301 case ECPGt_varchar:
1302 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1303 break;
1304 default:
1305 desc_inlist.varcharsize = 0;
1306 break;
1307 }
1308 desc_inlist.arrsize = 1;
1309 desc_inlist.offset = 0;
1310 if (sqlda->sqlvar[i].sqlind)
1311 {
1312 desc_inlist.ind_type = ECPGt_short;
1313 /* ECPG expects indicator value < 0 */
1314 if (*(sqlda->sqlvar[i].sqlind))
1315 *(sqlda->sqlvar[i].sqlind) = -1;
1316 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1317 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1318 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1319 desc_inlist.ind_offset = 0;
1320 }
1321 else
1322 {
1324 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1325 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1326 }
1327 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1328 return false;
1329
1330 break;
1331 }
1332 }
1333 if (sqlda->sqld == desc_counter)
1334 desc_counter = 0;
1335 }
1336 else
1337 {
1338 struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1339 struct variable desc_inlist;
1340 int i;
1341
1342 if (sqlda == NULL)
1343 return false;
1344
1345 desc_counter++;
1346 for (i = 0; i < sqlda->sqln; i++)
1347 {
1348 if (i + 1 == desc_counter)
1349 {
1350 desc_inlist.type = sqlda->sqlvar[i].sqltype;
1351 desc_inlist.value = sqlda->sqlvar[i].sqldata;
1352 desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1353 switch (desc_inlist.type)
1354 {
1355 case ECPGt_char:
1356 case ECPGt_varchar:
1357 desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1358 break;
1359 default:
1360 desc_inlist.varcharsize = 0;
1361 break;
1362 }
1363 desc_inlist.arrsize = 1;
1364 desc_inlist.offset = 0;
1365 if (sqlda->sqlvar[i].sqlind)
1366 {
1367 desc_inlist.ind_type = ECPGt_short;
1368 /* ECPG expects indicator value < 0 */
1369 if (*(sqlda->sqlvar[i].sqlind))
1370 *(sqlda->sqlvar[i].sqlind) = -1;
1371 desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1372 desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1373 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1374 desc_inlist.ind_offset = 0;
1375 }
1376 else
1377 {
1379 desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1380 desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1381 }
1382 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1383 return false;
1384
1385 break;
1386 }
1387 }
1388 if (sqlda->sqln == desc_counter)
1389 desc_counter = 0;
1390 }
1391 }
1392 else
1393 {
1394 if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1395 return false;
1396
1397 if (var->type == ECPGt_bytea)
1398 {
1399 binary_length = ((struct ECPGgeneric_bytea *) (var->value))->len;
1400 binary_format = true;
1401 }
1402 }
1403
1404 /*
1405 * now tobeinserted points to an area that contains the next
1406 * parameter; now find the position in the string where it belongs
1407 */
1408 if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1409 {
1410 /*
1411 * We have an argument but we don't have the matched up
1412 * placeholder in the string
1413 */
1416 NULL);
1417 ecpg_free_params(stmt, false);
1419 return false;
1420 }
1421
1422 /*
1423 * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1424 * to simulate a dynamic cursor because there is no backend
1425 * functionality for it
1426 */
1427 if (var->type == ECPGt_char_variable)
1428 {
1429 int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1430
1431 if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1432 {
1433 ecpg_free_params(stmt, false);
1434 return false;
1435 }
1437 }
1438
1439 /*
1440 * if the placeholder is '$0' we have to replace it on the client side
1441 * this is for places we want to support variables at that are not
1442 * supported in the backend
1443 */
1444 else if (stmt->command[position] == '0')
1445 {
1446 if (stmt->statement_type == ECPGst_prepare ||
1447 stmt->statement_type == ECPGst_exec_with_exprlist)
1448 {
1449 /* Need to double-quote the inserted statement name. */
1450 char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1451 stmt->lineno);
1452
1453 if (!str)
1454 {
1456 ecpg_free_params(stmt, false);
1457 return false;
1458 }
1459 sprintf(str, "\"%s\"", tobeinserted);
1461 tobeinserted = str;
1462 }
1463
1464 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1465 {
1466 ecpg_free_params(stmt, false);
1467 return false;
1468 }
1470 }
1471 else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1472 {
1473 if (binary_format)
1474 {
1477 stmt->lineno);
1478
1480 if (!p)
1481 {
1482 ecpg_free_params(stmt, false);
1483 return false;
1484 }
1485 tobeinserted = p;
1486 }
1487
1488 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1489 {
1490 ecpg_free_params(stmt, false);
1491 return false;
1492 }
1494 }
1495 else
1496 {
1497 bool realloc_failed = false;
1498 char **newparamvalues;
1499 int *newparamlengths;
1500 int *newparamformats;
1501
1502 /* enlarge all the param arrays */
1503 if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1504 stmt->paramvalues = newparamvalues;
1505 else
1506 realloc_failed = true;
1507
1508 if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1509 stmt->paramlengths = newparamlengths;
1510 else
1511 realloc_failed = true;
1512
1513 if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1514 stmt->paramformats = newparamformats;
1515 else
1516 realloc_failed = true;
1517
1518 if (realloc_failed)
1519 {
1520 ecpg_free_params(stmt, false);
1522 return false;
1523 }
1524
1525 /* only now can we assign ownership of "tobeinserted" to stmt */
1526 stmt->paramvalues[stmt->nparams] = tobeinserted;
1527 stmt->paramlengths[stmt->nparams] = binary_length;
1528 stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1529 stmt->nparams++;
1530
1531 /* let's see if this was an old style placeholder */
1532 if (stmt->command[position] == '?')
1533 {
1534 /* yes, replace with new style */
1535 int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1536 * size we need */
1537
1538 if (!(tobeinserted = ecpg_alloc(buffersize, stmt->lineno)))
1539 {
1540 ecpg_free_params(stmt, false);
1541 return false;
1542 }
1543
1544 snprintf(tobeinserted, buffersize, "$%d", counter++);
1545
1546 if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1547 {
1548 ecpg_free_params(stmt, false);
1549 return false;
1550 }
1552 }
1553 }
1554
1555 if (desc_counter == 0)
1556 var = var->next;
1557 }
1558
1559 /*
1560 * Check if there are unmatched things left. PREPARE AS has no parameter.
1561 * Check other statement.
1562 */
1563 if (stmt->statement_type != ECPGst_prepare &&
1564 next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1565 {
1568 ecpg_free_params(stmt, false);
1569 return false;
1570 }
1571
1572 return true;
1573}
1574
1575/*
1576 * ecpg_autostart_transaction
1577 * If we are in non-autocommit mode, automatically start a transaction.
1578 */
1579bool
1581{
1582 if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
1583 {
1584 stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1585 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1586 {
1587 ecpg_free_params(stmt, false);
1588 return false;
1589 }
1590 PQclear(stmt->results);
1591 stmt->results = NULL;
1592 }
1593 return true;
1594}
1595
1596/*
1597 * ecpg_execute
1598 * Execute the SQL statement.
1599 */
1600bool
1602{
1603 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);
1604 if (stmt->statement_type == ECPGst_execute)
1605 {
1606 stmt->results = PQexecPrepared(stmt->connection->connection,
1607 stmt->name,
1608 stmt->nparams,
1609 (const char *const *) stmt->paramvalues,
1610 (const int *) stmt->paramlengths,
1611 (const int *) stmt->paramformats,
1612 0);
1613 ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1614 }
1615 else
1616 {
1617 if (stmt->nparams == 0)
1618 {
1619 stmt->results = PQexec(stmt->connection->connection, stmt->command);
1620 ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1621 }
1622 else
1623 {
1624 stmt->results = PQexecParams(stmt->connection->connection,
1625 stmt->command, stmt->nparams, NULL,
1626 (const char *const *) stmt->paramvalues,
1627 (const int *) stmt->paramlengths,
1628 (const int *) stmt->paramformats,
1629 0);
1630
1631 ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1632 }
1633
1634 if (stmt->statement_type == ECPGst_prepare)
1635 {
1637 {
1638 ecpg_free_params(stmt, true);
1639 return false;
1640 }
1641 }
1642 }
1643
1644 ecpg_free_params(stmt, true);
1645
1646 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1647 return false;
1648
1649 return true;
1650}
1651
1652/*-------
1653 * ecpg_process_output
1654 *
1655 * Process the statement result and store it into application variables. This
1656 * function can be called repeatedly during the same statement in case cursor
1657 * readahead is used and the application does FETCH N which overflows the
1658 * readahead window.
1659 *
1660 * Parameters
1661 * stmt statement structure holding the PGresult and
1662 * the list of output variables
1663 * clear_result
1664 * PQclear() the result upon returning from this function
1665 *
1666 * Returns success as boolean. Also an SQL error is raised in case of failure.
1667 *-------
1668 */
1669bool
1671{
1672 struct variable *var;
1673 bool status = false;
1674 char *cmdstat;
1676 struct sqlca_t *sqlca = ECPGget_sqlca();
1677 int nfields,
1678 ntuples,
1679 act_field;
1680
1681 if (sqlca == NULL)
1682 {
1685 return false;
1686 }
1687
1688 var = stmt->outlist;
1689 switch (PQresultStatus(stmt->results))
1690 {
1691 case PGRES_TUPLES_OK:
1692 nfields = PQnfields(stmt->results);
1693 sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1694
1695 ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1696 status = true;
1697
1698 if (ntuples < 1)
1699 {
1700 if (ntuples)
1701 ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1702 stmt->lineno, ntuples);
1704 status = false;
1705 break;
1706 }
1707
1708 if (var != NULL && var->type == ECPGt_descriptor)
1709 {
1710 struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1711
1712 if (desc == NULL)
1713 status = false;
1714 else
1715 {
1716 PQclear(desc->result);
1717 desc->result = stmt->results;
1718 clear_result = false;
1719 ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1720 stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1721 }
1722 var = var->next;
1723 }
1724 else if (var != NULL && var->type == ECPGt_sqlda)
1725 {
1726 if (INFORMIX_MODE(stmt->compat))
1727 {
1728 struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1729 struct sqlda_compat *sqlda = *_sqlda;
1730 struct sqlda_compat *sqlda_new;
1731 int i;
1732
1733 /*
1734 * If we are passed in a previously existing sqlda (chain)
1735 * then free it.
1736 */
1737 while (sqlda)
1738 {
1740 free(sqlda);
1741 sqlda = sqlda_new;
1742 }
1743 *_sqlda = sqlda = sqlda_new = NULL;
1744 for (i = ntuples - 1; i >= 0; i--)
1745 {
1746 /*
1747 * Build a new sqlda structure. Note that only
1748 * fetching 1 record is supported
1749 */
1750 sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1751
1752 if (!sqlda_new)
1753 {
1754 /* cleanup all SQLDAs we created up */
1755 while (sqlda)
1756 {
1757 sqlda_new = sqlda->desc_next;
1758 free(sqlda);
1759 sqlda = sqlda_new;
1760 }
1761 *_sqlda = NULL;
1762
1763 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1764 status = false;
1765 break;
1766 }
1767 else
1768 {
1769 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1770
1771 *_sqlda = sqlda_new;
1772
1773 ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1774 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1775 stmt->lineno, PQnfields(stmt->results));
1776
1777 sqlda_new->desc_next = sqlda;
1778 sqlda = sqlda_new;
1779 }
1780 }
1781 }
1782 else
1783 {
1784 struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1785 struct sqlda_struct *sqlda = *_sqlda;
1786 struct sqlda_struct *sqlda_new;
1787 int i;
1788
1789 /*
1790 * If we are passed in a previously existing sqlda (chain)
1791 * then free it.
1792 */
1793 while (sqlda)
1794 {
1796 free(sqlda);
1797 sqlda = sqlda_new;
1798 }
1799 *_sqlda = sqlda = sqlda_new = NULL;
1800 for (i = ntuples - 1; i >= 0; i--)
1801 {
1802 /*
1803 * Build a new sqlda structure. Note that only
1804 * fetching 1 record is supported
1805 */
1806 sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1807
1808 if (!sqlda_new)
1809 {
1810 /* cleanup all SQLDAs we created up */
1811 while (sqlda)
1812 {
1813 sqlda_new = sqlda->desc_next;
1814 free(sqlda);
1815 sqlda = sqlda_new;
1816 }
1817 *_sqlda = NULL;
1818
1819 ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1820 status = false;
1821 break;
1822 }
1823 else
1824 {
1825 ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1826
1827 *_sqlda = sqlda_new;
1828
1829 ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1830 ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1831 stmt->lineno, PQnfields(stmt->results));
1832
1833 sqlda_new->desc_next = sqlda;
1834 sqlda = sqlda_new;
1835 }
1836 }
1837 }
1838
1839 var = var->next;
1840 }
1841 else
1842 for (act_field = 0; act_field < nfields && status; act_field++)
1843 {
1844 if (var != NULL)
1845 {
1846 status = ecpg_store_result(stmt->results, act_field, stmt, var);
1847 var = var->next;
1848 }
1849 else if (!INFORMIX_MODE(stmt->compat))
1850 {
1852 return false;
1853 }
1854 }
1855
1856 if (status && var != NULL)
1857 {
1859 status = false;
1860 }
1861
1862 break;
1863 case PGRES_COMMAND_OK:
1864 status = true;
1865 cmdstat = PQcmdStatus(stmt->results);
1866 sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1867 sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1868 ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1869 if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1870 !sqlca->sqlerrd[2] &&
1871 (strncmp(cmdstat, "UPDATE", 6) == 0
1872 || strncmp(cmdstat, "INSERT", 6) == 0
1873 || strncmp(cmdstat, "DELETE", 6) == 0))
1875 break;
1876 case PGRES_COPY_OUT:
1877 {
1878 char *buffer;
1879 int res;
1880
1881 ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1882 while ((res = PQgetCopyData(stmt->connection->connection,
1883 &buffer, 0)) > 0)
1884 {
1885 printf("%s", buffer);
1886 PQfreemem(buffer);
1887 }
1888 if (res == -1)
1889 {
1890 /* COPY done */
1891 PQclear(stmt->results);
1892 stmt->results = PQgetResult(stmt->connection->connection);
1893 if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1894 ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1895 else
1896 ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1897 }
1898 break;
1899 }
1900 default:
1901
1902 /*
1903 * execution should never reach this code because it is already
1904 * handled in ecpg_check_PQresult()
1905 */
1906 ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1907 stmt->lineno);
1908 ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1909 status = false;
1910 break;
1911 }
1912
1913 if (clear_result)
1914 {
1915 PQclear(stmt->results);
1916 stmt->results = NULL;
1917 }
1918
1919 /* check for asynchronous returns */
1920 PQconsumeInput(stmt->connection->connection);
1921 while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1922 {
1923 ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1924 stmt->lineno, notify->relname, notify->be_pid);
1926 PQconsumeInput(stmt->connection->connection);
1927 }
1928
1929 return status;
1930}
1931
1932/*
1933 * ecpg_do_prologue
1934 *
1935 * Initialize various infrastructure elements for executing the statement:
1936 *
1937 * - create the statement structure
1938 * - set the C numeric locale for communicating with the backend
1939 * - preprocess the variable list of input/output parameters into
1940 * linked lists
1941 */
1942bool
1943ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
1944 const char *connection_name, const bool questionmarks,
1945 enum ECPG_statement_type statement_type, const char *query,
1946 va_list args, struct statement **stmt_out)
1947{
1948 struct statement *stmt = NULL;
1949 struct connection *con;
1950 enum ECPGttype type;
1951 struct variable **list;
1952 char *prepname;
1954
1955 *stmt_out = NULL;
1956
1957 if (!query)
1958 {
1960 return false;
1961 }
1962
1964
1966
1967 if (!ecpg_init(con, connection_name, lineno))
1968 return false;
1969
1970 stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1971
1972 if (stmt == NULL)
1973 return false;
1974
1975 /*
1976 * Make sure we do NOT honor the locale for numeric input/output since the
1977 * database wants the standard decimal point. If available, use
1978 * uselocale() for this because it's thread-safe. Windows doesn't have
1979 * that, but it does have _configthreadlocale().
1980 */
1981#ifdef HAVE_USELOCALE
1982
1983 /*
1984 * Since ecpg_init() succeeded, we have a connection. Any successful
1985 * connection initializes ecpg_clocale.
1986 */
1988 stmt->oldlocale = uselocale(ecpg_clocale);
1989 if (stmt->oldlocale == (locale_t) 0)
1990 {
1992 return false;
1993 }
1994#else
1995#ifdef WIN32
1997 if (stmt->oldthreadlocale == -1)
1998 {
2000 return false;
2001 }
2002#endif
2004 NULL);
2005 if (stmt->oldlocale == NULL)
2006 {
2008 return false;
2009 }
2010 setlocale(LC_NUMERIC, "C");
2011#endif
2012
2013 /*
2014 * If statement type is ECPGst_prepnormal we are supposed to prepare the
2015 * statement before executing them
2016 */
2018 {
2019 if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2020 {
2022 return false;
2023 }
2024
2025 /*
2026 * statement is now prepared, so instead of the query we have to
2027 * execute the name
2028 */
2029 stmt->command = prepname;
2031 }
2032 else
2033 {
2034 stmt->command = ecpg_strdup(query, lineno, NULL);
2035 if (!stmt->command)
2036 {
2038 return false;
2039 }
2040 }
2041
2042 stmt->name = NULL;
2043
2045 {
2046 /* if we have an EXECUTE command, only the name is send */
2047 char *command = ecpg_prepared(stmt->command, con);
2048
2049 if (command)
2050 {
2051 stmt->name = stmt->command;
2052 stmt->command = ecpg_strdup(command, lineno, NULL);
2053 if (!stmt->command)
2054 {
2056 return false;
2057 }
2058 }
2059 else
2060 {
2063 return false;
2064 }
2065 }
2066 /* name of PREPARE AS will be set in loop of inlist */
2067
2068 stmt->connection = con;
2069 stmt->lineno = lineno;
2070 stmt->compat = compat;
2071 stmt->force_indicator = force_indicator;
2072 stmt->questionmarks = questionmarks;
2073 stmt->statement_type = statement_type;
2074
2075 /*------
2076 * create a list of variables
2077 *
2078 * The variables are listed with input variables preceding output
2079 * variables. The end of each group is marked by an end marker.
2080 * Per variable we list:
2081 *
2082 * type - as defined in ecpgtype.h
2083 * value - where to store the data
2084 * varcharsize - length of string in case we have a stringvariable, else 0
2085 * arraysize - 0 for pointer (we don't know the size of the array), 1 for
2086 * simple variable, size for arrays
2087 * offset - offset between ith and (i+1)th entry in an array, normally
2088 * that means sizeof(type)
2089 * ind_type - type of indicator variable
2090 * ind_pointer - pointer to indicator variable
2091 * ind_varcharsize - empty
2092 * ind_arrsize - arraysize of indicator array
2093 * ind_offset - indicator offset
2094 *------
2095 */
2096
2097 is_prepared_name_set = false;
2098
2099 list = &(stmt->inlist);
2100
2101 type = va_arg(args, enum ECPGttype);
2102
2103 while (type != ECPGt_EORT)
2104 {
2105 if (type == ECPGt_EOIT)
2106 list = &(stmt->outlist);
2107 else
2108 {
2109 struct variable *var,
2110 *ptr;
2111
2112 if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2113 {
2115 return false;
2116 }
2117
2118 var->type = type;
2119 var->pointer = va_arg(args, char *);
2120
2121 var->varcharsize = va_arg(args, long);
2122 var->arrsize = va_arg(args, long);
2123 var->offset = va_arg(args, long);
2124
2125 /*
2126 * Unknown array size means pointer to an array. Unknown
2127 * varcharsize usually also means pointer. But if the type is
2128 * character and the array size is known, it is an array of
2129 * pointers to char, so use var->pointer as it is.
2130 */
2131 if (var->arrsize == 0 ||
2132 (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
2133 var->value = *((char **) (var->pointer));
2134 else
2135 var->value = var->pointer;
2136
2137 /*
2138 * negative values are used to indicate an array without given
2139 * bounds
2140 */
2141 /* reset to zero for us */
2142 if (var->arrsize < 0)
2143 var->arrsize = 0;
2144 if (var->varcharsize < 0)
2145 var->varcharsize = 0;
2146
2147 var->next = NULL;
2148
2149 var->ind_type = va_arg(args, enum ECPGttype);
2150 var->ind_pointer = va_arg(args, char *);
2151 var->ind_varcharsize = va_arg(args, long);
2152 var->ind_arrsize = va_arg(args, long);
2153 var->ind_offset = va_arg(args, long);
2154
2155 if (var->ind_type != ECPGt_NO_INDICATOR
2156 && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2157 var->ind_value = *((char **) (var->ind_pointer));
2158 else
2159 var->ind_value = var->ind_pointer;
2160
2161 /*
2162 * negative values are used to indicate an array without given
2163 * bounds
2164 */
2165 /* reset to zero for us */
2166 if (var->ind_arrsize < 0)
2167 var->ind_arrsize = 0;
2168 if (var->ind_varcharsize < 0)
2169 var->ind_varcharsize = 0;
2170
2171 /* if variable is NULL, the statement hasn't been prepared */
2172 if (var->pointer == NULL)
2173 {
2175 ecpg_free(var);
2177 return false;
2178 }
2179
2180 for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2181 ;
2182
2183 if (ptr == NULL)
2184 *list = var;
2185 else
2186 ptr->next = var;
2187
2188 if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2189 {
2190 stmt->name = ecpg_strdup(var->value, lineno, NULL);
2191 if (!stmt->name)
2192 {
2194 return false;
2195 }
2196 is_prepared_name_set = true;
2197 }
2198 }
2199
2200 type = va_arg(args, enum ECPGttype);
2201 }
2202
2203 /* are we connected? */
2204 if (con == NULL || con->connection == NULL)
2205 {
2206 ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2208 return false;
2209 }
2210
2211 if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2212 {
2215 return false;
2216 }
2217
2218 /* initialize auto_mem struct */
2220
2221 *stmt_out = stmt;
2222
2223 return true;
2224}
2225
2226/*
2227 * ecpg_do_epilogue
2228 * Restore the application locale and free the statement structure.
2229 */
2230void
2232{
2233 if (stmt == NULL)
2234 return;
2235
2236#ifdef HAVE_USELOCALE
2237 if (stmt->oldlocale != (locale_t) 0)
2238 uselocale(stmt->oldlocale);
2239#else
2240 if (stmt->oldlocale)
2241 {
2242 setlocale(LC_NUMERIC, stmt->oldlocale);
2243#ifdef WIN32
2244 _configthreadlocale(stmt->oldthreadlocale);
2245#endif
2246 }
2247#endif
2248
2250}
2251
2252/*
2253 * Execute SQL statements in the backend.
2254 * The input/output parameters (variable argument list) are passed
2255 * in a va_list, so other functions can use this interface.
2256 */
2257bool
2258ecpg_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)
2259{
2260 struct statement *stmt = NULL;
2261
2264 query, args, &stmt))
2265 goto fail;
2266
2267 if (!ecpg_build_params(stmt))
2268 goto fail;
2269
2271 goto fail;
2272
2273 if (!ecpg_execute(stmt))
2274 goto fail;
2275
2276 if (!ecpg_process_output(stmt, true))
2277 goto fail;
2278
2280 return true;
2281
2282fail:
2284 return false;
2285}
2286
2287/*
2288 * Execute SQL statements in the backend.
2289 * The input/output parameters are passed as variable-length argument list.
2290 */
2291bool
2292ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
2293{
2294 va_list args;
2295 bool ret;
2296
2297 va_start(args, query);
2299 questionmarks, st, query, args);
2300 va_end(args);
2301
2302 return ret;
2303}
2304
2305/* old descriptor interface */
2306bool
2307ECPGdo_descriptor(int line, const char *connection,
2308 const char *descriptor, const char *query)
2309{
2310 return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2313}
void print(const void *obj)
Definition print.c:36
#define Assert(condition)
Definition c.h:945
#define ESCAPE_STRING_SYNTAX
Definition c.h:1257
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
Datum arg
Definition elog.c:1322
bool ecpg_build_params(struct statement *stmt)
Definition execute.c:1212
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition execute.c:304
static void sprintf_double_value(char *ptr, double value, const char *delim)
Definition execute.c:457
static void print_param_value(char *value, int len, int is_binary, int lineno, int nth)
Definition execute.c:1077
static bool insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
Definition execute.c:1128
bool ecpg_execute(struct statement *stmt)
Definition execute.c:1601
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:1107
static char * convert_bytea_to_string(char *from_data, int from_len, int lineno)
Definition execute.c:489
static enum ARRAY_TYPE ecpg_is_type_an_array(Oid 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:507
static bool store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item, char **tobeinserted)
Definition execute.c:1158
static void sprintf_float_value(char *ptr, float value, const char *delim)
Definition execute.c:473
void ecpg_do_epilogue(struct statement *stmt)
Definition execute.c:2231
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:2258
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:1943
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition execute.c:1670
bool ecpg_autostart_transaction(struct statement *stmt)
Definition execute.c:1580
bool ECPGdo_descriptor(int line, const char *connection, const char *descriptor, const char *query)
Definition execute.c:2307
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
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, Oid oid, enum ARRAY_TYPE isarray, int lineno)
Definition execute.c:148
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:2292
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
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
const char * str
#define stmt
#define false
static struct @174 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:1058
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:264
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
#define printf(...)
Definition port.h:266
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:191
void * ind_value
long ind_varcharsize
struct variable * next
enum ECPGttype type
enum ECPGttype ind_type
void * ind_pointer
void * pointer
Definition c.h:778
const char * type
#define locale_t
Definition win32_port.h:429
#define setlocale(a, b)
Definition win32_port.h:472