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