3
3
* back to source text
4
4
*
5
5
* IDENTIFICATION
6
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.174 2004/06/25 17:20:24 tgl Exp $
6
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.175 2004/07/06 04:50:21 tgl Exp $
7
7
*
8
8
* This software is copyrighted by Jan Wieck - Hamburg.
9
9
*
@@ -2066,6 +2066,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
2066
2066
TupleDesc resultDesc )
2067
2067
{
2068
2068
StringInfo buf = context -> buf ;
2069
+ bool need_paren ;
2069
2070
2070
2071
if (IsA (setOp , RangeTblRef ))
2071
2072
{
@@ -2074,24 +2075,37 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
2074
2075
Query * subquery = rte -> subquery ;
2075
2076
2076
2077
Assert (subquery != NULL );
2078
+ Assert (subquery -> setOperations == NULL );
2079
+ /* Need parens if ORDER BY, FOR UPDATE, or LIMIT; see gram.y */
2080
+ need_paren = (subquery -> sortClause ||
2081
+ subquery -> rowMarks ||
2082
+ subquery -> limitOffset ||
2083
+ subquery -> limitCount );
2084
+ if (need_paren )
2085
+ appendStringInfoChar (buf , '(' );
2077
2086
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
2078
2087
context -> prettyFlags , context -> indentLevel );
2088
+ if (need_paren )
2089
+ appendStringInfoChar (buf , ')' );
2079
2090
}
2080
2091
else if (IsA (setOp , SetOperationStmt ))
2081
2092
{
2082
2093
SetOperationStmt * op = (SetOperationStmt * ) setOp ;
2083
- bool need_paren ;
2084
-
2085
- need_paren = (PRETTY_PAREN (context ) ?
2086
- !IsA (op -> rarg , RangeTblRef ) : true);
2087
2094
2088
- if (!PRETTY_PAREN (context ))
2089
- appendStringInfoString (buf , "((" );
2095
+ /*
2096
+ * We force parens whenever nesting two SetOperationStmts.
2097
+ * There are some cases in which parens are needed around a leaf
2098
+ * query too, but those are more easily handled at the next level
2099
+ * down (see code above).
2100
+ */
2101
+ need_paren = !IsA (op -> larg , RangeTblRef );
2090
2102
2103
+ if (need_paren )
2104
+ appendStringInfoChar (buf , '(' );
2091
2105
get_setop_query (op -> larg , query , context , resultDesc );
2092
-
2093
- if (!PRETTY_PAREN (context ))
2106
+ if (need_paren )
2094
2107
appendStringInfoChar (buf , ')' );
2108
+
2095
2109
if (!PRETTY_INDENT (context ))
2096
2110
appendStringInfoChar (buf , ' ' );
2097
2111
switch (op -> op )
@@ -2118,27 +2132,13 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
2118
2132
if (PRETTY_INDENT (context ))
2119
2133
appendStringInfoChar (buf , '\n' );
2120
2134
2121
- if (PRETTY_PAREN (context ))
2122
- {
2123
- if (need_paren )
2124
- {
2125
- appendStringInfoChar (buf , '(' );
2126
- if (PRETTY_INDENT (context ))
2127
- appendStringInfoChar (buf , '\n' );
2128
- }
2129
- }
2130
- else
2131
- appendStringInfoChar (buf , '(' );
2135
+ need_paren = !IsA (op -> rarg , RangeTblRef );
2132
2136
2137
+ if (need_paren )
2138
+ appendStringInfoChar (buf , '(' );
2133
2139
get_setop_query (op -> rarg , query , context , resultDesc );
2134
-
2135
- if (PRETTY_PAREN (context ))
2136
- {
2137
- if (need_paren )
2138
- appendStringInfoChar (buf , ')' );
2139
- }
2140
- else
2141
- appendStringInfoString (buf , "))" );
2140
+ if (need_paren )
2141
+ appendStringInfoChar (buf , ')' );
2142
2142
}
2143
2143
else
2144
2144
{
0 commit comments