mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
refactored
added tests
This commit is contained in:
parent
e31f9199b2
commit
e80405410e
@ -14,6 +14,7 @@ import com.mysema.query.grammar.GrammarTypes.OrderSpecifier;
|
||||
public interface Query<A extends Query<?>>{
|
||||
A select(Expr... objects);
|
||||
A from(DomainType<?>... objects);
|
||||
A innerJoin(DomainType<?>... objects);
|
||||
A leftJoin(DomainType<?>... objects);
|
||||
A with(BooleanExpr... objects);
|
||||
A where(BooleanExpr... objects);
|
||||
|
||||
@ -2,7 +2,7 @@ package com.mysema.query.grammar;
|
||||
|
||||
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanExpr;
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanExprType;
|
||||
import com.mysema.query.grammar.GrammarTypes.Op;
|
||||
import com.mysema.query.grammar.GrammarTypes.Expr;
|
||||
import com.mysema.query.grammar.GrammarTypes.Order;
|
||||
import com.mysema.query.grammar.GrammarTypes.OrderSpecifier;
|
||||
@ -33,32 +33,44 @@ public class Grammar {
|
||||
|
||||
// boolean
|
||||
|
||||
public static BooleanExpr and(Object left, Object right){
|
||||
return bbe(BooleanExprType.AND, left, right);
|
||||
public static BooleanExpr and(BooleanExpr left, BooleanExpr right){
|
||||
return bbe(Op.AND, left, right);
|
||||
}
|
||||
public static BooleanExpr eq(Object left, Object right){
|
||||
return bbe(BooleanExprType.EQ, left, right);
|
||||
}
|
||||
public static BooleanExpr goe(Object left, Object right){
|
||||
return bbe(BooleanExprType.GOE, left, right);
|
||||
}
|
||||
public static BooleanExpr gt(Object left, Object right){
|
||||
return bbe(BooleanExprType.GT, left, right);
|
||||
}
|
||||
public static BooleanExpr loe(Object left, Object right){
|
||||
return bbe(BooleanExprType.LOE, left, right);
|
||||
}
|
||||
public static BooleanExpr lt(Object left, Object right){
|
||||
return bbe(BooleanExprType.LT, left, right);
|
||||
}
|
||||
public static BooleanExpr ne(Object left, Object right){
|
||||
return bbe(BooleanExprType.NE, left, right);
|
||||
}
|
||||
public static BooleanExpr not(BooleanExpr left){
|
||||
return bue(BooleanExprType.NE, left);
|
||||
return bue(Op.NE, left);
|
||||
}
|
||||
public static BooleanExpr or(BooleanExpr left, BooleanExpr right){
|
||||
return bbe(Op.OR, left, right);
|
||||
}
|
||||
|
||||
// arithmetic
|
||||
// number compariosn
|
||||
|
||||
public static BooleanExpr eq(Object left, Object right){
|
||||
return bbe(Op.EQ, left, right);
|
||||
}
|
||||
public static BooleanExpr goe(Object left, Object right){
|
||||
return bbe(Op.GOE, left, right);
|
||||
}
|
||||
public static BooleanExpr gt(Object left, Object right){
|
||||
return bbe(Op.GT, left, right);
|
||||
}
|
||||
public static BooleanExpr loe(Object left, Object right){
|
||||
return bbe(Op.LOE, left, right);
|
||||
}
|
||||
public static BooleanExpr lt(Object left, Object right){
|
||||
return bbe(Op.LT, left, right);
|
||||
}
|
||||
public static BooleanExpr ne(Object left, Object right){
|
||||
return bbe(Op.NE, left, right);
|
||||
}
|
||||
|
||||
// string comparison
|
||||
|
||||
public static BooleanExpr like(Object left, String right){
|
||||
return bbe(Op.LIKE, left, right);
|
||||
}
|
||||
|
||||
// arithmetic operations
|
||||
|
||||
// TODO
|
||||
|
||||
|
||||
@ -33,15 +33,15 @@ public class GrammarTypes {
|
||||
|
||||
public interface BooleanExpr extends Expr{ }
|
||||
|
||||
public enum BooleanExprType { AND, EQ, GOE, GT,LOE, LT, NE, NOT, OR, XOR }
|
||||
public enum Op { AND, EQ, GOE, GT,LOE, LT, NE, NOT, OR, XOR, LIKE }
|
||||
|
||||
public static class BooleanUnaryExpr implements BooleanExpr{
|
||||
public BooleanExprType type;
|
||||
public Op type;
|
||||
public Expr left;
|
||||
}
|
||||
|
||||
public static class BooleanBinaryExpr implements BooleanExpr{
|
||||
public BooleanExprType type;
|
||||
public Op type;
|
||||
public Expr left; public Expr right;
|
||||
}
|
||||
|
||||
@ -51,13 +51,13 @@ public class GrammarTypes {
|
||||
public Reference(String path) {
|
||||
this._path = path;
|
||||
}
|
||||
protected CharProperty charProp(String path) {
|
||||
protected CharProperty ch(String path) {
|
||||
return new CharProperty(this._path+"."+path);
|
||||
}
|
||||
protected StringProperty stringProp(String path) {
|
||||
protected StringProperty str(String path) {
|
||||
return new StringProperty(this._path+"."+path);
|
||||
}
|
||||
protected NumberProperty numberProp(String path) {
|
||||
protected NumberProperty num(String path) {
|
||||
return new NumberProperty(this._path+"."+path);
|
||||
}
|
||||
// protected CollectionProperty colProp(String path) {
|
||||
|
||||
@ -2,7 +2,7 @@ package com.mysema.query.grammar;
|
||||
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanBinaryExpr;
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanExpr;
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanExprType;
|
||||
import com.mysema.query.grammar.GrammarTypes.Op;
|
||||
import com.mysema.query.grammar.GrammarTypes.BooleanUnaryExpr;
|
||||
import com.mysema.query.grammar.GrammarTypes.ConstantExpr;
|
||||
import com.mysema.query.grammar.GrammarTypes.Expr;
|
||||
@ -20,14 +20,14 @@ class InternalGrammar {
|
||||
e.constant = str;
|
||||
return e;
|
||||
}
|
||||
static BooleanBinaryExpr bbe(BooleanExprType type,Object left, Object right){
|
||||
static BooleanBinaryExpr bbe(Op type,Object left, Object right){
|
||||
BooleanBinaryExpr bbe = new BooleanBinaryExpr();
|
||||
bbe.type = type;
|
||||
bbe.left = left instanceof Expr ? (Expr)left : co(left);
|
||||
bbe.right = right instanceof Expr ? (Expr)right : co(left);
|
||||
return bbe;
|
||||
}
|
||||
static BooleanUnaryExpr bue(BooleanExprType type, BooleanExpr left){
|
||||
static BooleanUnaryExpr bue(Op type, BooleanExpr left){
|
||||
BooleanUnaryExpr bue = new BooleanUnaryExpr();
|
||||
bue.type = type;
|
||||
bue.left = left;
|
||||
|
||||
@ -1,51 +0,0 @@
|
||||
package com.mysema.query.test;
|
||||
|
||||
import com.mysema.query.grammar.GrammarTypes.DomainType;
|
||||
import com.mysema.query.grammar.GrammarTypes.NumberProperty;
|
||||
import com.mysema.query.grammar.GrammarTypes.StringProperty;
|
||||
|
||||
|
||||
/**
|
||||
* Domain provides
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public class Domain {
|
||||
public static final User user = new User("user");
|
||||
public static final User user1 = new User("user1");
|
||||
public static final User user2 = new User("user2");
|
||||
|
||||
public static final Company company = new Company("company");
|
||||
public static final Company company1 = new Company("company1");
|
||||
public static final Company company2 = new Company("company2");
|
||||
|
||||
public static final Cat cat = new Cat("cat");
|
||||
public static final Cat cat1 = new Cat("cat1");
|
||||
public static final Cat cat2 = new Cat("cat2");
|
||||
|
||||
public static final Cat kitten = new Cat("kitten");
|
||||
|
||||
public static class User extends DomainType<User>{
|
||||
User(String path) {super(path);}
|
||||
public final NumberProperty id = numberProp("id");
|
||||
public final StringProperty userName = stringProp("userName");
|
||||
public final StringProperty firstName = stringProp("firstName");
|
||||
public final StringProperty lastName = stringProp("lastName");
|
||||
public final Company company = new Company(_path+".company");
|
||||
}
|
||||
|
||||
public static class Company extends DomainType<Company>{
|
||||
Company(String path) {super(path);}
|
||||
public final NumberProperty id = numberProp("id");
|
||||
public final StringProperty name = stringProp("name");
|
||||
}
|
||||
|
||||
public static class Cat extends DomainType<Cat>{
|
||||
Cat(String path) {super(path);}
|
||||
public final NumberProperty bodyWeight = numberProp("bodyWeight");
|
||||
public final Cat kittens = new Cat(_path+".kittens");
|
||||
public final Cat mate = new Cat(_path+".mate");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,11 @@
|
||||
package com.mysema.query.test;
|
||||
|
||||
import static com.mysema.query.grammar.Grammar.*;
|
||||
import static com.mysema.query.test.Domain.*;
|
||||
import static com.mysema.query.grammar.Grammar.gt;
|
||||
import static com.mysema.query.grammar.Grammar.like;
|
||||
import static com.mysema.query.test.domain.Domain.cat;
|
||||
import static com.mysema.query.test.domain.Domain.child;
|
||||
import static com.mysema.query.test.domain.Domain.kitten;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
@ -16,24 +20,24 @@ public class DomainTest extends QueryBase{
|
||||
|
||||
@Test
|
||||
public void testQuery1(){
|
||||
// from Cat as cat
|
||||
// left join cat.kittens as kitten
|
||||
// from Cat as cat left join cat.kittens as kitten
|
||||
// with kitten.bodyWeight > 10.0
|
||||
from(cat)
|
||||
.leftJoin(cat.kittens.as(kitten))
|
||||
.with(gt(kitten.bodyWeight,10));
|
||||
|
||||
from(cat).leftJoin(cat.kittens().as(kitten))
|
||||
.with(gt(kitten.bodyWeight,10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery2(){
|
||||
// from Cat as cat
|
||||
// inner join fetch cat.mate
|
||||
// left join fetch cat.kittens child
|
||||
// left join fetch child.kittens
|
||||
from(cat)
|
||||
.innerJoin(cat.mate)
|
||||
.leftJoin()
|
||||
// from Cat as cat inner join fetch cat.mate
|
||||
// left join fetch cat.kittens child left join fetch child.kittens
|
||||
from(cat).innerJoin(cat.mate())
|
||||
.leftJoin(cat.kittens().as(child)).leftJoin(child.kittens());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery3(){
|
||||
// from Cat as cat where cat.mate.name like '%s%'
|
||||
from(cat).where(like(cat.mate().name,"%s%"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -42,4 +42,8 @@ public class QueryBase implements Query<QueryBase> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBase innerJoin(DomainType<?>... objects) {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
66
src/test/java/com/mysema/query/test/domain/Domain.java
Normal file
66
src/test/java/com/mysema/query/test/domain/Domain.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.mysema.query.test.domain;
|
||||
|
||||
import com.mysema.query.grammar.GrammarTypes.DomainType;
|
||||
import com.mysema.query.grammar.GrammarTypes.NumberProperty;
|
||||
import com.mysema.query.grammar.GrammarTypes.StringProperty;
|
||||
|
||||
|
||||
/**
|
||||
* Domain provides
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public class Domain {
|
||||
// User
|
||||
public static final User user = new User("user");
|
||||
public static final User user1 = new User("user1");
|
||||
public static final User user2 = new User("user2");
|
||||
public static final User user3 = new User("user3");
|
||||
public static final User user4 = new User("user4");
|
||||
public static final User user5 = new User("user5");
|
||||
|
||||
// Company
|
||||
public static final Company company = new Company("company");
|
||||
public static final Company company1 = new Company("company1");
|
||||
public static final Company company2 = new Company("company2");
|
||||
public static final Company company3 = new Company("company3");
|
||||
public static final Company company4 = new Company("company4");
|
||||
public static final Company company5 = new Company("company5");
|
||||
|
||||
// Cat
|
||||
public static final Cat cat = new Cat("cat");
|
||||
public static final Cat cat1 = new Cat("cat1");
|
||||
public static final Cat cat2 = new Cat("cat2");
|
||||
public static final Cat cat3 = new Cat("cat3");
|
||||
public static final Cat cat4 = new Cat("cat4");
|
||||
public static final Cat cat5 = new Cat("cat5");
|
||||
|
||||
public static final Cat kitten = new Cat("kitten");
|
||||
public static final Cat child = new Cat("child");
|
||||
public static final Cat mate = new Cat("mate");
|
||||
|
||||
public static class User extends DomainType<User>{
|
||||
User(String path) {super(path);}
|
||||
public final NumberProperty id = num("id");
|
||||
public final StringProperty userName = str("userName");
|
||||
public final StringProperty firstName = str("firstName");
|
||||
public final StringProperty lastName = str("lastName");
|
||||
public final Company company(){ return new Company(_path+".company");}
|
||||
}
|
||||
|
||||
public static class Company extends DomainType<Company>{
|
||||
Company(String path) {super(path);}
|
||||
public final NumberProperty id = num("id");
|
||||
public final StringProperty name = str("name");
|
||||
}
|
||||
|
||||
public static class Cat extends DomainType<Cat>{
|
||||
Cat(String path) {super(path);}
|
||||
public final NumberProperty bodyWeight = num("bodyWeight");
|
||||
public final Cat kittens(){ return new Cat(_path+".kittens");}
|
||||
public final Cat mate(){ return new Cat(_path+".mate");}
|
||||
public final StringProperty name = str("name");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user