Package com.datastax.driver.core.querybuilder

Examples of com.datastax.driver.core.querybuilder.Insert


   * @return The Query object to run with session.execute();
   */
  public static Insert createInsertQuery(String tableName, Object objectToSave, WriteOptions options,
      EntityWriter<Object, Object> entityWriter) {

    Insert insert = QueryBuilder.insertInto(tableName);
    entityWriter.write(objectToSave, insert);
    CqlTemplate.addWriteOptions(insert, options);
    return insert;
  }
View Full Code Here


    }

    @Test
    public void should_add_native_statement_to_batch() throws Exception {
        //Given
        final Insert statement = insertInto("test").value("id", bindMarker("id"));
        ArgumentCaptor<NativeStatementWrapper> statementCaptor = ArgumentCaptor.forClass(NativeStatementWrapper.class);

        //When
        batch.batchNativeStatement(statement,10L);

        //Then
        verify(validator).validateUpsertOrDelete(statement);
        verify(flushContext).pushStatement(statementCaptor.capture());
        final NativeStatementWrapper statementWrapper = statementCaptor.getValue();
        assertThat(statementWrapper.getStatement()).isInstanceOf(SimpleStatement.class);
        assertThat(((RegularStatement)statementWrapper.getStatement()).getQueryString()).isEqualTo(statement.getQueryString());
        assertThat(statementWrapper.getValues()).contains(10L);
    }
View Full Code Here

    public PreparedStatement prepareInsert(Session session, EntityMeta entityMeta, List<PropertyMeta> pms, Options options) {
        log.trace("Generate prepared statement for INSERT on {}", entityMeta);
        PropertyMeta idMeta = entityMeta.getIdMeta();
        final EntityMetaConfig metaConfig = entityMeta.config();
        Insert insert = insertInto(metaConfig.getKeyspaceName(), metaConfig.getTableName());
        if (options.isIfNotExists()) {
            insert.ifNotExists();
        }

        idMeta.forStatementGeneration().generateInsertPrimaryKey(insert);

        for (PropertyMeta pm : pms) {
            String cql3ColumnName = pm.getCQL3ColumnName();
            insert.value(cql3ColumnName, bindMarker(cql3ColumnName));
        }

        final Insert.Options insertOptions = insert.using(ttl(bindMarker("ttl")));
        if (options.getTimestamp().isPresent()) {
            insertOptions.and(timestamp(bindMarker("timestamp")));
        }
        return session.prepare(insert.getQueryString());
    }
View Full Code Here

        //Given
        CompleteBean entity = builder().randomId().name("DuyHai").label("label").buid();

        manager.insert(entity);

        final Insert statement = insertInto("CompleteBean").ifNotExists().value("id", bindMarker("id")).value("name", bindMarker("name"));

        final AtomicBoolean error = new AtomicBoolean(false);
        final AtomicReference<CASResult> result = new AtomicReference<>(null);

        CASResultListener listener = new CASResultListener() {
View Full Code Here

    @Test
    public void should_prepare_insert_primary_key_for_embedded_id() throws Exception {
        //Given
        when(meta.structure().isEmbeddedId()).thenReturn(true);
        when(embeddedIdProperties.getCQL3ComponentNames()).thenReturn(asList("id", "name"));
        Insert insert = QueryBuilder.insertInto("table");

        //When
        final Insert actual = view.generateInsertPrimaryKey(insert);

        //Then
        assertThat(actual.getQueryString()).isEqualTo("INSERT INTO table(id,name) VALUES (:id,:name);");
    }
View Full Code Here

    @Test
    public void should_prepare_insert_primary_key_for_simple_id() throws Exception {
        //Given
        when(meta.structure().isEmbeddedId()).thenReturn(false);
        when(meta.getCQL3ColumnName()).thenReturn("id");
        Insert insert = QueryBuilder.insertInto("table");

        //When
        final Insert actual = view.generateInsertPrimaryKey(insert);

        //Then
        assertThat(actual.getQueryString()).isEqualTo("INSERT INTO table(id) VALUES (:id);");
    }
View Full Code Here

public class NativeStatementWrapperTest {

    @Test
    public void should_build_parameterized_statement() throws Exception {
        //Given
        final Insert statement = insertInto("test").value("id", bindMarker("id"));
        statement.setConsistencyLevel(ConsistencyLevel.ALL);
        statement.setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL);
        final NativeStatementWrapper wrapper = new NativeStatementWrapper(NativeQueryLog.class, statement, new Object[] { 10L }, Optional.<CASResultListener>absent());
        final ByteBuffer[] boundValues = new SimpleStatement("select", 10L).getValues();

        //When
        final SimpleStatement actual = (SimpleStatement)wrapper.buildParameterizedStatement();

        //Then
        assertThat(actual.getQueryString()).isEqualTo(statement.getQueryString());
        assertThat(actual.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ALL);
        assertThat(actual.getSerialConsistencyLevel()).isEqualTo(ConsistencyLevel.LOCAL_SERIAL);

        assertThat(actual.getValues()).hasSize(1);
        assertThat(actual.getValues()).isEqualTo(boundValues);
View Full Code Here

    }

    @Test
    public void should_return_regular_statement() throws Exception {
        //Given
        final Insert statement = insertInto("test").value("id", 10L).value("uuid", new UUID(10,10));
        statement.setConsistencyLevel(ConsistencyLevel.ALL);
        statement.setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL);
        final NativeStatementWrapper wrapper = new NativeStatementWrapper(NativeQueryLog.class, statement, new Object[] {}, Optional.<CASResultListener>absent());

        //When
        final RegularStatement actual = (RegularStatement)wrapper.buildParameterizedStatement();

        //Then
        assertThat(actual).isSameAs(statement);
        assertThat(actual.getQueryString()).isEqualTo(statement.getQueryString());
        assertThat(actual.getValues()).isNotEmpty();
        assertThat(actual.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ALL);
        assertThat(actual.getSerialConsistencyLevel()).isEqualTo(ConsistencyLevel.LOCAL_SERIAL);
    }
View Full Code Here

    @Test
    public void should_decode_enum_correctly() throws Exception {
        //Given
        Long id = RandomUtils.nextLong(0,Long.MAX_VALUE);
        final Insert insert = insertInto(TABLE_NAME).value("id", id)
                .value("consistency_level", ConsistencyLevel.LOCAL_ONE.ordinal())
                .value("element_types", asList(FIELD.name(), METHOD.name()))
                .value("retention_policies", ImmutableMap.of(0, ANNOTATION_TYPE.name(), 2, CONSTRUCTOR.name()));

        manager.nativeQuery(insert).execute();
View Full Code Here

    public void should_batch_native_statement_with_CAS_result_listener() throws Exception {
        //Given
        CompleteBean entity = CompleteBeanTestBuilder.builder().randomId().name("name1000").buid();
        manager.insert(entity);
        final Batch batch = manager.createBatch();
        final Insert statement = insertInto("CompleteBean").value("id", bindMarker("id")).value("name", bindMarker("name")).ifNotExists();

        final AtomicBoolean error = new AtomicBoolean(false);
        final AtomicReference<CASResultListener.CASResult> result = new AtomicReference<>(null);

        CASResultListener listener = new CASResultListener() {
View Full Code Here

TOP

Related Classes of com.datastax.driver.core.querybuilder.Insert

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.