Skip to content

Commit

Permalink
Merge branch 'issue-4-fix-sum-and-count-for-cb-4.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sükein Alex committed May 31, 2017
2 parents 91951b2 + 4467bf7 commit 292bcd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/wanari/utils/couchbase/CouchbasePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CouchbasePage<T> {
public List<T> data;
Expand All @@ -11,12 +13,27 @@ public class CouchbasePage<T> {
public int size;
public int totalPages;

private CouchbasePage() {
}

public CouchbasePage(Pageable pageable) {
pageNumber = pageable.getPageNumber();
size = pageable.getPageSize();
}

private <U> CouchbasePage(CouchbasePage<U> other, Function<U, T> mapper) {
data = other.data.stream().map(mapper).collect(Collectors.toList());
totalElements = other.totalElements;
pageNumber = other.pageNumber;
size = other.size;
totalPages = other.totalPages;
}

public void calculateTotalPages() {
totalPages = ((int) totalElements - 1) / size + 1;
}

public <R> CouchbasePage<R> map(Function<T, R> mapper) {
return new CouchbasePage<>(this, mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,16 @@ private Statement createCountStatement(JsonObject params) {
Expression bucketName = i(couchbaseConfiguration.getBucketName());
return count(bucketName)
.from(bucketName)
.where(composeWhere(bucketName, params));
.where(composeWhere(bucketName, params))
.groupBy(meta(bucketName));
}

private Statement createSumStatement(JsonObject params, String field) {
Expression bucketName = i(couchbaseConfiguration.getBucketName());
return sum(bucketName, field)
.from(bucketName)
.where(composeWhere(bucketName, params));
.where(composeWhere(bucketName, params))
.groupBy(meta(bucketName));
}

private Statement createQueryStatement(JsonObject params, Pageable pageable) {
Expand Down Expand Up @@ -150,6 +152,9 @@ private FromPath selectWithMeta(Expression bucketName) {
return select(bucketName + " as data, meta(" + bucketName + ").id AS id ");
}

private String meta(Expression bucketName) {
return "meta(" + bucketName + ").id";
}

private Expression composeWhere(Expression bucketName, JsonObject params) {
List<Expression> expressions = params.getNames()
Expand Down

0 comments on commit 292bcd9

Please sign in to comment.