Search in sources :

Example 86 with Column

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));
}
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 87 with Column

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;
}
Also used : Column(jakarta.nosql.column.Column) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put)

Example 88 with Column

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));
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) ParamsBinder(org.eclipse.jnosql.mapping.util.ParamsBinder) ColumnQueryParams(jakarta.nosql.column.ColumnQueryParams) Params(jakarta.nosql.Params) Method(java.lang.reflect.Method) ColumnQueryParams(jakarta.nosql.column.ColumnQueryParams) SelectQuery(jakarta.nosql.query.SelectQuery) ColumnQuery(jakarta.nosql.column.ColumnQuery) Column(jakarta.nosql.column.Column) SelectQueryConverter(jakarta.nosql.column.SelectQueryConverter) SelectMethodProvider(org.eclipse.jnosql.communication.query.method.SelectMethodProvider) List(java.util.List) ColumnCondition(jakarta.nosql.column.ColumnCondition) Test(org.junit.jupiter.api.Test)

Example 89 with Column

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());
}
Also used : ColumnPreparedStatement(jakarta.nosql.column.ColumnPreparedStatement) Column(jakarta.nosql.column.Column) ColumnCondition(jakarta.nosql.column.ColumnCondition) ColumnDeleteQuery(jakarta.nosql.column.ColumnDeleteQuery) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 90 with Column

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());
}
Also used : ColumnQuery(jakarta.nosql.column.ColumnQuery) ColumnEntity(jakarta.nosql.column.ColumnEntity) ColumnPreparedStatement(jakarta.nosql.column.ColumnPreparedStatement) Column(jakarta.nosql.column.Column) ColumnCondition(jakarta.nosql.column.ColumnCondition) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Column (jakarta.nosql.column.Column)113 Test (org.junit.jupiter.api.Test)72 ColumnCondition (jakarta.nosql.column.ColumnCondition)57 ColumnEntity (jakarta.nosql.column.ColumnEntity)39 ColumnQuery (jakarta.nosql.column.ColumnQuery)35 List (java.util.List)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 ValueSource (org.junit.jupiter.params.provider.ValueSource)26 ColumnDeleteQuery (jakarta.nosql.column.ColumnDeleteQuery)24 ArrayList (java.util.ArrayList)13 Value (jakarta.nosql.Value)10 TypeReference (jakarta.nosql.TypeReference)9 Arrays.asList (java.util.Arrays.asList)9 Collections.singletonList (java.util.Collections.singletonList)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ColumnPreparedStatement (jakarta.nosql.column.ColumnPreparedStatement)6 Optional (java.util.Optional)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)3