-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from eclipse/create-pagination-query
Create Special Paramter converter
- Loading branch information
Showing
7 changed files
with
505 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...ql-mapping-column/src/main/java/org/eclipse/jnosql/mapping/column/query/DynamicQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.column.query; | ||
|
||
import jakarta.data.repository.Limit; | ||
import jakarta.data.repository.Sort; | ||
import org.eclipse.jnosql.communication.column.ColumnQuery; | ||
import org.eclipse.jnosql.mapping.NoSQLPage; | ||
import org.eclipse.jnosql.mapping.column.MappingColumnQuery; | ||
import org.eclipse.jnosql.mapping.repository.DynamicReturn; | ||
import org.eclipse.jnosql.mapping.repository.SpecialParameters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A query converter to update dynamic query to a {@link ColumnQuery} | ||
*/ | ||
public class DynamicQuery implements Supplier<ColumnQuery> { | ||
|
||
private final SpecialParameters special; | ||
private final ColumnQuery query; | ||
|
||
private DynamicQuery(SpecialParameters special, ColumnQuery query) { | ||
this.special = special; | ||
this.query = query; | ||
} | ||
|
||
@Override | ||
public ColumnQuery get() { | ||
if (special.isEmpty()) { | ||
return query; | ||
} | ||
Optional<Limit> limit = special.limit(); | ||
if (special.hasOnlySort()) { | ||
List<Sort> sorts = new ArrayList<>(); | ||
sorts.addAll(query.sorts()); | ||
sorts.addAll(special.sorts()); | ||
long skip = limit.map(l -> l.startAt() - 1).orElse(query.skip()); | ||
long max = limit.map(Limit::maxResults).orElse((int) query.limit()); | ||
return new MappingColumnQuery(sorts, max, | ||
skip, | ||
query.condition().orElse(null), | ||
query.name()); | ||
} | ||
|
||
if (limit.isPresent()) { | ||
long skip = limit.map(l -> l.startAt() - 1).orElse(query.skip()); | ||
long max = limit.map(Limit::maxResults).orElse((int) query.limit()); | ||
List<Sort> sorts = query.sorts(); | ||
if (!special.sorts().isEmpty()) { | ||
sorts = new ArrayList<>(query.sorts()); | ||
sorts.addAll(special.sorts()); | ||
} | ||
return new MappingColumnQuery(sorts, max, | ||
skip, | ||
query.condition().orElse(null), | ||
query.name()); | ||
} | ||
|
||
return special.pageable().<ColumnQuery>map(p -> { | ||
long size = p.size(); | ||
long skip = NoSQLPage.skip(p); | ||
List<Sort> sorts = query.sorts(); | ||
if (!special.sorts().isEmpty()) { | ||
sorts = new ArrayList<>(query.sorts()); | ||
sorts.addAll(special.sorts()); | ||
} | ||
return new MappingColumnQuery(sorts, size, skip, | ||
query.condition().orElse(null), query.name()); | ||
}).orElse(query); | ||
} | ||
|
||
/** | ||
* Creates a {@link DynamicQuery} instance | ||
* @param args the method parameters | ||
* @param query the column query | ||
* @return the {@link DynamicQuery} instance | ||
* @throws NullPointerException when either args or query are null | ||
*/ | ||
public static DynamicQuery of(Object[] args, ColumnQuery query) { | ||
Objects.requireNonNull(args, "args is required"); | ||
Objects.requireNonNull(query, "query is required"); | ||
return new DynamicQuery(DynamicReturn.findSpecialParameters(args), query); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
...apping-column/src/test/java/org/eclipse/jnosql/mapping/column/query/DynamicQueryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.column.query; | ||
|
||
import jakarta.data.repository.Limit; | ||
import jakarta.data.repository.Pageable; | ||
import jakarta.data.repository.Sort; | ||
import org.eclipse.jnosql.communication.column.ColumnQuery; | ||
import org.eclipse.jnosql.mapping.repository.SpecialParameters; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
class DynamicQueryTest { | ||
|
||
@Mock | ||
private SpecialParameters special; | ||
|
||
@Mock | ||
private ColumnQuery query; | ||
|
||
@Mock | ||
private Limit limit; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void shouldCreateDynamicQuery() { | ||
when(special.isEmpty()).thenReturn(true); | ||
when(query.condition()).thenReturn(Optional.empty()); | ||
when(query.name()).thenReturn("sampleQuery"); | ||
|
||
DynamicQuery dynamicQuery = DynamicQuery.of(new Object[]{}, query); | ||
|
||
assertEquals(query, dynamicQuery.get()); | ||
} | ||
|
||
@Test | ||
public void shouldCreateDynamicQueryWithSortsAndLimit() { | ||
when(special.isEmpty()).thenReturn(false); | ||
when(special.hasOnlySort()).thenReturn(true); | ||
when(special.sorts()).thenReturn(List.of(mock(Sort.class))); | ||
when(limit.startAt()).thenReturn(1L); | ||
when(special.limit()).thenReturn(Optional.of(limit)); | ||
when(query.condition()).thenReturn(Optional.empty()); | ||
when(query.name()).thenReturn("sampleQuery"); | ||
when(query.sorts()).thenReturn(List.of(mock(Sort.class))); | ||
when(query.skip()).thenReturn(0L); | ||
when(query.limit()).thenReturn(10L); | ||
|
||
DynamicQuery dynamicQuery = DynamicQuery.of(new Object[]{}, query); | ||
|
||
assertEquals("sampleQuery", dynamicQuery.get().name()); | ||
assertEquals(0, dynamicQuery.get().skip()); | ||
assertEquals(10, dynamicQuery.get().limit()); | ||
assertEquals(1, dynamicQuery.get().sorts().size()); | ||
} | ||
|
||
@Test | ||
public void shouldCreateDynamicQueryWithLimit() { | ||
when(special.isEmpty()).thenReturn(false); | ||
when(special.hasOnlySort()).thenReturn(false); | ||
when(limit.startAt()).thenReturn(1L); | ||
when(special.limit()).thenReturn(Optional.of(limit)); | ||
when(query.condition()).thenReturn(Optional.empty()); | ||
when(query.name()).thenReturn("sampleQuery"); | ||
when(query.sorts()).thenReturn(List.of(mock(Sort.class))); | ||
when(query.skip()).thenReturn(0L); | ||
when(query.limit()).thenReturn(10L); | ||
|
||
DynamicQuery dynamicQuery = DynamicQuery.of(new Object[]{}, query); | ||
|
||
assertEquals("sampleQuery", dynamicQuery.get().name()); | ||
assertEquals(0, dynamicQuery.get().skip()); | ||
assertEquals(10, dynamicQuery.get().limit()); | ||
assertEquals(1, dynamicQuery.get().sorts().size()); | ||
} | ||
|
||
@Test | ||
public void shouldCreateDynamicQueryWithPageable() { | ||
when(special.isEmpty()).thenReturn(false); | ||
when(special.pageable()).thenReturn(Optional.of(mock(Pageable.class))); | ||
when(special.sorts()).thenReturn(List.of(mock(Sort.class))); | ||
when(query.condition()).thenReturn(Optional.empty()); | ||
when(query.name()).thenReturn("sampleQuery"); | ||
when(query.sorts()).thenReturn(List.of(mock(Sort.class))); | ||
when(query.skip()).thenReturn(0L); | ||
when(query.limit()).thenReturn(10L); | ||
|
||
DynamicQuery dynamicQuery = DynamicQuery.of(new Object[]{}, query); | ||
|
||
assertEquals("sampleQuery", dynamicQuery.get().name()); | ||
assertEquals(0, dynamicQuery.get().skip()); | ||
assertEquals(10, dynamicQuery.get().limit()); | ||
assertEquals(1, dynamicQuery.get().sorts().size()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnWhenThereIsLimitAndSort(){ | ||
when(special.isEmpty()).thenReturn(false); | ||
when(special.pageable()).thenReturn(Optional.of(mock(Pageable.class))); | ||
when(query.condition()).thenReturn(Optional.empty()); | ||
when(query.name()).thenReturn("sampleQuery"); | ||
when(query.sorts()).thenReturn(Collections.emptyList()); | ||
when(query.skip()).thenReturn(0L); | ||
when(query.limit()).thenReturn(10L); | ||
|
||
DynamicQuery dynamicQuery = DynamicQuery.of(new Object[]{Sort.asc("name"), Limit.of(20)} | ||
, query); | ||
|
||
ColumnQuery columnQuery = dynamicQuery.get(); | ||
assertEquals("sampleQuery", columnQuery.name()); | ||
assertEquals(0, columnQuery.skip()); | ||
assertEquals(20, columnQuery.limit()); | ||
assertEquals(1, columnQuery.sorts().size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.