Debian Codegraph @

src/interfaces/ecpg/ecpglib/prepare.c

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