Java Code Examples for org.springframework.data.repository.query.ParameterAccessor#getPageable()

The following examples show how to use org.springframework.data.repository.query.ParameterAccessor#getPageable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartTreeFirestoreQuery.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute(Object[] parameters) {
	StructuredQuery.Builder builder = createBuilderWithFilter(parameters);

	// Handle Pageable parameters.
	if (!getQueryMethod().getParameters().isEmpty()) {
		ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(),
				parameters);
		Pageable pageable = paramAccessor.getPageable();
		if (pageable != null && pageable.isPaged()) {
			builder.setOffset((int) Math.min(Integer.MAX_VALUE, pageable.getOffset()));
			builder.setLimit(Int32Value.newBuilder().setValue(pageable.getPageSize()));
		}

		Sort sort = paramAccessor.getSort();
		if (sort != null) {
			builder.addAllOrderBy(createFirestoreSortOrders(sort));
		}
	}

	if (this.tree.isCountProjection()) {
		return this.reactiveOperations.count(this.persistentEntity.getType(), builder);
	}
	else {
		return this.reactiveOperations.execute(builder, this.persistentEntity.getType());
	}
}
 
Example 2
Source File: AbstractDynamoDBQuery.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute(AbstractDynamoDBQuery<T, ID> dynamoDBQuery, Object[] values) {

	ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
	Pageable pageable = accessor.getPageable();
	Query<T> query = dynamoDBQuery.doCreateQueryWithPermissions(values);

	List<T> results = query.getResultList();
	return createPage(results, pageable,dynamoDBQuery,values);
}
 
Example 3
Source File: AbstractDynamoDBQuery.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute(AbstractDynamoDBQuery<T, ID> dynamoDBQuery, Object[] values) {

	ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
	Pageable pageable = accessor.getPageable();
	Query<T> query = dynamoDBQuery.doCreateQueryWithPermissions(values);
	List<T> results = query.getResultList();
	return createSlice(results, pageable);
}
 
Example 4
Source File: PartTreeDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public Object execute(Object[] parameters) {
	Class<?> returnedObjectType = getQueryMethod().getReturnedObjectType();
	if (isPageQuery()) {
		ExecutionResult executionResult = (ExecutionResult) runQuery(parameters, returnedObjectType, List.class,
				false);

		List<?> resultEntries = (List) executionResult.getPayload();

		ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(),
				parameters);

		Pageable pageableParam = paramAccessor.getPageable();

		Long totalCount;
		if (pageableParam instanceof DatastorePageable) {
			Long previousCount = ((DatastorePageable) pageableParam).getTotalCount();
			Assert.notNull(previousCount, "Previous total count can not be null.");

			totalCount = ((DatastorePageable) pageableParam).getTotalCount();
		}
		else {
			totalCount = (Long) runQuery(parameters, Long.class, null, true);
		}

		Pageable pageable = DatastorePageable.from(pageableParam, executionResult.getCursor(), totalCount);

		return new PageImpl<>(resultEntries, pageable, totalCount);
	}

	if (isSliceQuery()) {
		return executeSliceQuery(parameters);
	}

	Object result = runQuery(parameters, returnedObjectType,
			((DatastoreQueryMethod) getQueryMethod()).getCollectionReturnType(), false);

	result = result instanceof PartTreeDatastoreQuery.ExecutionResult ? ((ExecutionResult) result).getPayload()
			: result;
	if (result == null && ((DatastoreQueryMethod) getQueryMethod()).isOptionalReturnType()) {
		return Optional.empty();
	}
	return result;
}
 
Example 5
Source File: PartTreeDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private StructuredQuery applyQueryBody(Object[] parameters,
		Builder builder, boolean total, boolean singularResult, Cursor cursor) {
	ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
	if (this.tree.hasPredicate()) {
		applySelectWithFilter(parameters, builder);
	}

	Pageable pageable = paramAccessor.getPageable();
	Integer limit = null;
	Integer offset = null;
	if (singularResult || this.tree.isExistsProjection()) {
		limit = 1;
	}
	else if (this.tree.isLimiting()) {
		limit = this.tree.getMaxResults();
	}

	if (!singularResult && !total && pageable.isPaged()) {
		limit = pageable.getPageSize();
	}

	Sort sort = this.tree.getSort();
	if (getQueryMethod().getParameters().hasPageableParameter()) {
		sort = sort.and(pageable.getSort());
	}
	if (getQueryMethod().getParameters().hasSortParameter()) {
		sort = sort.and(paramAccessor.getSort());
	}

	if (pageable.isPaged() && !total) {
		offset = (int) pageable.getOffset();
	}

	Cursor cursorToApply = null;
	if (cursor != null) {
		cursorToApply = cursor;
	}
	else if (pageable instanceof DatastorePageable) {
		cursorToApply = ((DatastorePageable) pageable).toCursor();
	}
	DatastoreTemplate.applyQueryOptions(
			builder, new DatastoreQueryOptions.Builder().setLimit(limit).setOffset(offset).setSort(sort)
					.setCursor(cursorToApply).build(),
			this.datastorePersistentEntity);
	return builder.build();
}
 
Example 6
Source File: GqlDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
ParsedQueryWithTagsAndValues(List<String> initialTags, Object[] rawParams) {
	this.params = Arrays.stream(rawParams).filter(e -> !(e instanceof Pageable || e instanceof Sort))
			.collect(Collectors.toList());
	this.rawParams = rawParams;
	this.tagsOrdered = new ArrayList<>(initialTags);

	SpelEvaluator spelEvaluator = GqlDatastoreQuery.this.evaluatingSpelQueryContext.parse(
			GqlDatastoreQuery.this.gqlResolvedEntityClassName,
			GqlDatastoreQuery.this.queryMethod.getParameters());
	Map<String, Object> results = spelEvaluator.evaluate(this.rawParams);
	this.finalGql = spelEvaluator.getQueryString();

	for (Map.Entry<String, Object> entry : results.entrySet()) {
		this.params.add(entry.getValue());
		// Cloud Datastore requires the tag name without the @
		this.tagsOrdered.add(entry.getKey().substring(1));
	}

	ParameterAccessor paramAccessor =
			new ParametersParameterAccessor(getQueryMethod().getParameters(), rawParams);
	Sort sort = paramAccessor.getSort();

	this.finalGql = addSort(this.finalGql, sort);

	this.noLimitQuery = this.finalGql;
	Pageable pageable = paramAccessor.getPageable();
	if (!pageable.equals(Pageable.unpaged())) {
		this.finalGql += LIMIT_CLAUSE;
		this.tagsOrdered.add(LIMIT_TAG_NAME);
		this.limitPosition = this.params.size();
		this.params.add(pageable.getPageSize());

		this.finalGql += OFFSET_CLAUSE;
		this.tagsOrdered.add(OFFSET_TAG_NAME);
		this.cursorPosition = this.params.size();
		if (pageable instanceof DatastorePageable && ((DatastorePageable) pageable).toCursor() != null) {
			this.params.add(((DatastorePageable) pageable).toCursor());
		}
		else {
			this.params.add(pageable.getOffset());
		}
	}
}