added Projection for inline projections

This commit is contained in:
Timo Westkämper 2009-01-30 11:46:38 +00:00
parent 8b8b91d24c
commit af7df11b69
6 changed files with 87 additions and 5 deletions

View File

@ -38,13 +38,15 @@ public class QueryBase<JoinMeta,A extends QueryBase<JoinMeta,A>> implements Quer
where.clear();
}
private A self = (A)this;
private final Metadata metadata = new Metadata();
public A from(Expr.EEntity<?>... o) {
for (EEntity<?> expr : o){
joins.add(new JoinExpression<JoinMeta>(JoinType.DEFAULT,expr));
}
return (A) this;
return self;
}
public A groupBy(Expr<?>... o) {

View File

@ -5,10 +5,7 @@
*/
package com.mysema.query.grammar.types;
import com.mysema.query.Query;
import com.mysema.query.QueryBase;
import com.mysema.query.grammar.Grammar;
import com.mysema.query.grammar.OrderSpecifier;
/**

View File

@ -0,0 +1,74 @@
package com.mysema.query.grammar.types;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
/**
* Projection provides
*
* @author tiwe
* @version $Id$
*/
public class Projection extends Path.PEntity<Object>{
private String entityName;
private static Method _numberMethod, _comparableMethod;
static{
try {
_numberMethod = PEntity.class.getDeclaredMethod("_number", String.class, Class.class);
_comparableMethod = PEntity.class.getDeclaredMethod("_comparable", String.class, Class.class);
_numberMethod.setAccessible(true);
_comparableMethod.setAccessible(true);
} catch (Exception e) {
String error = "Caught " + e.getClass().getName();
throw new RuntimeException(error, e);
}
}
public Projection(String entityName) {
super(Object.class, entityName, PathMetadata.forVariable(entityName));
Class<?> cl = getClass();
try{
while (!cl.equals(Projection.class)){
for (Field field : cl.getDeclaredFields()){
if (Expr.class.isAssignableFrom(field.getType()) && field.getGenericType() instanceof ParameterizedType){
field.setAccessible(true);
handleField(field);
}
}
cl = cl.getSuperclass();
}
}catch(Exception e){
String error = "Caught " + e.getClass().getName();
throw new RuntimeException(error, e);
}
this.entityName = entityName;
}
private void handleField(Field field) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
ParameterizedType type = (ParameterizedType) field.getGenericType();
Class<?> exprType = (Class<?>) type.getActualTypeArguments()[0];
Expr<?> fieldVal;
if (Boolean.class.isAssignableFrom(exprType)){
fieldVal = _boolean(field.getName());
}else if (Number.class.isAssignableFrom(exprType)){
fieldVal = (Expr<?>) _numberMethod.invoke(this, field.getName(), exprType);
}else if (String.class.isAssignableFrom(exprType)){
fieldVal = _string(field.getName());
}else if (Comparable.class.isAssignableFrom(exprType)){
fieldVal = (Expr<?>) _comparableMethod.invoke(this, field.getName(), exprType);
}else{
fieldVal = _simple(field.getName(), exprType);
}
field.set(this, fieldVal);
}
public String getName(){
return entityName;
}
}

View File

@ -41,6 +41,9 @@ public class SubQuery<JM,A> extends Expr<A> implements Query<SubQuery<JM,A>>, Co
public SubQuery<JM,A> select(Expr<?>... o) {query.s(o); return this;}
public SubQuery<JM,A> where(EBoolean... o) {query.where(o); return this;}
public SubQuery<JM,A> with(EBoolean o) {query.with(o); return this;}
// TODO : add some validation that the given Projection is valid for this subquery
public Alias.ASimple<A> as(Projection to) { return new Alias.ASimple<A>(this, to.getName()); }
public Alias.ASimple<A> as(String to) { return new Alias.ASimple<A>(this, to); }
private static class QueryWithPublicSelect<JM> extends QueryBase<JM,QueryWithPublicSelect<JM>>{
public void s(Expr<?>... expr){

View File

@ -121,4 +121,6 @@ public abstract class Visitor<T extends Visitor<T>> {
protected abstract void visit(Path<?> expr);
protected abstract void visit(Projection expr);
}

View File

@ -90,7 +90,7 @@ public abstract class VisitorAdapter<V extends VisitorAdapter<V>> extends Visito
@Override
protected void visit(PComponentMap<?,?> expr) {
visit((PMap<?,?>) expr);
}
}
@Override
protected void visit(PEntity<?> expr) {
visit((Path<?>) expr);
@ -123,4 +123,8 @@ public abstract class VisitorAdapter<V extends VisitorAdapter<V>> extends Visito
protected void visit(PStringArray expr) {
visit((PArray<?>) expr);
}
@Override
protected void visit(Projection expr){
visit((PEntity<?>)expr);
}
}