mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
#136 added Expression.as for general aliasing
This commit is contained in:
parent
1a5e002e9e
commit
d4aa7bf337
@ -18,6 +18,7 @@ import javax.annotation.Nullable;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.NullExpression;
|
||||
import com.mysema.query.types.Operator;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
@ -61,10 +62,24 @@ import com.mysema.query.types.template.StringTemplate;
|
||||
*
|
||||
*/
|
||||
public final class Expressions {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static <D> SimpleExpression<D> as(D source, Path<D> alias) {
|
||||
if (source == null) {
|
||||
return as((Expression)NullExpression.DEFAULT, alias);
|
||||
} else {
|
||||
return as(new ConstantImpl<D>(source), alias);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <D> SimpleExpression<D> as(Expression<D> source, Path<D> alias) {
|
||||
return SimpleOperation.create((Class<D>)alias.getType(), Ops.ALIAS, source, alias);
|
||||
if (source == null) {
|
||||
return as((Expression)NullExpression.DEFAULT, alias);
|
||||
} else {
|
||||
return SimpleOperation.create((Class<D>)alias.getType(), Ops.ALIAS, source, alias);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@ -32,6 +32,13 @@ public class ExpressionsTest {
|
||||
|
||||
private static final BooleanExpression a = new BooleanPath("a"), b = new BooleanPath("b");
|
||||
|
||||
@Test
|
||||
public void As() {
|
||||
assertEquals("null as str", Expressions.as(null, str).toString());
|
||||
assertEquals("str as str", Expressions.as("str", str).toString());
|
||||
assertEquals("s as str", Expressions.as(new StringPath("s"), str).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void AllOf() {
|
||||
assertEquals("a && b", Expressions.allOf(a, b).toString());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user