Search in sources :

Example 6 with SimpleStatementBuilder

use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project spring-data-cassandra by spring-projects.

the class StatementBuilder method build.

/**
 * Build a {@link SimpleStatement statement} by applying builder and bind functions using the given
 * {@link CodecRegistry} and {@link ParameterHandling}.
 *
 * @param parameterHandling {@link ParameterHandling} used to determine how to render parameters.
 * @param codecRegistry registry of Apache Cassandra codecs for converting to/from Java types and CQL types.
 * @return the built {@link SimpleStatement}.
 */
public SimpleStatement build(ParameterHandling parameterHandling, CodecRegistry codecRegistry) {
    Assert.notNull(parameterHandling, "ParameterHandling must not be null");
    Assert.notNull(codecRegistry, "CodecRegistry must not be null");
    S statement = this.statement;
    if (parameterHandling == ParameterHandling.INLINE) {
        TermFactory termFactory = new TermFactory() {

            @Override
            public Term create(@Nullable Object value) {
                return toLiteralTerms(value, codecRegistry);
            }

            @Override
            public boolean canBindCollection() {
                return false;
            }
        };
        for (BuilderRunnable<S> runnable : queryActions) {
            statement = runnable.run(statement, termFactory);
        }
        return build(statement.builder());
    }
    if (parameterHandling == ParameterHandling.BY_INDEX) {
        List<Object> values = new ArrayList<>();
        TermFactory termFactory = value -> {
            values.add(value);
            return QueryBuilder.bindMarker();
        };
        for (BuilderRunnable<S> runnable : queryActions) {
            statement = runnable.run(statement, termFactory);
        }
        SimpleStatementBuilder builder = statement.builder();
        values.forEach(builder::addPositionalValue);
        return build(builder);
    }
    if (parameterHandling == ParameterHandling.BY_NAME) {
        Map<String, Object> values = new LinkedHashMap<>();
        TermFactory termFactory = value -> {
            String name = "p" + values.size();
            values.put(name, value);
            return QueryBuilder.bindMarker(name);
        };
        for (BuilderRunnable<S> runnable : queryActions) {
            statement = runnable.run(statement, termFactory);
        }
        SimpleStatementBuilder builder = statement.builder();
        values.forEach(builder::addNamedValue);
        return build(builder);
    }
    throw new UnsupportedOperationException(String.format("ParameterHandling %s not supported", parameterHandling));
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CqlHelper(com.datastax.oss.driver.internal.querybuilder.CqlHelper) Collection(java.util.Collection) Set(java.util.Set) UnaryOperator(java.util.function.UnaryOperator) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Consumer(java.util.function.Consumer) List(java.util.List) BuildableQuery(com.datastax.oss.driver.api.querybuilder.BuildableQuery) Map(java.util.Map) NonNull(org.springframework.lang.NonNull) Nullable(org.springframework.lang.Nullable) Term(com.datastax.oss.driver.api.querybuilder.term.Term) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) LinkedHashMap(java.util.LinkedHashMap) Nullable(org.springframework.lang.Nullable)

Example 7 with SimpleStatementBuilder

use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project vertx-cassandra-client by vert-x3.

the class StreamingTest method streamFetchesDoesNotOverflowDefault512KbJVMStack.

@Test
public void streamFetchesDoesNotOverflowDefault512KbJVMStack(TestContext testContext) throws Exception {
    int fetchSize = 100_000;
    initializeRandomStringKeyspace();
    insertRandomStrings(5_000);
    final SimpleStatement query = new SimpleStatementBuilder(String.format("select random_string from random_strings.random_string_by_first_letter limit %d", fetchSize)).setPageSize(fetchSize).build();
    Async async = testContext.async();
    client.queryStream(query, testContext.asyncAssertSuccess(stream -> {
        stream.endHandler(end -> async.countDown()).exceptionHandler(testContext::fail).handler(item -> {
        });
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) Row(com.datastax.oss.driver.api.core.cql.Row) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Async(io.vertx.ext.unit.Async) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) Test(org.junit.Test)

Aggregations

SimpleStatementBuilder (com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder)7 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 ArrayList (java.util.ArrayList)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 List (java.util.List)2 Function (java.util.function.Function)2 Test (org.junit.Test)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)1 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 BuildableQuery (com.datastax.oss.driver.api.querybuilder.BuildableQuery)1