3
3
* back to source text
4
4
*
5
5
* IDENTIFICATION
6
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.212 2005/12/30 18:34:22 tgl Exp $
6
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.213 2006/01/26 17:08:19 tgl Exp $
7
7
*
8
8
* This software is copyrighted by Jan Wieck - Hamburg.
9
9
*
@@ -2018,6 +2018,7 @@ get_basic_select_query(Query *query, deparse_context *context,
2018
2018
{
2019
2019
TargetEntry * tle = (TargetEntry * ) lfirst (l );
2020
2020
char * colname ;
2021
+ char * attname ;
2021
2022
2022
2023
if (tle -> resjunk )
2023
2024
continue ; /* ignore junk entries */
@@ -2026,7 +2027,55 @@ get_basic_select_query(Query *query, deparse_context *context,
2026
2027
sep = ", " ;
2027
2028
colno ++ ;
2028
2029
2029
- get_rule_expr ((Node * ) tle -> expr , context , true);
2030
+ /*
2031
+ * We special-case Var nodes rather than using get_rule_expr.
2032
+ * This is needed because get_rule_expr will display a whole-row Var
2033
+ * as "foo.*", which is the preferred notation in most contexts, but
2034
+ * at the top level of a SELECT list it's not right (the parser will
2035
+ * expand that notation into multiple columns, yielding behavior
2036
+ * different from a whole-row Var). We want just "foo", instead.
2037
+ */
2038
+ if (tle -> expr && IsA (tle -> expr , Var ))
2039
+ {
2040
+ Var * var = (Var * ) (tle -> expr );
2041
+ char * schemaname ;
2042
+ char * refname ;
2043
+
2044
+ get_names_for_var (var , 0 , context ,
2045
+ & schemaname , & refname , & attname );
2046
+ if (refname && (context -> varprefix || attname == NULL ))
2047
+ {
2048
+ if (schemaname )
2049
+ appendStringInfo (buf , "%s." ,
2050
+ quote_identifier (schemaname ));
2051
+
2052
+ if (strcmp (refname , "*NEW*" ) == 0 )
2053
+ appendStringInfoString (buf , "new" );
2054
+ else if (strcmp (refname , "*OLD*" ) == 0 )
2055
+ appendStringInfoString (buf , "old" );
2056
+ else
2057
+ appendStringInfoString (buf , quote_identifier (refname ));
2058
+
2059
+ if (attname )
2060
+ appendStringInfoChar (buf , '.' );
2061
+ }
2062
+ if (attname )
2063
+ appendStringInfoString (buf , quote_identifier (attname ));
2064
+ else
2065
+ {
2066
+ /*
2067
+ * In the whole-row Var case, refname is what the default AS
2068
+ * name would be.
2069
+ */
2070
+ attname = refname ;
2071
+ }
2072
+ }
2073
+ else
2074
+ {
2075
+ get_rule_expr ((Node * ) tle -> expr , context , true);
2076
+ /* We'll show the AS name unless it's this: */
2077
+ attname = "?column?" ;
2078
+ }
2030
2079
2031
2080
/*
2032
2081
* Figure out what the result column should be called. In the context
@@ -2039,28 +2088,10 @@ get_basic_select_query(Query *query, deparse_context *context,
2039
2088
else
2040
2089
colname = tle -> resname ;
2041
2090
2091
+ /* Show AS unless the column's name is correct as-is */
2042
2092
if (colname ) /* resname could be NULL */
2043
2093
{
2044
- /* Check if we must say AS ... */
2045
- bool tell_as ;
2046
-
2047
- if (!IsA (tle -> expr , Var ))
2048
- tell_as = (strcmp (colname , "?column?" ) != 0 );
2049
- else
2050
- {
2051
- Var * var = (Var * ) (tle -> expr );
2052
- char * schemaname ;
2053
- char * refname ;
2054
- char * attname ;
2055
-
2056
- get_names_for_var (var , 0 , context ,
2057
- & schemaname , & refname , & attname );
2058
- tell_as = (attname == NULL ||
2059
- strcmp (attname , colname ) != 0 );
2060
- }
2061
-
2062
- /* and do if so */
2063
- if (tell_as )
2094
+ if (attname == NULL || strcmp (attname , colname ) != 0 )
2064
2095
appendStringInfo (buf , " AS %s" , quote_identifier (colname ));
2065
2096
}
2066
2097
}
@@ -3098,17 +3129,17 @@ get_rule_expr(Node *node, deparse_context *context,
3098
3129
quote_identifier (schemaname ));
3099
3130
3100
3131
if (strcmp (refname , "*NEW*" ) == 0 )
3101
- appendStringInfo (buf , "new." );
3132
+ appendStringInfoString (buf , "new." );
3102
3133
else if (strcmp (refname , "*OLD*" ) == 0 )
3103
- appendStringInfo (buf , "old." );
3134
+ appendStringInfoString (buf , "old." );
3104
3135
else
3105
3136
appendStringInfo (buf , "%s." ,
3106
3137
quote_identifier (refname ));
3107
3138
}
3108
3139
if (attname )
3109
3140
appendStringInfoString (buf , quote_identifier (attname ));
3110
3141
else
3111
- appendStringInfo (buf , "*" );
3142
+ appendStringInfoString (buf , "*" );
3112
3143
}
3113
3144
break ;
3114
3145
0 commit comments