Search in sources :

Example 1 with Term

use of com.datastax.oss.driver.api.querybuilder.term.Term in project java-driver by datastax.

the class RegularInsertTest method should_throw_exception_with_invalid_timestamp.

@Test
public void should_throw_exception_with_invalid_timestamp() {
    DefaultInsert defaultInsert = (DefaultInsert) insertInto("foo").value("a", bindMarker()).usingTimestamp(1);
    Throwable t = catchThrowable(() -> new DefaultInsert(defaultInsert.getKeyspace(), defaultInsert.getTable(), (Term) defaultInsert.getJson(), defaultInsert.getMissingJsonBehavior(), defaultInsert.getAssignments(), // invalid timestamp object)
    new Object(), defaultInsert.getTtlInSeconds(), defaultInsert.isIfNotExists()));
    assertThat(t).isInstanceOf(IllegalArgumentException.class).hasMessage("TIMESTAMP value must be a BindMarker or a Long");
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Term(com.datastax.oss.driver.api.querybuilder.term.Term) DefaultInsert(com.datastax.oss.driver.internal.querybuilder.insert.DefaultInsert) Test(org.junit.Test)

Example 2 with Term

use of com.datastax.oss.driver.api.querybuilder.term.Term in project jnosql-diana-driver by eclipse.

the class QueryUtils method insert.

static Insert insert(ColumnEntity entity, String keyspace, CqlSession session, Duration duration) {
    Map<String, Term> values = new HashMap<>();
    InsertInto insert = QueryBuilder.insertInto(keyspace, entity.getName());
    entity.getColumns().stream().forEach(c -> {
        if (UDT.class.isInstance(c)) {
            insertUDT(UDT.class.cast(c), keyspace, entity.getName(), session, values);
        } else {
            insertSingleField(c, values);
        }
    });
    RegularInsert regularInsert = insert.values(values);
    if (duration != null) {
        return regularInsert.usingTtl((int) duration.getSeconds());
    }
    return regularInsert;
}
Also used : HashMap(java.util.HashMap) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Term(com.datastax.oss.driver.api.querybuilder.term.Term)

Example 3 with Term

use of com.datastax.oss.driver.api.querybuilder.term.Term in project jnosql-diana-driver by eclipse.

the class QueryUtils method insertUDT.

private static void insertUDT(UDT udt, String keyspace, String columnFamily, CqlSession session, Map<String, Term> values) {
    final Optional<KeyspaceMetadata> keyspaceMetadata = session.getMetadata().getKeyspace(keyspace);
    UserDefinedType userType = keyspaceMetadata.flatMap(ks -> ks.getUserDefinedType(udt.getUserType())).orElseThrow(() -> new IllegalArgumentException("Missing UDT definition"));
    final TableMetadata tableMetadata = keyspaceMetadata.flatMap(k -> k.getTable(columnFamily)).orElseThrow(() -> new IllegalArgumentException("Missing Table definition"));
    final ColumnMetadata columnMetadata = tableMetadata.getColumn(getName(udt)).orElseThrow(() -> new IllegalArgumentException("Missing the column definition"));
    final DataType type = columnMetadata.getType();
    Iterable elements = Iterable.class.cast(udt.get());
    Object udtValue = getUdtValue(userType, elements, type);
    values.put(getName(udt), QueryBuilder.literal(udtValue));
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) SortType(jakarta.nosql.SortType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) HashMap(java.util.HashMap) Function(java.util.function.Function) ValueUtil(org.eclipse.jnosql.communication.driver.ValueUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Duration(java.time.Duration) Map(java.util.Map) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) Select(com.datastax.oss.driver.api.querybuilder.select.Select) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnEntity(jakarta.nosql.column.ColumnEntity) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Collection(java.util.Collection) DataType(com.datastax.oss.driver.api.core.type.DataType) Collectors(java.util.stream.Collectors) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Value(jakarta.nosql.Value) List(java.util.List) Column(jakarta.nosql.column.Column) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) Term(com.datastax.oss.driver.api.querybuilder.term.Term) ColumnQuery(jakarta.nosql.column.ColumnQuery) Sort(jakarta.nosql.Sort) CommunicationException(jakarta.nosql.CommunicationException) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)

Example 4 with Term

use of com.datastax.oss.driver.api.querybuilder.term.Term in project java-driver by datastax.

the class RegularInsertTest method should_throw_exception_with_invalid_ttl.

@Test
public void should_throw_exception_with_invalid_ttl() {
    DefaultInsert defaultInsert = (DefaultInsert) insertInto("foo").value("a", bindMarker()).usingTtl(10);
    Throwable t = catchThrowable(() -> new DefaultInsert(defaultInsert.getKeyspace(), defaultInsert.getTable(), (Term) defaultInsert.getJson(), defaultInsert.getMissingJsonBehavior(), defaultInsert.getAssignments(), defaultInsert.getTimestamp(), // invalid TTL object
    new Object(), defaultInsert.isIfNotExists()));
    assertThat(t).isInstanceOf(IllegalArgumentException.class).hasMessage("TTL value must be a BindMarker or an Integer");
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Term(com.datastax.oss.driver.api.querybuilder.term.Term) DefaultInsert(com.datastax.oss.driver.internal.querybuilder.insert.DefaultInsert) Test(org.junit.Test)

Aggregations

Term (com.datastax.oss.driver.api.querybuilder.term.Term)4 InsertInto (com.datastax.oss.driver.api.querybuilder.insert.InsertInto)2 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)2 DefaultInsert (com.datastax.oss.driver.internal.querybuilder.insert.DefaultInsert)2 HashMap (java.util.HashMap)2 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)1 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 SetType (com.datastax.oss.driver.api.core.type.SetType)1 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)1 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)1 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)1