mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Add valid eq/ne behaviour for RelationalPath
This commit is contained in:
parent
63e206d685
commit
b87107fd0c
@ -209,7 +209,11 @@ public abstract class SimpleExpression<T> extends DslExpression<T> {
|
||||
* @return
|
||||
*/
|
||||
public BooleanExpression ne(T right) {
|
||||
return ne(ConstantImpl.create(right));
|
||||
if (right == null) {
|
||||
throw new IllegalArgumentException("ne(null) is not allowed. Use isNotNull() instead");
|
||||
} else {
|
||||
return ne(ConstantImpl.create(right));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -21,6 +21,8 @@ import java.util.Map;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
import com.mysema.query.types.expr.BooleanOperation;
|
||||
import com.mysema.query.types.expr.NumberExpression;
|
||||
import com.mysema.query.types.expr.NumberOperation;
|
||||
import com.mysema.query.types.path.BeanPath;
|
||||
@ -129,6 +131,81 @@ public class RelationalPathBase<T> extends BeanPath<T> implements RelationalPath
|
||||
return countDistinct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two relational paths using primary key columns
|
||||
*
|
||||
* @param right rhs of the comparison
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BooleanExpression eq(T right) {
|
||||
if (right instanceof RelationalPath) {
|
||||
return primaryKeyOperation(Ops.EQ, primaryKey, ((RelationalPath) right).getPrimaryKey());
|
||||
} else {
|
||||
return super.eq(right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two relational paths using primary key columns
|
||||
*
|
||||
* @param right rhs of the comparison
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BooleanExpression eq(Expression<? super T> right) {
|
||||
if (right instanceof RelationalPath) {
|
||||
return primaryKeyOperation(Ops.EQ, primaryKey, ((RelationalPath) right).getPrimaryKey());
|
||||
} else {
|
||||
return super.eq(right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two relational paths using primary key columns
|
||||
*
|
||||
* @param right rhs of the comparison
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BooleanExpression ne(T right) {
|
||||
if (right instanceof RelationalPath) {
|
||||
return primaryKeyOperation(Ops.NE, primaryKey, ((RelationalPath) right).getPrimaryKey());
|
||||
} else {
|
||||
return super.ne(right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two relational paths using primary key columns
|
||||
*
|
||||
* @param right rhs of the comparison
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BooleanExpression ne(Expression<? super T> right) {
|
||||
if (right instanceof RelationalPath) {
|
||||
return primaryKeyOperation(Ops.NE, primaryKey, ((RelationalPath) right).getPrimaryKey());
|
||||
} else {
|
||||
return super.ne(right);
|
||||
}
|
||||
}
|
||||
|
||||
private BooleanExpression primaryKeyOperation(Operator<Boolean> op, PrimaryKey<?> pk1, PrimaryKey<?> pk2) {
|
||||
if (pk1 == null || pk2 == null) {
|
||||
throw new UnsupportedOperationException("No primary keys available");
|
||||
}
|
||||
if (pk1.getLocalColumns().size() != pk2.getLocalColumns().size()) {
|
||||
throw new UnsupportedOperationException("Size mismatch for primary key columns");
|
||||
}
|
||||
BooleanExpression rv = null;
|
||||
for (int i = 0; i < pk1.getLocalColumns().size(); i++) {
|
||||
BooleanExpression pred = BooleanOperation.create(op, pk1.getLocalColumns().get(i), pk2.getLocalColumns().get(i));
|
||||
rv = rv != null ? rv.and(pred) : pred;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FactoryExpression<T> getProjection() {
|
||||
if (projection == null) {
|
||||
|
||||
@ -1082,6 +1082,34 @@ public class SelectBase extends AbstractBaseTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RelationalPath_Eq() {
|
||||
query().from(employee, employee2)
|
||||
.where(employee.eq(employee2))
|
||||
.list(employee.id, employee2.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RelationalPath_Ne() {
|
||||
query().from(employee, employee2)
|
||||
.where(employee.ne(employee2))
|
||||
.list(employee.id, employee2.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RelationalPath_Eq2() {
|
||||
query().from(survey, survey2)
|
||||
.where(survey.eq(survey2))
|
||||
.list(survey.id, survey2.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RelationalPath_Ne2() {
|
||||
query().from(survey, survey2)
|
||||
.where(survey.ne(survey2))
|
||||
.list(survey.id, survey2.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExcludeIn(SQLITE)
|
||||
public void Right_Join() throws SQLException {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user