1 /**********************************************************************
2 * pl_funcs.c - Misc functins for the PL/pgSQL
6 * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.10 2001/02/19 19:49:53 tgl Exp $
8 * This software is copyrighted by Jan Wieck - Hamburg.
10 * The author hereby grants permission to use, copy, modify,
11 * distribute, and license this software and its documentation
12 * for any purpose, provided that existing copyright notices are
13 * retained in all copies and that this notice is included
14 * verbatim in any distributions. No written agreement, license,
15 * or royalty fee is required for any of the authorized uses.
16 * Modifications to this software may be copyrighted by their
17 * author and need not follow the licensing terms described
18 * here, provided that the new terms are clearly indicated on
19 * the first page of each file where they apply.
21 * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
22 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
23 * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
24 * SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
25 * IF THE AUTHOR HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
28 * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
31 * PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON
32 * AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAVE NO
33 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
34 * ENHANCEMENTS, OR MODIFICATIONS.
36 **********************************************************************/
51 * Local variables for the namestack handling
54 static PLpgSQL_ns *ns_current = NULL;
55 static bool ns_localmode = false;
59 * plpgsql_dstring_init Dynamic string initialization
63 plpgsql_dstring_init(PLpgSQL_dstring * ds)
65 ds->value = palloc(ds->alloc = 512);
71 * plpgsql_dstring_free Dynamic string destruction
75 plpgsql_dstring_free(PLpgSQL_dstring * ds)
82 * plpgsql_dstring_append Dynamic string extending
86 plpgsql_dstring_append(PLpgSQL_dstring * ds, char *str)
88 int len = strlen(str);
90 if (ds->used + len + 1 > ds->alloc)
93 ds->value = repalloc(ds->value, ds->alloc);
96 strcpy(&(ds->value[ds->used]), str);
102 * plpgsql_dstring_get Dynamic string get value
106 plpgsql_dstring_get(PLpgSQL_dstring * ds)
113 * plpgsql_ns_init Initialize the namestack
117 plpgsql_ns_init(void)
120 ns_localmode = false;
125 * plpgsql_ns_setlocal Tell plpgsql_ns_lookup to or to
126 * not look into the current level
131 plpgsql_ns_setlocal(bool flag)
135 oldstate = ns_localmode;
142 * plpgsql_ns_push Enter a new namestack level
146 plpgsql_ns_push(char *label)
150 new = palloc(sizeof(PLpgSQL_ns));
151 memset(new, 0, sizeof(PLpgSQL_ns));
152 new->upper = ns_current;
155 plpgsql_ns_additem(PLPGSQL_NSTYPE_LABEL, 0, label);
160 * plpgsql_ns_pop Return to the previous level
170 ns_current = old->upper;
172 for (i = 0; i < old->items_used; i++)
173 pfree(old->items[i]);
180 * plpgsql_ns_additem Add an item to the current
185 plpgsql_ns_additem(int itemtype, int itemno, char *name)
187 PLpgSQL_ns *ns = ns_current;
192 name = plpgsql_tolower(name);
194 if (ns->items_used == ns->items_alloc)
196 if (ns->items_alloc == 0)
198 ns->items_alloc = 32;
199 ns->items = palloc(sizeof(PLpgSQL_nsitem *) * ns->items_alloc);
203 ns->items_alloc *= 2;
204 ns->items = repalloc(ns->items,
205 sizeof(PLpgSQL_nsitem *) * ns->items_alloc);
209 nse = palloc(sizeof(PLpgSQL_nsitem) + strlen(name));
210 nse->itemtype = itemtype;
211 nse->itemno = itemno;
212 strcpy(nse->name, name);
213 ns->items[ns->items_used++] = nse;
218 * plpgsql_ns_lookup Lookup for a word in the namestack
222 plpgsql_ns_lookup(char *name, char *label)
228 * If a label is specified, lookup only in that
233 for (ns = ns_current; ns != NULL; ns = ns->upper)
235 if (!strcmp(ns->items[0]->name, label))
237 for (i = 1; i < ns->items_used; i++)
239 if (!strcmp(ns->items[i]->name, name))
242 return NULL; /* name not found in specified label */
245 return NULL; /* label not found */
249 * No label given, lookup for visible labels ignoring localmode
252 for (ns = ns_current; ns != NULL; ns = ns->upper)
254 if (!strcmp(ns->items[0]->name, name))
259 * Finally lookup name in the namestack
262 for (ns = ns_current; ns != NULL; ns = ns->upper)
264 for (i = 1; i < ns->items_used; i++)
266 if (!strcmp(ns->items[i]->name, name))
271 return NULL; /* name not found in current namespace */
280 * plpgsql_ns_rename Rename a namespace entry
284 plpgsql_ns_rename(char *oldname, char *newname)
287 PLpgSQL_nsitem *newitem;
291 * Lookup in the current namespace only
295 * Lookup name in the namestack
298 for (ns = ns_current; ns != NULL; ns = ns->upper)
300 for (i = 1; i < ns->items_used; i++)
302 if (!strcmp(ns->items[i]->name, oldname))
304 newitem = palloc(sizeof(PLpgSQL_nsitem) + strlen(newname));
305 newitem->itemtype = ns->items[i]->itemtype;
306 newitem->itemno = ns->items[i]->itemno;
307 strcpy(newitem->name, newname);
313 ns->items[i] = newitem;
319 elog(ERROR, "there is no variable '%s' in the current block", oldname);
324 * plpgsql_tolower Translate a string to lower case
325 * but honor "" escaping.
329 plpgsql_tolower(char *s)
334 ret = palloc(strlen(s) + 1);
350 plpgsql_comperrinfo();
351 elog(ERROR, "unterminated \"");
357 if (isupper((unsigned char) *s))
358 *cp++ = tolower((unsigned char) *s++);
372 /**********************************************************************
373 * Debug functions for analyzing the compiled code
374 **********************************************************************/
375 static int dump_indent;
377 static void dump_ind();
378 static void dump_stmt(PLpgSQL_stmt * stmt);
379 static void dump_block(PLpgSQL_stmt_block * block);
380 static void dump_assign(PLpgSQL_stmt_assign * stmt);
381 static void dump_if(PLpgSQL_stmt_if * stmt);
382 static void dump_loop(PLpgSQL_stmt_loop * stmt);
383 static void dump_while(PLpgSQL_stmt_while * stmt);
384 static void dump_fori(PLpgSQL_stmt_fori * stmt);
385 static void dump_fors(PLpgSQL_stmt_fors * stmt);
386 static void dump_select(PLpgSQL_stmt_select * stmt);
387 static void dump_exit(PLpgSQL_stmt_exit * stmt);
388 static void dump_return(PLpgSQL_stmt_return * stmt);
389 static void dump_raise(PLpgSQL_stmt_raise * stmt);
390 static void dump_execsql(PLpgSQL_stmt_execsql * stmt);
391 static void dump_dynexecute(PLpgSQL_stmt_dynexecute * stmt);
392 static void dump_dynfors(PLpgSQL_stmt_dynfors * stmt);
393 static void dump_getdiag(PLpgSQL_stmt_getdiag * stmt);
394 static void dump_expr(PLpgSQL_expr * expr);
402 for (i = 0; i < dump_indent; i++)
407 dump_stmt(PLpgSQL_stmt * stmt)
409 printf("%3d:", stmt->lineno);
410 switch (stmt->cmd_type)
412 case PLPGSQL_STMT_BLOCK:
413 dump_block((PLpgSQL_stmt_block *) stmt);
415 case PLPGSQL_STMT_ASSIGN:
416 dump_assign((PLpgSQL_stmt_assign *) stmt);
418 case PLPGSQL_STMT_IF:
419 dump_if((PLpgSQL_stmt_if *) stmt);
421 case PLPGSQL_STMT_LOOP:
422 dump_loop((PLpgSQL_stmt_loop *) stmt);
424 case PLPGSQL_STMT_WHILE:
425 dump_while((PLpgSQL_stmt_while *) stmt);
427 case PLPGSQL_STMT_FORI:
428 dump_fori((PLpgSQL_stmt_fori *) stmt);
430 case PLPGSQL_STMT_FORS:
431 dump_fors((PLpgSQL_stmt_fors *) stmt);
433 case PLPGSQL_STMT_SELECT:
434 dump_select((PLpgSQL_stmt_select *) stmt);
436 case PLPGSQL_STMT_EXIT:
437 dump_exit((PLpgSQL_stmt_exit *) stmt);
439 case PLPGSQL_STMT_RETURN:
440 dump_return((PLpgSQL_stmt_return *) stmt);
442 case PLPGSQL_STMT_RAISE:
443 dump_raise((PLpgSQL_stmt_raise *) stmt);
445 case PLPGSQL_STMT_EXECSQL:
446 dump_execsql((PLpgSQL_stmt_execsql *) stmt);
448 case PLPGSQL_STMT_DYNEXECUTE:
449 dump_dynexecute((PLpgSQL_stmt_dynexecute *) stmt);
451 case PLPGSQL_STMT_DYNFORS:
452 dump_dynfors((PLpgSQL_stmt_dynfors *) stmt);
454 case PLPGSQL_STMT_GETDIAG:
455 dump_getdiag((PLpgSQL_stmt_getdiag *) stmt);
458 elog(ERROR, "plpgsql_dump: unknown cmd_type %d\n", stmt->cmd_type);
464 dump_block(PLpgSQL_stmt_block * block)
469 if (block->label == NULL)
475 printf("BLOCK <<%s>>\n", name);
478 for (i = 0; i < block->body->stmts_used; i++)
479 dump_stmt((PLpgSQL_stmt *) (block->body->stmts[i]));
483 printf(" END -- %s\n", name);
487 dump_assign(PLpgSQL_stmt_assign * stmt)
490 printf("ASSIGN var %d := ", stmt->varno);
491 dump_expr(stmt->expr);
496 dump_if(PLpgSQL_stmt_if * stmt)
502 dump_expr(stmt->cond);
506 for (i = 0; i < stmt->true_body->stmts_used; i++)
507 dump_stmt((PLpgSQL_stmt *) (stmt->true_body->stmts[i]));
514 for (i = 0; i < stmt->false_body->stmts_used; i++)
515 dump_stmt((PLpgSQL_stmt *) (stmt->false_body->stmts[i]));
523 dump_loop(PLpgSQL_stmt_loop * stmt)
531 for (i = 0; i < stmt->body->stmts_used; i++)
532 dump_stmt((PLpgSQL_stmt *) (stmt->body->stmts[i]));
536 printf(" ENDLOOP\n");
540 dump_while(PLpgSQL_stmt_while * stmt)
546 dump_expr(stmt->cond);
550 for (i = 0; i < stmt->body->stmts_used; i++)
551 dump_stmt((PLpgSQL_stmt *) (stmt->body->stmts[i]));
555 printf(" ENDWHILE\n");
559 dump_fori(PLpgSQL_stmt_fori * stmt)
564 printf("FORI %s %s\n", stmt->var->refname, (stmt->reverse) ? "REVERSE" : "NORMAL");
569 dump_expr(stmt->lower);
573 dump_expr(stmt->upper);
576 for (i = 0; i < stmt->body->stmts_used; i++)
577 dump_stmt((PLpgSQL_stmt *) (stmt->body->stmts[i]));
581 printf(" ENDFORI\n");
585 dump_fors(PLpgSQL_stmt_fors * stmt)
590 printf("FORS %s ", (stmt->rec != NULL) ? stmt->rec->refname : stmt->row->refname);
591 dump_expr(stmt->query);
595 for (i = 0; i < stmt->body->stmts_used; i++)
596 dump_stmt((PLpgSQL_stmt *) (stmt->body->stmts[i]));
600 printf(" ENDFORS\n");
604 dump_select(PLpgSQL_stmt_select * stmt)
608 dump_expr(stmt->query);
612 if (stmt->rec != NULL)
615 printf(" target = %d %s\n", stmt->rec->recno, stmt->rec->refname);
617 if (stmt->row != NULL)
620 printf(" target = %d %s\n", stmt->row->rowno, stmt->row->refname);
627 dump_exit(PLpgSQL_stmt_exit * stmt)
630 printf("EXIT lbl='%s'", stmt->label);
631 if (stmt->cond != NULL)
634 dump_expr(stmt->cond);
640 dump_return(PLpgSQL_stmt_return * stmt)
644 if (stmt->retrecno > 0)
645 printf("record %d", stmt->retrecno);
648 if (stmt->expr == NULL)
651 dump_expr(stmt->expr);
657 dump_raise(PLpgSQL_stmt_raise * stmt)
662 printf("RAISE '%s'", stmt->message);
663 for (i = 0; i < stmt->nparams; i++)
664 printf(" %d", stmt->params[i]);
669 dump_execsql(PLpgSQL_stmt_execsql * stmt)
673 dump_expr(stmt->sqlstmt);
678 dump_dynexecute(PLpgSQL_stmt_dynexecute * stmt)
682 dump_expr(stmt->query);
687 dump_dynfors(PLpgSQL_stmt_dynfors * stmt)
692 printf("FORS %s EXECUTE ", (stmt->rec != NULL) ? stmt->rec->refname : stmt->row->refname);
693 dump_expr(stmt->query);
697 for (i = 0; i < stmt->body->stmts_used; i++)
698 dump_stmt((PLpgSQL_stmt *) (stmt->body->stmts[i]));
702 printf(" ENDFORS\n");
706 dump_getdiag(PLpgSQL_stmt_getdiag * stmt)
711 printf("GET DIAGNOSTICS ");
712 for (i = 0; i < stmt->ndtitems; i++)
714 PLpgSQL_diag_item *dtitem = & stmt->dtitems[i];
719 printf("{var %d} = ", dtitem->target);
721 switch (dtitem->item)
723 case PLPGSQL_GETDIAG_ROW_COUNT:
727 case PLPGSQL_GETDIAG_RESULT_OID:
728 printf("RESULT_OID");
740 dump_expr(PLpgSQL_expr * expr)
744 printf("'%s", expr->query);
745 if (expr->nparams > 0)
748 for (i = 0; i < expr->nparams; i++)
752 printf("$%d=%d", i + 1, expr->params[i]);
760 plpgsql_dumptree(PLpgSQL_function * func)
765 printf("\nExecution tree of successfully compiled PL/pgSQL function %s:\n",
768 printf("\nFunctions data area:\n");
769 for (i = 0; i < func->ndatums; i++)
773 printf(" entry %d: ", i);
776 case PLPGSQL_DTYPE_VAR:
778 PLpgSQL_var *var = (PLpgSQL_var *) d;
780 printf("VAR %-16s type %s (typoid %u) atttypmod %d\n",
781 var->refname, var->datatype->typname,
782 var->datatype->typoid,
783 var->datatype->atttypmod);
786 case PLPGSQL_DTYPE_ROW:
788 PLpgSQL_row *row = (PLpgSQL_row *) d;
791 printf("ROW %-16s fields", row->refname);
792 for (i = 0; i < row->nfields; i++)
794 printf(" %s=var %d", row->fieldnames[i],
800 case PLPGSQL_DTYPE_REC:
801 printf("REC %s\n", ((PLpgSQL_rec *) d)->refname);
803 case PLPGSQL_DTYPE_RECFIELD:
804 printf("RECFIELD %-16s of REC %d\n", ((PLpgSQL_recfield *) d)->fieldname, ((PLpgSQL_recfield *) d)->recno);
806 case PLPGSQL_DTYPE_TRIGARG:
808 dump_expr(((PLpgSQL_trigarg *) d)->argnum);
812 printf("??? unknown data type %d\n", d->dtype);
815 printf("\nFunctions statements:\n");
818 printf("%3d:", func->action->lineno);
819 dump_block(func->action);
820 printf("\nEnd of execution tree of function %s\n\n", func->fn_name);