[#4724] Add Table.eq(Table<R>) and Table.ne(Table<R>)

This commit is contained in:
lukaseder 2015-11-10 10:28:13 +01:00
parent ab3e79a814
commit d3835638e2
2 changed files with 134 additions and 0 deletions

View File

@ -1582,6 +1582,98 @@ public interface Table<R extends Record> extends TableLike<R> {
@PlainSQL
TableOnStep<Record> straightJoin(Name name);
// -------------------------------------------------------------------------
// XXX: Convenience methods and synthetic methods
// -------------------------------------------------------------------------
/**
* Create a predicate comparing records from self-joined tables.
* <p>
* This is a convenience method for self-joins, comparing either:
* <ul>
* <li>Primary key values, if {@link #getPrimaryKey()} is available</li>
* <li>Complete records, otherwise</li>
* </ul>
* For example:
* <code><pre>
* MyTable a = MY_TABLE.as("a");
* MyTable b = MY_TABLE.as("b");
*
* DSL.using(configuration)
* .select()
* .from(a)
* .join(b).on(a.eq(b));
* </pre></code>
*
* @see #equal(Table)
*/
Condition eq(Table<R> table);
/**
* Create a predicate comparing records from self-joined tables.
* <p>
* This is a convenience method for self-joins, comparing either:
* <ul>
* <li>Primary key values, if {@link #getPrimaryKey()} is available</li>
* <li>Complete records, otherwise</li>
* </ul>
* For example:
* <code><pre>
* MyTable a = MY_TABLE.as("a");
* MyTable b = MY_TABLE.as("b");
*
* DSL.using(configuration)
* .select()
* .from(a)
* .join(b).on(a.equal(b));
* </pre></code>
*/
Condition equal(Table<R> table);
/**
* Create a predicate comparing records from self-non-equi-joined tables.
* <p>
* This is a convenience method for self-non-equi-joins, comparing either:
* <ul>
* <li>Primary key values, if {@link #getPrimaryKey()} is available</li>
* <li>Complete records, otherwise</li>
* </ul>
* For example:
* <code><pre>
* MyTable a = MY_TABLE.as("a");
* MyTable b = MY_TABLE.as("b");
*
* DSL.using(configuration)
* .select()
* .from(a)
* .join(b).on(a.ne(b));
* </pre></code>
*
* @see #notEqual(Table)
*/
Condition ne(Table<R> table);
/**
* Create a predicate comparing records from self-non-equi-joined tables.
* <p>
* This is a convenience method for self-non-equi-joins, comparing either:
* <ul>
* <li>Primary key values, if {@link #getPrimaryKey()} is available</li>
* <li>Complete records, otherwise</li>
* </ul>
* For example:
* <code><pre>
* MyTable a = MY_TABLE.as("a");
* MyTable b = MY_TABLE.as("b");
*
* DSL.using(configuration)
* .select()
* .from(a)
* .join(b).on(a.notEqual(b));
* </pre></code>
*/
Condition notEqual(Table<R> table);
// -------------------------------------------------------------------------
// XXX: Exotic and vendor-specific clauses on tables
// -------------------------------------------------------------------------

View File

@ -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<R extends Record> extends AbstractQueryPart impleme
return createField(name, type, this, comment, converter, binding);
}
// ------------------------------------------------------------------------
// XXX: Convenience methods and synthetic methods
// ------------------------------------------------------------------------
@Override
public final Condition eq(Table<R> that) {
return equal(that);
}
@Override
public final Condition equal(Table<R> that) {
UniqueKey<R> thisPK = this.getPrimaryKey();
UniqueKey<R> 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<R> that) {
return notEqual(that);
}
@Override
public final Condition notEqual(Table<R> that) {
UniqueKey<R> thisPK = this.getPrimaryKey();
UniqueKey<R> 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
// ------------------------------------------------------------------------