Search in sources :

Example 11 with Sort

use of jakarta.nosql.Sort in project jnosql-diana-driver by eclipse.

the class QueryUtils method select.

public static Select select(ColumnQuery query, String keyspace) {
    String columnFamily = query.getColumnFamily();
    final List<String> columns = query.getColumns();
    Select select = null;
    if (columns.isEmpty()) {
        select = QueryBuilder.selectFrom(keyspace, columnFamily).all();
    } else {
        select = QueryBuilder.selectFrom(keyspace, columnFamily).columns(columns);
    }
    select = select.where(Relations.createClause(query.getCondition().orElse(null)));
    final Map<String, ClusteringOrder> sort = query.getSorts().stream().collect(Collectors.toMap(s -> s.getName(), mapSort()));
    select = select.orderBy(sort);
    return select;
}
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) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

Example 12 with Sort

use of jakarta.nosql.Sort in project jnosql-diana-driver by eclipse.

the class QueryOSQLConverter method appendSort.

private static void appendSort(List<Sort> sorts, StringBuilder query) {
    query.append(SORT);
    String separator = SPACE;
    for (Sort sort : sorts) {
        query.append(separator).append(sort.getName()).append(SPACE).append(sort.getType());
        separator = ", ";
    }
}
Also used : Sort(jakarta.nosql.Sort)

Example 13 with Sort

use of jakarta.nosql.Sort in project jnosql-diana by eclipse.

the class DynamicReturnTest method shouldShouldFindSortAtMethod.

@Test
public void shouldShouldFindSortAtMethod() {
    Sort name = Sort.asc("name");
    Sort age = Sort.desc("age");
    List<Sort> sorts = DynamicReturn.findSorts(new Object[] { "Otavio", 23, Pagination.page(2).size(2), name, age });
    assertThat(sorts, Matchers.contains(name, age));
}
Also used : Sort(jakarta.nosql.Sort) Test(org.junit.jupiter.api.Test)

Example 14 with Sort

use of jakarta.nosql.Sort in project jnosql-diana by eclipse.

the class DynamicReturnTest method shouldShouldFindSortsAtMethod.

@Test
public void shouldShouldFindSortsAtMethod() {
    Sort name = Sort.asc("name");
    Sort age = Sort.desc("age");
    List<Sort> sorts = DynamicReturn.findSorts(new Object[] { "Otavio", 23, Pagination.page(2).size(2), sorts().add(name).add(age) });
    assertThat(sorts, Matchers.contains(name, age));
}
Also used : Sort(jakarta.nosql.Sort) Test(org.junit.jupiter.api.Test)

Example 15 with Sort

use of jakarta.nosql.Sort in project jnosql-diana by eclipse.

the class DocumentRepositoryProxyTest method shouldFindBySalary_CurrencyOrderByCurrency_Name.

@Test
public void shouldFindBySalary_CurrencyOrderByCurrency_Name() {
    Person ada = Person.builder().withAge(20).withName("Ada").build();
    when(template.select(any(DocumentQuery.class))).thenReturn(Stream.of(ada));
    personRepository.findBySalary_CurrencyOrderByCurrency_Name("USD");
    ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
    verify(template).select(captor.capture());
    DocumentQuery query = captor.getValue();
    DocumentCondition condition = query.getCondition().get();
    final Sort sort = query.getSorts().get(0);
    final Document document = condition.getDocument();
    assertEquals("Person", query.getDocumentCollection());
    assertEquals("salary.currency", document.getName());
    assertEquals("currency.name", sort.getName());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) Sort(jakarta.nosql.Sort) Document(jakarta.nosql.document.Document) Person(jakarta.nosql.tck.entities.Person) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Aggregations

Sort (jakarta.nosql.Sort)22 Test (org.junit.jupiter.api.Test)10 SelectQuery (jakarta.nosql.query.SelectQuery)9 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Params (jakarta.nosql.Params)4 QueryException (jakarta.nosql.QueryException)4 ServiceLoaderProvider (jakarta.nosql.ServiceLoaderProvider)4 ColumnQuery (jakarta.nosql.column.ColumnQuery)4 SelectQueryProvider (jakarta.nosql.query.SelectQuery.SelectQueryProvider)4 Objects (java.util.Objects)4 Collectors.toList (java.util.stream.Collectors.toList)4 Stream (java.util.stream.Stream)4 ColumnCondition (jakarta.nosql.column.ColumnCondition)3 ColumnEntity (jakarta.nosql.column.ColumnEntity)3 DocumentCondition (jakarta.nosql.document.DocumentCondition)3 DocumentQuery (jakarta.nosql.document.DocumentQuery)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 Column (jakarta.nosql.column.Column)2