[#4258] Bad SQL generated for quantified row value expression predicates in Oracle

This commit is contained in:
lukaseder 2015-05-01 14:45:17 +02:00
parent e7dec0fb21
commit da10707bc5
2 changed files with 37 additions and 24 deletions

View File

@ -40,9 +40,12 @@
*/
package org.jooq.impl;
import static java.util.Arrays.asList;
// ...
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.Utils.DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY;
import org.jooq.Clause;
import org.jooq.Configuration;
@ -83,21 +86,23 @@ class QuantifiedSelectImpl<R extends Record> extends AbstractQueryPart implement
@Override
public final void accept(Context<?> ctx) {
Object data = ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY);
boolean extraParentheses = data != null && asList().contains(ctx.family());
// If this is already a subquery, proceed
if (ctx.subquery()) {
ctx.keyword(quantifier.toSQL())
.sql(" (")
.sql(extraParentheses ? " ((" : " (")
.formatIndentStart()
.formatNewLine()
.visit(delegate(ctx.configuration()))
.formatIndentEnd()
.formatNewLine()
.sql(')');
.sql(extraParentheses ? "))" : ")");
}
else {
ctx.keyword(quantifier.toSQL())
.sql(" (")
.sql(extraParentheses ? " ((" : " (")
.subquery(true)
.formatIndentStart()
.formatNewLine()
@ -105,7 +110,7 @@ class QuantifiedSelectImpl<R extends Record> extends AbstractQueryPart implement
.formatIndentEnd()
.formatNewLine()
.subquery(false)
.sql(')');
.sql(extraParentheses ? "))" : ")");
}
}

View File

@ -218,31 +218,39 @@ class RowSubqueryCondition extends AbstractCondition {
@Override
public final void accept(Context<?> ctx) {
// [#2054] Quantified row value expression comparison predicates shouldn't have parentheses before ANY or ALL
boolean parentheses = rightQuantified == null;
// Some databases need extra parentheses around the RHS
boolean extraParentheses = asList().contains(ctx.configuration().dialect().family());
boolean subquery = ctx.subquery();
ctx.visit(left)
.sql(' ')
.keyword(comparator.toSQL())
.sql(' ')
.sql( parentheses ? "(" : "")
.sql(extraParentheses ? "(" : "");
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, true);
ctx.subquery(true)
.formatIndentStart()
.formatNewLine()
.visit(right != null ? right : rightQuantified)
.formatIndentEnd()
.formatNewLine()
.subquery(subquery);
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, null);
ctx.sql(extraParentheses ? ")" : "")
.sql( parentheses ? ")" : "");
.sql(' ');
if (rightQuantified == null) {
// Some databases need extra parentheses around the RHS
boolean extraParentheses = asList().contains(ctx.family());
ctx.sql(extraParentheses ? "((" : "(");
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, true);
ctx.subquery(true)
.formatIndentStart()
.formatNewLine()
.visit(right)
.formatIndentEnd()
.formatNewLine()
.subquery(subquery);
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, null);
ctx.sql(extraParentheses ? "))" : ")");
}
// [#2054] Quantified row value expression comparison predicates shouldn't have parentheses before ANY or ALL
else {
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, true);
ctx.subquery(true)
.visit(rightQuantified)
.subquery(subquery);
ctx.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, null);
}
}
@Override