From b87107fd0ccca90d96472e9973d8518de8da2fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sat, 23 Aug 2014 19:12:16 +0300 Subject: [PATCH] Add valid eq/ne behaviour for RelationalPath --- .../query/types/expr/SimpleExpression.java | 6 +- .../mysema/query/sql/RelationalPathBase.java | 77 +++++++++++++++++++ .../java/com/mysema/query/SelectBase.java | 28 +++++++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/querydsl-core/src/main/java/com/mysema/query/types/expr/SimpleExpression.java b/querydsl-core/src/main/java/com/mysema/query/types/expr/SimpleExpression.java index f61afb457..ecd6a5c94 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/expr/SimpleExpression.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/expr/SimpleExpression.java @@ -209,7 +209,11 @@ public abstract class SimpleExpression extends DslExpression { * @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)); + } } /** diff --git a/querydsl-sql/src/main/java/com/mysema/query/sql/RelationalPathBase.java b/querydsl-sql/src/main/java/com/mysema/query/sql/RelationalPathBase.java index f9dfc8e2f..dbdd2d3ce 100644 --- a/querydsl-sql/src/main/java/com/mysema/query/sql/RelationalPathBase.java +++ b/querydsl-sql/src/main/java/com/mysema/query/sql/RelationalPathBase.java @@ -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 extends BeanPath 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 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 right) { + if (right instanceof RelationalPath) { + return primaryKeyOperation(Ops.NE, primaryKey, ((RelationalPath) right).getPrimaryKey()); + } else { + return super.ne(right); + } + } + + private BooleanExpression primaryKeyOperation(Operator 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 getProjection() { if (projection == null) { diff --git a/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java b/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java index 83021e2a8..96cfdfd12 100644 --- a/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java +++ b/querydsl-sql/src/test/java/com/mysema/query/SelectBase.java @@ -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 {