Merge pull request #907 from querydsl/i906

Add valid eq/ne behaviour for RelationalPath
This commit is contained in:
Timo Westkämper 2014-08-24 11:45:21 +03:00
commit 572eb5725c
4 changed files with 120 additions and 13 deletions

View File

@ -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));
}
}
/**

View File

@ -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) {

View File

@ -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 {

View File

@ -13,16 +13,14 @@
*/
package com.mysema.query.sql;
import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import com.mysema.query.JoinFlag;
import com.mysema.query.sql.domain.QSurvey;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JoinFlagsTest {
@ -45,13 +43,13 @@ public class JoinFlagsTest {
}
@Test
public void JoinFlag_BeforeCondition() {
public void JoinFlags_BeforeCondition() {
query.innerJoin(s2).on(s1.eq(s2));
query.addJoinFlag(" a ", JoinFlag.Position.BEFORE_CONDITION);
assertEquals("from SURVEY s\n" +
"inner join SURVEY s2 a \n" +
"on s = ?", query.toString());
"on s.ID = s2.ID", query.toString());
}
@Test
@ -61,7 +59,7 @@ public class JoinFlagsTest {
assertEquals("from SURVEY s\n" +
"inner join b SURVEY s3\n" +
"on s = ?", query.toString());
"on s.ID = s3.ID", query.toString());
}
@Test
@ -71,7 +69,7 @@ public class JoinFlagsTest {
assertEquals("from SURVEY s\n" +
"inner join SURVEY s4\n" +
"on s = ? c", query.toString());
"on s.ID = s4.ID c", query.toString());
}
@Test
@ -80,7 +78,7 @@ public class JoinFlagsTest {
query.addJoinFlag(" d ", JoinFlag.Position.OVERRIDE);
assertEquals("from SURVEY s d SURVEY s5\n" +
"on s = ?", query.toString());
"on s.ID = s5.ID", query.toString());
}
@Test
@ -90,7 +88,7 @@ public class JoinFlagsTest {
assertEquals("from SURVEY s e \n" +
"inner join SURVEY s6\n" +
"on s = ?", query.toString());
"on s.ID = s6.ID", query.toString());
}