Skip to content

Commit d9ddf0d

Browse files
committed
Polishing.
Consistently use toCql(…) and String.format for CQL retrieval. See #1186
1 parent 99ad379 commit d9ddf0d

File tree

5 files changed

+80
-82
lines changed

5 files changed

+80
-82
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
import java.util.concurrent.ExecutionException;
2323
import java.util.function.Function;
2424

25+
import com.datastax.oss.driver.api.core.CqlSession;
26+
import com.datastax.oss.driver.api.core.DriverException;
27+
import com.datastax.oss.driver.api.core.cql.AsyncResultSet;
28+
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
29+
import com.datastax.oss.driver.api.core.cql.ResultSet;
30+
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
31+
import com.datastax.oss.driver.api.core.cql.Statement;
32+
2533
import org.springframework.dao.DataAccessException;
2634
import org.springframework.dao.support.DataAccessUtils;
2735
import org.springframework.dao.support.PersistenceExceptionTranslator;
@@ -32,14 +40,6 @@
3240
import org.springframework.util.concurrent.ListenableFuture;
3341
import org.springframework.util.concurrent.SettableListenableFuture;
3442

35-
import com.datastax.oss.driver.api.core.CqlSession;
36-
import com.datastax.oss.driver.api.core.DriverException;
37-
import com.datastax.oss.driver.api.core.cql.AsyncResultSet;
38-
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
39-
import com.datastax.oss.driver.api.core.cql.ResultSet;
40-
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
41-
import com.datastax.oss.driver.api.core.cql.Statement;
42-
4343
/**
4444
* <b>This is the central class in the CQL core package for asynchronous Cassandra data access.</b> It simplifies the
4545
* use of CQL and helps to avoid common errors. It executes core CQL workflow, leaving application code to provide CQL
@@ -163,7 +163,7 @@ public <T> ListenableFuture<T> query(String cql, AsyncResultSetExtractor<T> resu
163163

164164
try {
165165
if (logger.isDebugEnabled()) {
166-
logger.debug("Executing CQL statement [{}]", cql);
166+
logger.debug(String.format("Executing CQL statement [%s]", cql));
167167
}
168168

169169
CompletionStage<T> results = getCurrentSession().executeAsync(applyStatementSettings(newStatement(cql)))
@@ -285,7 +285,7 @@ public <T> ListenableFuture<T> query(Statement<?> statement, AsyncResultSetExtra
285285

286286
try {
287287
if (logger.isDebugEnabled()) {
288-
logger.debug("Executing statement [{}]", QueryExtractorDelegate.getCql(statement));
288+
logger.debug(String.format("Executing statement [%s]", toCql(statement)));
289289
}
290290

291291
CompletionStage<T> results = getCurrentSession() //
@@ -294,9 +294,9 @@ public <T> ListenableFuture<T> query(Statement<?> statement, AsyncResultSetExtra
294294
.thenCompose(ListenableFuture::completable);
295295

296296
return new CassandraFutureAdapter<>(results,
297-
ex -> translateExceptionIfPossible("Query", statement.toString(), ex));
297+
ex -> translateExceptionIfPossible("Query", toCql(statement), ex));
298298
} catch (DriverException e) {
299-
throw translateException("Query", statement.toString(), e);
299+
throw translateException("Query", toCql(statement), e);
300300
}
301301
}
302302

@@ -446,7 +446,7 @@ public <T> ListenableFuture<T> execute(AsyncPreparedStatementCreator preparedSta
446446
toCql(preparedStatementCreator), ex);
447447
try {
448448
if (logger.isDebugEnabled()) {
449-
logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator);
449+
logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
450450
}
451451

452452
CqlSession currentSession = getCurrentSession();
@@ -517,15 +517,15 @@ public <T> ListenableFuture<T> query(AsyncPreparedStatementCreator preparedState
517517
try {
518518

519519
if (logger.isDebugEnabled()) {
520-
logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator);
520+
logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
521521
}
522522

523523
CqlSession session = getCurrentSession();
524524

525525
ListenableFuture<Statement<?>> statementFuture = new MappingListenableFutureAdapter<>(
526526
preparedStatementCreator.createPreparedStatement(session), preparedStatement -> {
527527
if (logger.isDebugEnabled()) {
528-
logger.debug("Executing prepared statement [{}]", QueryExtractorDelegate.getCql(preparedStatement));
528+
logger.debug(String.format("Executing prepared statement [%s]", toCql(preparedStatement)));
529529
}
530530

531531
return applyStatementSettings(psb != null ? psb.bindValues(preparedStatement) : preparedStatement.bind());

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
import java.util.stream.Stream;
2626
import java.util.stream.StreamSupport;
2727

28-
import org.springframework.dao.DataAccessException;
29-
import org.springframework.dao.support.DataAccessUtils;
30-
import org.springframework.data.cassandra.SessionFactory;
31-
import org.springframework.lang.Nullable;
32-
import org.springframework.util.Assert;
33-
3428
import com.datastax.oss.driver.api.core.CqlSession;
3529
import com.datastax.oss.driver.api.core.DriverException;
3630
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
@@ -40,6 +34,12 @@
4034
import com.datastax.oss.driver.api.core.cql.Statement;
4135
import com.datastax.oss.driver.api.core.metadata.Node;
4236

37+
import org.springframework.dao.DataAccessException;
38+
import org.springframework.dao.support.DataAccessUtils;
39+
import org.springframework.data.cassandra.SessionFactory;
40+
import org.springframework.lang.Nullable;
41+
import org.springframework.util.Assert;
42+
4343
/**
4444
* <b>This is the central class in the CQL core package.</b> It simplifies the use of CQL and helps to avoid common
4545
* errors. It executes core CQL workflow, leaving application code to provide CQL and extract results. This class
@@ -166,7 +166,7 @@ public <T> T query(String cql, ResultSetExtractor<T> resultSetExtractor) throws
166166

167167
try {
168168
if (logger.isDebugEnabled()) {
169-
logger.debug("Executing CQL statement [{}]", cql);
169+
logger.debug(String.format("Executing CQL statement [%s]", cql));
170170
}
171171

172172
Statement<?> statement = applyStatementSettings(newStatement(cql));
@@ -292,12 +292,12 @@ public <T> T query(Statement<?> statement, ResultSetExtractor<T> resultSetExtrac
292292

293293
try {
294294
if (logger.isDebugEnabled()) {
295-
logger.debug("Executing statement [{}]", QueryExtractorDelegate.getCql(statement));
295+
logger.debug(String.format("Executing statement [%s]", toCql(statement)));
296296
}
297297

298298
return resultSetExtractor.extractData(getCurrentSession().execute(applyStatementSettings(statement)));
299299
} catch (DriverException e) {
300-
throw translateException("Query", statement.toString(), e);
300+
throw translateException("Query", toCql(statement), e);
301301
}
302302
}
303303

@@ -453,7 +453,7 @@ public <T> T execute(PreparedStatementCreator preparedStatementCreator, Prepared
453453

454454
try {
455455
if (logger.isDebugEnabled()) {
456-
logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator);
456+
logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
457457
}
458458

459459
CqlSession session = getCurrentSession();
@@ -523,15 +523,15 @@ public <T> T query(PreparedStatementCreator preparedStatementCreator, @Nullable
523523

524524
try {
525525
if (logger.isDebugEnabled()) {
526-
logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator);
526+
logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
527527
}
528528

529529
CqlSession session = getCurrentSession();
530530

531531
PreparedStatement preparedStatement = preparedStatementCreator.createPreparedStatement(session);
532532

533533
if (logger.isDebugEnabled()) {
534-
logger.debug("Executing prepared statement [{}]", QueryExtractorDelegate.getCql(preparedStatement));
534+
logger.debug(String.format("Executing prepared statement [%s]", QueryExtractorDelegate.getCql(preparedStatement)));
535535
}
536536

537537
Statement<?> boundStatement = applyStatementSettings(

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core.cql;
1717

18+
import com.datastax.oss.driver.api.core.DriverException;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021

@@ -25,8 +26,6 @@
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728

28-
import com.datastax.oss.driver.api.core.DriverException;
29-
3029
/**
3130
* Base class for {@link ReactiveCqlTemplate} and other CQL-accessing DAO helpers, defining common properties such as
3231
* {@link ReactiveSessionFactory} and exception translator.
@@ -149,4 +148,17 @@ protected DataAccessException translate(String task, @Nullable String cql, Runti
149148

150149
return getExceptionTranslator().translate(task, cql, ex);
151150
}
151+
152+
/**
153+
* Determine CQL from potential provider object.
154+
*
155+
* @param cqlProvider object that's potentially a {@link CqlProvider}
156+
* @return the CQL string, or {@literal null}
157+
* @see CqlProvider
158+
* @since 3.2.7
159+
*/
160+
@Nullable
161+
protected static String toCql(@Nullable Object cqlProvider) {
162+
return QueryExtractorDelegate.getCql(cqlProvider);
163+
}
152164
}

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

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,9 @@
1515
*/
1616
package org.springframework.data.cassandra.core.cql;
1717

18-
import reactor.core.publisher.Flux;
19-
import reactor.core.publisher.Mono;
20-
2118
import java.util.Map;
2219
import java.util.function.Function;
2320

24-
import org.reactivestreams.Publisher;
25-
26-
import org.springframework.dao.DataAccessException;
27-
import org.springframework.dao.support.DataAccessUtils;
28-
import org.springframework.data.cassandra.ReactiveResultSet;
29-
import org.springframework.data.cassandra.ReactiveSession;
30-
import org.springframework.data.cassandra.ReactiveSessionFactory;
31-
import org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory;
32-
import org.springframework.lang.Nullable;
33-
import org.springframework.util.Assert;
34-
3521
import com.datastax.oss.driver.api.core.ConsistencyLevel;
3622
import com.datastax.oss.driver.api.core.CqlIdentifier;
3723
import com.datastax.oss.driver.api.core.CqlSession;
@@ -43,6 +29,18 @@
4329
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
4430
import com.datastax.oss.driver.api.core.cql.Statement;
4531
import com.datastax.oss.driver.api.core.retry.RetryPolicy;
32+
import org.reactivestreams.Publisher;
33+
import reactor.core.publisher.Flux;
34+
import reactor.core.publisher.Mono;
35+
36+
import org.springframework.dao.DataAccessException;
37+
import org.springframework.dao.support.DataAccessUtils;
38+
import org.springframework.data.cassandra.ReactiveResultSet;
39+
import org.springframework.data.cassandra.ReactiveSession;
40+
import org.springframework.data.cassandra.ReactiveSessionFactory;
41+
import org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory;
42+
import org.springframework.lang.Nullable;
43+
import org.springframework.util.Assert;
4644

4745
/**
4846
* <b>This is the central class in the CQL core package for reactive Cassandra data access.</b> It simplifies the use of
@@ -302,7 +300,7 @@ public <T> Flux<T> execute(ReactiveSessionCallback<T> action) throws DataAccessE
302300

303301
Assert.notNull(action, "Callback object must not be null");
304302

305-
return createFlux(action).onErrorMap(translateException("ReactiveSessionCallback", getCql(action)));
303+
return createFlux(action).onErrorMap(translateException("ReactiveSessionCallback", toCql(action)));
306304
}
307305

308306
// -------------------------------------------------------------------------
@@ -439,11 +437,12 @@ public <T> Flux<T> query(Statement<?> statement, ReactiveResultSetExtractor<T> r
439437
return createFlux(statement, (session, stmt) -> {
440438

441439
if (logger.isDebugEnabled()) {
442-
logger.debug("Executing statement [{}]", QueryExtractorDelegate.getCql(statement));
440+
logger.debug(String.format("Executing statement [%s]", toCql(statement)));
443441
}
444442

445-
return session.execute(applyStatementSettings(statement)).flatMapMany(rse::extractData);
446-
}).onErrorMap(translateException("Query", statement.toString()));
443+
return session.execute(applyStatementSettings(statement))
444+
.flatMapMany(rse::extractData);
445+
}).onErrorMap(translateException("Query", toCql(statement)));
447446
}
448447

449448
/* (non-Javadoc)
@@ -506,17 +505,17 @@ public Mono<ReactiveResultSet> queryForResultSet(Statement<?> statement) throws
506505
return createMono(statement, (session, executedStatement) -> {
507506

508507
if (logger.isDebugEnabled()) {
509-
logger.debug("Executing statement [{}]", QueryExtractorDelegate.getCql(statement));
508+
logger.debug(String.format("Executing statement [%s]", toCql(statement)));
510509
}
511510

512511
return session.execute(applyStatementSettings(executedStatement));
513-
}).onErrorMap(translateException("QueryForResultSet", statement.toString()));
512+
}).onErrorMap(translateException("QueryForResultSet", toCql(statement)));
514513
}
515514

516515
@Override
517516
public Flux<Row> queryForRows(Statement<?> statement) throws DataAccessException {
518517
return queryForResultSet(statement).flatMapMany(ReactiveResultSet::rows)
519-
.onErrorMap(translateException("QueryForRows", statement.toString()));
518+
.onErrorMap(translateException("QueryForRows", toCql(statement)));
520519
}
521520

522521
// -------------------------------------------------------------------------
@@ -535,10 +534,11 @@ public <T> Flux<T> execute(ReactivePreparedStatementCreator psc, ReactivePrepare
535534

536535
return createFlux(session -> {
537536

538-
logger.debug("Preparing statement [{}] using {}", getCql(psc), psc);
537+
logger.debug(String.format("Preparing statement [%s] using %s", toCql(psc), psc));
539538

540-
return psc.createPreparedStatement(session).flatMapMany(ps -> action.doInPreparedStatement(session, ps));
541-
}).onErrorMap(translateException("ReactivePreparedStatementCallback", getCql(psc)));
539+
return psc.createPreparedStatement(session)
540+
.flatMapMany(ps -> action.doInPreparedStatement(session, ps));
541+
}).onErrorMap(translateException("ReactivePreparedStatementCallback", toCql(psc)));
542542
}
543543

544544
/* (non-Javadoc)
@@ -569,15 +569,15 @@ public <T> Flux<T> query(ReactivePreparedStatementCreator psc,
569569
return execute(psc, (session, preparedStatement) -> Mono.just(preparedStatement).flatMapMany(pps -> {
570570

571571
if (logger.isDebugEnabled()) {
572-
logger.debug("Executing prepared statement [{}]", QueryExtractorDelegate.getCql(preparedStatement));
572+
logger.debug(String.format("Executing prepared statement [%s]", toCql(preparedStatement)));
573573
}
574574

575575
BoundStatement boundStatement = (preparedStatementBinder != null
576576
? preparedStatementBinder.bindValues(preparedStatement)
577577
: preparedStatement.bind());
578578

579579
return session.execute(applyStatementSettings(boundStatement));
580-
}).flatMap(rse::extractData)).onErrorMap(translateException("Query", getCql(psc)));
580+
}).flatMap(rse::extractData)).onErrorMap(translateException("Query", toCql(psc)));
581581
}
582582

583583
/* (non-Javadoc)
@@ -737,7 +737,7 @@ public Flux<Boolean> execute(String cql, Publisher<Object[]> args) throws DataAc
737737
return execute(newReactivePreparedStatementCreator(cql), (session, ps) -> Flux.from(args).flatMap(objects -> {
738738

739739
if (logger.isDebugEnabled()) {
740-
logger.debug("Executing prepared CQL statement [{}]", cql);
740+
logger.debug(String.format("Executing prepared CQL statement [%s]", cql));
741741
}
742742

743743
BoundStatement boundStatement = newArgPreparedStatementBinder(objects).bindValues(ps);
@@ -908,18 +908,6 @@ private Mono<ReactiveSession> getSession() {
908908
return sessionFactory.getSession();
909909
}
910910

911-
/**
912-
* Determine CQL from potential provider object.
913-
*
914-
* @param cqlProvider object that's potentially a {@link CqlProvider}
915-
* @return the CQL string, or {@literal null}
916-
* @see CqlProvider
917-
*/
918-
@Nullable
919-
private static String getCql(@Nullable Object cqlProvider) {
920-
return QueryExtractorDelegate.getCql(cqlProvider);
921-
}
922-
923911
static class SimpleReactivePreparedStatementCreator implements ReactivePreparedStatementCreator, CqlProvider {
924912

925913
private final SimpleStatement statement;

0 commit comments

Comments
 (0)