Improve SimpleExpression.in(...) signature

This commit is contained in:
Timo Westkämper 2016-03-01 19:00:07 +02:00
parent 0cfeeb5595
commit 6bb7a28649
4 changed files with 55 additions and 1 deletions

View File

@ -1263,6 +1263,33 @@ public final class Expressions {
return rv;
}
/**
* Combine the given expressions into a list expression
*
* @param clazz type of list expression
* @param exprs list elements
* @return list expression
*/
@SuppressWarnings("unchecked")
public static <T> Expression<T> list(Class<T> clazz, Expression<?>... exprs) {
Expression<T> rv = (Expression<T>) exprs[0];
for (int i = 1; i < exprs.length; i++) {
rv = new SimpleOperation<T>(clazz, Ops.LIST, rv, exprs[i]);
}
return rv;
}
/**
* Combine the given expressions into a list expression
*
* @param exprs list elements
* @return list expression
*/
public static Expression<Tuple> list(Expression<?>... exprs) {
return list(Tuple.class, exprs);
}
/**
* Create a null expression for the specified type
*

View File

@ -229,6 +229,16 @@ public abstract class SimpleExpression<T> extends DslExpression<T> {
return Expressions.booleanOperation(Ops.IN, mixin, right);
}
/**
* Create a {@code this in right} expression
*
* @param right rhs of the comparison
* @return this in right
*/
public BooleanExpression in(Expression<? extends T>... right) {
return Expressions.booleanOperation(Ops.IN, mixin, Expressions.list(right));
}
/**
* Create a {@code this <> right} expression
*

View File

@ -890,7 +890,13 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
}
}
if (operator == SQLOps.UNION || operator == SQLOps.UNION_ALL) {
if (operator == Ops.LIST && args.get(0) instanceof SubQueryExpression) {
boolean oldUnion = inUnion;
inUnion = true;
super.visitOperation(type, SQLOps.UNION, args);
inUnion = oldUnion;
} else if (operator == SQLOps.UNION || operator == SQLOps.UNION_ALL) {
boolean oldUnion = inUnion;
inUnion = true;
super.visitOperation(type, operator, args);

View File

@ -895,6 +895,17 @@ public class SelectBase extends AbstractBaseTest {
assertEquals(0, query().from(employee).where(employee.id.in(ImmutableList.<Integer>of())).fetchCount());
}
@Test
@ExcludeIn({MYSQL, TERADATA})
public void in_subqueries() {
QEmployee e1 = new QEmployee("e1");
QEmployee e2 = new QEmployee("e2");
assertEquals(2, query().from(employee).where(employee.id.in(
query().from(e1).where(e1.firstname.eq("Mike")).select(e1.id),
query().from(e2).where(e2.firstname.eq("Mary")).select(e2.id)
)).fetchCount());
}
@Test
public void notIn_empty() {
long count = query().from(employee).fetchCount();