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