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");
}
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;
}
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));
}
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");
}
Aggregations