mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
improved Query interface by adding varargs for where and other methods
This commit is contained in:
parent
a74d2cb915
commit
8520231bd4
@ -71,7 +71,7 @@ public class ColQuery<S extends ColQuery<S>>{
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public S where(Expr.EBoolean o) {
|
||||
public S where(Expr.EBoolean... o) {
|
||||
query.where(o);
|
||||
return (S)this;
|
||||
}
|
||||
|
||||
@ -22,38 +22,28 @@ import com.mysema.query.grammar.types.Path.*;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
// TODO : consider moving this later to querydsl-core
|
||||
class ExprFactory {
|
||||
|
||||
private static final PString str = new PString(PathMetadata.forVariable("str"));
|
||||
|
||||
private static final ExtString strExt = new ExtString(PathMetadata.forVariable("str"));
|
||||
|
||||
private final Map<String,PString> strToPath = new PathFactory<String,PString>(new Transformer<String,PString>(){
|
||||
public PString transform(String str) {
|
||||
return new PString(md());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private final Map<Object,PSimple<?>> simToPath = new PathFactory<Object,PSimple<?>>(new Transformer<Object,PSimple<?>>(){
|
||||
public PSimple<?> transform(Object arg) {
|
||||
return new PSimple(arg.getClass(), md());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private final Map<Object,PComparable<?>> comToPath = new PathFactory<Object,PComparable<?>>(new Transformer<Object,PComparable<?>>(){
|
||||
public PComparable<?> transform(Object arg) {
|
||||
return new PComparable(arg.getClass(), md());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private final Map<String,ExtString> strToExtPath = new PathFactory<String,ExtString>(new Transformer<String,ExtString>(){
|
||||
public ExtString transform(String str) {
|
||||
return new ExtString(md());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static long counter = 0;
|
||||
|
||||
@ -68,13 +58,8 @@ class ExprFactory {
|
||||
return (PComparable<D>) comToPath.get(arg);
|
||||
}
|
||||
|
||||
public PString create(String arg){
|
||||
return strToPath.get(arg);
|
||||
}
|
||||
|
||||
public ExtString createExt(String arg){
|
||||
public ExtString create(String arg){
|
||||
return StringUtils.isEmpty(arg) ? strExt : strToExtPath.get(arg);
|
||||
// return strToExtget(arg);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@ -7,8 +7,6 @@ package com.mysema.query.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.mysema.query.grammar.Grammar;
|
||||
import com.mysema.query.grammar.OrderSpecifier;
|
||||
import com.mysema.query.grammar.types.Expr;
|
||||
@ -30,10 +28,24 @@ public class MiniApi {
|
||||
|
||||
private static final Path.PSimple<Object> it = new Path.PSimple<Object>(Object.class,PathMetadata.forVariable("it"));
|
||||
|
||||
/**
|
||||
* Create a new ColQuery instance with the given array bound to the default variable, which can be accessed via $()
|
||||
*
|
||||
* @param <A>
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static <A> ColQuery<?> from(A... arr){
|
||||
return from(Arrays.asList(arr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ColQuery instance with the given collection bound to the default variable, which can be accessed via $()
|
||||
*
|
||||
* @param <A>
|
||||
* @param col
|
||||
* @return
|
||||
*/
|
||||
public static <A> ColQuery<?> from(Iterable<A> col){
|
||||
return from(MiniApi.<A>$(), col);
|
||||
}
|
||||
@ -67,7 +79,7 @@ public class MiniApi {
|
||||
}
|
||||
|
||||
public static ExtString $(String arg){
|
||||
return exprFactory.createExt(arg);
|
||||
return exprFactory.create(arg);
|
||||
}
|
||||
|
||||
public static Path.PBooleanArray $(Boolean[] args){
|
||||
|
||||
@ -39,20 +39,30 @@ public class MiniApiTest {
|
||||
myInts.add(4);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testVarious(){
|
||||
public void testVarious1(){
|
||||
for(String s : from($("str"), "a","ab","cd","de")
|
||||
.where($("str").startsWith("a"))
|
||||
.iterate($("str"))){
|
||||
.where($("str").startsWith("a"))
|
||||
.iterate($("str"))){
|
||||
assertTrue(s.equals("a") || s.equals("ab"));
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
for (Object o : from(1,2,"abc",5,3).where($().ne("abc"))
|
||||
.iterate($())){
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVarious2(){
|
||||
for (Object o : from(1,2,"abc",5,3).where($().ne("abc")).iterate($())){
|
||||
int i = (Integer)o;
|
||||
assertTrue(i > 0 && i < 6);
|
||||
System.out.println(o);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVarious3(){
|
||||
for (Integer i : from($(0),1,2,3,4).where($(0).lt(4)).iterate($(0))){
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user