Skip to content

Commit

Permalink
[V 1.3.0]feat:add aggregate query
Browse files Browse the repository at this point in the history
  • Loading branch information
darmi_zhang committed Nov 2, 2023
1 parent c62654b commit 8812295
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.darmi7</groupId>
<artifactId>mongo-lambda-query</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.darmi.plugin.core;

import com.darmi.plugin.spring.SpringUtil;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.bson.Document;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

/**
* @author darmi
Expand All @@ -17,9 +21,11 @@ public abstract class MongoAbstractLambdaQuery<
T, Children extends MongoAbstractLambdaQuery<T, Children>>
extends AbstractQuery<T, SFunction<T, ?>, Children> {

private MongoTemplate mongoTemplate;
public static final int ZERO = 0;

private CombineSqlSegment combineSqlSegment;
protected MongoTemplate mongoTemplate;

private SqlSegment<T> sqlSegment;

public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
Expand All @@ -31,34 +37,39 @@ public Page<T> page(PaginationDTO pagination) {
Pageable pageable;
if (pagination.sortIsNotEmpty()) {
pageable =
PageRequest.of(
pagination.getPage(),
pagination.getPageSize(),
Sort.by(
Sort.Direction.fromString(pagination.getSort().getDirection()),
pagination.getSort().getSortBy()));
PageRequest.of(
pagination.getPage(),
pagination.getPageSize(),
Sort.by(
Sort.Direction.fromString(pagination.getSort().getDirection()),
pagination.getSort().getSortBy()));
} else {
pageable = PageRequest.of(pagination.getPage(), pagination.getPageSize());
}
Query query = combineSqlSegment.getQuery();
return new PageImpl<>(
mongoTemplate.find(query.with(pageable), entityClass),
pageable,
mongoTemplate.count(query, entityClass));
Query query = sqlSegment.getQuery();
List<T> list = mongoTemplate.find(query.with(pageable), entityClass);
rollBackPageConfig(query);
long count = mongoTemplate.count(query, entityClass);
return new PageImpl<>(list, pageable, count);
}
return new PageImpl<>(list());
}

@Override
public Page<T> aggregate(PaginationDTO pagination) {
Document aggregateDocument = sqlSegment.getAggregateDocument(pagination, entityClass);
Document cursor = mongoTemplate.executeCommand(aggregateDocument).get("cursor", Document.class);
return getResult(pagination, cursor);
}

@Override
public T one() {
Query query = combineSqlSegment.getQuery();
return this.mongoTemplate.findOne(query, entityClass);
return this.mongoTemplate.findOne(sqlSegment.getQuery(), entityClass);
}

@Override
public List<T> list() {
Query query = combineSqlSegment.getQuery();
return this.mongoTemplate.find(query, entityClass);
return this.mongoTemplate.find(sqlSegment.getQuery(), entityClass);
}


Expand All @@ -69,17 +80,49 @@ public Children sort(SFunction<T, ?> column, Sort.Direction direction) {

@Override
protected final void columnToSqlSegment(
SFunction<T, ?> column, Object val, SqlKeyword keyWord, Object... key) {
combineSqlSegment.combineSqlSegment(val, keyWord, LambdaUtils.getField(column), key);
SFunction<T, ?> column, Object val, SqlKeyword keyWord, Object... key) {
sqlSegment.combine(val, keyWord, columnToString(column), key);
}

@Override
protected void columnToSqlSegment(String column, Object val, SqlKeyword keyWord, Object... key) {
combineSqlSegment.combineSqlSegment(val, keyWord, column, key);
sqlSegment.combine(val, keyWord, column, key);
}

protected void initNeed(){
@Override
protected String columnToString(SFunction<T, ?> column) {
return LambdaUtils.getField(column);
}

protected void initNeed() {
mongoTemplate = SpringUtil.getBean(MongoTemplate.class);
combineSqlSegment = new CombineSqlSegment();
sqlSegment = new SqlSegment<>();
}

private void rollBackPageConfig(Query query) {
query.skip(ZERO);
query.limit(Integer.MAX_VALUE);
}

private PageImpl<T> getResult(PaginationDTO pagination, Document cursor) {
List<Document> firstBatch = cursor.get("firstBatch", List.class);
if (CollectionUtils.isEmpty(firstBatch)) {
return new PageImpl<>(Collections.emptyList());
}
List<Document> metadata = firstBatch.get(ZERO).get("metadata", List.class);
List<T> list = getContent(firstBatch);
cursor.clear();
int total = CollectionUtils.isEmpty(metadata) ? ZERO : metadata.get(ZERO).getInteger("total");
return pagination == null ? new PageImpl<>(list)
: new PageImpl<>(list, PageRequest.of(pagination.getPage(), pagination.getPageSize()),
total);
}

private List<T> getContent(List<Document> firstBatch) {
List<Document> data = firstBatch.get(ZERO).get("data", List.class);
return CollectionUtils.isEmpty(data) ? Collections.emptyList()
: data.stream().map(e -> mongoTemplate.getConverter().read(entityClass, e))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ public MongoLambdaQuery(Class<T> entityClass, MongoTemplate mongoTemplate) {
super.setMongoTemplate(mongoTemplate);
}

@Override
protected String columnToString(SFunction<T, ?> column) {
return LambdaUtils.getField(column);
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/com/darmi/plugin/core/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface Query<T> {

Page<T> page(PaginationDTO pagination);

Page<T> aggregate(PaginationDTO pagination);

}
22 changes: 22 additions & 0 deletions core/src/main/java/com/darmi/plugin/core/SortEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.darmi.plugin.core;


public enum SortEnum {
DESC("desc", -1),
ASC("asc", 1);
private String queryCode;
private Integer mongoCode;

public String getQueryCode() {
return queryCode;
}

public Integer getMongoCode() {
return mongoCode;
}

SortEnum(String queryCode, Integer mongoCode) {
this.queryCode = queryCode;
this.mongoCode = mongoCode;
}
}
Loading

0 comments on commit 8812295

Please sign in to comment.