Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8045003

Browse files
committed
DATACASS-594 Polishing
1 parent 8f2ed0c commit 8045003

File tree

7 files changed

+114
-91
lines changed

7 files changed

+114
-91
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class CassandraAccessor implements InitializingBean {
8585
*/
8686
private @Nullable ConsistencyLevel serialConsistencyLevel;
8787

88-
private @NullableSessionFactory sessionFactory;
88+
private SessionFactory sessionFactory;
8989

9090
/**
9191
* Ensures the Cassandra {@link CqlSession} and exception translator has been propertly set.
@@ -314,7 +314,6 @@ public void setSessionFactory(SessionFactory sessionFactory) {
314314
* @since 2.0
315315
* @see SessionFactory
316316
*/
317-
@Nullable
318317
public SessionFactory getSessionFactory() {
319318
return this.sessionFactory;
320319
}
@@ -361,11 +360,10 @@ protected Statement<?> applyStatementSettings(Statement<?> statement) {
361360
}
362361

363362
if (keyspace != null) {
364-
if (statementToUse instanceof BatchStatement) {
365-
statementToUse = ((BatchStatement) statementToUse).setKeyspace(keyspace);
366-
}
367-
if (statementToUse instanceof SimpleStatement) {
368-
statementToUse = ((SimpleStatement) statementToUse).setKeyspace(keyspace);
363+
if (statementToUse instanceof BatchStatement bs) {
364+
statementToUse = bs.setKeyspace(keyspace);
365+
} else if (statementToUse instanceof SimpleStatement ss) {
366+
statementToUse = ss.setKeyspace(keyspace);
369367
}
370368
}
371369

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ private CqlSession getCurrentSession() {
620620

621621
SessionFactory sessionFactory = getSessionFactory();
622622

623-
Assert.state(sessionFactory != null, "SessionFactory is null");
623+
Assert.notNull(sessionFactory, "SessionFactory is null");
624624

625625
return sessionFactory.getSession();
626626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.Optional;
6+
7+
import org.jetbrains.annotations.NotNull;
8+
import org.springframework.lang.NonNull;
9+
10+
import com.datastax.oss.driver.api.querybuilder.term.Term;
11+
import com.datastax.oss.driver.internal.querybuilder.CqlHelper;
12+
13+
/**
14+
* Represents an abstract collection like {@link Term} such as Set, List, Tuple in CQL
15+
*
16+
* @author Mikhail Polivakha
17+
*/
18+
public abstract class AbstractCollectionTerm implements Term {
19+
20+
@NonNull
21+
private final Collection<? extends Term> components;
22+
23+
/**
24+
* @return EnclosingLiterals that are used to render the collection of terms
25+
*/
26+
public abstract EnclosingLiterals enclosingLiterals();
27+
28+
public AbstractCollectionTerm(Collection<? extends Term> components) {
29+
this.components = Optional.ofNullable(components).orElse(Collections.emptySet());
30+
}
31+
32+
@Override
33+
public boolean isIdempotent() {
34+
return components.stream().allMatch(Term::isIdempotent);
35+
}
36+
37+
@Override
38+
public void appendTo(@NotNull StringBuilder builder) {
39+
EnclosingLiterals literals = this.enclosingLiterals();
40+
41+
if (components.isEmpty()) {
42+
builder.append(literals.prefix).append(literals.postfix);
43+
} else {
44+
CqlHelper.append(components, builder, literals.prefix, ",", literals.postfix);
45+
}
46+
}
47+
48+
protected static class EnclosingLiterals {
49+
50+
private final String prefix;
51+
private final String postfix;
52+
53+
protected EnclosingLiterals(String prefix, String postfix) {
54+
this.prefix = prefix;
55+
this.postfix = postfix;
56+
}
57+
58+
protected static EnclosingLiterals of(String prefix, String postfix) {
59+
return new EnclosingLiterals(prefix, postfix);
60+
}
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
5+
import com.datastax.oss.driver.api.querybuilder.term.Term;
6+
7+
public class ListTerm extends AbstractCollectionTerm {
8+
9+
public ListTerm(Collection<? extends Term> components) {
10+
super(components);
11+
}
12+
13+
@Override
14+
public EnclosingLiterals enclosingLiterals() {
15+
return EnclosingLiterals.of("[", "]");
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
5+
import com.datastax.oss.driver.api.querybuilder.term.Term;
6+
7+
public class SetTerm extends AbstractCollectionTerm {
8+
9+
public SetTerm(Collection<? extends Term> components) {
10+
super(components);
11+
}
12+
13+
@Override
14+
public EnclosingLiterals enclosingLiterals() {
15+
return EnclosingLiterals.of("{", "}");
16+
}
17+
}

‎spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/util/StatementBuilder.java

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.function.Function;
2626
import java.util.function.Supplier;
2727
import java.util.function.UnaryOperator;
28+
import java.util.stream.Collectors;
2829

2930
import org.springframework.lang.NonNull;
3031
import org.springframework.lang.Nullable;
@@ -42,7 +43,7 @@
4243
/**
4344
* Functional builder for Cassandra {@link BuildableQuery statements}. Statements are built by applying
4445
* {@link UnaryOperator builder functions} that get applied when {@link #build() building} the actual
45-
* {@link SimpleStatement statement}. The {@code StatementBuilder} provides a mutable container for statement creation
46+
* {@link SimpleStatement statement}. The {@link StatementBuilder} provides a mutable container for statement creation
4647
* allowing a functional declaration of actions that are necessary to build a statement. This class helps building CQL
4748
* statements as a {@link BuildableQuery} classes are typically immutable and require return value tracking across
4849
* methods that want to apply modifications to a statement.
@@ -289,13 +290,13 @@ public <T> T ifBoundOrInline(Function<Bindings, T> bindingFunction, Supplier<T>
289290

290291
private SimpleStatement build(SimpleStatementBuilder builder) {
291292

292-
SimpleStatement statmentToUse = onBuild(builder).build();
293+
SimpleStatement statementToUse = onBuild(builder).build();
293294

294295
for (UnaryOperator<SimpleStatement> operator : onBuilt) {
295-
statmentToUse = operator.apply(statmentToUse);
296+
statementToUse = operator.apply(statementToUse);
296297
}
297298

298-
return statmentToUse;
299+
return statementToUse;
299300
}
300301

301302
private SimpleStatementBuilder onBuild(SimpleStatementBuilder statementBuilder) {
@@ -308,26 +309,13 @@ private SimpleStatementBuilder onBuild(SimpleStatementBuilder statementBuilder)
308309
@SuppressWarnings("unchecked")
309310
private static Term toLiteralTerms(@Nullable Object value, CodecRegistry codecRegistry) {
310311

311-
if (value instanceof List) {
312+
if (value instanceof Collection<?> c) {
312313

313-
List<Term> terms = new ArrayList<>();
314+
List<Term> mappedTerms = c.stream()
315+
.map(o -> toLiteralTerms(o, codecRegistry))
316+
.toList();
314317

315-
for (Object o : (List<Object>) value) {
316-
terms.add(toLiteralTerms(o, codecRegistry));
317-
}
318-
319-
return new ListTerm(terms);
320-
}
321-
322-
if (value instanceof Set) {
323-
324-
List<Term> terms = new ArrayList<>();
325-
326-
for (Object o : (Set<Object>) value) {
327-
terms.add(toLiteralTerms(o, codecRegistry));
328-
}
329-
330-
return new SetTerm(terms);
318+
return c instanceof Set ? new SetTerm(mappedTerms) : new ListTerm(mappedTerms);
331319
}
332320

333321
if (value instanceof Map) {
@@ -387,66 +375,6 @@ public enum ParameterHandling {
387375
BY_NAME
388376
}
389377

390-
static class ListTerm implements Term {
391-
392-
private final Collection<? extends Term> components;
393-
394-
public ListTerm(@NonNull Collection<? extends Term> components) {
395-
this.components = components;
396-
}
397-
398-
@Override
399-
public void appendTo(@NonNull StringBuilder builder) {
400-
401-
if (components.isEmpty()) {
402-
builder.append("[]");
403-
return;
404-
}
405-
406-
CqlHelper.append(components, builder, "[", ",", "]");
407-
}
408-
409-
@Override
410-
public boolean isIdempotent() {
411-
for (Term component : components) {
412-
if (!component.isIdempotent()) {
413-
return false;
414-
}
415-
}
416-
return true;
417-
}
418-
}
419-
420-
static class SetTerm implements Term {
421-
422-
private final Collection<? extends Term> components;
423-
424-
public SetTerm(@NonNull Collection<? extends Term> components) {
425-
this.components = components;
426-
}
427-
428-
@Override
429-
public void appendTo(@NonNull StringBuilder builder) {
430-
431-
if (components.isEmpty()) {
432-
builder.append("{}");
433-
return;
434-
}
435-
436-
CqlHelper.append(components, builder, "{", ",", "}");
437-
}
438-
439-
@Override
440-
public boolean isIdempotent() {
441-
for (Term component : components) {
442-
if (!component.isIdempotent()) {
443-
return false;
444-
}
445-
}
446-
return true;
447-
}
448-
}
449-
450378
static class MapTerm implements Term {
451379

452380
private final Map<? extends Term, ? extends Term> components;

‎spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/util/TermFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
import org.springframework.lang.Nullable;
2222

23+
import com.datastax.oss.driver.api.querybuilder.BindMarker;
2324
import com.datastax.oss.driver.api.querybuilder.term.Term;
2425

2526
/**
2627
* Factory for {@link Term} objects encapsulating a binding {@code value}. Classes implementing this factory interface
27-
* may return inline terms to render values as part of the query string, or bind markers to supply parameters on
28+
* may return inline {@link Term terms} to render values as part of the query string, or {@link BindMarker bind markers} to supply parameters on
2829
* statement creation/parameter binding.
2930
* <p>
3031
* A {@link TermFactory} is typically used with {@link StatementBuilder}.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /