Skip to content

Commit

Permalink
Introduce KeyspaceProvider to StatementFactory.
Browse files Browse the repository at this point in the history
StatementFactory now accepts a KeyspaceProvider to determine a specific Keyspace for a built statement.

StatementFactory can be obtained through CassandraTemplate and its async/reactive variants.

Closes #1275
  • Loading branch information
mp911de committed Jul 24, 2024
1 parent 24e1564 commit 0cd9b43
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -39,20 +40,7 @@
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.cql.AsyncCqlOperations;
import org.springframework.data.cassandra.core.cql.AsyncCqlTemplate;
import org.springframework.data.cassandra.core.cql.AsyncPreparedStatementCreator;
import org.springframework.data.cassandra.core.cql.AsyncResultSetExtractor;
import org.springframework.data.cassandra.core.cql.AsyncSessionCallback;
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
import org.springframework.data.cassandra.core.cql.CqlProvider;
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.RowCallbackHandler;
import org.springframework.data.cassandra.core.cql.RowMapper;
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.cql.*;
import org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory;
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
Expand Down Expand Up @@ -244,6 +232,17 @@ public CassandraConverter getConverter() {
return this.converter;
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see StatementFactory
* @since 2.1
*/
public StatementFactory getStatementFactory() {
return this.statementFactory;
}

/**
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
Expand Down Expand Up @@ -300,17 +299,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
return getEntityOperations().getRequiredPersistentEntity(entityType);
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see StatementFactory
* @since 2.1
*/
protected StatementFactory getStatementFactory() {
return this.statementFactory;
}

private CqlIdentifier getTableName(Class<?> entityClass) {
return getEntityOperations().getTableName(entityClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.UpdateMapper;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
Expand Down Expand Up @@ -66,7 +65,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
* @param batchType must not be {@literal null}.
* @since 3.2.6
*/
CassandraBatchTemplate(CassandraOperations operations, BatchType batchType) {
CassandraBatchTemplate(CassandraTemplate operations, BatchType batchType) {

Assert.notNull(operations, "CassandraOperations must not be null");
Assert.notNull(batchType, "BatchType must not be null");
Expand All @@ -75,7 +74,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
this.batch = BatchStatement.builder(batchType);
this.converter = operations.getConverter();
this.mappingContext = this.converter.getMappingContext();
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
this.statementFactory = operations.getStatementFactory();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -35,20 +36,7 @@
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.convert.QueryMapper;
import org.springframework.data.cassandra.core.convert.UpdateMapper;
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
import org.springframework.data.cassandra.core.cql.CqlOperations;
import org.springframework.data.cassandra.core.cql.CqlProvider;
import org.springframework.data.cassandra.core.cql.CqlTemplate;
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
import org.springframework.data.cassandra.core.cql.PreparedStatementCreator;
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.RowMapper;
import org.springframework.data.cassandra.core.cql.SessionCallback;
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.cql.*;
import org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory;
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
Expand Down Expand Up @@ -193,7 +181,7 @@ public CassandraTemplate(CqlOperations cqlOperations, CassandraConverter convert
this.converter = converter;
this.cqlOperations = cqlOperations;
this.entityOperations = new EntityOperations(converter);
this.statementFactory = new StatementFactory(new QueryMapper(converter), new UpdateMapper(converter));
this.statementFactory = new StatementFactory(converter);
this.eventDelegate = new EntityLifecycleEventDelegate();
}

Expand Down Expand Up @@ -246,6 +234,17 @@ public CassandraConverter getConverter() {
return this.converter;
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see org.springframework.data.cassandra.core.StatementFactory
* @since 2.1
*/
public StatementFactory getStatementFactory() {
return this.statementFactory;
}

/**
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
Expand Down Expand Up @@ -301,17 +300,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
return getEntityOperations().getRequiredPersistentEntity(entityType);
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see org.springframework.data.cassandra.core.StatementFactory
* @since 2.1
*/
protected StatementFactory getStatementFactory() {
return this.statementFactory;
}

@Override
public CqlIdentifier getTableName(Class<?> entityClass) {
return getEntityOperations().getTableName(entityClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.UpdateMapper;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
Expand Down Expand Up @@ -74,7 +73,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
* @param batchType must not be {@literal null}.
* @since 3.2.6
*/
ReactiveCassandraBatchTemplate(ReactiveCassandraOperations operations, BatchType batchType) {
ReactiveCassandraBatchTemplate(ReactiveCassandraTemplate operations, BatchType batchType) {

Assert.notNull(operations, "CassandraOperations must not be null");
Assert.notNull(batchType, "BatchType must not be null");
Expand All @@ -83,7 +82,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
this.batch = BatchStatement.builder(batchType);
this.converter = operations.getConverter();
this.mappingContext = this.converter.getMappingContext();
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
this.statementFactory = operations.getStatementFactory();
}

private void assertNotExecuted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -41,18 +42,7 @@
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
import org.springframework.data.cassandra.core.cql.CqlProvider;
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.ReactiveCqlOperations;
import org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate;
import org.springframework.data.cassandra.core.cql.ReactivePreparedStatementCreator;
import org.springframework.data.cassandra.core.cql.ReactiveSessionCallback;
import org.springframework.data.cassandra.core.cql.RowMapper;
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.cql.*;
import org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory;
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
Expand Down Expand Up @@ -253,6 +243,17 @@ public CassandraConverter getConverter() {
return this.converter;
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see org.springframework.data.cassandra.core.StatementFactory
* @since 2.1
*/
public StatementFactory getStatementFactory() {
return this.statementFactory;
}

/**
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
Expand Down Expand Up @@ -309,17 +310,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
return getEntityOperations().getRequiredPersistentEntity(entityType);
}

/**
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
*
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
* @see org.springframework.data.cassandra.core.StatementFactory
* @since 2.1
*/
protected StatementFactory getStatementFactory() {
return this.statementFactory;
}

CqlIdentifier getTableName(Class<?> entityClass) {
return getRequiredPersistentEntity(entityClass).getTableName();
}
Expand Down
Loading

0 comments on commit 0cd9b43

Please sign in to comment.