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
8451 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 == '.')
+ - - + ]
8451 meskes@postgresql.or 50 :UBC 0 : return true;
51 : :
8451 meskes@postgresql.or 52 [ - + ]:CBC 824 : if (c >= 128)
8451 meskes@postgresql.or 53 :UBC 0 : return true;
54 : :
3183 peter_e@gmx.net 55 :CBC 824 : return false;
56 : : }
57 : :
58 : : bool
2540 meskes@postgresql.or 59 : 5 : ecpg_register_prepared_stmt(struct statement *stmt)
60 : : {
61 : : struct statement *prep_stmt;
62 : : struct prepared_statement *this;
2419 tgl@sss.pgh.pa.us 63 : 5 : struct connection *con = stmt->connection;
2540 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))
2540 tgl@sss.pgh.pa.us 70 :UBC 0 : return false;
71 : :
72 : : /* allocate new statement */
2540 tgl@sss.pgh.pa.us 73 :CBC 5 : this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno);
74 [ - + ]: 5 : if (!this)
2540 tgl@sss.pgh.pa.us 75 :UBC 0 : return false;
76 : :
2540 tgl@sss.pgh.pa.us 77 :CBC 5 : prep_stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
2536 78 [ - + ]: 5 : if (!prep_stmt)
79 : : {
2540 tgl@sss.pgh.pa.us 80 :UBC 0 : ecpg_free(this);
81 : 0 : return false;
82 : : }
2540 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;
286 michael@paquier.xyz 88 :GNC 5 : prep_stmt->command = ecpg_strdup(stmt->command, lineno, NULL);
89 [ - + ]: 5 : if (!prep_stmt->command)
90 : : {
286 michael@paquier.xyz 91 :UNC 0 : ecpg_free(prep_stmt);
92 : 0 : ecpg_free(this);
93 : 0 : return false;
94 : : }
2540 tgl@sss.pgh.pa.us 95 :CBC 5 : prep_stmt->inlist = prep_stmt->outlist = NULL;
286 michael@paquier.xyz 96 :GNC 5 : this->name = ecpg_strdup(stmt->name, lineno, NULL);
97 [ - + ]: 5 : if (!this->name)
98 : : {
286 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 : : }
2540 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)
2540 tgl@sss.pgh.pa.us 108 :UBC 0 : this->next = NULL;
109 : : else
2540 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
6194 meskes@postgresql.or 117 : 857 : replace_variables(char **text, int lineno)
118 : : {
6746 bruce@momjian.us 119 : 857 : bool string = false;
120 : 857 : int counter = 1,
121 : 857 : ptr = 0;
122 : :
6839 meskes@postgresql.or 123 [ + + ]: 24392 : for (; (*text)[ptr] != '\0'; ptr++)
124 : : {
125 [ + + ]: 23535 : if ((*text)[ptr] == '\'')
8451 126 : 2 : string = string ? false : true;
127 : :
6839 128 [ + + + + : 23535 : if (string || (((*text)[ptr] != ':') && ((*text)[ptr] != '?')))
+ + ]
129 : 22705 : continue;
130 : :
6746 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: */
2758 tgl@sss.pgh.pa.us 136 : 828 : int buffersize = sizeof(int) * CHAR_BIT * 10 / 3;
137 : : int len;
138 : : char *buffer,
139 : : *newcopy;
140 : :
154 peter@eisentraut.org 141 [ - + ]:GNC 828 : if (!(buffer = ecpg_alloc(buffersize, lineno)))
6839 meskes@postgresql.or 142 :UBC 0 : return false;
143 : :
6839 meskes@postgresql.or 144 :CBC 828 : snprintf(buffer, buffersize, "$%d", counter++);
145 : :
2758 tgl@sss.pgh.pa.us 146 [ + + + + ]: 831 : for (len = 1; (*text)[ptr + len] && isvarchar((*text)[ptr + len]); len++)
147 : : /* skip */ ;
154 peter@eisentraut.org 148 [ - + ]:GNC 828 : if (!(newcopy = ecpg_alloc(strlen(*text) - len + strlen(buffer) + 1, lineno)))
149 : : {
6789 meskes@postgresql.or 150 :UBC 0 : ecpg_free(buffer);
6839 151 : 0 : return false;
152 : : }
153 : :
4119 tgl@sss.pgh.pa.us 154 :CBC 828 : memcpy(newcopy, *text, ptr);
6839 meskes@postgresql.or 155 : 828 : strcpy(newcopy + ptr, buffer);
6746 bruce@momjian.us 156 : 828 : strcat(newcopy, (*text) +ptr + len);
157 : :
6789 meskes@postgresql.or 158 : 828 : ecpg_free(*text);
159 : 828 : ecpg_free(buffer);
160 : :
6839 161 : 828 : *text = newcopy;
162 : :
6746 bruce@momjian.us 163 [ - + ]: 828 : if ((*text)[ptr] == '\0') /* we reached the end */
6746 bruce@momjian.us 164 :UBC 0 : ptr--; /* since we will (*text)[ptr]++ in the top
165 : : * level for loop */
166 : : }
167 : : }
6839 meskes@postgresql.or 168 :CBC 857 : return true;
169 : : }
170 : :
171 : : static bool
3240 tgl@sss.pgh.pa.us 172 : 857 : 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 */
6789 meskes@postgresql.or 179 : 857 : this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno);
8451 180 [ - + ]: 857 : if (!this)
8451 meskes@postgresql.or 181 :UBC 0 : return false;
182 : :
6789 meskes@postgresql.or 183 :CBC 857 : stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
8451 184 [ - + ]: 857 : if (!stmt)
185 : : {
6789 meskes@postgresql.or 186 :UBC 0 : ecpg_free(this);
8451 187 : 0 : return false;
188 : : }
189 : :
190 : : /* create statement */
8451 meskes@postgresql.or 191 :CBC 857 : stmt->lineno = lineno;
6796 192 : 857 : stmt->connection = con;
286 michael@paquier.xyz 193 :GNC 857 : stmt->command = ecpg_strdup(variable, lineno, NULL);
194 [ - + ]: 857 : if (!stmt->command)
195 : : {
286 michael@paquier.xyz 196 :UNC 0 : ecpg_free(stmt);
197 : 0 : ecpg_free(this);
198 : 0 : return false;
199 : : }
8451 meskes@postgresql.or 200 :CBC 857 : stmt->inlist = stmt->outlist = NULL;
201 : :
202 : : /* if we have C variables in our statement replace them with '?' */
200 dgustafsson@postgres 203 [ - + ]:GNC 857 : if (!replace_variables(&(stmt->command), lineno))
204 : : {
200 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 */
286 michael@paquier.xyz 212 :GNC 857 : this->name = ecpg_strdup(name, lineno, NULL);
213 [ - + ]: 857 : if (!this->name)
214 : : {
286 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 : : }
8451 meskes@postgresql.or 220 :CBC 857 : this->stmt = stmt;
221 : :
222 : : /* and finally really prepare the statement */
6839 223 : 857 : query = PQprepare(stmt->connection->connection, name, stmt->command, 0, NULL);
6789 224 [ - + ]: 857 : if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
225 : : {
6789 meskes@postgresql.or 226 :UBC 0 : ecpg_free(stmt->command);
6046 227 : 0 : ecpg_free(this->name);
6789 228 : 0 : ecpg_free(this);
229 : 0 : ecpg_free(stmt);
6839 230 : 0 : return false;
231 : : }
232 : :
5889 meskes@postgresql.or 233 :CBC 857 : ecpg_log("prepare_common on line %d: name %s; query: \"%s\"\n", stmt->lineno, name, stmt->command);
6839 234 : 857 : PQclear(query);
235 : 857 : this->prepared = true;
236 : :
6796 237 [ + + ]: 857 : if (con->prep_stmts == NULL)
8451 238 : 831 : this->next = NULL;
239 : : else
6796 240 : 26 : this->next = con->prep_stmts;
241 : :
242 : 857 : con->prep_stmts = this;
8451 243 : 857 : 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
2758 tgl@sss.pgh.pa.us 249 : 855 : 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 : :
2419 258 : 855 : con = ecpg_get_connection(connection_name);
259 [ + + ]: 855 : if (!ecpg_init(con, connection_name, lineno))
5947 meskes@postgresql.or 260 : 1 : return false;
261 : :
262 : : /* check if we already have prepared this statement */
263 : 854 : this = ecpg_find_prepared_statement(name, con, &prev);
264 [ + + - + ]: 854 : if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
5947 meskes@postgresql.or 265 :UBC 0 : return false;
266 : :
5663 meskes@postgresql.or 267 :CBC 854 : return prepare_common(lineno, con, name, variable);
268 : : }
269 : :
270 : : struct prepared_statement *
5954 271 : 1794 : ecpg_find_prepared_statement(const char *name,
272 : : struct connection *con, struct prepared_statement **prev_)
273 : : {
274 : : struct prepared_statement *this,
275 : : *prev;
276 : :
2758 tgl@sss.pgh.pa.us 277 : 1794 : for (this = con->prep_stmts, prev = NULL;
278 [ + + ]: 1861 : this != NULL;
279 : 67 : prev = this, this = this->next)
280 : : {
6796 meskes@postgresql.or 281 [ + + ]: 1785 : if (strcmp(this->name, name) == 0)
282 : : {
283 [ + + ]: 1718 : if (prev_)
284 : 840 : *prev_ = prev;
285 : 1718 : return this;
286 : : }
287 : : }
288 : 76 : return NULL;
289 : : }
290 : :
291 : : static bool
2758 tgl@sss.pgh.pa.us 292 : 860 : deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con,
293 : : struct prepared_statement *prev, struct prepared_statement *this)
294 : : {
6746 bruce@momjian.us 295 : 860 : bool r = false;
296 : :
5889 meskes@postgresql.or 297 : 860 : ecpg_log("deallocate_one on line %d: name %s\n", lineno, this->name);
298 : :
299 : : /* first deallocate the statement in the backend */
6796 300 [ + - ]: 860 : if (this->prepared)
301 : : {
302 : : char *text;
303 : : PGresult *query;
304 : :
154 peter@eisentraut.org 305 :GNC 860 : text = ecpg_alloc(strlen("deallocate \"\" ") + strlen(this->name), this->stmt->lineno);
306 : :
6796 meskes@postgresql.or 307 [ + - ]:CBC 860 : if (text)
308 : : {
309 : 860 : sprintf(text, "deallocate \"%s\"", this->name);
310 : 860 : query = PQexec(this->stmt->connection->connection, text);
6789 311 : 860 : ecpg_free(text);
2758 tgl@sss.pgh.pa.us 312 [ + - ]: 860 : if (ecpg_check_PQresult(query, lineno,
313 : 860 : this->stmt->connection->connection,
314 : 860 : this->stmt->compat))
315 : : {
6839 meskes@postgresql.or 316 : 860 : PQclear(query);
6796 317 : 860 : 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 [ - + - - : 860 : if (!r && !INFORMIX_MODE(c))
- - ]
327 : : {
6789 meskes@postgresql.or 328 :UBC 0 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, this->name);
6796 329 : 0 : return false;
330 : : }
331 : :
332 : : /* okay, free all the resources */
6789 meskes@postgresql.or 333 :CBC 860 : ecpg_free(this->stmt->command);
334 : 860 : ecpg_free(this->stmt);
6046 335 : 860 : ecpg_free(this->name);
6796 336 [ + + ]: 860 : if (prev != NULL)
337 : 3 : prev->next = this->next;
338 : : else
339 : 857 : con->prep_stmts = this->next;
340 : :
6789 341 : 860 : ecpg_free(this);
6796 342 : 860 : return true;
343 : : }
344 : :
345 : : /* handle the EXEC SQL DEALLOCATE PREPARE statement */
346 : : bool
347 : 54 : 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 : :
2419 tgl@sss.pgh.pa.us 353 : 54 : con = ecpg_get_connection(connection_name);
354 [ - + ]: 54 : if (!ecpg_init(con, connection_name, lineno))
6567 meskes@postgresql.or 355 :UBC 0 : return false;
356 : :
5954 meskes@postgresql.or 357 :CBC 54 : this = ecpg_find_prepared_statement(name, con, &prev);
6796 358 [ + + ]: 54 : if (this)
359 : 53 : return deallocate_one(lineno, c, con, prev, this);
360 : :
361 : : /* prepared statement is not found */
362 [ + - - + ]: 1 : if (INFORMIX_MODE(c))
6796 meskes@postgresql.or 363 :UBC 0 : return true;
6789 meskes@postgresql.or 364 :CBC 1 : ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, name);
6796 365 : 1 : return false;
366 : : }
367 : :
368 : : bool
3240 tgl@sss.pgh.pa.us 369 : 126 : ecpg_deallocate_all_conn(int lineno, enum COMPAT_MODE c, struct connection *con)
370 : : {
371 : : /* deallocate all prepared statements */
6796 meskes@postgresql.or 372 [ + + ]: 146 : while (con->prep_stmts)
373 : : {
6792 374 [ - + ]: 20 : if (!deallocate_one(lineno, c, con, NULL, con->prep_stmts))
8451 meskes@postgresql.or 375 :UBC 0 : return false;
376 : : }
377 : :
8451 meskes@postgresql.or 378 :CBC 126 : return true;
379 : : }
380 : :
381 : : bool
6792 382 : 3 : ECPGdeallocate_all(int lineno, int compat, const char *connection_name)
383 : : {
4 andrew@dunslane.net 384 : 3 : struct connection *con = ecpg_get_connection(connection_name);
385 : :
386 [ + + ]: 3 : if (!ecpg_init(con, connection_name, lineno))
387 : 2 : return false;
388 : :
389 : 1 : return ecpg_deallocate_all_conn(lineno, compat, con);
390 : : }
391 : :
392 : : char *
3240 tgl@sss.pgh.pa.us 393 : 855 : ecpg_prepared(const char *name, struct connection *con)
394 : : {
395 : : struct prepared_statement *this;
396 : :
5954 meskes@postgresql.or 397 : 855 : this = ecpg_find_prepared_statement(name, con, NULL);
6796 398 [ + - ]: 855 : return this ? this->stmt->command : NULL;
399 : : }
400 : :
401 : : /* return the prepared statement */
402 : : char *
403 : 22 : ECPGprepared_statement(const char *connection_name, const char *name, int lineno)
404 : : {
4 andrew@dunslane.net 405 : 22 : struct connection *con = ecpg_get_connection(connection_name);
406 : :
407 [ + + ]: 22 : if (!ecpg_init(con, connection_name, lineno))
408 : 1 : return NULL;
409 : :
410 : 21 : return ecpg_prepared(name, con);
411 : : }
412 : :
413 : : /*
414 : : * hash a SQL statement - returns entry # of first entry in the bucket
415 : : */
416 : : static int
6839 meskes@postgresql.or 417 : 10 : HashStmt(const char *ecpgQuery)
418 : : {
419 : : int stmtIx,
420 : : bucketNo,
421 : : hashLeng,
422 : : stmtLeng;
423 : : uint64 hashVal,
424 : : rotVal;
425 : :
6746 bruce@momjian.us 426 : 10 : stmtLeng = strlen(ecpgQuery);
2758 tgl@sss.pgh.pa.us 427 : 10 : hashLeng = 50; /* use 1st 50 characters of statement */
428 [ + - ]: 10 : if (hashLeng > stmtLeng) /* if the statement isn't that long */
429 : 10 : hashLeng = stmtLeng; /* use its actual length */
430 : :
6746 bruce@momjian.us 431 : 10 : hashVal = 0;
432 [ + + ]: 371 : for (stmtIx = 0; stmtIx < hashLeng; ++stmtIx)
433 : : {
2757 tgl@sss.pgh.pa.us 434 : 361 : hashVal = hashVal + (unsigned char) ecpgQuery[stmtIx];
435 : : /* rotate 32-bit hash value left 13 bits */
6746 bruce@momjian.us 436 : 361 : hashVal = hashVal << 13;
2757 tgl@sss.pgh.pa.us 437 : 361 : rotVal = (hashVal & UINT64CONST(0x1fff00000000)) >> 32;
438 : 361 : hashVal = (hashVal & UINT64CONST(0xffffffff)) | rotVal;
439 : : }
440 : :
6746 bruce@momjian.us 441 : 10 : bucketNo = hashVal % stmtCacheNBuckets;
442 : :
443 : : /* Add 1 so that array entry 0 is never used */
2757 tgl@sss.pgh.pa.us 444 : 10 : return bucketNo * stmtCacheEntPerBucket + 1;
445 : : }
446 : :
447 : : /*
448 : : * search the statement cache - search for entry with matching ECPG-format query
449 : : * Returns entry # in cache if found
450 : : * OR zero if not present (zero'th entry isn't used)
451 : : */
452 : : static int
6839 meskes@postgresql.or 453 : 8 : SearchStmtCache(const char *ecpgQuery)
454 : : {
455 : : int entNo,
456 : : entIx;
457 : :
458 : : /* quick failure if cache not set up */
2757 tgl@sss.pgh.pa.us 459 [ + + ]: 8 : if (stmtCacheEntries == NULL)
460 : 1 : return 0;
461 : :
462 : : /* hash the statement */
6746 bruce@momjian.us 463 : 7 : entNo = HashStmt(ecpgQuery);
464 : :
465 : : /* search the cache */
466 [ + + ]: 23 : for (entIx = 0; entIx < stmtCacheEntPerBucket; ++entIx)
467 : : {
2758 tgl@sss.pgh.pa.us 468 [ + + ]: 21 : if (stmtCacheEntries[entNo].stmtID[0]) /* check if entry is in use */
469 : : {
5243 peter_e@gmx.net 470 [ + - ]: 5 : if (strcmp(ecpgQuery, stmtCacheEntries[entNo].ecpgQuery) == 0)
2758 tgl@sss.pgh.pa.us 471 : 5 : break; /* found it */
472 : : }
473 : 16 : ++entNo; /* incr entry # */
474 : : }
475 : :
476 : : /* if entry wasn't found - set entry # to zero */
6746 bruce@momjian.us 477 [ + + ]: 7 : if (entIx >= stmtCacheEntPerBucket)
478 : 2 : entNo = 0;
479 : :
3183 peter_e@gmx.net 480 : 7 : return entNo;
481 : : }
482 : :
483 : : /*
484 : : * free an entry in the statement cache
485 : : * Returns entry # in cache used
486 : : * OR negative error code
487 : : */
488 : : static int
2758 tgl@sss.pgh.pa.us 489 : 3 : ecpg_freeStmtCacheEntry(int lineno, int compat,
490 : : int entNo) /* entry # to free */
491 : : {
492 : : stmtCacheEntry *entry;
493 : : struct connection *con;
494 : : struct prepared_statement *this,
495 : : *prev;
496 : :
497 : : /* fail if cache isn't set up */
2757 498 [ - + ]: 3 : if (stmtCacheEntries == NULL)
2757 tgl@sss.pgh.pa.us 499 :UBC 0 : return -1;
500 : :
6746 bruce@momjian.us 501 :CBC 3 : entry = &stmtCacheEntries[entNo];
2758 tgl@sss.pgh.pa.us 502 [ + - ]: 3 : if (!entry->stmtID[0]) /* return if the entry isn't in use */
3183 peter_e@gmx.net 503 : 3 : return 0;
504 : :
6746 bruce@momjian.us 505 :UBC 0 : con = ecpg_get_connection(entry->connection);
506 : :
507 : : /*
508 : : * If the connection is gone, the prepared_statement list it owned is
509 : : * already unreachable, so just skip that cleanup. We must still clear
510 : : * the cache slot below so it can be reused.
511 : : */
4 andrew@dunslane.net 512 [ # # ]: 0 : if (con)
513 : : {
514 : : /* free the 'prepared_statement' list entry */
515 : 0 : this = ecpg_find_prepared_statement(entry->stmtID, con, &prev);
516 [ # # # # ]: 0 : if (this && !deallocate_one(lineno, compat, con, prev, this))
517 : 0 : return -1;
518 : : }
519 : :
6746 bruce@momjian.us 520 : 0 : entry->stmtID[0] = '\0';
521 : :
522 : : /* free the memory used by the cache entry */
523 [ # # ]: 0 : if (entry->ecpgQuery)
524 : : {
525 : 0 : ecpg_free(entry->ecpgQuery);
154 peter@eisentraut.org 526 :UNC 0 : entry->ecpgQuery = NULL;
527 : : }
528 : :
3183 peter_e@gmx.net 529 :UBC 0 : return entNo;
530 : : }
531 : :
532 : : /*
533 : : * add an entry to the statement cache
534 : : * returns entry # in cache used OR negative error code
535 : : */
536 : : static int
2758 tgl@sss.pgh.pa.us 537 :CBC 3 : AddStmtToCache(int lineno, /* line # of statement */
538 : : const char *stmtID, /* statement ID */
539 : : const char *connection, /* connection */
540 : : int compat, /* compatibility level */
541 : : const char *ecpgQuery) /* query */
542 : : {
543 : : int ix,
544 : : initEntNo,
545 : : luEntNo,
546 : : entNo;
547 : : stmtCacheEntry *entry;
548 : :
549 : : /* allocate and zero cache array if we haven't already */
2757 550 [ + + ]: 3 : if (stmtCacheEntries == NULL)
551 : : {
552 : 1 : stmtCacheEntries = (stmtCacheEntry *)
553 : 1 : ecpg_alloc(sizeof(stmtCacheEntry) * stmtCacheArraySize, lineno);
554 [ - + ]: 1 : if (stmtCacheEntries == NULL)
2757 tgl@sss.pgh.pa.us 555 :UBC 0 : return -1;
556 : : }
557 : :
558 : : /* hash the statement */
6746 bruce@momjian.us 559 :CBC 3 : initEntNo = HashStmt(ecpgQuery);
560 : :
561 : : /* search for an unused entry */
562 : 3 : entNo = initEntNo; /* start with the initial entry # for the
563 : : * bucket */
2758 tgl@sss.pgh.pa.us 564 : 3 : luEntNo = initEntNo; /* use it as the initial 'least used' entry */
6746 bruce@momjian.us 565 [ + - ]: 3 : for (ix = 0; ix < stmtCacheEntPerBucket; ++ix)
566 : : {
567 : 3 : entry = &stmtCacheEntries[entNo];
2758 tgl@sss.pgh.pa.us 568 [ + - ]: 3 : if (!entry->stmtID[0]) /* unused entry - use it */
6746 bruce@momjian.us 569 : 3 : break;
6746 bruce@momjian.us 570 [ # # ]:UBC 0 : if (entry->execs < stmtCacheEntries[luEntNo].execs)
2758 tgl@sss.pgh.pa.us 571 : 0 : luEntNo = entNo; /* save new 'least used' entry */
572 : 0 : ++entNo; /* increment entry # */
573 : : }
574 : :
575 : : /*
576 : : * if no unused entries were found, re-use the 'least used' entry found in
577 : : * the bucket
578 : : */
2758 tgl@sss.pgh.pa.us 579 [ - + ]:CBC 3 : if (ix >= stmtCacheEntPerBucket)
2758 tgl@sss.pgh.pa.us 580 :UBC 0 : entNo = luEntNo;
581 : :
582 : : /* 'entNo' is the entry to use - make sure its free */
6662 meskes@postgresql.or 583 [ - + ]:CBC 3 : if (ecpg_freeStmtCacheEntry(lineno, compat, entNo) < 0)
3183 peter_e@gmx.net 584 :UBC 0 : return -1;
585 : :
586 : : /* add the query to the entry */
6746 bruce@momjian.us 587 :CBC 3 : entry = &stmtCacheEntries[entNo];
588 : 3 : entry->lineno = lineno;
286 michael@paquier.xyz 589 :GNC 3 : entry->ecpgQuery = ecpg_strdup(ecpgQuery, lineno, NULL);
590 [ - + ]: 3 : if (!entry->ecpgQuery)
286 michael@paquier.xyz 591 :UNC 0 : return -1;
5177 peter_e@gmx.net 592 :CBC 3 : entry->connection = connection;
6746 bruce@momjian.us 593 : 3 : entry->execs = 0;
594 : 3 : memcpy(entry->stmtID, stmtID, sizeof(entry->stmtID));
595 : :
3183 peter_e@gmx.net 596 : 3 : return entNo;
597 : : }
598 : :
599 : : /* handle cache and preparation of statements in auto-prepare mode */
600 : : bool
6131 magnus@hagander.net 601 : 8 : ecpg_auto_prepare(int lineno, const char *connection_name, const int compat, char **name, const char *query)
602 : : {
603 : : int entNo;
604 : :
605 : : /* search the statement cache for this statement */
6839 meskes@postgresql.or 606 : 8 : entNo = SearchStmtCache(query);
607 : :
608 : : /* if not found - add the statement to the cache */
6746 bruce@momjian.us 609 [ + + ]: 8 : if (entNo)
610 : : {
611 : : char *stmtID;
612 : : struct connection *con;
613 : : struct prepared_statement *prep;
614 : :
6563 peter_e@gmx.net 615 : 5 : ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
616 : :
5947 meskes@postgresql.or 617 : 5 : stmtID = stmtCacheEntries[entNo].stmtID;
286 michael@paquier.xyz 618 :GNC 5 : *name = ecpg_strdup(stmtID, lineno, NULL);
619 [ - + ]: 5 : if (*name == NULL)
286 michael@paquier.xyz 620 :UNC 0 : return false;
621 : :
5947 meskes@postgresql.or 622 :CBC 5 : con = ecpg_get_connection(connection_name);
623 : 5 : prep = ecpg_find_prepared_statement(stmtID, con, NULL);
624 : : /* This prepared name doesn't exist on this connection. */
5663 625 [ + + - + ]: 5 : if (!prep && !prepare_common(lineno, con, stmtID, query))
626 : : {
281 michael@paquier.xyz 627 :UNC 0 : ecpg_free(*name);
3183 peter_e@gmx.net 628 :UBC 0 : return false;
629 : : }
630 : :
631 : : }
632 : : else
633 : : {
634 : : char stmtID[STMTID_SIZE];
635 : :
6563 peter_e@gmx.net 636 :CBC 3 : ecpg_log("ecpg_auto_prepare on line %d: statement not in cache; inserting\n", lineno);
637 : :
638 : : /* generate a statement ID */
5947 meskes@postgresql.or 639 : 3 : sprintf(stmtID, "ecpg%d", nextStmtID++);
286 michael@paquier.xyz 640 :GNC 3 : *name = ecpg_strdup(stmtID, lineno, NULL);
641 [ - + ]: 3 : if (*name == NULL)
286 michael@paquier.xyz 642 :UNC 0 : return false;
643 : :
5947 meskes@postgresql.or 644 [ - + ]:CBC 3 : if (!ECPGprepare(lineno, connection_name, 0, stmtID, query))
645 : : {
281 michael@paquier.xyz 646 :UNC 0 : ecpg_free(*name);
3183 peter_e@gmx.net 647 :UBC 0 : return false;
648 : : }
649 : :
2757 tgl@sss.pgh.pa.us 650 :CBC 3 : entNo = AddStmtToCache(lineno, stmtID, connection_name, compat, query);
651 [ - + ]: 3 : if (entNo < 0)
652 : : {
281 michael@paquier.xyz 653 :UNC 0 : ecpg_free(*name);
3183 peter_e@gmx.net 654 :UBC 0 : return false;
655 : : }
656 : : }
657 : :
658 : : /* increase usage counter */
6839 meskes@postgresql.or 659 :CBC 8 : stmtCacheEntries[entNo].execs++;
660 : :
3183 peter_e@gmx.net 661 : 8 : return true;
662 : : }
|