[英]Dynamically building conditions in JOOQ
I have an endpoint that receives a string with all the columns and value they want to filter by. 我有一个端点,该端点接收一个包含所有列和它们要作为过滤依据的值的字符串。 Here's an example of what the filters param looks like:
?filter=active=true,type=ADMIN
, basically each filter is separated by a ,
and then the column name and filter value are separated by an =
So I made this method to dynamically build a condition statement with all the different filters for any table: 这是过滤器参数的示例:
?filter=active=true,type=ADMIN
,基本上每个过滤器都用a分隔,
然后列名和过滤器值用=
分隔,所以我使此方法动态使用任何表的所有不同过滤器构建条件语句:
public Condition mapFilterToCondition(String tableName, String filters) {
Condition condition = DSL.trueCondition();
String[] filterFields = filters.split(",");
Table<?> table = PUBLIC.getTable(tableName);
Field<?>[] fields = table.fields();
for(String filterField : filterFields) {
String[] filter = filterField.split("=");
for(Field<?> field : fields) {
String fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, filter[0]);
if(field.getName().equals(fieldName)) {
if(field.getType() == String.class) {
condition.and(table.field(field).like(filter[1]));
} else if (Boolean.valueOf(filter[1])) {
condition.and(table.field(field).isTrue());
} else if (!Boolean.valueOf(filter[1])) {
condition.and(table.field(field).isFalse());
}
}
}
}
return condition;
}
I then use the condition built by this method to call a repository that makes the sql request: 然后,我使用此方法构建的条件来调用发出sql请求的存储库:
public List<UserAccount> findAllByFilter(Condition condition, Pageable pageable) {
return dsl.select()
.from(USER_ACCOUNT)
.where(condition)
.limit(pageable.getPageSize())
.offset((int) pageable.getOffset())
.fetchInto(UserAccount.class);
}
As I understood, the condition should have something like this: 据我了解,该条件应具有以下内容:
.where(USER_ACCOUNT.TYPE.like("ADMIN"))
.where(USER_ACCOUNT.ACTIVE.isTrue())
.where(USER_ACCOUNT.TYPE.like("ADMIN"))
.where(USER_ACCOUNT.ACTIVE.isTrue())
based on the condition I built, but whenever I make the call, it returns every single user, not taking into account the filters. 根据我建立的条件,但是每当我打电话时,它都会返回每个用户,而不考虑过滤器。 While debugging I found out the condition is never changing from
1 = 1
, which is the value it has from DSL.trueCondition();
在调试时,我发现条件永远不会从
1 = 1
更改,这是它从DSL.trueCondition();
获得的值DSL.trueCondition();
, so my question is how I should be handling the construction of this condition? ,所以我的问题是我应该如何处理这种情况? Am I using
condition.and(...)
wrong, or should I be using something else instead? 我是否使用
condition.and(...)
错误,还是应该使用其他东西?
Any help is appreciated! 任何帮助表示赞赏!
EDIT 编辑
@GetMapping(value = "/admin", produces = "application/json")
public ResponseEntity listAll(String filters, Pageable pageable) {
Condition condition = filterMapService.mapFilterToCondition("user_account", filters);
List<UserAccount> adminAccounts = userAccountRepository.findAllByFilter(condition, pageable);
if (adminAccounts.isEmpty()) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
List<AdminDTO> accounts = adminAccounts.stream()
.map(account -> modelMapper.map(account, AdminDTO.class)).collect(Collectors.toList());
return new ResponseEntity<>(accounts, HttpStatus.OK);
}
Change all references to 将所有引用更改为
condition.and(...)
to: 至:
condition = condition.and(...)
The value of the Condition object is not actually getting updated. Condition对象的值实际上并没有得到更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.