Search in sources :

Example 1 with PagingState

use of com.datastax.oss.driver.api.core.cql.PagingState in project java-driver by datastax.

the class PagingStateIT method should_inject_in_simple_statement_with_custom_codecs.

@Test
public void should_inject_in_simple_statement_with_custom_codecs() {
    try (CqlSession session = (CqlSession) SessionUtils.baseBuilder().addTypeCodecs(new IntWrapperCodec()).addContactEndPoints(CCM_RULE.getContactPoints()).withKeyspace(SESSION_RULE.keyspace()).build()) {
        SimpleStatement statement = SimpleStatement.newInstance("SELECT * FROM foo WHERE k = ?", new IntWrapper(1)).setPageSize(15);
        ResultSet resultSet = session.execute(statement);
        assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(15);
        assertThat(resultSet.isFullyFetched()).isFalse();
        PagingState pagingState = resultSet.getExecutionInfo().getSafePagingState();
        // setPagingState() cannot find the custom codec.
        try {
            @SuppressWarnings("unused") SimpleStatement ignored = statement.setPagingState(pagingState);
            fail("Expected a CodecNotFoundException");
        } catch (CodecNotFoundException e) {
        // expected
        }
        resultSet = session.execute(statement.setPagingState(pagingState, session));
        assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(5);
        assertThat(resultSet.isFullyFetched()).isTrue();
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) CqlSession(com.datastax.oss.driver.api.core.CqlSession) PagingState(com.datastax.oss.driver.api.core.cql.PagingState) Test(org.junit.Test)

Example 2 with PagingState

use of com.datastax.oss.driver.api.core.cql.PagingState in project java-driver by datastax.

the class PagingStateIT method should_fail.

private void should_fail(String query1, int value1, String query2, int value2) {
    CqlSession session = SESSION_RULE.session();
    BoundStatement boundStatement1 = session.prepare(SimpleStatement.newInstance(query1).setPageSize(15)).bind(value1);
    ResultSet resultSet = session.execute(boundStatement1);
    PagingState pagingState = resultSet.getExecutionInfo().getSafePagingState();
    @SuppressWarnings("ResultOfMethodCallIgnored") Throwable t = catchThrowable(() -> session.prepare(SimpleStatement.newInstance(query2).setPageSize(15)).bind(value2).setPagingState(pagingState));
    assertThat(t).isInstanceOf(IllegalArgumentException.class);
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PagingState(com.datastax.oss.driver.api.core.cql.PagingState)

Example 3 with PagingState

use of com.datastax.oss.driver.api.core.cql.PagingState in project java-driver by datastax.

the class PagingStateIT method should_extract_and_reuse.

private void should_extract_and_reuse(UnaryOperator<PagingState> transformation) {
    CqlSession session = SESSION_RULE.session();
    BoundStatement boundStatement = session.prepare(SimpleStatement.newInstance("SELECT * FROM foo WHERE k = ?").setPageSize(15)).bind(1);
    ResultSet resultSet = session.execute(boundStatement);
    assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(15);
    assertThat(resultSet.isFullyFetched()).isFalse();
    PagingState pagingState = transformation.apply(resultSet.getExecutionInfo().getSafePagingState());
    assertThat(pagingState.matches(boundStatement)).isTrue();
    resultSet = session.execute(boundStatement.setPagingState(pagingState));
    assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(5);
    assertThat(resultSet.isFullyFetched()).isTrue();
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PagingState(com.datastax.oss.driver.api.core.cql.PagingState)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 PagingState (com.datastax.oss.driver.api.core.cql.PagingState)3 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)3 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Test (org.junit.Test)1