From d3835638e258d3f5d32186bcdeb09d328af7ed4e Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 10 Nov 2015 10:28:13 +0100 Subject: [PATCH] [#4724] Add Table.eq(Table) and Table.ne(Table) --- jOOQ/src/main/java/org/jooq/Table.java | 92 +++++++++++++++++++ .../java/org/jooq/impl/AbstractTable.java | 42 +++++++++ 2 files changed, 134 insertions(+) diff --git a/jOOQ/src/main/java/org/jooq/Table.java b/jOOQ/src/main/java/org/jooq/Table.java index 443ba00a8e..f91d170b7c 100644 --- a/jOOQ/src/main/java/org/jooq/Table.java +++ b/jOOQ/src/main/java/org/jooq/Table.java @@ -1582,6 +1582,98 @@ public interface Table extends TableLike { @PlainSQL TableOnStep straightJoin(Name name); + // ------------------------------------------------------------------------- + // XXX: Convenience methods and synthetic methods + // ------------------------------------------------------------------------- + + /** + * Create a predicate comparing records from self-joined tables. + *

+ * This is a convenience method for self-joins, comparing either: + *

    + *
  • Primary key values, if {@link #getPrimaryKey()} is available
  • + *
  • Complete records, otherwise
  • + *
+ * For example: + *
+     * MyTable a = MY_TABLE.as("a");
+     * MyTable b = MY_TABLE.as("b");
+     *
+     * DSL.using(configuration)
+     *    .select()
+     *    .from(a)
+     *    .join(b).on(a.eq(b));
+     * 
+ * + * @see #equal(Table) + */ + Condition eq(Table table); + + /** + * Create a predicate comparing records from self-joined tables. + *

+ * This is a convenience method for self-joins, comparing either: + *

    + *
  • Primary key values, if {@link #getPrimaryKey()} is available
  • + *
  • Complete records, otherwise
  • + *
+ * For example: + *
+     * MyTable a = MY_TABLE.as("a");
+     * MyTable b = MY_TABLE.as("b");
+     *
+     * DSL.using(configuration)
+     *    .select()
+     *    .from(a)
+     *    .join(b).on(a.equal(b));
+     * 
+ */ + Condition equal(Table table); + + /** + * Create a predicate comparing records from self-non-equi-joined tables. + *

+ * This is a convenience method for self-non-equi-joins, comparing either: + *

    + *
  • Primary key values, if {@link #getPrimaryKey()} is available
  • + *
  • Complete records, otherwise
  • + *
+ * For example: + *
+     * MyTable a = MY_TABLE.as("a");
+     * MyTable b = MY_TABLE.as("b");
+     *
+     * DSL.using(configuration)
+     *    .select()
+     *    .from(a)
+     *    .join(b).on(a.ne(b));
+     * 
+ * + * @see #notEqual(Table) + */ + Condition ne(Table table); + + /** + * Create a predicate comparing records from self-non-equi-joined tables. + *

+ * This is a convenience method for self-non-equi-joins, comparing either: + *

    + *
  • Primary key values, if {@link #getPrimaryKey()} is available
  • + *
  • Complete records, otherwise
  • + *
+ * For example: + *
+     * MyTable a = MY_TABLE.as("a");
+     * MyTable b = MY_TABLE.as("b");
+     *
+     * DSL.using(configuration)
+     *    .select()
+     *    .from(a)
+     *    .join(b).on(a.notEqual(b));
+     * 
+ */ + Condition notEqual(Table table); + // ------------------------------------------------------------------------- // XXX: Exotic and vendor-specific clauses on tables // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java index ecae487e04..a76dcdae19 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java @@ -55,6 +55,7 @@ import static org.jooq.JoinType.OUTER_APPLY; import static org.jooq.JoinType.RIGHT_OUTER_JOIN; import static org.jooq.JoinType.STRAIGHT_JOIN; // ... +import static org.jooq.impl.DSL.row; import static org.jooq.impl.DSL.table; import static org.jooq.impl.DSL.val; @@ -66,6 +67,7 @@ import java.util.List; import org.jooq.Binding; import org.jooq.Clause; +import org.jooq.Condition; import org.jooq.Context; import org.jooq.Converter; import org.jooq.DataType; @@ -499,6 +501,46 @@ abstract class AbstractTable extends AbstractQueryPart impleme return createField(name, type, this, comment, converter, binding); } + // ------------------------------------------------------------------------ + // XXX: Convenience methods and synthetic methods + // ------------------------------------------------------------------------ + + @Override + public final Condition eq(Table that) { + return equal(that); + } + + @Override + public final Condition equal(Table that) { + UniqueKey thisPK = this.getPrimaryKey(); + UniqueKey thatPK = that.getPrimaryKey(); + + if (thisPK != null && thatPK != null) { + return row(fields(thisPK.getFieldsArray())).eq(row(that.fields(thatPK.getFieldsArray()))); + } + else { + return row(fields()).eq(row(that.fields())); + } + } + + @Override + public final Condition ne(Table that) { + return notEqual(that); + } + + @Override + public final Condition notEqual(Table that) { + UniqueKey thisPK = this.getPrimaryKey(); + UniqueKey thatPK = that.getPrimaryKey(); + + if (thisPK != null && thatPK != null) { + return row(fields(thisPK.getFieldsArray())).ne(row(that.fields(thatPK.getFieldsArray()))); + } + else { + return row(fields()).ne(row(that.fields())); + } + } + // ------------------------------------------------------------------------ // XXX: Other API // ------------------------------------------------------------------------