|
3 | 3 | *back to source text
|
4 | 4 | *
|
5 | 5 | * 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 $ |
7 | 7 | *
|
8 | 8 | * This software is copyrighted by Jan Wieck - Hamburg.
|
9 | 9 | *
|
@@ -109,7 +109,8 @@ static void get_from_clause_item(Node *jtnode, Query *query,
|
109 | 109 | staticbooltleIsArrayAssign(TargetEntry*tle);
|
110 | 110 | staticchar*quote_identifier(char*ident);
|
111 | 111 | 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); |
113 | 114 |
|
114 | 115 | #defineonly_marker(rte) ((rte)->inh ? "" : "ONLY ")
|
115 | 116 |
|
@@ -445,7 +446,7 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
|
445 | 446 | * ----------
|
446 | 447 | */
|
447 | 448 | appendStringInfo(&keybuf,"%s",
|
448 |
| -quote_identifier(get_attribute_name(idxrec->indrelid, |
| 449 | +quote_identifier(get_relid_attribute_name(idxrec->indrelid, |
449 | 450 | idxrec->indkey[keyno])));
|
450 | 451 |
|
451 | 452 | /* ----------
|
@@ -696,8 +697,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc)
|
696 | 697 | quote_identifier(get_relation_name(ev_class)));
|
697 | 698 | if (ev_attr>0)
|
698 | 699 | 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))); |
701 | 702 |
|
702 | 703 | /* If the rule has an event qualification, add it */
|
703 | 704 | if (ev_qual==NULL)
|
@@ -908,7 +909,7 @@ get_select_query_def(Query *query, deparse_context *context)
|
908 | 909 | char*attname;
|
909 | 910 |
|
910 | 911 | 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); |
912 | 913 | tell_as= (strcmp(attname,tle->resdom->resname)!=0);
|
913 | 914 | }
|
914 | 915 |
|
@@ -1164,7 +1165,7 @@ get_rule_expr(Node *node, deparse_context *context)
|
1164 | 1165 | quote_identifier(rte->eref->relname));
|
1165 | 1166 | }
|
1166 | 1167 | appendStringInfo(buf,"%s",
|
1167 |
| -quote_identifier(get_attribute_name(rte->relid, |
| 1168 | +quote_identifier(get_rte_attribute_name(rte, |
1168 | 1169 | var->varattno)));
|
1169 | 1170 | }
|
1170 | 1171 | break;
|
@@ -1340,7 +1341,8 @@ get_rule_expr(Node *node, deparse_context *context)
|
1340 | 1341 | if (!OidIsValid(typrelid))
|
1341 | 1342 | elog(ERROR,"Argument type %s of FieldSelect is not a tuple type",
|
1342 | 1343 | NameStr(typeStruct->typname));
|
1343 |
| -fieldname=get_attribute_name(typrelid,fselect->fieldnum); |
| 1344 | +fieldname=get_relid_attribute_name(typrelid, |
| 1345 | +fselect->fieldnum); |
1344 | 1346 | appendStringInfo(buf,".%s",quote_identifier(fieldname));
|
1345 | 1347 | }
|
1346 | 1348 | break;
|
@@ -2000,23 +2002,51 @@ get_relation_name(Oid relid)
|
2000 | 2002 |
|
2001 | 2003 |
|
2002 | 2004 | /* ----------
|
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. |
2005 | 2010 | * ----------
|
2006 | 2011 | */
|
2007 | 2012 | staticchar*
|
2008 |
| -get_attribute_name(Oidrelid,int2attnum) |
| 2013 | +get_relid_attribute_name(Oidrelid,AttrNumberattnum) |
2009 | 2014 | {
|
2010 |
| -HeapTupleatttup; |
2011 |
| -Form_pg_attributeattStruct; |
| 2015 | +char*attname; |
2012 | 2016 |
|
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) |
2017 | 2019 | elog(ERROR,"cache lookup of attribute %d in relation %u failed",
|
2018 | 2020 | attnum,relid);
|
| 2021 | +returnattname; |
| 2022 | +} |
2019 | 2023 |
|
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); |
2022 | 2052 | }
|