Skip to content

Commit

Permalink
feat-IQueryAbility-可以通过对queryGroup的配置进行and、or复杂条件的拼接查询
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Jan 13, 2025
1 parent efb4cdc commit 26e28ac
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package net.ximatai.muyun.test.core;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.common.mapper.TypeRef;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.ability.ITableCreateAbility;
import net.ximatai.muyun.ability.curd.std.ICURDAbility;
import net.ximatai.muyun.ability.curd.std.IQueryAbility;
import net.ximatai.muyun.core.Scaffold;
import net.ximatai.muyun.database.IDatabaseOperations;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.ColumnType;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.model.PageResult;
import net.ximatai.muyun.model.QueryGroup;
import net.ximatai.muyun.model.QueryItem;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class)
class TestGroupQuery {

private String path = "/test_group_query";

@Inject
IDatabaseOperations databaseOperations;

@Inject
TestGroupQueryController testController;

String tableName;

@BeforeEach
void setUp() {
tableName = testController.getMainTable();
databaseOperations.execute("TRUNCATE TABLE %s".formatted(tableName));

testController.create(Map.of("id", "1", "name", "test1", "av_name", List.of("a"), "t_create", "2024-01-01 12:00:00"));
testController.create(Map.of("id", "2", "name", "test2", "av_name", List.of("a", "b"), "t_create", "2024-01-02 12:00:00"));
testController.create(Map.of("id", "3", "name", "test3", "av_name", List.of("a", "b", "c"), "t_create", "2024-01-03 12:00:00"));
testController.create(Map.of("id", "4", "name", "test4", "av_name", List.of("a", "b", "c", "d"), "t_create", "2024-01-04 12:00:00"));
testController.create(Map.of("id", "5", "name", "test5", "av_name", List.of("a", "b", "c"), "t_create", "2024-01-05 12:00:00"));
testController.create(Map.of("id", "6", "name", "test6", "av_name", List.of("b", "c"), "t_create", "2024-01-06 12:00:00"));
testController.create(Map.of("id", "7", "name", "test7", "av_name", List.of("a", "c"), "t_create", "2024-01-07 12:00:00"));
testController.create(Map.of("id", "8", "name", "test8", "av_name", List.of("c"), "t_create", "2024-01-08 12:00:00"));
}

@Test
@DisplayName("测试id等于 or name等于")
void testEqual() {
Map<String, String> request = Map.of("id", "1", "name", "test3");

PageResult<Map> response = given()
.contentType("application/json")
.queryParam("noPage", true)
.body(request)
.when()
.post("/api%s/view".formatted(path))
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {
});

assertEquals(2, response.getTotal());
}

}

@Path("/test_group_query")
class TestGroupQueryController extends Scaffold implements ICURDAbility, ITableCreateAbility, IQueryAbility {

@Override
public String getSchemaName() {
return "test";
}

@Override
public String getMainTable() {
return "test_table_query";
}

@Override
public void fitOut(TableWrapper wrapper) {
wrapper
.setPrimaryKey(Column.ID_POSTGRES)
.addColumn(Column.of("name").setType(ColumnType.VARCHAR))
.addColumn(Column.of("av_name").setType(ColumnType.VARCHAR_ARRAY))
.addColumn(Column.of("t_create").setDefaultValue("now()"));

}

@Override
public List<QueryItem> queryItemList() {
return List.of(
QueryItem.of("id"),
QueryItem.of("t_create").setTime(true).setSymbolType(QueryItem.SymbolType.RANGE)
);
}

@Override
public QueryGroup queryGroup() {
return QueryItem.of("id").toGroup()
.or(QueryItem.of("name").setSymbolType(QueryItem.SymbolType.EQUAL).toGroup());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IChildrenAbility {
default List<Map> getChildTableList(@Parameter(description = "主表id") @PathParam("id") String id, @Parameter(description = "子表别名") @PathParam("childAlias") String childAlias, @QueryParam("sort") List<String> sort) {
ChildTableInfo ct = getChildTable(childAlias);
String foreignKey = ct.getForeignKey();
return ct.getCtrl().view(null, null, true, sort, Map.of(foreignKey, id), List.of(QueryItem.of(foreignKey))).getList();
return ct.getCtrl().view(null, null, true, sort, Map.of(foreignKey, id), QueryItem.of(foreignKey).toGroup()).getList();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import net.ximatai.muyun.model.PageResult;
import net.ximatai.muyun.model.QueryGroup;
import net.ximatai.muyun.model.QueryItem;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
Expand All @@ -21,6 +22,10 @@ public interface IQueryAbility extends ISelectAbility {

List<QueryItem> queryItemList();

default QueryGroup queryGroup() {
return QueryGroup.of(queryItemList());
}

@GET
@Path("/queryColumns")
@Operation(summary = "已配置可供查询的字段")
Expand All @@ -36,15 +41,15 @@ default PageResult view(@Parameter(description = "页码") @QueryParam("page") I
@Parameter(description = "是否分页") @QueryParam("noPage") Boolean noPage,
@Parameter(description = "排序", example = "t_create,desc") @QueryParam("sort") List<String> sort,
@RequestBody(description = "查询条件信息") Map<String, Object> queryBody) {
return this.view(page, size, noPage, sort, queryBody, queryItemList());
return this.view(page, size, noPage, sort, queryBody, queryGroup());
}

default PageResult query(Map<String, Object> queryBody) {
return this.view(null, null, true, null, queryBody, queryItemList());
return this.view(null, null, true, null, queryBody, queryGroup());
}

default PageResult query(Map<String, Object> queryBody, String authCondition) {
return this.view(null, null, true, null, queryBody, queryItemList(), authCondition);
return this.view(null, null, true, null, queryBody, queryGroup(), authCondition);
}

}
Loading

0 comments on commit 26e28ac

Please sign in to comment.