Class StatementBuilder<S extends com.datastax.oss.driver.api.querybuilder.BuildableQuery>
java.lang.Object
org.springframework.data.cassandra.core.cql.util.StatementBuilder<S>
- Type Parameters:
S- Statement type
public class StatementBuilder<S extends com.datastax.oss.driver.api.querybuilder.BuildableQuery>
extends Object
Functional builder for Cassandra
statements. Statements are built by applying
builder functions that get applied when building the actual
statement. The StatementBuilder provides a mutable container for statement creation
allowing a functional declaration of actions that are necessary to build a statement. This class helps building CQL
statements as a BuildableQuery classes are typically immutable and require return value tracking across
methods that want to apply modifications to a statement.
Building a statement consists of three phases:
- Creation of the
StatementBuilderwith aquery stub - Functional declaration applying
builder functions,bind functionsandon build signals - Building the statement using
build()
query stub is used as base object for all built queries. Builder functions are
applied each time a statement is built allowing to build multiple statement instances while evolving the actual
statement.
The builder can be used for structural evolution and value evolution of statements. Values are bound through
binding functions that accept the statement and a TermFactory. Values can be bound
inline or through bind markers when building the statement. All
functions are applied in the order of their declaration.
All methods returning StatementBuilder point to the same instance. This class is intended for internal use.
- Since:
- 3.0
- Author:
- Mark Paluch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBinding function.static enumEnumeration to represent how parameters are rendered. -
Method Summary
Modifier and TypeMethodDescription<R extends com.datastax.oss.driver.api.querybuilder.BuildableQuery>
StatementBuilder<S>Apply abuilder functionto the statement.bind(StatementBuilder.BindFunction<S> action) Apply aStatementBuilder.BindFunctionto the statement.com.datastax.oss.driver.api.core.cql.SimpleStatementbuild()Build astatementby applying builder and bind functions using the defaultCodecRegistryandStatementBuilder.ParameterHandling.BY_INDEXparameter rendering.com.datastax.oss.driver.api.core.cql.SimpleStatementbuild(StatementBuilder.ParameterHandling parameterHandling) Build astatementby applying builder and bind functions using the givenStatementBuilder.ParameterHandling.com.datastax.oss.driver.api.core.cql.SimpleStatementbuild(StatementBuilder.ParameterHandling parameterHandling, com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry codecRegistry) Build astatementby applying builder and bind functions using the givenCodecRegistryandStatementBuilder.ParameterHandling.static <S extends com.datastax.oss.driver.api.querybuilder.BuildableQuery>
StatementBuilder<S>of(S stub) Factory method used to create a newStatementBuilderwith the givenquery stub.Add behavior when the statement is built.transform(UnaryOperator<com.datastax.oss.driver.api.core.cql.SimpleStatement> mappingFunction) Add behavior after theSimpleStatementhas been built.
-
Method Details
-
of
public static <S extends com.datastax.oss.driver.api.querybuilder.BuildableQuery> StatementBuilder<S> of(S stub) Factory method used to create a newStatementBuilderwith the givenquery stub. The stub is used as base for the built query so each query inherits properties of this stub.- Type Parameters:
S- query type.- Parameters:
stub- thequery stubto use.- Returns:
- a
StatementBuilderfor the givenquery stub. - Throws:
IllegalArgumentException- if thequery stubis null.- See Also:
-
BuildableQuery
-
bind
Apply aStatementBuilder.BindFunctionto the statement. Bind functions are applied onbuild().- Parameters:
action- the bind function to be applied to the statement.- Returns:
thisStatementBuilder.
-
apply
public <R extends com.datastax.oss.driver.api.querybuilder.BuildableQuery> StatementBuilder<S> apply(Function<S, R> action) Apply abuilder functionto the statement. Builder functions are applied onbuild().- Parameters:
action- the builder function to be applied to the statement.- Returns:
thisStatementBuilder.
-
onBuild
public StatementBuilder<S> onBuild(Consumer<com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder> action) Add behavior when the statement is built. TheConsumergets invoked with aSimpleStatementBuilderallowing association of the final statement with additional settings. TheConsumeris applied onbuild().- Parameters:
action- theConsumerfunction that gets notified onbuild().- Returns:
thisStatementBuilder.
-
transform
public StatementBuilder<S> transform(UnaryOperator<com.datastax.oss.driver.api.core.cql.SimpleStatement> mappingFunction) Add behavior after theSimpleStatementhas been built. TheUnaryOperatorgets invoked with aSimpleStatementallowing association of the final statement with additional settings. TheUnaryOperatoris applied onbuild().- Parameters:
mappingFunction- theUnaryOperatorfunction that gets notified onbuild().- Returns:
thisStatementBuilder.
-
build
public com.datastax.oss.driver.api.core.cql.SimpleStatement build()Build astatementby applying builder and bind functions using the defaultCodecRegistryandStatementBuilder.ParameterHandling.BY_INDEXparameter rendering.- Returns:
- the built
SimpleStatement.
-
build
public com.datastax.oss.driver.api.core.cql.SimpleStatement build(StatementBuilder.ParameterHandling parameterHandling) Build astatementby applying builder and bind functions using the givenStatementBuilder.ParameterHandling.- Parameters:
parameterHandling-StatementBuilder.ParameterHandlingused to determine how to render parameters.- Returns:
- the built
SimpleStatement.
-
build
public com.datastax.oss.driver.api.core.cql.SimpleStatement build(StatementBuilder.ParameterHandling parameterHandling, com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry codecRegistry) Build astatementby applying builder and bind functions using the givenCodecRegistryandStatementBuilder.ParameterHandling.- Parameters:
parameterHandling-StatementBuilder.ParameterHandlingused to determine how to render parameters.codecRegistry- registry of Apache Cassandra codecs for converting to/from Java types and CQL types.- Returns:
- the built
SimpleStatement.
-