Skip to content

Commit

Permalink
Refactor limit, offset
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Sep 12, 2024
1 parent 0539e29 commit 3f23f6e
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public Map<String, String> getAdditionalRequiredParameters() {

@NonNull
@Override
public QueryResult buildPagination(@NonNull Pageable pageable) {
public String buildPagination(@NonNull Pageable pageable) {
if (pageable.getMode() != Mode.OFFSET) {
throw new UnsupportedOperationException("Pageable mode " + pageable.getMode() + " is not supported by cosmos operations");
}
Expand All @@ -386,19 +386,9 @@ public QueryResult buildPagination(@NonNull Pageable pageable) {
StringBuilder builder = new StringBuilder(" ");
long from = pageable.getOffset();
builder.append("OFFSET ").append(from).append(" LIMIT ").append(size).append(" ");
return QueryResult.of(
builder.toString(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyMap()
);
return builder.toString();
}
return QueryResult.of(
"",
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyMap()
);
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -451,35 +451,7 @@ public QueryResult buildQuery(AnnotationMetadata annotationMetadata, QueryModel
} else {
q = toJsonString(pipeline);
}
return new QueryResult() {

@NonNull
@Override
public String getQuery() {
return q;
}

@Override
public int getMax() {
return query.getMax();
}

@Override
public long getOffset() {
return query.getOffset();
}

@Override
public List<String> getQueryParts() {
return Collections.emptyList();
}

@Override
public List<QueryParameterBinding> getParameterBindings() {
return queryState.getParameterBindings();
}

};
return QueryResult.of(q, queryState.getParameterBindings());
}

private void addLookups(Collection<JoinPath> joins, QueryState queryState) {
Expand Down Expand Up @@ -991,9 +963,7 @@ public QueryResult buildDelete(AnnotationMetadata annotationMetadata, QueryModel
predicateQuery,
Collections.emptyList(),
queryState.getParameterBindings(),
queryState.getAdditionalRequiredParameters(),
query.getMax(),
query.getOffset()
queryState.getAdditionalRequiredParameters()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,35 +186,7 @@ public QueryResult buildSelect(AnnotationMetadata annotationMetadata, SelectQuer
} else {
q = toJsonString(pipeline);
}
return new QueryResult() {

@NonNull
@Override
public String getQuery() {
return q;
}

@Override
public int getMax() {
return selectQueryDefinition.limit();
}

@Override
public long getOffset() {
return selectQueryDefinition.offset();
}

@Override
public List<String> getQueryParts() {
return Collections.emptyList();
}

@Override
public List<QueryParameterBinding> getParameterBindings() {
return queryState.getParameterBindings();
}

};
return QueryResult.of(q, queryState.getParameterBindings());
}

private void addLookups(Collection<JoinPath> joins, QueryState queryState) {
Expand Down Expand Up @@ -646,14 +618,12 @@ public QueryResult buildDelete(AnnotationMetadata annotationMetadata, DeleteQuer
predicateQuery,
Collections.emptyList(),
queryState.getParameterBindings(),
queryState.getAdditionalRequiredParameters(),
queryDefinition.limit(),
queryDefinition.offset()
queryState.getAdditionalRequiredParameters()
);
}

@Override
public QueryResult buildPagination(Pageable pageable) {
public String buildPagination(Pageable pageable) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,7 @@ private QueryResult getQueryResult(MethodMatchContext matchContext,
}
List<QueryParameterBinding> parameterBindings = new ArrayList<>(parameters.size());
String filterQuery = processCustomQuery(matchContext, filterQueryString, parameters, entityParam, persistentEntity, parameterBindings);
return new QueryResult() {
@Override
public String getQuery() {
return filterQuery;
}

@Override
public List<String> getQueryParts() {
return Collections.emptyList();
}

@Override
public List<QueryParameterBinding> getParameterBindings() {
return parameterBindings;
}

@Override
public Map<String, String> getAdditionalRequiredParameters() {
return Collections.emptyMap();
}
};
return QueryResult.of(filterQuery, parameterBindings);
}

private QueryResult getUpdateQueryResult(MethodMatchContext matchContext,
Expand Down Expand Up @@ -278,10 +258,6 @@ public List<QueryParameterBinding> getParameterBindings() {
return parameterBindings;
}

@Override
public Map<String, String> getAdditionalRequiredParameters() {
return Collections.emptyMap();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ public <T> Flux<T> findAll(CriteriaQuery<T> query) {
public <T> Flux<T> findAll(CriteriaQuery<T> query, int offset, int limit) {
return withSession(session -> helper.monoFromCompletionStage(() -> {
Stage.SelectionQuery<T> sessionQuery = session.createQuery(query);
if (offset != -1) {
if (offset > 0) {
sessionQuery = sessionQuery.setFirstResult(offset);
}
if (limit != -1) {
if (limit > 0) {
sessionQuery = sessionQuery.setMaxResults(limit);
}
return sessionQuery.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,28 @@

/**
* The parameter that holds the pageSize value.
* @deprecated Replaced with {@link #META_MEMBER_LIMIT}
*/
@Deprecated(forRemoval = true, since = "4.10")
String META_MEMBER_PAGE_SIZE = "pageSize";

/**
* The parameter that holds the offset value.
* @deprecated Replaced with {@link #META_MEMBER_OFFSET}
*/
@Deprecated(forRemoval = true, since = "4.10")
String META_MEMBER_PAGE_INDEX = "pageIndex";

/**
* The parameter that holds the offset value.
*/
String META_MEMBER_OFFSET = "offset";

/**
* The parameter that holds the limit value.
*/
String META_MEMBER_LIMIT = "limit";

/**
* The parameter that references the entity.
*/
Expand Down Expand Up @@ -258,14 +272,18 @@
/**
* An explicit pageSize (in absence of a pageable).
* @return The pageSize
* @deprecated Not used
*/
@Deprecated(forRemoval = true, since = "4.10")
int pageSize() default -1;

/**
* An explicit offset (in absence of a pageable).
*
* @return The offset
* @deprecated Not used
*/
@Deprecated(forRemoval = true, since = "4.10")
long pageIndex() default 0;

/**
Expand Down
10 changes: 10 additions & 0 deletions data-model/src/main/java/io/micronaut/data/model/Pageable.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ default Pageable order(@NonNull String propertyName, @NonNull Order.Direction di
return Pageable.from(getNumber(), getSize(), newSort);
}

@NonNull
@Override
default Pageable order(@NonNull List<Order> orders) {
Sort newSort = getSort();
for (Order order : orders) {
newSort = newSort.order(order);
}
return Pageable.from(getNumber(), getSize(), newSort);
}

@NonNull
@Override
@JsonIgnore
Expand Down
15 changes: 15 additions & 0 deletions data-model/src/main/java/io/micronaut/data/model/Sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public interface Sort {
*/
@NonNull Sort order(@NonNull Sort.Order order);

/**
* Adds an orders.
*
* @param orders The orders
* @return A new sort with the order applied
* @since 4.10
*/
@NonNull
default Sort order(@NonNull List<Sort.Order> orders) {
for (Order order : orders) {
order(order);
}
return this;
}

/**
* Orders by the specified property name and direction.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public QueryResult buildQuery(AnnotationMetadata annotationMetadata, QueryBuilde
return queryBuilder.buildSelect(annotationMetadata, definition);
}

protected boolean hasDynamicSort() {
return false;
}

/**
* @return Build {@link io.micronaut.data.model.query.builder.QueryBuilder2.SelectQueryDefinition}.
*/
Expand All @@ -125,7 +129,8 @@ public QueryBuilder2.SelectQueryDefinition toSelectQueryDefinition() {
distinct,
orders == null ? List.of() : orders,
max,
offset
offset,
hasDynamicSort()
);
}

Expand All @@ -140,7 +145,8 @@ public QueryResult buildCountQuery(AnnotationMetadata annotationMetadata, QueryB
distinct,
List.of(),
-1,
-1
-1,
false
);
return queryBuilder.buildSelect(annotationMetadata, definition);
}
Expand Down Expand Up @@ -423,6 +429,7 @@ private static final class SelectQueryDefinitionImpl extends BaseQueryDefinition
private final List<Order> order;
private final int limit;
private final int offset;
private final boolean hasDynamicSort;

public SelectQueryDefinitionImpl(PersistentEntity persistentEntity,
Predicate predicate,
Expand All @@ -432,14 +439,21 @@ public SelectQueryDefinitionImpl(PersistentEntity persistentEntity,
boolean isDistinct,
List<Order> order,
int limit,
int offset) {
int offset,
boolean hasDynamicSort) {
super(persistentEntity, predicate, joinPaths);
this.selection = selection;
this.isForUpdate = isForUpdate;
this.isDistinct = isDistinct;
this.order = order;
this.limit = limit;
this.offset = offset;
this.hasDynamicSort = hasDynamicSort;
}

@Override
public boolean hasDynamicSort() {
return hasDynamicSort;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class SubqueryExpression<T> extends AbstractExpression<T> {
private final Type type;
private final PersistentEntitySubquery<T> subquery;

public SubqueryExpression(@NonNull Type type,@NonNull PersistentEntitySubquery<T> subquery) {
public SubqueryExpression(@NonNull Type type, @NonNull PersistentEntitySubquery<T> subquery) {
super(subquery.getExpressionType());
this.type = type;
this.subquery = subquery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public interface QueryBuilder2 {
* @return The encoded query
*/
@NonNull
QueryResult buildPagination(@NonNull Pageable pageable);
String buildPagination(@NonNull Pageable pageable);

/**
* The select query definition.
Expand Down Expand Up @@ -120,6 +120,13 @@ default boolean isDistinct() {
return false;
}

/**
* @return If the query is supposted to
*/
default boolean hasDynamicSort() {
return false;
}

}

/**
Expand Down
Loading

0 comments on commit 3f23f6e

Please sign in to comment.