Age Owner Branch data TLA Line data Source code
1 : : /* src/interfaces/ecpg/ecpglib/prepare.c */
2 : :
3 : : #define POSTGRES_ECPG_INTERNAL
4 : : #include "postgres_fe.h"
5 : :
6 : : #include <ctype.h>
7 : :
8 : : #include "ecpgerrno.h"
9 : : #include "ecpglib.h"
10 : : #include "ecpglib_extern.h"
11 : : #include "ecpgtype.h"
12 : : #include "sqlca.h"
13 : :
14 : : #define STMTID_SIZE 32
15 : :
16 : : /*
17 : : * The statement cache contains stmtCacheNBuckets hash buckets, each
18 : : * having stmtCacheEntPerBucket entries, which we recycle as needed,
19 : : * giving up the least-executed entry in the bucket.
20 : : * stmtCacheEntries[0] is never used, so that zero can be a "not found"
21 : : * indicator.
22 : : */
23 : : #define stmtCacheNBuckets 2039 /* should be a prime number */
24 : : #define stmtCacheEntPerBucket 8
25 : :
26 : : #define stmtCacheArraySize (stmtCacheNBuckets * stmtCacheEntPerBucket + 1)
27 : :
28 : : typedef struct
29 : : {
30 : : int lineno;
31 : : char stmtID[STMTID_SIZE];
32 : : char *ecpgQuery;
33 : : long execs; /* # of executions */
34 : : const char *connection; /* connection for the statement */
35 : : } stmtCacheEntry;
36 : :
37 : : static int nextStmtID = 1;
38 : : static stmtCacheEntry *stmtCacheEntries = NULL;
39 : :
40 : : static bool deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con,
41 : : struct prepared_statement *prev, struct prepared_statement *this);
42 : :
43 : : static bool
8261 meskes@postgresql.or 44 :CBC 827 : isvarchar(unsigned char c)
45 : : {
46 [ + + ]: 827 : if (isalnum(c))
47 : 3 : return true;
48 : :
49 [ + - + - : 824 : if (c == '_' || c == '>' || c == '-' || c == '.')
+ - - + ]
8261 meskes@postgresql.or 50 :UBC 0 : return true;
51 : :
8261 meskes@postgresql.or 52 [ - + ]:CBC 824 : if (c >= 128)
8261 meskes@postgresql.or 53 :UBC 0 : return true;
54 : :
2993 peter_e@gmx.net 55 :CBC 824 : return false;
56 : : }
57 : :
58 : : bool
2350 meskes@postgresql.or 59 : 5 : ecpg_register_prepared_stmt(struct statement *stmt)
60 : : {
61 : : struct statement *prep_stmt;
62 : : struct prepared_statement *this;
2229 tgl@sss.pgh.pa.us 63 : 5 : struct connection *con = stmt->connection;
2350 meskes@postgresql.or 64 : 5 : struct prepared_statement *prev = NULL;
tgl@sss.pgh.pa.us 65 : 5 : int lineno = stmt->lineno;
66 : :
67 : : /* check if we already have prepared this statement */
68 : 5 : this = ecpg_find_prepared_statement(stmt->name, con, &prev);
69 [ - + - - ]: 5 : if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
2350 tgl@sss.pgh.pa.us 70 :UBC 0 : return false;
71 : :
72 : : /* allocate new statement */
2350 tgl@sss.pgh.pa.us 73 :CBC 5 : this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno);
74 [ - + ]: 5 : if (!this)
2350 tgl@sss.pgh.pa.us 75 :UBC 0 : return false;
76 : :
2350 tgl@sss.pgh.pa.us 77 :CBC 5 : prep_stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
2346 78 [ - + ]: 5 : if (!prep_stmt)
79 : : {
2350 tgl@sss.pgh.pa.us 80 :UBC 0 : ecpg_free(this);
81 : 0 : return false;
82 : : }
2350 tgl@sss.pgh.pa.us 83 :CBC 5 : memset(prep_stmt, 0, sizeof(struct statement));
84 : :
85 : : /* create statement */
86 : 5 : prep_stmt->lineno = lineno;
87 : 5 : prep_stmt->connection = con;
96 michael@paquier.xyz 88 :GNC 5 : prep_stmt->command = ecpg_strdup(stmt->command, lineno, NULL);
89 [ - + ]: 5 : if (!prep_stmt->command)
90 : : {
96 michael@paquier.xyz 91 :UNC 0 : ecpg_free(prep_stmt);
92 : 0 : ecpg_free(this);
93 : 0 : return false;
94 : : }
2350 tgl@sss.pgh.pa.us 95 :CBC 5 : prep_stmt->inlist = prep_stmt->outlist = NULL;
96 michael@paquier.xyz 96 :GNC 5 : this->name = ecpg_strdup(stmt->name, lineno, NULL);
97 [ - + ]: 5 : if (!this->name)
98 : : {
96 michael@paquier.xyz 99 :UNC 0 : ecpg_free(prep_stmt->command);
100 : 0 : ecpg_free(prep_stmt);
101 : 0 : ecpg_free(this);
102 : 0 : return false;
103 : : }
2350 tgl@sss.pgh.pa.us 104 :CBC 5 : this->stmt = prep_stmt;
105 : 5 : this->prepared = true;
106 : :
107 [ - + ]: 5 : if (con->prep_stmts == NULL)
2350 tgl@sss.pgh.pa.us 108 :UBC 0 : this->next = NULL;
109 : : else
2350 tgl@sss.pgh.pa.us 110 :CBC 5 : this->next = con->prep_stmts;
111 : :
112 : 5 : con->prep_stmts = this;
113 : 5 : return true;
114 : : }
115 : :
116 : : static bool
6004 meskes@postgresql.or 117 : 855 : replace_variables(char **text, int lineno)
118 : : {
6556 bruce@momjian.us 119 : 855 : bool string = false;
120 : 855 : int counter = 1,
121 : 855 : ptr = 0;
122 : :
6649 meskes@postgresql.or 123 [ + + ]: 24201 : for (; (*text)[ptr] != '\0'; ptr++)
124 : : {
125 [ + + ]: 23346 : if ((*text)[ptr] == '\'')
8261 126 : 2 : string = string ? false : true;
127 : :
6649 128 [ + + + + : 23346 : if (string || (((*text)[ptr] != ':') && ((*text)[ptr] != '?')))
+ + ]
129 : 22516 : continue;
130 : :
6556 bruce@momjian.us 131 [ + + + + ]: 830 : if (((*text)[ptr] == ':') && ((*text)[ptr + 1] == ':'))
132 : 2 : ptr += 2; /* skip '::' */
133 : : else
134 : : {
135 : : /* a rough guess of the size we need: */
2568 tgl@sss.pgh.pa.us 136 : 828 : int buffersize = sizeof(int) * CHAR_BIT * 10 / 3;
137 : : int len;
138 : : char *buffer,
139 : : *newcopy;
140 : :
6599 meskes@postgresql.or 141 [ - + ]: 828 : if (!(buffer = (char *) ecpg_alloc(buffersize, lineno)))
6649 meskes@postgresql.or 142 :UBC 0 : return false;
143 : :
6649 meskes@postgresql.or 144 :CBC 828 : snprintf(buffer, buffersize, "$%d", counter++);
145 : :
2568 tgl@sss.pgh.pa.us 146 [ + + + + ]: 831 : for (len = 1; (*text)[ptr + len] && isvarchar((*text)[ptr + len]); len++)
147 : : /* skip */ ;
1990 148 [ - + ]: 828 : if (!(newcopy = (char *) ecpg_alloc(strlen(*text) - len + strlen(buffer) + 1, lineno)))
149 : : {
6599 meskes@postgresql.or 150 :UBC 0 : ecpg_free(buffer);
6649 151 : 0 : return false;
152 : : }
153 : :
3929 tgl@sss.pgh.pa.us 154 :CBC 828 : memcpy(newcopy, *text, ptr);
6649 meskes@postgresql.or 155 : 828 : strcpy(newcopy + ptr, buffer);
6556 bruce@momjian.us 156 : 828 : strcat(newcopy, (*text) +ptr + len);
157 : :
6599 meskes@postgresql.or 158 : 828 : ecpg_free(*text);
159 : 828 : ecpg_free(buffer);
160 : :
6649 161 : 828 : *text = newcopy;
162 : :
6556 bruce@momjian.us 163 [ - + ]: 828 : if ((*text)[ptr] == '\0') /* we reached the end */
6556 bruce@momjian.us 164 :UBC 0 : ptr--; /* since we will (*text)[ptr]++ in the top
165 : : * level for loop */
166 : : }
167 : : }
6649 meskes@postgresql.or 168 :CBC 855 : return true;
169 : : }
170 : :
171 : : static bool
3050 tgl@sss.pgh.pa.us 172 : 855 : prepare_common(int lineno, struct connection *con, const char *name, const char *variable)
173 : : {
174 : : struct statement *stmt;
175 : : struct prepared_statement *this;
176 : : PGresult *query;
177 : :
178 : : /* allocate new statement */
6599 meskes@postgresql.or 179 : 855 : this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno);
8261 180 [ - + ]: 855 : if (!this)
8261 meskes@postgresql.or 181 :UBC 0 : return false;
182 : :
6599 meskes@postgresql.or 183 :CBC 855 : stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
8261 184 [ - + ]: 855 : if (!stmt)
185 : : {
6599 meskes@postgresql.or 186 :UBC 0 : ecpg_free(this);
8261 187 : 0 : return false;
188 : : }
189 : :
190 : : /* create statement */
8261 meskes@postgresql.or 191 :CBC 855 : stmt->lineno = lineno;
6606 192 : 855 : stmt->connection = con;
96 michael@paquier.xyz 193 :GNC 855 : stmt->command = ecpg_strdup(variable, lineno, NULL);
194 [ - + ]: 855 : if (!stmt->command)
195 : : {
96 michael@paquier.xyz 196 :UNC 0 : ecpg_free(stmt);
197 : 0 : ecpg_free(this);
198 : 0 : return false;
199 : : }
8261 meskes@postgresql.or 200 :CBC 855 : stmt->inlist = stmt->outlist = NULL;
201 : :
202 : : /* if we have C variables in our statement replace them with '?' */
10 dgustafsson@postgres 203 [ - + ]:GNC 855 : if (!replace_variables(&(stmt->command), lineno))
204 : : {
10 dgustafsson@postgres 205 :UNC 0 : ecpg_free(stmt->command);
206 : 0 : ecpg_free(stmt);
207 : 0 : ecpg_free(this);
208 : 0 : return false;
209 : : }
210 : :
211 : : /* add prepared statement to our list */
96 michael@paquier.xyz 212 :GNC 855 : this->name = ecpg_strdup(name, lineno, NULL);
213 [ - + ]: 855 : if (!this->name)
214 : : {
96 michael@paquier.xyz 215 :UNC 0 : ecpg_free(stmt->command);
216 : 0 : ecpg_free(stmt);
217 : 0 : ecpg_free(this);
218 : 0 : return false;
219 : : }
8261 meskes@postgresql.or 220 :CBC 855 : this->stmt = stmt;
221 : :
222 : : /* and finally really prepare the statement */
6649 223 : 855 : query = PQprepare(stmt->connection->connection, name, stmt->command, 0, NULL);
6599 224 [ - + ]: 855 : if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
225 : : {
6599 meskes@postgresql.or 226 :UBC 0 : ecpg_free(stmt->command);
5856 227 : 0 : ecpg_free(this->name);
6599 228 : 0 : ecpg_free(this);
229 : 0 : ecpg_free(stmt);
6649 230 : 0 : return false;
231 : : }
232 : :
5699 meskes@postgresql.or 233 :CBC 855 : ecpg_log("prepare_common on line %d: name %s; query: \"%s\"\n", stmt->lineno, name, stmt->command);
6649 234 : 855 : PQclear(query);
235 : 855 : this->prepared = true;
236 : :
6606 237 [ + + ]: 855 : if (con->prep_stmts == NULL)
8261 238 : 829 : this->next = NULL;
239 : : else
6606 240 : 26 : this->next = con->prep_stmts;
241 : :
242 : 855 : con->prep_stmts = this;
8261 243 : 855 : return true;
244 : : }
245 : :
246 : : /* handle the EXEC SQL PREPARE statement */
247 : : /* questionmarks is not needed but remains in there for the time being to not change the API */
248 : : bool
2568 tgl@sss.pgh.pa.us 249 : 852 : ECPGprepare(int lineno, const char *connection_name, const bool questionmarks,
250 : : const char *name, const char *variable)
251 : : {
252 : : struct connection *con;
253 : : struct prepared_statement *this,
254 : : *prev;
255 : :
256 : : (void) questionmarks; /* quiet the compiler */
257 : :
2229 258 : 852 : con = ecpg_get_connection(connection_name);
259 [ - + ]: 852 : if (!ecpg_init(con, connection_name, lineno))
5757 meskes@postgresql.or 260 :UBC 0 : return false;
261 : :
262 : : /* check if we already have prepared this statement */
5757 meskes@postgresql.or 263 :CBC 852 : this = ecpg_find_prepared_statement(name, con, &prev);
264 [ + + - + ]: 852 : if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
5757 meskes@postgresql.or 265 :UBC 0 : return false;
266 : :
5473 meskes@postgresql.or 267 :CBC 852 : return prepare_common(lineno, con, name, variable);
268 : : }
269 : :
270 : : struct prepared_statement *
5764 271 : 1789 : ecpg_find_prepared_statement(const char *name,
272 : : struct connection *con, struct prepared_statement **prev_)
273 : : {
274 : : struct prepared_statement *this,
275 : : *prev;
276 : :
2568 tgl@sss.pgh.pa.us 277 : 1789 : for (this = con->prep_stmts, prev = NULL;
278 [ + + ]: 1856 : this != NULL;
279 : 67 : prev = this, this = this->next)
280 : : {
6606 meskes@postgresql.or 281 [ + + ]: 1782 : if (strcmp(this->name, name) == 0)
282 : : {
283 [ + + ]: 1715 : if (prev_)
284 : 839 : *prev_ = prev;
285 : 1715 : return this;
286 : : }
287 : : }
288 : 74 : return NULL;
289 : : }
290 : :
291 : : static bool
2568 tgl@sss.pgh.pa.us 292 : 858 : deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con,
293 : : struct prepared_statement *prev, struct prepared_statement *this)
294 : : {
6556 bruce@momjian.us 295 : 858 : bool r = false;
296 : :
5699 meskes@postgresql.or 297 : 858 : ecpg_log("deallocate_one on line %d: name %s\n", lineno, this->name);
298 : :
299 : : /* first deallocate the statement in the backend */
6606 300 [ + - ]: 858 : if (this->prepared)
301 : : {
302 : : char *text;
303 : : PGresult *query;
304 : :
6599 305 : 858 : text = (char *) ecpg_alloc(strlen("deallocate \"\" ") + strlen(this->name), this->stmt->lineno);
306 : :
6606 307 [ + - ]: 858 : if (text)
308 : : {
309 : 858 : sprintf(text, "deallocate \"%s\"", this->name);
310 : 858 : query = PQexec(this->stmt->connection->connection, text);
6599 311 : 858 : ecpg_free(text);
2568 tgl@sss.pgh.pa.us 312 [ + - ]: 858 : if (ecpg_check_PQresult(query, lineno,
313 : 858 : this->stmt->connection->connection,
314 : 858 : this->stmt->compat))
315 : : {
6649 meskes@postgresql.or 316 : 858 : PQclear(query);
6606 317 : 858 : r = true;
318 : : }
319 : : }
320 : : }
321 : :
322 : : /*
323 : : * Just ignore all errors since we do not know the list of cursors we are
324 : : * allowed to free. We have to trust the software.
325 : : */
326 [ - + - - : 858 : if (!r && !INFORMIX_MODE(c))
- - ]
327 : : {
6599 meskes@postgresql.or 328 :UBC 0 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, this->name);
6606 329 : 0 : return false;
330 : : }
331 : :
332 : : /* okay, free all the resources */
6599 meskes@postgresql.or 333 :CBC 858 : ecpg_free(this->stmt->command);
334 : 858 : ecpg_free(this->stmt);
5856 335 : 858 : ecpg_free(this->name);
6606 336 [ + + ]: 858 : if (prev != NULL)
337 : 3 : prev->next = this->next;
338 : : else
339 : 855 : con->prep_stmts = this->next;
340 : :
6599 341 : 858 : ecpg_free(this);
6606 342 : 858 : return true;
343 : : }
344 : :
345 : : /* handle the EXEC SQL DEALLOCATE PREPARE statement */
346 : : bool
347 : 53 : ECPGdeallocate(int lineno, int c, const char *connection_name, const char *name)
348 : : {
349 : : struct connection *con;
350 : : struct prepared_statement *this,
351 : : *prev;
352 : :
2229 tgl@sss.pgh.pa.us 353 : 53 : con = ecpg_get_connection(connection_name);
354 [ - + ]: 53 : if (!ecpg_init(con, connection_name, lineno))
6377 meskes@postgresql.or 355 :UBC 0 : return false;
356 : :
5764 meskes@postgresql.or 357 :CBC 53 : this = ecpg_find_prepared_statement(name, con, &prev);
6606 358 [ + + ]: 53 : if (this)
359 : 52 : return deallocate_one(lineno, c, con, prev, this);
360 : :
361 : : /* prepared statement is not found */
362 [ + - - + ]: 1 : if (INFORMIX_MODE(c))
6606 meskes@postgresql.or 363 :UBC 0 : return true;
6599 meskes@postgresql.or 364 :CBC 1 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, name);
6606 365 : 1 : return false;
366 : : }
367 : :
368 : : bool
3050 tgl@sss.pgh.pa.us 369 : 124 : ecpg_deallocate_all_conn(int lineno, enum COMPAT_MODE c, struct connection *con)
370 : : {
371 : : /* deallocate all prepared statements */
6606 meskes@postgresql.or 372 [ + + ]: 143 : while (con->prep_stmts)
373 : : {
6602 374 [ - + ]: 19 : if (!deallocate_one(lineno, c, con, NULL, con->prep_stmts))
8261 meskes@postgresql.or 375 :UBC 0 : return false;
376 : : }
377 : :
8261 meskes@postgresql.or 378 :CBC 124 : return true;
379 : : }
380 : :
381 : : bool
6602 382 : 1 : ECPGdeallocate_all(int lineno, int compat, const char *connection_name)
383 : : {
2568 tgl@sss.pgh.pa.us 384 : 1 : return ecpg_deallocate_all_conn(lineno, compat,
385 : : ecpg_get_connection(connection_name));
386 : : }
387 : :
388 : : char *
3050 389 : 853 : ecpg_prepared(const char *name, struct connection *con)
390 : : {
391 : : struct prepared_statement *this;
392 : :
5764 meskes@postgresql.or 393 : 853 : this = ecpg_find_prepared_statement(name, con, NULL);
6606 394 [ + - ]: 853 : return this ? this->stmt->command : NULL;
395 : : }
396 : :
397 : : /* return the prepared statement */
398 : : /* lineno is not used here, but kept in to not break API */
399 : : char *
400 : 21 : ECPGprepared_statement(const char *connection_name, const char *name, int lineno)
401 : : {
402 : : (void) lineno; /* keep the compiler quiet */
403 : :
2229 tgl@sss.pgh.pa.us 404 : 21 : return ecpg_prepared(name, ecpg_get_connection(connection_name));
405 : : }
406 : :
407 : : /*
408 : : * hash a SQL statement - returns entry # of first entry in the bucket
409 : : */
410 : : static int
6649 meskes@postgresql.or 411 : 10 : HashStmt(const char *ecpgQuery)
412 : : {
413 : : int stmtIx,
414 : : bucketNo,
415 : : hashLeng,
416 : : stmtLeng;
417 : : uint64 hashVal,
418 : : rotVal;
419 : :
6556 bruce@momjian.us 420 : 10 : stmtLeng = strlen(ecpgQuery);
2568 tgl@sss.pgh.pa.us 421 : 10 : hashLeng = 50; /* use 1st 50 characters of statement */
422 [ + - ]: 10 : if (hashLeng > stmtLeng) /* if the statement isn't that long */
423 : 10 : hashLeng = stmtLeng; /* use its actual length */
424 : :
6556 bruce@momjian.us 425 : 10 : hashVal = 0;
426 [ + + ]: 371 : for (stmtIx = 0; stmtIx < hashLeng; ++stmtIx)
427 : : {
2567 tgl@sss.pgh.pa.us 428 : 361 : hashVal = hashVal + (unsigned char) ecpgQuery[stmtIx];
429 : : /* rotate 32-bit hash value left 13 bits */
6556 bruce@momjian.us 430 : 361 : hashVal = hashVal << 13;
2567 tgl@sss.pgh.pa.us 431 : 361 : rotVal = (hashVal & UINT64CONST(0x1fff00000000)) >> 32;
432 : 361 : hashVal = (hashVal & UINT64CONST(0xffffffff)) | rotVal;
433 : : }
434 : :
6556 bruce@momjian.us 435 : 10 : bucketNo = hashVal % stmtCacheNBuckets;
436 : :
437 : : /* Add 1 so that array entry 0 is never used */
2567 tgl@sss.pgh.pa.us 438 : 10 : return bucketNo * stmtCacheEntPerBucket + 1;
439 : : }
440 : :
441 : : /*
442 : : * search the statement cache - search for entry with matching ECPG-format query
443 : : * Returns entry # in cache if found
444 : : * OR zero if not present (zero'th entry isn't used)
445 : : */
446 : : static int
6649 meskes@postgresql.or 447 : 8 : SearchStmtCache(const char *ecpgQuery)
448 : : {
449 : : int entNo,
450 : : entIx;
451 : :
452 : : /* quick failure if cache not set up */
2567 tgl@sss.pgh.pa.us 453 [ + + ]: 8 : if (stmtCacheEntries == NULL)
454 : 1 : return 0;
455 : :
456 : : /* hash the statement */
6556 bruce@momjian.us 457 : 7 : entNo = HashStmt(ecpgQuery);
458 : :
459 : : /* search the cache */
460 [ + + ]: 23 : for (entIx = 0; entIx < stmtCacheEntPerBucket; ++entIx)
461 : : {
2568 tgl@sss.pgh.pa.us 462 [ + + ]: 21 : if (stmtCacheEntries[entNo].stmtID[0]) /* check if entry is in use */
463 : : {
5053 peter_e@gmx.net 464 [ + - ]: 5 : if (strcmp(ecpgQuery, stmtCacheEntries[entNo].ecpgQuery) == 0)
2568 tgl@sss.pgh.pa.us 465 : 5 : break; /* found it */
466 : : }
467 : 16 : ++entNo; /* incr entry # */
468 : : }
469 : :
470 : : /* if entry wasn't found - set entry # to zero */
6556 bruce@momjian.us 471 [ + + ]: 7 : if (entIx >= stmtCacheEntPerBucket)
472 : 2 : entNo = 0;
473 : :
2993 peter_e@gmx.net 474 : 7 : return entNo;
475 : : }
476 : :
477 : : /*
478 : : * free an entry in the statement cache
479 : : * Returns entry # in cache used
480 : : * OR negative error code
481 : : */
482 : : static int
2568 tgl@sss.pgh.pa.us 483 : 3 : ecpg_freeStmtCacheEntry(int lineno, int compat,
484 : : int entNo) /* entry # to free */
485 : : {
486 : : stmtCacheEntry *entry;
487 : : struct connection *con;
488 : : struct prepared_statement *this,
489 : : *prev;
490 : :
491 : : /* fail if cache isn't set up */
2567 492 [ - + ]: 3 : if (stmtCacheEntries == NULL)
2567 tgl@sss.pgh.pa.us 493 :UBC 0 : return -1;
494 : :
6556 bruce@momjian.us 495 :CBC 3 : entry = &stmtCacheEntries[entNo];
2568 tgl@sss.pgh.pa.us 496 [ + - ]: 3 : if (!entry->stmtID[0]) /* return if the entry isn't in use */
2993 peter_e@gmx.net 497 : 3 : return 0;
498 : :
6556 bruce@momjian.us 499 :UBC 0 : con = ecpg_get_connection(entry->connection);
500 : :
501 : : /* free the 'prepared_statement' list entry */
5764 meskes@postgresql.or 502 : 0 : this = ecpg_find_prepared_statement(entry->stmtID, con, &prev);
6472 503 [ # # # # ]: 0 : if (this && !deallocate_one(lineno, compat, con, prev, this))
2993 peter_e@gmx.net 504 : 0 : return -1;
505 : :
6556 bruce@momjian.us 506 : 0 : entry->stmtID[0] = '\0';
507 : :
508 : : /* free the memory used by the cache entry */
509 [ # # ]: 0 : if (entry->ecpgQuery)
510 : : {
511 : 0 : ecpg_free(entry->ecpgQuery);
512 : 0 : entry->ecpgQuery = 0;
513 : : }
514 : :
2993 peter_e@gmx.net 515 : 0 : return entNo;
516 : : }
517 : :
518 : : /*
519 : : * add an entry to the statement cache
520 : : * returns entry # in cache used OR negative error code
521 : : */
522 : : static int
2568 tgl@sss.pgh.pa.us 523 :CBC 3 : AddStmtToCache(int lineno, /* line # of statement */
524 : : const char *stmtID, /* statement ID */
525 : : const char *connection, /* connection */
526 : : int compat, /* compatibility level */
527 : : const char *ecpgQuery) /* query */
528 : : {
529 : : int ix,
530 : : initEntNo,
531 : : luEntNo,
532 : : entNo;
533 : : stmtCacheEntry *entry;
534 : :
535 : : /* allocate and zero cache array if we haven't already */
2567 536 [ + + ]: 3 : if (stmtCacheEntries == NULL)
537 : : {
538 : 1 : stmtCacheEntries = (stmtCacheEntry *)
539 : 1 : ecpg_alloc(sizeof(stmtCacheEntry) * stmtCacheArraySize, lineno);
540 [ - + ]: 1 : if (stmtCacheEntries == NULL)
2567 tgl@sss.pgh.pa.us 541 :UBC 0 : return -1;
542 : : }
543 : :
544 : : /* hash the statement */
6556 bruce@momjian.us 545 :CBC 3 : initEntNo = HashStmt(ecpgQuery);
546 : :
547 : : /* search for an unused entry */
548 : 3 : entNo = initEntNo; /* start with the initial entry # for the
549 : : * bucket */
2568 tgl@sss.pgh.pa.us 550 : 3 : luEntNo = initEntNo; /* use it as the initial 'least used' entry */
6556 bruce@momjian.us 551 [ + - ]: 3 : for (ix = 0; ix < stmtCacheEntPerBucket; ++ix)
552 : : {
553 : 3 : entry = &stmtCacheEntries[entNo];
2568 tgl@sss.pgh.pa.us 554 [ + - ]: 3 : if (!entry->stmtID[0]) /* unused entry - use it */
6556 bruce@momjian.us 555 : 3 : break;
6556 bruce@momjian.us 556 [ # # ]:UBC 0 : if (entry->execs < stmtCacheEntries[luEntNo].execs)
2568 tgl@sss.pgh.pa.us 557 : 0 : luEntNo = entNo; /* save new 'least used' entry */
558 : 0 : ++entNo; /* increment entry # */
559 : : }
560 : :
561 : : /*
562 : : * if no unused entries were found, re-use the 'least used' entry found in
563 : : * the bucket
564 : : */
2568 tgl@sss.pgh.pa.us 565 [ - + ]:CBC 3 : if (ix >= stmtCacheEntPerBucket)
2568 tgl@sss.pgh.pa.us 566 :UBC 0 : entNo = luEntNo;
567 : :
568 : : /* 'entNo' is the entry to use - make sure its free */
6472 meskes@postgresql.or 569 [ - + ]:CBC 3 : if (ecpg_freeStmtCacheEntry(lineno, compat, entNo) < 0)
2993 peter_e@gmx.net 570 :UBC 0 : return -1;
571 : :
572 : : /* add the query to the entry */
6556 bruce@momjian.us 573 :CBC 3 : entry = &stmtCacheEntries[entNo];
574 : 3 : entry->lineno = lineno;
96 michael@paquier.xyz 575 :GNC 3 : entry->ecpgQuery = ecpg_strdup(ecpgQuery, lineno, NULL);
576 [ - + ]: 3 : if (!entry->ecpgQuery)
96 michael@paquier.xyz 577 :UNC 0 : return -1;
4987 peter_e@gmx.net 578 :CBC 3 : entry->connection = connection;
6556 bruce@momjian.us 579 : 3 : entry->execs = 0;
580 : 3 : memcpy(entry->stmtID, stmtID, sizeof(entry->stmtID));
581 : :
2993 peter_e@gmx.net 582 : 3 : return entNo;
583 : : }
584 : :
585 : : /* handle cache and preparation of statements in auto-prepare mode */
586 : : bool
5941 magnus@hagander.net 587 : 8 : ecpg_auto_prepare(int lineno, const char *connection_name, const int compat, char **name, const char *query)
588 : : {
589 : : int entNo;
590 : :
591 : : /* search the statement cache for this statement */
6649 meskes@postgresql.or 592 : 8 : entNo = SearchStmtCache(query);
593 : :
594 : : /* if not found - add the statement to the cache */
6556 bruce@momjian.us 595 [ + + ]: 8 : if (entNo)
596 : : {
597 : : char *stmtID;
598 : : struct connection *con;
599 : : struct prepared_statement *prep;
600 : :
6373 peter_e@gmx.net 601 : 5 : ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
602 : :
5757 meskes@postgresql.or 603 : 5 : stmtID = stmtCacheEntries[entNo].stmtID;
96 michael@paquier.xyz 604 :GNC 5 : *name = ecpg_strdup(stmtID, lineno, NULL);
605 [ - + ]: 5 : if (*name == NULL)
96 michael@paquier.xyz 606 :UNC 0 : return false;
607 : :
5757 meskes@postgresql.or 608 :CBC 5 : con = ecpg_get_connection(connection_name);
609 : 5 : prep = ecpg_find_prepared_statement(stmtID, con, NULL);
610 : : /* This prepared name doesn't exist on this connection. */
5473 611 [ + + - + ]: 5 : if (!prep && !prepare_common(lineno, con, stmtID, query))
612 : : {
91 michael@paquier.xyz 613 :UNC 0 : ecpg_free(*name);
2993 peter_e@gmx.net 614 :UBC 0 : return false;
615 : : }
616 : :
617 : : }
618 : : else
619 : : {
620 : : char stmtID[STMTID_SIZE];
621 : :
6373 peter_e@gmx.net 622 :CBC 3 : ecpg_log("ecpg_auto_prepare on line %d: statement not in cache; inserting\n", lineno);
623 : :
624 : : /* generate a statement ID */
5757 meskes@postgresql.or 625 : 3 : sprintf(stmtID, "ecpg%d", nextStmtID++);
96 michael@paquier.xyz 626 :GNC 3 : *name = ecpg_strdup(stmtID, lineno, NULL);
627 [ - + ]: 3 : if (*name == NULL)
96 michael@paquier.xyz 628 :UNC 0 : return false;
629 : :
5757 meskes@postgresql.or 630 [ - + ]:CBC 3 : if (!ECPGprepare(lineno, connection_name, 0, stmtID, query))
631 : : {
91 michael@paquier.xyz 632 :UNC 0 : ecpg_free(*name);
2993 peter_e@gmx.net 633 :UBC 0 : return false;
634 : : }
635 : :
2567 tgl@sss.pgh.pa.us 636 :CBC 3 : entNo = AddStmtToCache(lineno, stmtID, connection_name, compat, query);
637 [ - + ]: 3 : if (entNo < 0)
638 : : {
91 michael@paquier.xyz 639 :UNC 0 : ecpg_free(*name);
2993 peter_e@gmx.net 640 :UBC 0 : return false;
641 : : }
642 : : }
643 : :
644 : : /* increase usage counter */
6649 meskes@postgresql.or 645 :CBC 8 : stmtCacheEntries[entNo].execs++;
646 : :
2993 peter_e@gmx.net 647 : 8 : return true;
648 : : }
|