Search in sources :

Example 71 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class ResultSetConversionStrategiesTest method testAll.

@Test
public void testAll() {
    ResultSetConversionStrategy strategy = ResultSetConversionStrategies.fromName("ALL");
    ResultSet resultSet = mock(ResultSet.class);
    List<Row> rows = Collections.nCopies(20, mock(Row.class));
    when(resultSet.all()).thenReturn(rows);
    Object body = strategy.getBody(resultSet);
    assertTrue(body instanceof List);
    assertSame(rows, body);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) List(java.util.List) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 72 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class ResultSetConversionStrategiesTest method testLimit.

@Test
public void testLimit() {
    ResultSetConversionStrategy strategy = ResultSetConversionStrategies.fromName("LIMIT_10");
    ResultSet resultSet = mock(ResultSet.class);
    List<Row> rows = Collections.nCopies(20, mock(Row.class));
    when(resultSet.iterator()).thenReturn(rows.iterator());
    Object body = strategy.getBody(resultSet);
    assertTrue(body instanceof List);
    assertEquals(10, ((List) body).size());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) List(java.util.List) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 73 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class CassandraProducer method execute.

/**
     * Execute CQL query using incoming message body has statement parameters.
     */
private ResultSet execute(Message message) {
    Object messageCql = message.getHeader(CassandraConstants.CQL_QUERY);
    // Convert Empty string to null
    if (messageCql instanceof String && ((String) messageCql).isEmpty()) {
        messageCql = null;
    }
    Object[] cqlParams = getCqlParams(message);
    ResultSet resultSet;
    Session session = getEndpoint().getSessionHolder().getSession();
    if (isPrepareStatements()) {
        resultSet = executePreparedStatement(session, messageCql, cqlParams);
    } else {
        resultSet = executeStatement(session, messageCql, cqlParams);
    }
    return resultSet;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Session(com.datastax.driver.core.Session)

Example 74 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class CassandraProducer method executePreparedStatement.

/**
     * Execute CQL as PreparedStatement
     */
private ResultSet executePreparedStatement(Session session, Object messageCql, Object[] cqlParams) {
    ResultSet resultSet;
    PreparedStatement lPreparedStatement;
    if (messageCql == null) {
        // URI CQL
        lPreparedStatement = this.preparedStatement;
    } else if (messageCql instanceof String) {
        // Message CQL
        lPreparedStatement = getEndpoint().prepareStatement((String) messageCql);
    } else if (messageCql instanceof RegularStatement) {
        // Message Statement
        lPreparedStatement = getEndpoint().getSession().prepare((RegularStatement) messageCql);
    } else {
        throw new IllegalArgumentException("Invalid " + CassandraConstants.CQL_QUERY + " header");
    }
    if (isEmpty(cqlParams)) {
        resultSet = session.execute(lPreparedStatement.bind());
    } else {
        resultSet = session.execute(lPreparedStatement.bind(cqlParams));
    }
    return resultSet;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement)

Example 75 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class CassandraProducer method executeStatement.

/**
     * Execute CQL as is
     */
private ResultSet executeStatement(Session session, Object messageCql, Object[] cqlParams) {
    ResultSet resultSet;
    String cql = null;
    RegularStatement statement = null;
    if (messageCql == null) {
        // URI CQL
        cql = getEndpoint().getCql();
    } else if (messageCql instanceof String) {
        // Message CQL
        cql = (String) messageCql;
    } else if (messageCql instanceof RegularStatement) {
        // Message Statement
        statement = (RegularStatement) messageCql;
    } else {
        throw new IllegalArgumentException("Invalid " + CassandraConstants.CQL_QUERY + " header");
    }
    if (statement != null) {
        resultSet = session.execute(statement);
    } else if (isEmpty(cqlParams)) {
        resultSet = session.execute(cql);
    } else {
        resultSet = session.execute(cql, cqlParams);
    }
    return resultSet;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) RegularStatement(com.datastax.driver.core.RegularStatement)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)95 Row (com.datastax.driver.core.Row)61 Test (org.junit.Test)43 ArrayList (java.util.ArrayList)17 BoundStatement (com.datastax.driver.core.BoundStatement)13 Session (com.datastax.driver.core.Session)12 Cluster (com.datastax.driver.core.Cluster)10 Statement (com.datastax.driver.core.Statement)8 List (java.util.List)8 PreparedStatement (com.datastax.driver.core.PreparedStatement)7 ByteBuffer (java.nio.ByteBuffer)5 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)5 BatchStatement (com.datastax.driver.core.BatchStatement)4 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 Tuple (io.vavr.Tuple)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 Entry (org.janusgraph.diskstorage.Entry)4