org.springframework.data.cassandra.repository.query.CassandraQueryMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-cassandra Show documentation
Show all versions of spring-data-cassandra Show documentation
Cassandra support for Spring Data
/*
* Copyright 2016-2020 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.lang.reflect.Method;
import java.util.Locale;
import java.util.Optional;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty;
import org.springframework.data.cassandra.repository.Consistency;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.cassandra.repository.Query.Idempotency;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.cql.ResultSet;
/**
* Cassandra specific implementation of {@link QueryMethod}.
*
* @author Matthew Adams
* @author Oliver Gierke
* @author Mark Paluch
* @author John Blum
*/
public class CassandraQueryMethod extends QueryMethod {
private final Method method;
private final MappingContext, ? extends CassandraPersistentProperty> mappingContext;
private final Optional query;
private final Optional consistency;
private @Nullable CassandraEntityMetadata entityMetadata;
/**
* Create a new {@link CassandraQueryMethod} from the given {@link Method}.
*
* @param method must not be {@literal null}.
* @param repositoryMetadata must not be {@literal null}.
* @param projectionFactory must not be {@literal null}.
* @param mappingContext must not be {@literal null}.
*/
public CassandraQueryMethod(Method method, RepositoryMetadata repositoryMetadata, ProjectionFactory projectionFactory,
MappingContext, ? extends CassandraPersistentProperty> mappingContext) {
super(method, repositoryMetadata, projectionFactory);
Assert.notNull(mappingContext, "MappingContext must not be null");
verify(method, repositoryMetadata);
this.method = method;
this.mappingContext = mappingContext;
this.query = Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method, Query.class));
this.consistency = Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method, Consistency.class));
}
/**
* Validates that this query is not a page query.
*/
@SuppressWarnings("unused")
public void verify(Method method, RepositoryMetadata metadata) {
if (isPageQuery()) {
throw new InvalidDataAccessApiUsageException("Page queries are not supported. Use a Slice query.");
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryMethod#getEntityInformation()
*/
@Override
@SuppressWarnings("unchecked")
public CassandraEntityMetadata getEntityInformation() {
if (this.entityMetadata == null) {
Class returnedObjectType = getReturnedObjectType();
Class domainClass = getDomainClass();
if (ClassUtils.isPrimitiveOrWrapper(returnedObjectType)) {
this.entityMetadata = new SimpleCassandraEntityMetadata<>((Class