Change get_rule_expr so that when the input is a List, it displays the

list elements comma-separated instead of barfing.  This allows elimination
of half a dozen redundant copies of that behavior, and also makes the
world safe again for pg_get_expr() applied to pg_index.indexprs, per gripe
from Alexander Zhiltsov.
This commit is contained in:
Tom Lane 2004-10-07 20:36:52 +00:00
parent 05c561eec8
commit 4b10271037

View File

@ -3,7 +3,7 @@
* back to source text * back to source text
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.181 2004/09/13 20:07:13 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.182 2004/10/07 20:36:52 tgl Exp $
* *
* This software is copyrighted by Jan Wieck - Hamburg. * This software is copyrighted by Jan Wieck - Hamburg.
* *
@ -2351,15 +2351,7 @@ get_insert_query_def(Query *query, deparse_context *context)
{ {
appendContextKeyword(context, "VALUES (", appendContextKeyword(context, "VALUES (",
-PRETTYINDENT_STD, PRETTYINDENT_STD, 2); -PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
sep = ""; get_rule_expr((Node *) strippedexprs, context, false);
foreach(l, strippedexprs)
{
Node *expr = lfirst(l);
appendStringInfo(buf, sep);
sep = ", ";
get_rule_expr(expr, context, false);
}
appendStringInfoChar(buf, ')'); appendStringInfoChar(buf, ')');
} }
else else
@ -2938,7 +2930,9 @@ get_rule_expr(Node *node, deparse_context *context,
/* /*
* Each level of get_rule_expr must emit an indivisible term * Each level of get_rule_expr must emit an indivisible term
* (parenthesized if necessary) to ensure result is reparsed into the * (parenthesized if necessary) to ensure result is reparsed into the
* same expression tree. * same expression tree. The only exception is that when the input
* is a List, we emit the component items comma-separated with no
* surrounding decoration; this is convenient for most callers.
* *
* There might be some work left here to support additional node types. * There might be some work left here to support additional node types.
*/ */
@ -3269,20 +3263,10 @@ get_rule_expr(Node *node, deparse_context *context,
case T_ArrayExpr: case T_ArrayExpr:
{ {
ArrayExpr *arrayexpr = (ArrayExpr *) node; ArrayExpr *arrayexpr = (ArrayExpr *) node;
ListCell *element;
char *sep;
appendStringInfo(buf, "ARRAY["); appendStringInfo(buf, "ARRAY[");
sep = ""; get_rule_expr((Node *) arrayexpr->elements, context, true);
foreach(element, arrayexpr->elements) appendStringInfoChar(buf, ']');
{
Node *e = (Node *) lfirst(element);
appendStringInfo(buf, sep);
get_rule_expr(e, context, true);
sep = ", ";
}
appendStringInfo(buf, "]");
} }
break; break;
@ -3348,40 +3332,20 @@ get_rule_expr(Node *node, deparse_context *context,
case T_CoalesceExpr: case T_CoalesceExpr:
{ {
CoalesceExpr *coalesceexpr = (CoalesceExpr *) node; CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
ListCell *arg;
char *sep;
appendStringInfo(buf, "COALESCE("); appendStringInfo(buf, "COALESCE(");
sep = ""; get_rule_expr((Node *) coalesceexpr->args, context, true);
foreach(arg, coalesceexpr->args) appendStringInfoChar(buf, ')');
{
Node *e = (Node *) lfirst(arg);
appendStringInfo(buf, sep);
get_rule_expr(e, context, true);
sep = ", ";
}
appendStringInfo(buf, ")");
} }
break; break;
case T_NullIfExpr: case T_NullIfExpr:
{ {
NullIfExpr *nullifexpr = (NullIfExpr *) node; NullIfExpr *nullifexpr = (NullIfExpr *) node;
ListCell *arg;
char *sep;
appendStringInfo(buf, "NULLIF("); appendStringInfo(buf, "NULLIF(");
sep = ""; get_rule_expr((Node *) nullifexpr->args, context, true);
foreach(arg, nullifexpr->args) appendStringInfoChar(buf, ')');
{
Node *e = (Node *) lfirst(arg);
appendStringInfo(buf, sep);
get_rule_expr(e, context, true);
sep = ", ";
}
appendStringInfo(buf, ")");
} }
break; break;
@ -3478,6 +3442,21 @@ get_rule_expr(Node *node, deparse_context *context,
appendStringInfo(buf, "DEFAULT"); appendStringInfo(buf, "DEFAULT");
break; break;
case T_List:
{
char *sep;
ListCell *l;
sep = "";
foreach(l, (List *) node)
{
appendStringInfo(buf, sep);
get_rule_expr((Node *) lfirst(l), context, showimplicit);
sep = ", ";
}
}
break;
default: default:
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
break; break;
@ -3560,7 +3539,6 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
Oid argtypes[FUNC_MAX_ARGS]; Oid argtypes[FUNC_MAX_ARGS];
int nargs; int nargs;
ListCell *l; ListCell *l;
char *sep;
/* /*
* If the function call came from an implicit coercion, then just show * If the function call came from an implicit coercion, then just show
@ -3613,14 +3591,7 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
appendStringInfo(buf, "%s(", appendStringInfo(buf, "%s(",
generate_function_name(funcoid, nargs, argtypes)); generate_function_name(funcoid, nargs, argtypes));
get_rule_expr((Node *) expr->args, context, true);
sep = "";
foreach(l, expr->args)
{
appendStringInfo(buf, sep);
sep = ", ";
get_rule_expr((Node *) lfirst(l), context, true);
}
appendStringInfoChar(buf, ')'); appendStringInfoChar(buf, ')');
} }
@ -3787,8 +3758,6 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
{ {
StringInfo buf = context->buf; StringInfo buf = context->buf;
Query *query = (Query *) (sublink->subselect); Query *query = (Query *) (sublink->subselect);
ListCell *l;
char *sep;
bool need_paren; bool need_paren;
if (sublink->subLinkType == ARRAY_SUBLINK) if (sublink->subLinkType == ARRAY_SUBLINK)
@ -3801,19 +3770,10 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
need_paren = (list_length(sublink->lefthand) > 1); need_paren = (list_length(sublink->lefthand) > 1);
if (need_paren) if (need_paren)
appendStringInfoChar(buf, '('); appendStringInfoChar(buf, '(');
get_rule_expr((Node *) sublink->lefthand, context, true);
sep = "";
foreach(l, sublink->lefthand)
{
appendStringInfo(buf, sep);
sep = ", ";
get_rule_expr((Node *) lfirst(l), context, true);
}
if (need_paren) if (need_paren)
appendStringInfo(buf, ") "); appendStringInfoChar(buf, ')');
else appendStringInfoChar(buf, ' ');
appendStringInfoChar(buf, ' ');
} }
need_paren = true; need_paren = true;