Class CqlTemplate

java.lang.Object
org.springframework.data.cassandra.core.cql.CassandraAccessor
org.springframework.data.cassandra.core.cql.CqlTemplate
All Implemented Interfaces:
InitializingBean, CqlOperations

public class CqlTemplate extends CassandraAccessor implements CqlOperations
This is the central class in the CQL core package. It simplifies the use of CQL and helps to avoid common errors. It executes core CQL workflow, leaving application code to provide CQL and extract results. This class executes CQL queries or updates, initiating iteration over ResultSets and catching RuntimeException exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package.

Code using this class need only implement callback interfaces, giving them a clearly defined contract. The PreparedStatementCreator callback interface creates a prepared statement given a Connection, providing CQL and any necessary parameters. The ResultSetExtractor interface extracts values from a ResultSet. See also PreparedStatementBinder and RowMapper for two popular alternative callback interfaces.

Can be used within a service implementation via direct instantiation with a CqlSession reference, or get prepared in an application context and given to services as bean reference. Note: The CqlSession should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.

Because this class is parameterizable by the callback interfaces and the PersistenceExceptionTranslator interface, there should be no need to subclass it.

All CQL operations performed by this class are logged at debug level, using "org.springframework.data.cassandra.core.cqlTemplate" as log category.

NOTE: An instance of this class is thread-safe once configured.

Author:
David Webb, Matthew Adams, Ryan Scheidter, Antoine Toulme, John Blum, Mark Paluch, Mike Barlotta
See Also:
  • Constructor Details

  • Method Details

    • execute

      public <T> T execute(SessionCallback<T> action) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a CQL data access operation, implemented as callback action working on a CqlSession. This allows for implementing arbitrary data access operations, within Spring's managed CQL environment: that is, converting CQL DriverExceptions into Spring's DataAccessException hierarchy.

      The callback action can return a result object, for example a domain object or a collection of domain objects.

      Specified by:
      execute in interface CqlOperations
      Parameters:
      action - the callback object that specifies the action.
      Returns:
      a result object returned by the action, or null.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • execute

      public boolean execute(String cql) throws DataAccessException
      Description copied from interface: CqlOperations
      Issue a single CQL execute, typically a DDL statement, insert, update or delete statement.
      Specified by:
      execute in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      boolean value whether the statement was applied.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      @Nullable public <T> T query(String cql, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, reading the ResultSet with a ResultSetExtractor.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      resultSetExtractor - object that will extract all rows of results, must not be null.
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • query

      public void query(String cql, RowCallbackHandler rowCallbackHandler) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, reading the ResultSet on a per-row basis with a RowCallbackHandler.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • query

      public <T> List<T> query(String cql, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, mapping each row to a Java object via a RowMapper.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result List, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForList

      public List<Map<String,Object>> queryForList(String cql) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result List, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array.

      The results will be mapped to a List (one item for each row) of Maps (one entry for each column using the column name as the key). Each item in the List will be of the form returned by this interface's queryForMap() methods.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      a List that contains a Map per row.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForList

      public <T> List<T> queryForList(String cql, Class<T> elementType) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result List, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array.

      The results will be mapped to a List (one item for each row) of result objects, each of them matching the specified element type.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      elementType - the required type of element in the result List (for example, Integer.class), must not be null.
      Returns:
      a List of objects that match the specified element type.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForMap

      public Map<String,Object> queryForMap(String cql) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result Map, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForMap(String, Object...) method with null as argument array.

      The query is expected to be a single row query; the result row will be mapped to a Map (one entry for each column, using the column name as the key).

      Specified by:
      queryForMap in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      the result Map (one entry for each column, using the column name as the key), must not be null.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(String cql, Class<T> requiredType) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result object, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForObject(String, Class, Object...) method with null as argument array.

      This method is useful for running static CQL with a known outcome. The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      requiredType - the type that the result object is expected to match, must not be null.
      Returns:
      the result object of the required type, or null in case of CQL NULL.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row, or does not return exactly one column in that row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(String cql, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, mapping a single result row to a Java object via a RowMapper.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForObject(String, RowMapper, Object...) method with null as argument array.

      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the single mapped object.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForResultSet

      public com.datastax.oss.driver.api.core.cql.ResultSet queryForResultSet(String cql) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a ResultSet, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForResultSet method with null as argument array.

      The results will be mapped to an ResultSet.

      Specified by:
      queryForResultSet in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      a ResultSet representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForRows

      public Iterable<com.datastax.oss.driver.api.core.cql.Row> queryForRows(String cql) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for Rows, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForResultSet method with null as argument array.

      The results will be mapped to Rows.

      Specified by:
      queryForRows in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      a Row representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • execute

      public boolean execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
      Description copied from interface: CqlOperations
      Issue a single CQL execute, typically a DDL statement, insert, update or delete statement.
      Specified by:
      execute in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      Returns:
      boolean value whether the statement was applied.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> T query(com.datastax.oss.driver.api.core.cql.Statement<?> statement, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, reading the ResultSet with a ResultSetExtractor.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      resultSetExtractor - object that will extract all rows of results, must not be null.
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • query

      public void query(com.datastax.oss.driver.api.core.cql.Statement<?> statement, RowCallbackHandler rowCallbackHandler) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, reading the ResultSet on a per-row basis with a RowCallbackHandler.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • query

      public <T> List<T> query(com.datastax.oss.driver.api.core.cql.Statement<?> statement, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, mapping each row to a Java object via a RowMapper.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      query in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result List, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForStream

      public <T> Stream<T> queryForStream(com.datastax.oss.driver.api.core.cql.Statement<?> statement, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, mapping each row to a Java object via a RowMapper and turning it into an iterable Stream.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as argument array.

      Specified by:
      queryForStream in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result Stream, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForList

      public List<Map<String,Object>> queryForList(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result List, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array.

      The results will be mapped to a List (one item for each row) of Maps (one entry for each column using the column name as the key). Each item in the List will be of the form returned by this interface's queryForMap() methods.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      statement - static CQL Statement to execute, must not be empty or null.
      Returns:
      a List that contains a Map per row.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForList

      public <T> List<T> queryForList(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> elementType) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result List, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array.

      The results will be mapped to a List (one item for each row) of result objects, each of them matching the specified element type.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      elementType - the required type of element in the result List (for example, Integer.class), must not be null.
      Returns:
      a List of objects that match the specified element type.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForMap

      public Map<String,Object> queryForMap(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result Map, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForMap(String, Object...) method with null as argument array.

      The query is expected to be a single row query; the result row will be mapped to a Map (one entry for each column, using the column name as the key).

      Specified by:
      queryForMap in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      Returns:
      the result Map (one entry for each column, using the column name as the key), must not be null.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> requiredType) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a result object, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForObject(String, Class, Object...) method with null as argument array.

      This method is useful for running static CQL with a known outcome. The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      requiredType - the type that the result object is expected to match, must not be null.
      Returns:
      the result object of the required type, or null in case of CQL NULL.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row, or does not return exactly one column in that row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(com.datastax.oss.driver.api.core.cql.Statement<?> statement, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query given static CQL, mapping a single result row to a Java object via a RowMapper.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded CqlOperations.queryForObject(String, RowMapper, Object...) method with null as argument array.

      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the single mapped object.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForResultSet

      public com.datastax.oss.driver.api.core.cql.ResultSet queryForResultSet(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for a ResultSet, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForResultSet method with null as argument array.

      The results will be mapped to an ResultSet.

      Specified by:
      queryForResultSet in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      Returns:
      a ResultSet representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForRows

      public Iterable<com.datastax.oss.driver.api.core.cql.Row> queryForRows(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a query for Rows, given static CQL.

      Uses a CQL Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForResultSet method with null as argument array.

      The results will be mapped to Rows.

      Specified by:
      queryForRows in interface CqlOperations
      Parameters:
      statement - static CQL Statement, must not be null.
      Returns:
      a Row representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • execute

      public boolean execute(String cql, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Issue a single CQL operation (such as an insert, update or delete statement) via a prepared statement, binding the given arguments.
      Specified by:
      execute in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      boolean value whether the statement was applied.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • execute

      public boolean execute(String cql, @Nullable PreparedStatementBinder psb) throws DataAccessException
      Description copied from interface: CqlOperations
      Issue an statement using a PreparedStatementBinder to set bind parameters, with given CQL. Simpler than using a PreparedStatementCreator as this method will create the PreparedStatement: The PreparedStatementBinder just needs to set parameters.
      Specified by:
      execute in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      Returns:
      boolean value whether the statement was applied.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • execute

      @Nullable public <T> T execute(String cql, PreparedStatementCallback<T> action) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a CQL data access operation, implemented as callback action working on a CQL PreparedStatement. This allows for implementing arbitrary data access operations on a single Statement, within Spring's managed CQL environment: that is, participating in Spring-managed transactions and converting CQL DriverExceptions into Spring's DataAccessException hierarchy.

      The callback action can return a result object, for example a domain object or a collection of domain objects.

      Specified by:
      execute in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      action - callback object that specifies the action, must not be null.
      Returns:
      a result object returned by the action, or null
      Throws:
      DataAccessException - if there is any problem executing the query.
    • execute

      public boolean execute(PreparedStatementCreator preparedStatementCreator) throws DataAccessException
      Description copied from interface: CqlOperations
      Issue a single CQL execute operation (such as an insert, update or delete statement) using a PreparedStatementCreator to provide CQL and any required parameters.
      Specified by:
      execute in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that provides CQL and any necessary parameters, must not be null.
      Returns:
      boolean value whether the statement was applied.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • execute

      @Nullable public <T> T execute(PreparedStatementCreator preparedStatementCreator, PreparedStatementCallback<T> action) throws DataAccessException
      Description copied from interface: CqlOperations
      Execute a CQL data access operation, implemented as callback action working on a CQL PreparedStatement. This allows for implementing arbitrary data access operations on a single PreparedStatement, within Spring's managed CQL environment: that is, participating in Spring-managed transactions and converting CQL DriverExceptions into Spring's DataAccessException hierarchy.

      The callback action can return a result object, for example a domain object or a collection of domain objects.

      Specified by:
      execute in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      action - callback object that specifies the action, must not be null.
      Returns:
      a result object returned by the action, or null.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> T query(PreparedStatementCreator preparedStatementCreator, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement, reading the ResultSet with a ResultSetExtractor.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      resultSetExtractor - object that will extract results, must not be null.
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public void query(PreparedStatementCreator preparedStatementCreator, RowCallbackHandler rowCallbackHandler) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement, reading the ResultSet on a per-row basis with a RowCallbackHandler.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> List<T> query(PreparedStatementCreator preparedStatementCreator, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement, mapping each row to a Java object via a RowMapper.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result List, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • queryForStream

      public <T> Stream<T> queryForStream(PreparedStatementCreator preparedStatementCreator, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement, mapping each row to a Java object via a RowMapper and turning it into an iterable Stream.
      Specified by:
      queryForStream in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result Stream, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      @Nullable public <T> T query(PreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement and a PreparedStatementBinder implementation that knows how to bind values to the query, reading the ResultSet with a ResultSetExtractor.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      resultSetExtractor - object that will extract results, must not be null.
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public void query(PreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, RowCallbackHandler rowCallbackHandler) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement and a PreparedStatementBinder implementation that knows how to bind values to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> List<T> query(PreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement and a PreparedStatementBinder implementation that knows how to bind values to the query, mapping each row to a Java object via a RowMapper.
      Specified by:
      query in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result List, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • queryForStream

      public <T> Stream<T> queryForStream(PreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement and a PreparedStatementBinder implementation that knows how to bind values to the query, mapping each row to a Java object via a RowMapper and turning it into an iterable Stream.
      Specified by:
      queryForStream in interface CqlOperations
      Parameters:
      preparedStatementCreator - object that can create a PreparedStatement given a CqlSession, must not be null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result Stream, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> T query(String cql, ResultSetExtractor<T> resultSetExtractor, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, reading the ResultSet with a ResultSetExtractor.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      resultSetExtractor - object that will extract results, must not be null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public void query(String cql, RowCallbackHandler rowCallbackHandler, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type)
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> List<T> query(String cql, RowMapper<T> rowMapper, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowMapper - object that will map one object per row
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type)
      Returns:
      the result List, containing mapped objects
      Throws:
      DataAccessException - if there is any problem executing the query.
    • queryForStream

      public <T> Stream<T> queryForStream(String cql, RowMapper<T> rowMapper, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper and turning it into an iterable Stream.
      Specified by:
      queryForStream in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowMapper - object that will map one object per row
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type)
      Returns:
      the result Stream, containing mapped objects
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> T query(String cql, @Nullable PreparedStatementBinder psb, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException
      Description copied from interface: CqlOperations
      Query using a prepared statement, reading the ResultSet with a ResultSetExtractor.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      resultSetExtractor - object that will extract results, must not be null.
      Returns:
      an arbitrary result object, as returned by the ResultSetExtractor.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public void query(String cql, @Nullable PreparedStatementBinder psb, RowCallbackHandler rowCallbackHandler) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a PreparedStatementBinder implementation that knows how to bind values to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      rowCallbackHandler - object that will extract results, one row at a time, must not be null.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • query

      public <T> List<T> query(String cql, @Nullable PreparedStatementBinder psb, RowMapper<T> rowMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a PreparedStatementBinder implementation that knows how to bind values to the query, mapping each row to a Java object via a RowMapper.
      Specified by:
      query in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      psb - object that knows how to set values on the prepared statement. If this is null, the CQL will be assumed to contain no bind parameters. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
      rowMapper - object that will map one object per row, must not be null.
      Returns:
      the result List, containing mapped objects.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • queryForList

      public List<Map<String,Object>> queryForList(String cql, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting a result List.

      The results will be mapped to a List (one item for each row) of Maps (one entry for each column, using the column name as the key). Each item in the List will be of the form returned by this interface's queryForMap() methods.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      a List that contains a Map per row
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForList

      public <T> List<T> queryForList(String cql, Class<T> elementType, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting a result List.

      The results will be mapped to a List (one item for each row) of result objects, each of them matching the specified element type.

      Specified by:
      queryForList in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      elementType - the required type of element in the result List (for example, Integer.class), must not be null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      a List of objects that match the specified element type.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForMap

      public Map<String,Object> queryForMap(String cql, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting a result Map. The queryForMap() methods defined by this interface are appropriate when you don't have a domain model. Otherwise, consider using one of the queryForObject() methods.

      The query is expected to be a single row query; the result row will be mapped to a Map (one entry for each column, using the column name as the key).

      Specified by:
      queryForMap in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      the result Map (one entry for each column, using the column name as the key).
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(String cql, Class<T> requiredType, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting a result object.

      The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      requiredType - the type that the result object is expected to match, must not be null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type)
      Returns:
      the result object of the required type, or null in case of CQL NULL.
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row, or does not return exactly one column in that row.
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForObject

      public <T> T queryForObject(String cql, RowMapper<T> rowMapper, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, mapping a single result row to a Java object via a RowMapper.
      Specified by:
      queryForObject in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      rowMapper - object that will map one object per row, must not be null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type)
      Returns:
      the single mapped object
      Throws:
      IncorrectResultSizeDataAccessException - if the query does not return exactly one row.
      DataAccessException - if there is any problem executing the query.
    • queryForResultSet

      public com.datastax.oss.driver.api.core.cql.ResultSet queryForResultSet(String cql, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting a ResultSet.

      The results will be mapped to an ResultSet.

      Specified by:
      queryForResultSet in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      a ResultSet representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • queryForRows

      public Iterable<com.datastax.oss.driver.api.core.cql.Row> queryForRows(String cql, Object... args) throws DataAccessException
      Description copied from interface: CqlOperations
      Query given CQL to create a prepared statement from CQL and a list of arguments to bind to the query, expecting Rows.

      The results will be mapped to Rows.

      Specified by:
      queryForRows in interface CqlOperations
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding CQL type).
      Returns:
      a Row representation.
      Throws:
      DataAccessException - if there is any problem executing the query.
      See Also:
    • describeRing

      public List<RingMember> describeRing() throws DataAccessException
      Description copied from interface: CqlOperations
      Describe the current Ring. This uses the provided RingMemberHostMapper to provide the basics of the Cassandra Ring topology.
      Specified by:
      describeRing in interface CqlOperations
      Returns:
      The list of ring tokens that are active in the cluster
      Throws:
      DataAccessException - if there is any problem executing the query.
    • describeRing

      public <T> Collection<T> describeRing(HostMapper<T> hostMapper) throws DataAccessException
      Description copied from interface: CqlOperations
      Describe the current Ring. Application code must provide its own HostMapper implementation to process the lists of hosts returned by the Cassandra Cluster Metadata.
      Specified by:
      describeRing in interface CqlOperations
      Parameters:
      hostMapper - The implementation to use for host mapping.
      Returns:
      Collection generated by the provided HostMapper.
      Throws:
      DataAccessException - if there is any problem executing the query.
    • translateException

      protected DataAccessException translateException(String task, @Nullable String cql, RuntimeException RuntimeException)
      Translate the given RuntimeException into a generic DataAccessException.
      Parameters:
      task - readable text describing the task being attempted
      cql - CQL query or update that caused the problem (may be null)
      RuntimeException - the offending RuntimeException.
      Returns:
      the exception translation Function
      See Also:
    • newPreparedStatementCreator

      protected PreparedStatementCreator newPreparedStatementCreator(String cql)
      Create a new CQL-based PreparedStatementCreator using the CQL passed in. By default, we'll create an SimplePreparedStatementCreator. This method allows for the creation to be overridden by subclasses.
      Parameters:
      cql - static CQL to execute, must not be empty or null.
      Returns:
      the new PreparedStatementCreator to use
    • newResultSetExtractor

      protected CqlTemplate.RowCallbackHandlerResultSetExtractor newResultSetExtractor(RowCallbackHandler rowCallbackHandler)
      Constructs a new instance of the ResultSetExtractor adapting the given RowCallbackHandler.
      Parameters:
      rowCallbackHandler - RowCallbackHandler to adapt as a ResultSetExtractor.
      Returns:
      a ResultSetExtractor implementation adapting an instance of the RowCallbackHandler.
      See Also:
    • newResultSetExtractor

      protected <T> RowMapperResultSetExtractor<T> newResultSetExtractor(RowMapper<T> rowMapper)
      Constructs a new instance of the ResultSetExtractor adapting the given RowMapper.
      Parameters:
      rowMapper - RowMapper to adapt as a ResultSetExtractor.
      Returns:
      a ResultSetExtractor implementation adapting an instance of the RowMapper.
      See Also:
    • newResultSetExtractor

      protected <T> RowMapperResultSetExtractor<T> newResultSetExtractor(RowMapper<T> rowMapper, int rowsExpected)
      Constructs a new instance of the ResultSetExtractor adapting the given RowMapper.
      Parameters:
      rowMapper - RowMapper to adapt as a ResultSetExtractor.
      rowsExpected - number of expected rows in the ResultSet.
      Returns:
      a ResultSetExtractor implementation adapting an instance of the RowMapper.
      See Also:
    • newStreamExtractor

      protected <T> ResultSetExtractor<Stream<T>> newStreamExtractor(RowMapper<T> rowMapper)
      Constructs a new instance of the ResultSetExtractor adapting the given RowMapper.
      Parameters:
      rowMapper - RowMapper to adapt as a ResultSetExtractor.
      Returns:
      a ResultSetExtractor implementation adapting an instance of the RowMapper.
      Since:
      3.1
      See Also: