org.springframework.data.repository.query.parser.AbstractQueryCreator Maven / Gradle / Ivy
/*
* Copyright 2008-2019 the original author or authors.
*
* 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 org.springframework.data.repository.query.parser;
import java.util.Collections;
import java.util.Iterator;
import java.util.Optional;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base class for query creators that create criteria based queries from a {@link PartTree}.
*
* @param the actual query type to be created
* @param the intermediate criteria type
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
public abstract class AbstractQueryCreator {
private final Optional parameters;
private final PartTree tree;
/**
* Creates a new {@link AbstractQueryCreator} for the given {@link PartTree}. This will cause {@literal null} be
* handed for the {@link Iterator} in the callback methods.
*
* @param tree must not be {@literal null}.
* @since 2.0
*/
public AbstractQueryCreator(PartTree tree) {
this(tree, Optional.empty());
}
/**
* Creates a new {@link AbstractQueryCreator} for the given {@link PartTree} and {@link ParametersParameterAccessor}.
* The latter is used to hand actual parameter values into the callback methods as well as to apply dynamic sorting
* via a {@link Sort} parameter.
*
* @param tree must not be {@literal null}.
* @param parameters must not be {@literal null}.
*/
public AbstractQueryCreator(PartTree tree, ParameterAccessor parameters) {
this(tree, Optional.of(parameters));
}
private AbstractQueryCreator(PartTree tree, Optional parameters) {
Assert.notNull(tree, "PartTree must not be null");
Assert.notNull(parameters, "ParameterAccessor must not be null");
this.tree = tree;
this.parameters = parameters;
}
/**
* Creates the actual query object.
*
* @return
*/
public T createQuery() {
return createQuery(parameters.map(ParameterAccessor::getSort) //
.orElse(Sort.unsorted()));
}
/**
* Creates the actual query object applying the given {@link Sort} parameter. Use this method in case you haven't
* provided a {@link ParameterAccessor} in the first place but want to apply dynamic sorting nevertheless.
*
* @param dynamicSort must not be {@literal null}.
* @return
*/
public T createQuery(Sort dynamicSort) {
Assert.notNull(dynamicSort, "DynamicSort must not be null!");
return complete(createCriteria(tree), tree.getSort().and(dynamicSort));
}
/**
* Actual query building logic. Traverses the {@link PartTree} and invokes callback methods to delegate actual
* criteria creation and concatenation.
*
* @param tree must not be {@literal null}.
* @return
*/
@Nullable
private S createCriteria(PartTree tree) {
S base = null;
Iterator