Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5a290c

Browse files
committed
Make rule lister use aliases from FROM clause when a table column has
been given an alias. Otherwise, results are incorrect.
1 parent2acdef1 commita5a290c

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

‎src/backend/utils/adt/ruleutils.c

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.61 2000/09/12 21:07:05 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.62 2000/09/18 20:14:23 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -109,7 +109,8 @@ static void get_from_clause_item(Node *jtnode, Query *query,
109109
staticbooltleIsArrayAssign(TargetEntry*tle);
110110
staticchar*quote_identifier(char*ident);
111111
staticchar*get_relation_name(Oidrelid);
112-
staticchar*get_attribute_name(Oidrelid,int2attnum);
112+
staticchar*get_relid_attribute_name(Oidrelid,AttrNumberattnum);
113+
staticchar*get_rte_attribute_name(RangeTblEntry*rte,AttrNumberattnum);
113114

114115
#defineonly_marker(rte) ((rte)->inh ? "" : "ONLY ")
115116

@@ -445,7 +446,7 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
445446
* ----------
446447
*/
447448
appendStringInfo(&keybuf,"%s",
448-
quote_identifier(get_attribute_name(idxrec->indrelid,
449+
quote_identifier(get_relid_attribute_name(idxrec->indrelid,
449450
idxrec->indkey[keyno])));
450451

451452
/* ----------
@@ -696,8 +697,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc)
696697
quote_identifier(get_relation_name(ev_class)));
697698
if (ev_attr>0)
698699
appendStringInfo(buf,".%s",
699-
quote_identifier(get_attribute_name(ev_class,
700-
ev_attr)));
700+
quote_identifier(get_relid_attribute_name(ev_class,
701+
ev_attr)));
701702

702703
/* If the rule has an event qualification, add it */
703704
if (ev_qual==NULL)
@@ -908,7 +909,7 @@ get_select_query_def(Query *query, deparse_context *context)
908909
char*attname;
909910

910911
rte=get_rte_for_var(var,context);
911-
attname=get_attribute_name(rte->relid,var->varattno);
912+
attname=get_rte_attribute_name(rte,var->varattno);
912913
tell_as= (strcmp(attname,tle->resdom->resname)!=0);
913914
}
914915

@@ -1164,7 +1165,7 @@ get_rule_expr(Node *node, deparse_context *context)
11641165
quote_identifier(rte->eref->relname));
11651166
}
11661167
appendStringInfo(buf,"%s",
1167-
quote_identifier(get_attribute_name(rte->relid,
1168+
quote_identifier(get_rte_attribute_name(rte,
11681169
var->varattno)));
11691170
}
11701171
break;
@@ -1340,7 +1341,8 @@ get_rule_expr(Node *node, deparse_context *context)
13401341
if (!OidIsValid(typrelid))
13411342
elog(ERROR,"Argument type %s of FieldSelect is not a tuple type",
13421343
NameStr(typeStruct->typname));
1343-
fieldname=get_attribute_name(typrelid,fselect->fieldnum);
1344+
fieldname=get_relid_attribute_name(typrelid,
1345+
fselect->fieldnum);
13441346
appendStringInfo(buf,".%s",quote_identifier(fieldname));
13451347
}
13461348
break;
@@ -2000,23 +2002,51 @@ get_relation_name(Oid relid)
20002002

20012003

20022004
/* ----------
2003-
* get_attribute_name- Get an attribute name by its
2004-
* relations Oid and its attnum
2005+
* get_relid_attribute_name
2006+
*Get an attribute name by its relations Oid and its attnum
2007+
*
2008+
* Same as underlying syscache routine get_attname(), except that error
2009+
* is handled by elog() instead of returning NULL.
20052010
* ----------
20062011
*/
20072012
staticchar*
2008-
get_attribute_name(Oidrelid,int2attnum)
2013+
get_relid_attribute_name(Oidrelid,AttrNumberattnum)
20092014
{
2010-
HeapTupleatttup;
2011-
Form_pg_attributeattStruct;
2015+
char*attname;
20122016

2013-
atttup=SearchSysCacheTuple(ATTNUM,
2014-
ObjectIdGetDatum(relid), (Datum)attnum,
2015-
0,0);
2016-
if (!HeapTupleIsValid(atttup))
2017+
attname=get_attname(relid,attnum);
2018+
if (attname==NULL)
20172019
elog(ERROR,"cache lookup of attribute %d in relation %u failed",
20182020
attnum,relid);
2021+
returnattname;
2022+
}
20192023

2020-
attStruct= (Form_pg_attribute)GETSTRUCT(atttup);
2021-
returnpstrdup(NameStr(attStruct->attname));
2024+
/* ----------
2025+
* get_rte_attribute_name
2026+
*Get an attribute name from a RangeTblEntry
2027+
*
2028+
* This is unlike get_relid_attribute_name() because we use aliases if
2029+
* available.
2030+
* ----------
2031+
*/
2032+
staticchar*
2033+
get_rte_attribute_name(RangeTblEntry*rte,AttrNumberattnum)
2034+
{
2035+
/*
2036+
* If there is an alias, use it
2037+
*/
2038+
if (attnum>0&&attnum <=length(rte->eref->attrs))
2039+
returnstrVal(nth(attnum-1,rte->eref->attrs));
2040+
/*
2041+
* Can get here for a system attribute (which never has an alias),
2042+
* or if alias name list is too short (which probably can't happen
2043+
* anymore). Neither of these cases is valid for a subselect RTE.
2044+
*/
2045+
if (rte->relid==InvalidOid)
2046+
elog(ERROR,"Invalid attnum %d for rangetable entry %s",
2047+
attnum,rte->eref->relname);
2048+
/*
2049+
* Use the real name of the table's column
2050+
*/
2051+
returnget_relid_attribute_name(rte->relid,attnum);
20222052
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp