Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016-2023 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
*
* https://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.cassandra.repository.query;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.data.cassandra.repository.query.BindingContext.ParameterBinding;
import org.springframework.data.mapping.model.SpELExpressionEvaluator;
import org.springframework.data.spel.ExpressionDependencies;
import org.springframework.expression.ExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
/**
* String-based Query abstracting a CQL query with parameter bindings.
*
* @author Mark Paluch
* @since 2.0
*/
class StringBasedQuery {
private final String query;
private final CassandraParameters parameters;
private final ExpressionParser expressionParser;
private final List queryParameterBindings = new ArrayList<>();
private final ExpressionDependencies expressionDependencies;
/**
* Create a new {@link StringBasedQuery} given {@code query}, {@link CassandraParameters} and
* {@link ExpressionParser}.
*
* @param query must not be empty.
* @param parameters must not be {@literal null}.
* @param expressionParser must not be {@literal null}.
*/
StringBasedQuery(String query, CassandraParameters parameters, ExpressionParser expressionParser) {
this.query = ParameterBindingParser.INSTANCE.parseAndCollectParameterBindingsFromQueryIntoBindings(query,
this.queryParameterBindings);
this.parameters = parameters;
this.expressionParser = expressionParser;
this.expressionDependencies = createExpressionDependencies();
}
private ExpressionDependencies createExpressionDependencies() {
if (queryParameterBindings.isEmpty()) {
return ExpressionDependencies.none();
}
List dependencies = new ArrayList<>();
for (ParameterBinding binding : queryParameterBindings) {
if (binding.isExpression()) {
dependencies
.add(ExpressionDependencies.discover(expressionParser.parseExpression(binding.getRequiredExpression())));
}
}
return ExpressionDependencies.merged(dependencies);
}
/**
* Obtain {@link ExpressionDependencies} from the parsed query.
*
* @return the {@link ExpressionDependencies} from the parsed query.
*/
public ExpressionDependencies getExpressionDependencies() {
return expressionDependencies;
}
/**
* Bind the query to actual parameters using {@link CassandraParameterAccessor},
*
* @param parameterAccessor must not be {@literal null}.
* @param evaluator must not be {@literal null}.
* @return the bound String query containing formatted parameters.
*/
public SimpleStatement bindQuery(CassandraParameterAccessor parameterAccessor, SpELExpressionEvaluator evaluator) {
Assert.notNull(parameterAccessor, "CassandraParameterAccessor must not be null");
Assert.notNull(evaluator, "SpELExpressionEvaluator must not be null");
BindingContext bindingContext = new BindingContext(this.parameters, parameterAccessor, this.queryParameterBindings,
evaluator);
List