1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-21 16:02:15 +03:00

Allow plpgsql function parameter names to be qualified with the function's

name.  With this patch, it is always possible for the user to qualify a
plpgsql variable name if needed to avoid ambiguity.  While there is much more
work to be done in this area, this simple change removes one unnecessary
incompatibility with Oracle.  Per discussion.
This commit is contained in:
Tom Lane
2007-07-16 17:01:11 +00:00
parent 9f6f51d5d4
commit ae1b7e298c
7 changed files with 104 additions and 28 deletions

View File

@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.103 2007/07/15 02:15:04 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.104 2007/07/16 17:01:10 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -366,7 +366,7 @@ decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
plpgsql_ns_rename($2, $4);
}
| decl_varname opt_scrollable K_CURSOR
{ plpgsql_ns_push(NULL); }
{ plpgsql_ns_push($1.name); }
decl_cursor_args decl_is_for decl_cursor_query
{
PLpgSQL_var *new;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.116 2007/06/26 16:48:09 alvherre Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.117 2007/07/16 17:01:10 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -306,10 +306,12 @@ do_compile(FunctionCallInfo fcinfo,
error_context_stack = &plerrcontext;
/*
* Initialize the compiler
* Initialize the compiler, particularly the namespace stack. The
* outermost namespace contains function parameters and other special
* variables (such as FOUND), and is named after the function itself.
*/
plpgsql_ns_init();
plpgsql_ns_push(NULL);
plpgsql_ns_push(NameStr(procStruct->proname));
plpgsql_DumpExecTree = false;
datums_alloc = 128;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.60 2007/07/15 02:15:04 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.61 2007/07/16 17:01:11 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -147,15 +147,14 @@ plpgsql_ns_setlocal(bool flag)
* ----------
*/
void
plpgsql_ns_push(char *label)
plpgsql_ns_push(const char *label)
{
PLpgSQL_ns *new;
if (label == NULL)
label = "";
new = palloc(sizeof(PLpgSQL_ns));
memset(new, 0, sizeof(PLpgSQL_ns));
new = palloc0(sizeof(PLpgSQL_ns));
new->upper = ns_current;
ns_current = new;
@ -224,7 +223,7 @@ plpgsql_ns_additem(int itemtype, int itemno, const char *name)
* ----------
*/
PLpgSQL_nsitem *
plpgsql_ns_lookup(char *name, char *label)
plpgsql_ns_lookup(const char *name, const char *label)
{
PLpgSQL_ns *ns;
int i;
@ -236,11 +235,11 @@ plpgsql_ns_lookup(char *name, char *label)
{
for (ns = ns_current; ns != NULL; ns = ns->upper)
{
if (!strcmp(ns->items[0]->name, label))
if (strcmp(ns->items[0]->name, label) == 0)
{
for (i = 1; i < ns->items_used; i++)
{
if (!strcmp(ns->items[i]->name, name))
if (strcmp(ns->items[i]->name, name) == 0)
return ns->items[i];
}
return NULL; /* name not found in specified label */
@ -254,7 +253,7 @@ plpgsql_ns_lookup(char *name, char *label)
*/
for (ns = ns_current; ns != NULL; ns = ns->upper)
{
if (!strcmp(ns->items[0]->name, name))
if (strcmp(ns->items[0]->name, name) == 0)
return ns->items[0];
}
@ -265,7 +264,7 @@ plpgsql_ns_lookup(char *name, char *label)
{
for (i = 1; i < ns->items_used; i++)
{
if (!strcmp(ns->items[i]->name, name))
if (strcmp(ns->items[i]->name, name) == 0)
return ns->items[i];
}
if (ns_localmode)
@ -288,14 +287,13 @@ plpgsql_ns_rename(char *oldname, char *newname)
int i;
/*
* Lookup name in the namestack; do the lookup in the current namespace
* only.
* Lookup name in the namestack
*/
for (ns = ns_current; ns != NULL; ns = ns->upper)
{
for (i = 1; i < ns->items_used; i++)
{
if (!strcmp(ns->items[i]->name, oldname))
if (strcmp(ns->items[i]->name, oldname) == 0)
{
newitem = palloc(sizeof(PLpgSQL_nsitem) + strlen(newname));
newitem->itemtype = ns->items[i]->itemtype;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.89 2007/07/15 02:15:04 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.90 2007/07/16 17:01:11 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -770,10 +770,10 @@ extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
*/
extern void plpgsql_ns_init(void);
extern bool plpgsql_ns_setlocal(bool flag);
extern void plpgsql_ns_push(char *label);
extern void plpgsql_ns_push(const char *label);
extern void plpgsql_ns_pop(void);
extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name, const char *nsname);
extern void plpgsql_ns_rename(char *oldname, char *newname);
/* ----------

View File

@ -3051,3 +3051,31 @@ select * from sc_test();
(3 rows)
drop function sc_test();
-- test qualified variable names
create function pl_qual_names (param1 int) returns void as $$
<<outerblock>>
declare
param1 int := 1;
begin
<<innerblock>>
declare
param1 int := 2;
begin
raise notice 'param1 = %', param1;
raise notice 'pl_qual_names.param1 = %', pl_qual_names.param1;
raise notice 'outerblock.param1 = %', outerblock.param1;
raise notice 'innerblock.param1 = %', innerblock.param1;
end;
end;
$$ language plpgsql;
select pl_qual_names(42);
NOTICE: param1 = 2
NOTICE: pl_qual_names.param1 = 42
NOTICE: outerblock.param1 = 1
NOTICE: innerblock.param1 = 2
pl_qual_names
---------------
(1 row)
drop function pl_qual_names(int);

View File

@ -2535,3 +2535,25 @@ select * from sc_test();
drop function sc_test();
-- test qualified variable names
create function pl_qual_names (param1 int) returns void as $$
<<outerblock>>
declare
param1 int := 1;
begin
<<innerblock>>
declare
param1 int := 2;
begin
raise notice 'param1 = %', param1;
raise notice 'pl_qual_names.param1 = %', pl_qual_names.param1;
raise notice 'outerblock.param1 = %', outerblock.param1;
raise notice 'innerblock.param1 = %', innerblock.param1;
end;
end;
$$ language plpgsql;
select pl_qual_names(42);
drop function pl_qual_names(int);