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 with 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:

  1. Creation of the StatementBuilder with a query stub
  2. Functional declaration applying builder functions, bind functions and on build signals
  3. Building the statement using build()
The initial 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 remain in the order of their declaration.

Inline rendering of parameters requires a CodecRegistry. A StatementBuilder can be created by providing a custom CodecRegistry. Otherwise, the registry falls back to CodecRegistry.DEFAULT.

All methods returning StatementBuilder point to the same instance. This class is intended for internal use.

Since:
3.0
Author:
Mark Paluch
  • 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 new StatementBuilder with the given query stub. The stub is used as base for the built query so each query inherits properties of this stub. This factory method initializes StatementBuilder with the default CodecRegistry.
      Type Parameters:
      S - query type.
      Parameters:
      stub - the query stub to use.
      Returns:
      a StatementBuilder for the given query stub.
      Throws:
      IllegalArgumentException - if the query stub is null.
      See Also:
      • BuildableQuery
    • of

      public static <S extends com.datastax.oss.driver.api.querybuilder.BuildableQuery> StatementBuilder<S> of(S stub, com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry registry)
      Factory method used to create a new StatementBuilder with the given query 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 - the query stub to use.
      registry - the default CodecRegistry to use for inline parameter rendering.
      Returns:
      a StatementBuilder for the given query stub.
      Throws:
      IllegalArgumentException - if the query stub is null.
      Since:
      4.4
      See Also:
      • BuildableQuery
    • bind

      Apply a StatementBuilder.BindFunction to the statement. Bind functions are applied on build().
      Parameters:
      action - the bind function to be applied to the statement.
      Returns:
      this StatementBuilder.
    • apply

      public <R extends com.datastax.oss.driver.api.querybuilder.BuildableQuery> StatementBuilder<S> apply(Function<S,R> action)
      Apply a builder function to the statement. Builder functions are applied on build().
      Parameters:
      action - the builder function to be applied to the statement.
      Returns:
      this StatementBuilder.
    • onBuild

      public StatementBuilder<S> onBuild(Consumer<com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder> action)
      Add behavior when the statement is built. The Consumer gets invoked with a SimpleStatementBuilder allowing association of the final statement with additional settings. The Consumer is applied on build().
      Parameters:
      action - the Consumer function that gets notified on build().
      Returns:
      this StatementBuilder.
    • transform

      public StatementBuilder<S> transform(UnaryOperator<com.datastax.oss.driver.api.core.cql.SimpleStatement> mappingFunction)
      Add behavior after the SimpleStatement has been built. The UnaryOperator gets invoked with a SimpleStatement allowing association of the final statement with additional settings. The UnaryOperator is applied on build().
      Parameters:
      mappingFunction - the UnaryOperator function that gets notified on build().
      Returns:
      this StatementBuilder.
    • build

      public com.datastax.oss.driver.api.core.cql.SimpleStatement build()
      Build a statement by applying builder and bind functions using the default CodecRegistry and StatementBuilder.ParameterHandling.BY_INDEX parameter rendering.
      Returns:
      the built SimpleStatement.
    • build

      public com.datastax.oss.driver.api.core.cql.SimpleStatement build(StatementBuilder.ParameterHandling parameterHandling)
      Build a statement by applying builder and bind functions using the given StatementBuilder.ParameterHandling.
      Parameters:
      parameterHandling - StatementBuilder.ParameterHandling used 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 a statement by applying builder and bind functions using the given CodecRegistry and StatementBuilder.ParameterHandling.
      Parameters:
      parameterHandling - StatementBuilder.ParameterHandling used 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.