Hi all,

Please find the attachment.

By my friend asking, for convenience,
support to define multi variables in single PL/pgSQL line.

Like this:

CREATE OR REPLACE FUNCTION try_mutlivardef() RETURNS text AS $$
DECLARE
local_a, local_b, local_c text := 'a1----';
BEGIN
return local_a || local_b || local_c;
end;
$$ LANGUAGE plpgsql;


Regards,
Quan Zongliang


---
此电子邮件没有病毒和恶意软件,因为 avast! 防病毒保护处于活动状态。
http://www.avast.com
diff --git a/src/pl/plpgsql/src/pl_gram.y b/src/pl/plpgsql/src/pl_gram.y
index e3a992c..2737a3a 100644
--- a/src/pl/plpgsql/src/pl_gram.y
+++ b/src/pl/plpgsql/src/pl_gram.y
@@ -167,6 +167,7 @@ static	List			*read_raise_options(void);
 
 %type <declhdr> decl_sect
 %type <varname> decl_varname
+%type <list>	decl_varnames
 %type <boolean>	decl_const decl_notnull exit_type
 %type <expr>	decl_defval decl_cursor_query
 %type <dtype>	decl_datatype
@@ -471,9 +472,10 @@ decl_stmt		: decl_statement
 					}
 				;
 
-decl_statement	: decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
+decl_statement	: decl_varnames decl_const decl_datatype decl_collate decl_notnull decl_defval
 					{
 						PLpgSQL_variable	*var;
+						ListCell *lc;
 
 						/*
 						 * If a collation is supplied, insert it into the
@@ -492,38 +494,44 @@ decl_statement	: decl_varname decl_const decl_datatype decl_collate decl_notnull
 							$3->collation = $4;
 						}
 
-						var = plpgsql_build_variable($1.name, $1.lineno,
-													 $3, true);
-						if ($2)
-						{
-							if (var->dtype == PLPGSQL_DTYPE_VAR)
-								((PLpgSQL_var *) var)->isconst = $2;
-							else
-								ereport(ERROR,
-										(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-										 errmsg("row or record variable cannot be CONSTANT"),
-										 parser_errposition(@2)));
-						}
-						if ($5)
-						{
-							if (var->dtype == PLPGSQL_DTYPE_VAR)
-								((PLpgSQL_var *) var)->notnull = $5;
-							else
-								ereport(ERROR,
-										(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-										 errmsg("row or record variable cannot be NOT NULL"),
-										 parser_errposition(@4)));
 
-						}
-						if ($6 != NULL)
+						foreach(lc, $1)
 						{
-							if (var->dtype == PLPGSQL_DTYPE_VAR)
-								((PLpgSQL_var *) var)->default_val = $6;
-							else
-								ereport(ERROR,
-										(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-										 errmsg("default value for row or record variable is not supported"),
-										 parser_errposition(@5)));
+							YYSTYPE *v = (YYSTYPE *) lfirst(lc);
+
+							var = plpgsql_build_variable(v->varname.name, v->varname.lineno,
+													 	$3, true);
+							if ($2)
+							{
+								if (var->dtype == PLPGSQL_DTYPE_VAR)
+									((PLpgSQL_var *) var)->isconst = $2;
+								else
+									ereport(ERROR,
+											(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+											 errmsg("row or record variable cannot be CONSTANT"),
+											 parser_errposition(@2)));
+							}
+							if ($5)
+							{
+								if (var->dtype == PLPGSQL_DTYPE_VAR)
+									((PLpgSQL_var *) var)->notnull = $5;
+								else
+									ereport(ERROR,
+											(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+											 errmsg("row or record variable cannot be NOT NULL"),
+											 parser_errposition(@4)));
+
+							}
+							if ($6 != NULL)
+							{
+								if (var->dtype == PLPGSQL_DTYPE_VAR)
+									((PLpgSQL_var *) var)->default_val = $6;
+								else
+									ereport(ERROR,
+											(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+											 errmsg("default value for row or record variable is not supported"),
+											 parser_errposition(@5)));
+							}
 						}
 					}
 				| decl_varname K_ALIAS K_FOR decl_aliasitem ';'
@@ -773,6 +781,22 @@ decl_varname	: T_WORD
 					}
 				;
 
+decl_varnames : decl_varname
+					{
+						YYSTYPE *v = palloc(sizeof(YYSTYPE));
+						v->varname.name = pstrdup($1.name);
+						v->varname.lineno = $1.lineno;
+						$$ = list_make1(v);
+					}
+				| decl_varnames ',' decl_varname
+					{
+						YYSTYPE *v = palloc(sizeof(YYSTYPE));
+						v->varname.name = pstrdup($3.name);
+						v->varname.lineno = $3.lineno;
+						$$ = lappend($1, v);
+					}
+				;
+
 decl_const		:
 					{ $$ = false; }
 				| K_CONSTANT
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to