
com.mysema.query.mongodb.MongodbQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of querydsl-mongodb Show documentation
Show all versions of querydsl-mongodb Show documentation
Mongodb support for Querydsl
The newest version!
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.mongodb;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.mongodb.*;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.*;
import com.mysema.query.support.QueryMixin;
import com.mysema.query.types.*;
import com.mysema.query.types.path.CollectionPathBase;
/**
* MongodbQuery provides a general Querydsl query implementation with a pluggable DBObject to Bean transformation
*
* @author laimw
*
* @param
*/
public abstract class MongodbQuery implements SimpleQuery>, SimpleProjectable {
@SuppressWarnings("serial")
private static class NoResults extends RuntimeException {}
private final MongodbSerializer serializer;
private final QueryMixin> queryMixin;
private final DBCollection collection;
private final Function transformer;
private ReadPreference readPreference;
/**
* Create a new MongodbQuery instance
*
* @param collection
* @param transformer
* @param serializer
*/
public MongodbQuery(DBCollection collection, Function transformer, MongodbSerializer serializer) {
this.queryMixin = new QueryMixin>(this, new DefaultQueryMetadata().noValidate(), false);
this.transformer = transformer;
this.collection = collection;
this.serializer = serializer;
}
/**
* Define a join
*
* @param ref
* @param target
* @return
*/
public JoinBuilder join(Path ref, Path target) {
return new JoinBuilder(queryMixin, ref, target);
}
/**
* Define a join
*
* @param ref
* @param target
* @return
*/
public JoinBuilder join(CollectionPathBase,T,?> ref, Path target) {
return new JoinBuilder(queryMixin, ref, target);
}
/**
* Define a constraint for an embedded object
*
* @param collection
* @param target
* @return
*/
public AnyEmbeddedBuilder anyEmbedded(Path extends Collection> collection, Path target) {
return new AnyEmbeddedBuilder(queryMixin, collection);
}
protected abstract DBCollection getCollection(Class> type);
@Override
public boolean exists() {
try {
QueryMetadata metadata = queryMixin.getMetadata();
Predicate filter = createFilter(metadata);
return collection.findOne(createQuery(filter)) != null;
} catch (NoResults ex) {
return false;
}
}
@Nullable
protected Predicate createFilter(QueryMetadata metadata) {
Predicate filter;
if (!metadata.getJoins().isEmpty()) {
filter = ExpressionUtils.allOf(metadata.getWhere(), createJoinFilter(metadata));
} else {
filter = metadata.getWhere();
}
return filter;
}
@Nullable
protected Predicate createJoinFilter(QueryMetadata metadata) {
Multimap, Predicate> predicates = HashMultimap., Predicate>create();
List joins = metadata.getJoins();
for (int i = joins.size() - 1; i >= 0; i--) {
JoinExpression join = joins.get(i);
Path source = (Path)((Operation>)join.getTarget()).getArg(0);
Path target = (Path)((Operation>)join.getTarget()).getArg(1);
Collection extraFilters = predicates.get(target.getRoot());
Predicate filter = ExpressionUtils.allOf(join.getCondition(), allOf(extraFilters));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy