use of jakarta.nosql.column.Column 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 jakarta.nosql.column.Column in project jnosql-diana-driver by eclipse.
the class HBaseColumnFamilyManager method insert.
@Override
public ColumnEntity insert(ColumnEntity entity) {
Objects.requireNonNull(entity, "entity is required");
String family = entity.getName();
List<Column> columns = entity.getColumns();
if (columns.isEmpty()) {
return entity;
}
Column columnID = entity.find(HBaseUtils.KEY_COLUMN).orElseThrow(() -> new HBaseException(KEY_REQUIRED_ERROR));
Put put = new Put(Bytes.toBytes(valueToString(columnID.getValue())));
columns.stream().filter(Predicate.isEqual(columnID).negate()).forEach(column -> put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column.getName()), Bytes.toBytes(valueToString(column.getValue()))));
try {
table.put(put);
} catch (IOException e) {
throw new HBaseException("An error happened when try to save an entity", e);
}
return entity;
}
use of jakarta.nosql.column.Column in project jnosql-diana by eclipse.
the class ParamsBinderTest method shouldConvert2.
@Test
public void shouldConvert2() {
Method method = Stream.of(PersonRepository.class.getMethods()).filter(m -> m.getName().equals("findByAgeAndName")).findFirst().get();
ClassMapping classMapping = mappings.get(Person.class);
RepositoryColumnObserverParser parser = new RepositoryColumnObserverParser(classMapping);
paramsBinder = new ParamsBinder(classMapping, converters);
SelectMethodProvider selectMethodFactory = SelectMethodProvider.get();
SelectQuery selectQuery = selectMethodFactory.apply(method, classMapping.getName());
SelectQueryConverter converter = ServiceLoaderProvider.get(SelectQueryConverter.class);
ColumnQueryParams queryParams = converter.apply(selectQuery, parser);
Params params = queryParams.getParams();
paramsBinder.bind(params, new Object[] { 10L, "Ada" }, method);
ColumnQuery query = queryParams.getQuery();
ColumnCondition columnCondition = query.getCondition().get();
List<ColumnCondition> conditions = columnCondition.getColumn().get(new TypeReference<List<ColumnCondition>>() {
});
List<Object> values = conditions.stream().map(ColumnCondition::getColumn).map(Column::getValue).map(Value::get).collect(Collectors.toList());
assertEquals(10, values.get(0));
assertEquals("Ada", values.get(1));
}
use of jakarta.nosql.column.Column in project jnosql-diana by eclipse.
the class DefaultColumnQueryParserTest method shouldExecutePrepareStatement.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "delete from God where age = @age" })
public void shouldExecutePrepareStatement(String query) {
ArgumentCaptor<ColumnDeleteQuery> captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class);
ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY);
prepare.bind("age", 12);
prepare.getResult();
Mockito.verify(manager).delete(captor.capture());
ColumnDeleteQuery columnDeleteQuery = captor.getValue();
ColumnCondition columnCondition = columnDeleteQuery.getCondition().get();
Column column = columnCondition.getColumn();
assertEquals(Condition.EQUALS, columnCondition.getCondition());
assertEquals("age", column.getName());
assertEquals(12, column.get());
}
use of jakarta.nosql.column.Column in project jnosql-diana by eclipse.
the class DefaultColumnQueryParserTest method shouldSingleResult.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God where age = @age" })
public void shouldSingleResult(String query) {
ArgumentCaptor<ColumnQuery> captor = ArgumentCaptor.forClass(ColumnQuery.class);
Mockito.when(manager.select(Mockito.any(ColumnQuery.class))).thenReturn(Stream.of(mock(ColumnEntity.class)));
ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY);
prepare.bind("age", 12);
final Optional<ColumnEntity> result = prepare.getSingleResult();
Mockito.verify(manager).select(captor.capture());
ColumnQuery columnQuery = captor.getValue();
ColumnCondition columnCondition = columnQuery.getCondition().get();
Column column = columnCondition.getColumn();
assertEquals(Condition.EQUALS, columnCondition.getCondition());
assertEquals("age", column.getName());
assertEquals(12, column.get());
assertTrue(result.isPresent());
}
Aggregations