From b76289ea02d6ed84f1eb0747c8e12b3ed42b12ca Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 24 Dec 2012 10:18:41 +0100 Subject: [PATCH] [#2049] Add gt() / ge() / lt() / le() to Row[N] types - Factored out RowSubquery nested class to top-level type --- jOOQ-tools/src/org/jooq/xtend/Rows.xtend | 57 ++---------- jOOQ/src/main/java/org/jooq/impl/RowImpl.java | 49 +--------- .../main/java/org/jooq/impl/RowSubquery.java | 89 +++++++++++++++++++ 3 files changed, 101 insertions(+), 94 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/RowSubquery.java diff --git a/jOOQ-tools/src/org/jooq/xtend/Rows.xtend b/jOOQ-tools/src/org/jooq/xtend/Rows.xtend index 4eb6ebb069..b762e37c7f 100644 --- a/jOOQ-tools/src/org/jooq/xtend/Rows.xtend +++ b/jOOQ-tools/src/org/jooq/xtend/Rows.xtend @@ -874,7 +874,6 @@ class Rows extends Generators { import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.INGRES; - import static org.jooq.SQLDialect.ORACLE; import static org.jooq.SQLDialect.SQLITE; import static org.jooq.SQLDialect.SQLSERVER; import static org.jooq.SQLDialect.SYBASE; @@ -1689,7 +1688,7 @@ class Rows extends Generators { @Override public final Condition equal(Select select) { - return new Subquery(select, SubqueryOperator.EQUALS); + return new RowSubquery(this, select, SubqueryOperator.EQUALS); } @Override @@ -1699,7 +1698,7 @@ class Rows extends Generators { @Override public final Condition notEqual(Select select) { - return new Subquery(select, SubqueryOperator.NOT_EQUALS); + return new RowSubquery(this, select, SubqueryOperator.NOT_EQUALS); } @Override @@ -1709,7 +1708,7 @@ class Rows extends Generators { ««« @Override ««« public final Condition greaterThan(Select select) { -««« return new Subquery(select, SubqueryOperator.GREATER); +««« return new RowSubquery(this, select, SubqueryOperator.GREATER); ««« } ««« ««« @Override @@ -1719,7 +1718,7 @@ class Rows extends Generators { ««« ««« @Override ««« public final Condition greaterOrEqual(Select select) { -««« return new Subquery(select, SubqueryOperator.GREATER_OR_EQUAL); +««« return new RowSubquery(this, select, SubqueryOperator.GREATER_OR_EQUAL); ««« } ««« ««« @Override @@ -1729,7 +1728,7 @@ class Rows extends Generators { ««« ««« @Override ««« public final Condition lessThan(Select select) { -««« return new Subquery(select, SubqueryOperator.LESS); +««« return new RowSubquery(this, select, SubqueryOperator.LESS); ««« } ««« ««« @Override @@ -1739,7 +1738,7 @@ class Rows extends Generators { ««« ««« @Override ««« public final Condition lessOrEqual(Select select) { -««« return new Subquery(select, SubqueryOperator.LESS_OR_EQUAL); +««« return new RowSubquery(this, select, SubqueryOperator.LESS_OR_EQUAL); ««« } ««« ««« @Override @@ -1749,12 +1748,12 @@ class Rows extends Generators { ««« @Override public final Condition in(Select select) { - return new Subquery(select, SubqueryOperator.IN); + return new RowSubquery(this, select, SubqueryOperator.IN); } @Override public final Condition notIn(Select select) { - return new Subquery(select, SubqueryOperator.NOT_IN); + return new RowSubquery(this, select, SubqueryOperator.NOT_IN); } // ------------------------------------------------------------------------ @@ -1849,46 +1848,6 @@ class Rows extends Generators { } } } - - private class Subquery extends AbstractCondition { - - /** - * Generated UID - */ - private static final long serialVersionUID = -1806139685201770706L; - - private final Select other; - private final SubqueryOperator operator; - - Subquery(Select other, SubqueryOperator operator) { - this.other = other; - this.operator = operator; - } - - @Override - public final void toSQL(RenderContext context) { - - // Some databases need extra parentheses around the RHS - boolean extraParentheses = asList(ORACLE).contains(context.getDialect()); - boolean subquery = context.subquery(); - - context.sql(RowImpl.this) - .sql(" ") - .keyword(operator.toSQL()) - .sql(" (") - .sql(extraParentheses ? "(" : "") - .subquery(true) - .sql(other) - .subquery(subquery) - .sql(extraParentheses ? ")" : "") - .sql(")"); - } - - @Override - public final void bind(BindContext context) { - context.bind(RowImpl.this).bind(other); - } - } } '''); diff --git a/jOOQ/src/main/java/org/jooq/impl/RowImpl.java b/jOOQ/src/main/java/org/jooq/impl/RowImpl.java index 6903880102..57c893bbb3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RowImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/RowImpl.java @@ -42,7 +42,6 @@ import static org.jooq.SQLDialect.DB2; import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.INGRES; -import static org.jooq.SQLDialect.ORACLE; import static org.jooq.SQLDialect.SQLITE; import static org.jooq.SQLDialect.SQLSERVER; import static org.jooq.SQLDialect.SYBASE; @@ -2937,7 +2936,7 @@ implements @Override public final Condition equal(Select select) { - return new Subquery(select, SubqueryOperator.EQUALS); + return new RowSubquery(this, select, SubqueryOperator.EQUALS); } @Override @@ -2947,7 +2946,7 @@ implements @Override public final Condition notEqual(Select select) { - return new Subquery(select, SubqueryOperator.NOT_EQUALS); + return new RowSubquery(this, select, SubqueryOperator.NOT_EQUALS); } @Override @@ -2957,12 +2956,12 @@ implements @Override public final Condition in(Select select) { - return new Subquery(select, SubqueryOperator.IN); + return new RowSubquery(this, select, SubqueryOperator.IN); } @Override public final Condition notIn(Select select) { - return new Subquery(select, SubqueryOperator.NOT_IN); + return new RowSubquery(this, select, SubqueryOperator.NOT_IN); } // ------------------------------------------------------------------------ @@ -3057,44 +3056,4 @@ implements } } } - - private class Subquery extends AbstractCondition { - - /** - * Generated UID - */ - private static final long serialVersionUID = -1806139685201770706L; - - private final Select other; - private final SubqueryOperator operator; - - Subquery(Select other, SubqueryOperator operator) { - this.other = other; - this.operator = operator; - } - - @Override - public final void toSQL(RenderContext context) { - - // Some databases need extra parentheses around the RHS - boolean extraParentheses = asList(ORACLE).contains(context.getDialect()); - boolean subquery = context.subquery(); - - context.sql(RowImpl.this) - .sql(" ") - .keyword(operator.toSQL()) - .sql(" (") - .sql(extraParentheses ? "(" : "") - .subquery(true) - .sql(other) - .subquery(subquery) - .sql(extraParentheses ? ")" : "") - .sql(")"); - } - - @Override - public final void bind(BindContext context) { - context.bind(RowImpl.this).bind(other); - } - } } diff --git a/jOOQ/src/main/java/org/jooq/impl/RowSubquery.java b/jOOQ/src/main/java/org/jooq/impl/RowSubquery.java new file mode 100644 index 0000000000..ea626241d6 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/RowSubquery.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.impl; + +import static java.util.Arrays.asList; +import static org.jooq.SQLDialect.ORACLE; + +import org.jooq.BindContext; +import org.jooq.RenderContext; +import org.jooq.Row; +import org.jooq.Select; + +/** + * @author Lukas Eder + */ +class RowSubquery extends AbstractCondition { + + /** + * Generated UID + */ + private static final long serialVersionUID = -1806139685201770706L; + + private final Row left; + private final Select right; + private final SubqueryOperator operator; + + RowSubquery(Row left, Select right, SubqueryOperator operator) { + this.left = left; + this.right = right; + this.operator = operator; + } + + @Override + public final void toSQL(RenderContext context) { + + // Some databases need extra parentheses around the RHS + boolean extraParentheses = asList(ORACLE).contains(context.getDialect()); + boolean subquery = context.subquery(); + + context.sql(left) + .sql(" ") + .keyword(operator.toSQL()) + .sql(" (") + .sql(extraParentheses ? "(" : "") + .subquery(true) + .sql(right) + .subquery(subquery) + .sql(extraParentheses ? ")" : "") + .sql(")"); + } + + @Override + public final void bind(BindContext context) { + context.bind(left).bind(right); + } +} \ No newline at end of file