Skip to content

Commit 0cd9b43

Browse files
committed
Introduce KeyspaceProvider to StatementFactory.
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
1 parent 24e1564 commit 0cd9b43

File tree

10 files changed

+282
-187
lines changed

10 files changed

+282
-187
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/AsyncCassandraTemplate.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30+
3031
import org.springframework.beans.BeansException;
3132
import org.springframework.context.ApplicationContext;
3233
import org.springframework.context.ApplicationContextAware;
@@ -39,20 +40,7 @@
3940
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
4041
import org.springframework.data.cassandra.core.convert.CassandraConverter;
4142
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
42-
import org.springframework.data.cassandra.core.cql.AsyncCqlOperations;
43-
import org.springframework.data.cassandra.core.cql.AsyncCqlTemplate;
44-
import org.springframework.data.cassandra.core.cql.AsyncPreparedStatementCreator;
45-
import org.springframework.data.cassandra.core.cql.AsyncResultSetExtractor;
46-
import org.springframework.data.cassandra.core.cql.AsyncSessionCallback;
47-
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
48-
import org.springframework.data.cassandra.core.cql.CqlProvider;
49-
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
50-
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
51-
import org.springframework.data.cassandra.core.cql.QueryOptions;
52-
import org.springframework.data.cassandra.core.cql.RowCallbackHandler;
53-
import org.springframework.data.cassandra.core.cql.RowMapper;
54-
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
55-
import org.springframework.data.cassandra.core.cql.WriteOptions;
43+
import org.springframework.data.cassandra.core.cql.*;
5644
import org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory;
5745
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
5846
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
@@ -244,6 +232,17 @@ public CassandraConverter getConverter() {
244232
return this.converter;
245233
}
246234

235+
/**
236+
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
237+
*
238+
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
239+
* @see StatementFactory
240+
* @since 2.1
241+
*/
242+
public StatementFactory getStatementFactory() {
243+
return this.statementFactory;
244+
}
245+
247246
/**
248247
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
249248
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
@@ -300,17 +299,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
300299
return getEntityOperations().getRequiredPersistentEntity(entityType);
301300
}
302301

303-
/**
304-
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
305-
*
306-
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
307-
* @see StatementFactory
308-
* @since 2.1
309-
*/
310-
protected StatementFactory getStatementFactory() {
311-
return this.statementFactory;
312-
}
313-
314302
private CqlIdentifier getTableName(Class<?> entityClass) {
315303
return getEntityOperations().getTableName(entityClass);
316304
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.concurrent.atomic.AtomicBoolean;
2020

2121
import org.springframework.data.cassandra.core.convert.CassandraConverter;
22-
import org.springframework.data.cassandra.core.convert.UpdateMapper;
2322
import org.springframework.data.cassandra.core.cql.QueryOptions;
2423
import org.springframework.data.cassandra.core.cql.WriteOptions;
2524
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
@@ -66,7 +65,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
6665
* @param batchType must not be {@literal null}.
6766
* @since 3.2.6
6867
*/
69-
CassandraBatchTemplate(CassandraOperations operations, BatchType batchType) {
68+
CassandraBatchTemplate(CassandraTemplate operations, BatchType batchType) {
7069

7170
Assert.notNull(operations, "CassandraOperations must not be null");
7271
Assert.notNull(batchType, "BatchType must not be null");
@@ -75,7 +74,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
7574
this.batch = BatchStatement.builder(batchType);
7675
this.converter = operations.getConverter();
7776
this.mappingContext = this.converter.getMappingContext();
78-
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
77+
this.statementFactory = operations.getStatementFactory();
7978
}
8079

8180
/**

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
2627
import org.springframework.beans.BeansException;
2728
import org.springframework.context.ApplicationContext;
2829
import org.springframework.context.ApplicationContextAware;
@@ -35,20 +36,7 @@
3536
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
3637
import org.springframework.data.cassandra.core.convert.CassandraConverter;
3738
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
38-
import org.springframework.data.cassandra.core.convert.QueryMapper;
39-
import org.springframework.data.cassandra.core.convert.UpdateMapper;
40-
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
41-
import org.springframework.data.cassandra.core.cql.CqlOperations;
42-
import org.springframework.data.cassandra.core.cql.CqlProvider;
43-
import org.springframework.data.cassandra.core.cql.CqlTemplate;
44-
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
45-
import org.springframework.data.cassandra.core.cql.PreparedStatementCreator;
46-
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
47-
import org.springframework.data.cassandra.core.cql.QueryOptions;
48-
import org.springframework.data.cassandra.core.cql.RowMapper;
49-
import org.springframework.data.cassandra.core.cql.SessionCallback;
50-
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
51-
import org.springframework.data.cassandra.core.cql.WriteOptions;
39+
import org.springframework.data.cassandra.core.cql.*;
5240
import org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory;
5341
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
5442
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
@@ -193,7 +181,7 @@ public CassandraTemplate(CqlOperations cqlOperations, CassandraConverter convert
193181
this.converter = converter;
194182
this.cqlOperations = cqlOperations;
195183
this.entityOperations = new EntityOperations(converter);
196-
this.statementFactory = new StatementFactory(new QueryMapper(converter), new UpdateMapper(converter));
184+
this.statementFactory = new StatementFactory(converter);
197185
this.eventDelegate = new EntityLifecycleEventDelegate();
198186
}
199187

@@ -246,6 +234,17 @@ public CassandraConverter getConverter() {
246234
return this.converter;
247235
}
248236

237+
/**
238+
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
239+
*
240+
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
241+
* @see org.springframework.data.cassandra.core.StatementFactory
242+
* @since 2.1
243+
*/
244+
public StatementFactory getStatementFactory() {
245+
return this.statementFactory;
246+
}
247+
249248
/**
250249
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
251250
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
@@ -301,17 +300,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
301300
return getEntityOperations().getRequiredPersistentEntity(entityType);
302301
}
303302

304-
/**
305-
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
306-
*
307-
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
308-
* @see org.springframework.data.cassandra.core.StatementFactory
309-
* @since 2.1
310-
*/
311-
protected StatementFactory getStatementFactory() {
312-
return this.statementFactory;
313-
}
314-
315303
@Override
316304
public CqlIdentifier getTableName(Class<?> entityClass) {
317305
return getEntityOperations().getTableName(entityClass);

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.concurrent.atomic.AtomicBoolean;
2727

2828
import org.springframework.data.cassandra.core.convert.CassandraConverter;
29-
import org.springframework.data.cassandra.core.convert.UpdateMapper;
3029
import org.springframework.data.cassandra.core.cql.QueryOptions;
3130
import org.springframework.data.cassandra.core.cql.WriteOptions;
3231
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
@@ -74,7 +73,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
7473
* @param batchType must not be {@literal null}.
7574
* @since 3.2.6
7675
*/
77-
ReactiveCassandraBatchTemplate(ReactiveCassandraOperations operations, BatchType batchType) {
76+
ReactiveCassandraBatchTemplate(ReactiveCassandraTemplate operations, BatchType batchType) {
7877

7978
Assert.notNull(operations, "CassandraOperations must not be null");
8079
Assert.notNull(batchType, "BatchType must not be null");
@@ -83,7 +82,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
8382
this.batch = BatchStatement.builder(batchType);
8483
this.converter = operations.getConverter();
8584
this.mappingContext = this.converter.getMappingContext();
86-
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
85+
this.statementFactory = operations.getStatementFactory();
8786
}
8887

8988
private void assertNotExecuted() {

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraTemplate.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
2929
import org.reactivestreams.Publisher;
30+
3031
import org.springframework.beans.BeansException;
3132
import org.springframework.context.ApplicationContext;
3233
import org.springframework.context.ApplicationContextAware;
@@ -41,18 +42,7 @@
4142
import org.springframework.data.cassandra.core.EntityOperations.AdaptibleEntity;
4243
import org.springframework.data.cassandra.core.convert.CassandraConverter;
4344
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
44-
import org.springframework.data.cassandra.core.cql.CassandraAccessor;
45-
import org.springframework.data.cassandra.core.cql.CqlProvider;
46-
import org.springframework.data.cassandra.core.cql.PreparedStatementBinder;
47-
import org.springframework.data.cassandra.core.cql.QueryExtractorDelegate;
48-
import org.springframework.data.cassandra.core.cql.QueryOptions;
49-
import org.springframework.data.cassandra.core.cql.ReactiveCqlOperations;
50-
import org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate;
51-
import org.springframework.data.cassandra.core.cql.ReactivePreparedStatementCreator;
52-
import org.springframework.data.cassandra.core.cql.ReactiveSessionCallback;
53-
import org.springframework.data.cassandra.core.cql.RowMapper;
54-
import org.springframework.data.cassandra.core.cql.SingleColumnRowMapper;
55-
import org.springframework.data.cassandra.core.cql.WriteOptions;
45+
import org.springframework.data.cassandra.core.cql.*;
5646
import org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory;
5747
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
5848
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
@@ -253,6 +243,17 @@ public CassandraConverter getConverter() {
253243
return this.converter;
254244
}
255245

246+
/**
247+
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
248+
*
249+
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
250+
* @see org.springframework.data.cassandra.core.StatementFactory
251+
* @since 2.1
252+
*/
253+
public StatementFactory getStatementFactory() {
254+
return this.statementFactory;
255+
}
256+
256257
/**
257258
* Returns whether this instance is configured to use {@link PreparedStatement prepared statements}. If enabled
258259
* (default), then all persistence methods (such as {@link #select}, {@link #update}, and others) will make use of
@@ -309,17 +310,6 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
309310
return getEntityOperations().getRequiredPersistentEntity(entityType);
310311
}
311312

312-
/**
313-
* Returns the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
314-
*
315-
* @return the {@link StatementFactory} used by this template to construct and run Cassandra CQL statements.
316-
* @see org.springframework.data.cassandra.core.StatementFactory
317-
* @since 2.1
318-
*/
319-
protected StatementFactory getStatementFactory() {
320-
return this.statementFactory;
321-
}
322-
323313
CqlIdentifier getTableName(Class<?> entityClass) {
324314
return getRequiredPersistentEntity(entityClass).getTableName();
325315
}

0 commit comments

Comments
 (0)