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