33 *back to source text
44 *
55 * 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 $
77 *
88 * This software is copyrighted by Jan Wieck - Hamburg.
99 *
@@ -2066,6 +2066,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
20662066TupleDesc resultDesc )
20672067{
20682068StringInfo buf = context -> buf ;
2069+ bool need_paren ;
20692070
20702071if (IsA (setOp ,RangeTblRef ))
20712072{
@@ -2074,24 +2075,37 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
20742075Query * subquery = rte -> subquery ;
20752076
20762077Assert (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 ,'(' );
20772086get_query_def (subquery ,buf ,context -> namespaces ,resultDesc ,
20782087context -> prettyFlags ,context -> indentLevel );
2088+ if (need_paren )
2089+ appendStringInfoChar (buf ,')' );
20792090}
20802091else if (IsA (setOp ,SetOperationStmt ))
20812092{
20822093SetOperationStmt * op = (SetOperationStmt * )setOp ;
2083- bool need_paren ;
2084-
2085- need_paren = (PRETTY_PAREN (context ) ?
2086- !IsA (op -> rarg ,RangeTblRef ) : true);
20872094
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 );
20902102
2103+ if (need_paren )
2104+ appendStringInfoChar (buf ,'(' );
20912105get_setop_query (op -> larg ,query ,context ,resultDesc );
2092-
2093- if (!PRETTY_PAREN (context ))
2106+ if (need_paren )
20942107appendStringInfoChar (buf ,')' );
2108+
20952109if (!PRETTY_INDENT (context ))
20962110appendStringInfoChar (buf ,' ' );
20972111switch (op -> op )
@@ -2118,27 +2132,13 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
21182132if (PRETTY_INDENT (context ))
21192133appendStringInfoChar (buf ,'\n' );
21202134
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 );
21322136
2137+ if (need_paren )
2138+ appendStringInfoChar (buf ,'(' );
21332139get_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 ,')' );
21422142}
21432143else
21442144{