
com.mysema.query.collections.AbstractColQuery Maven / Gradle / Ivy
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.commons.lang.IteratorAdapter;
import com.mysema.query.JoinType;
import com.mysema.query.QueryException;
import com.mysema.query.QueryMetadata;
import com.mysema.query.SearchResults;
import com.mysema.query.support.ProjectableQuery;
import com.mysema.query.types.ArrayConstructorExpression;
import com.mysema.query.types.CollectionExpression;
import com.mysema.query.types.Expression;
import com.mysema.query.types.MapExpression;
import com.mysema.query.types.OperationImpl;
import com.mysema.query.types.Ops;
import com.mysema.query.types.Path;
/**
* AbstractColQuery provides a base class for Collection query implementations.
* Extend it like this
*
*
* public class MyType extends AbstractColQuery<MyType>{
* ...
* }
*
*
* @see ColQuery
*
* @author tiwe
*/
public abstract class AbstractColQuery> extends ProjectableQuery {
private final Map, Iterable>> iterables = new HashMap, Iterable>>();
private final QueryEngine queryEngine;
@SuppressWarnings("unchecked")
public AbstractColQuery(QueryMetadata metadata, QueryEngine queryEngine) {
super(new ColQueryMixin(metadata));
this.queryMixin.setSelf((Q) this);
this.queryEngine = queryEngine;
}
@Override
public long count() {
try {
return queryEngine.count(getMetadata(), iterables);
} catch (Exception e) {
throw new QueryException(e.getMessage(), e);
}finally{
reset();
}
}
@Override
public boolean exists(){
try {
return queryEngine.exists(getMetadata(), iterables);
} catch (Exception e) {
throw new QueryException(e.getMessage(), e);
}finally{
reset();
}
}
private Expression createAlias(CollectionExpression,D> target, Path alias){
return OperationImpl.create(alias.getType(), Ops.ALIAS, target, alias);
}
private Expression createAlias(MapExpression,D> target, Path alias){
return OperationImpl.create(alias.getType(), Ops.ALIAS, target, alias);
}
@SuppressWarnings("unchecked")
public Q from(Path entity, Iterable extends A> col) {
iterables.put(entity, col);
getMetadata().addJoin(JoinType.DEFAULT, entity);
return (Q)this;
}
@SuppressWarnings("unchecked")
public Q bind(Path entity, Iterable extends A> col) {
iterables.put(entity, col);
return (Q)this;
}
public abstract QueryMetadata getMetadata();
protected QueryEngine getQueryEngine(){
return queryEngine;
}
@SuppressWarnings("unchecked")
public Q innerJoin(CollectionExpression, P> target, Path
alias) {
getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias));
return (Q)this;
}
@SuppressWarnings("unchecked")
public
Q innerJoin(MapExpression,P> target, Path
alias) {
getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias));
return (Q)this;
}
@Override
public CloseableIterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy