Search in sources :

Example 86 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project apex-malhar by apache.

the class AbstractUpsertOutputOperator method setDefaultsAndPrepareBoundStatement.

private BoundStatement setDefaultsAndPrepareBoundStatement(UpsertExecutionContext tuple) {
    UpsertExecutionContext.NullHandlingMutationStyle nullHandlingMutationStyle = tuple.getNullHandlingMutationStyle();
    if (UpsertExecutionContext.NullHandlingMutationStyle.UNDEFINED == nullHandlingMutationStyle) {
        nullHandlingMutationStyle = UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS;
    }
    boolean setNulls = true;
    if (nullHandlingMutationStyle != UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS) {
        setNulls = false;
    }
    UpsertExecutionContext.CollectionMutationStyle collectionMutationStyle = tuple.getCollectionMutationStyle();
    if ((collectionMutationStyle == null) || (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.UNDEFINED)) {
        tuple.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
    }
    UpsertExecutionContext.ListPlacementStyle listPlacementStyle = tuple.getListPlacementStyle();
    if ((listPlacementStyle == null) || (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.UNDEFINED)) {
        tuple.setListPlacementStyle(UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST);
    }
    PreparedStatement preparedStatement = resolvePreparedStatementForCurrentExecutionContext(tuple);
    BoundStatement stmnt = processPayloadForExecution(preparedStatement, tuple, setNulls);
    if ((tuple.isTtlOverridden()) || (connectionStateManager.isTTLSet())) {
        int ttlToUse = connectionStateManager.getDefaultTtlInSecs();
        if (tuple.isTtlOverridden()) {
            ttlToUse = tuple.getOverridingTTL();
        }
        stmnt.setInt(CassandraPreparedStatementGenerator.TTL_PARAM_NAME, ttlToUse);
    }
    if (tuple.isOverridingConsistencyLevelSet()) {
        ConsistencyLevel currentConsistencyLevel = tuple.getOverridingConsistencyLevel();
        if (currentConsistencyLevel.isSerial()) {
            stmnt.setSerialConsistencyLevel(tuple.getOverridingConsistencyLevel());
        } else {
            stmnt.setConsistencyLevel(tuple.getOverridingConsistencyLevel());
        }
    }
    LOG.debug("Executing statement " + preparedStatement.getQueryString());
    return stmnt;
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 87 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project apex-malhar by apache.

the class CassandraPOJOOutputOperator method getUpdateCommand.

/**
 * {@inheritDoc} <br/>
 * If statement/query is not specified by user, insert query is constructed from fileInfo object and table name.
 */
@Override
protected PreparedStatement getUpdateCommand() {
    PreparedStatement statement;
    if (query == null) {
        statement = prepareStatementFromFieldsAndTableName();
    } else {
        statement = store.getSession().prepare(query);
    }
    LOG.debug("Statement is: " + statement.getQueryString());
    return statement;
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement)

Example 88 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project apex-malhar by apache.

the class CassandraPreparedStatementGenerator method generatePreparedStatements.

public void generatePreparedStatements(Session session, Map<Long, PreparedStatement> preparedStatementTypes, String keyspaceName, String tableName) {
    Map<Long, String> stringsWithoutPKAndExistsClauses = generatePreparedStatementsQueryStrings(keyspaceName, tableName);
    String ifExistsClause = " IF EXISTS";
    Map<Long, String> finalSetOfQueryStrings = new HashMap<>();
    for (Long currentIndexPos : stringsWithoutPKAndExistsClauses.keySet()) {
        StringBuilder aQueryStub = new StringBuilder(stringsWithoutPKAndExistsClauses.get(currentIndexPos));
        buildWhereClauseForPrimaryKeys(aQueryStub);
        finalSetOfQueryStrings.put(currentIndexPos + getSlotIndexForMutationContextPreparedStatement(EnumSet.of(AbstractUpsertOutputOperator.OperationContext.IF_EXISTS_CHECK_ABSENT)), aQueryStub.toString());
        if (counterColumns.size() == 0) {
            // IF exists cannot be used for counter column tables
            finalSetOfQueryStrings.put(currentIndexPos + getSlotIndexForMutationContextPreparedStatement(EnumSet.of(AbstractUpsertOutputOperator.OperationContext.IF_EXISTS_CHECK_PRESENT)), aQueryStub.toString() + ifExistsClause);
        }
    }
    for (Long currentIndexPos : finalSetOfQueryStrings.keySet()) {
        String currentQueryStr = finalSetOfQueryStrings.get(currentIndexPos);
        LOG.info("Registering query support for " + currentQueryStr);
        PreparedStatement preparedStatementForThisQuery = session.prepare(currentQueryStr);
        preparedStatementTypes.put(currentIndexPos, preparedStatementForThisQuery);
    }
}
Also used : HashMap(java.util.HashMap) PreparedStatement(com.datastax.driver.core.PreparedStatement)

Example 89 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project apex-malhar by apache.

the class CassandraPOJOInputOperator method fetchKeyTokenFromDB.

private Long fetchKeyTokenFromDB(Object keyValue) {
    PreparedStatement statement = store.getSession().prepare(tokenQuery);
    BoundStatement boundStatement = new BoundStatement(statement);
    boundStatement.bind(keyValue);
    ResultSet rs = store.getSession().execute(boundStatement);
    Long keyTokenValue = rs.one().getLong(0);
    return keyTokenValue;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 90 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project java-driver by datastax.

the class QueryBuilder21ExecutionTest method should_handle_contains_on_list_with_index.

@Test(groups = "short")
public void should_handle_contains_on_list_with_index() {
    PreparedStatement byBuyer = session().prepare(select("id", "description", "buyers").from("products").where(contains("buyers", bindMarker("buyer"))));
    ResultSet results = session().execute(byBuyer.bind().setInt("buyer", 4));
    Row row = results.one();
    assertThat(row).isNotNull();
    assertThat(row.getInt("id")).isEqualTo(38471);
    assertThat(row.getList("buyers", Integer.class)).contains(4);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row) Test(org.testng.annotations.Test)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)113 ResultSet (com.datastax.driver.core.ResultSet)60 BoundStatement (com.datastax.driver.core.BoundStatement)59 Session (com.datastax.driver.core.Session)39 Test (org.junit.Test)30 Row (com.datastax.driver.core.Row)27 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)27 XMLStreamException (javolution.xml.stream.XMLStreamException)25 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)15 Cluster (com.datastax.driver.core.Cluster)9 Date (java.util.Date)9 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 QueryProcessor (org.apache.cassandra.cql3.QueryProcessor)7 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)7 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)7 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)7 ICluster (org.apache.cassandra.distributed.api.ICluster)7