从树构建 JPA 规范
我创建了一个API,允许用户使用树构建查询。树是从类构建的。SearchOperationRequest
@Data
@ApiModel(value = "SearchOperationRequest", description = "Condition for the query")
public class SearchOperationRequest {
@ApiModelProperty(value = "Conditional statement for the where clause", allowableValues = "EQUALS, NOT_EQUALS, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS")
private SearchConditionOperation condition;
@ApiModelProperty(value = "Column name to be searched on")
private String column;
@ApiModelProperty(value = "Value of column")
private Object value;
@ApiModelProperty(value = "Value of OR / AND")
private SearchComparator comparator;
@ApiModelProperty(value = "Left node of comparator condition")
private SearchOperationRequest left;
@ApiModelProperty(value = "Right node of comparator condition")
private SearchOperationRequest right;
public boolean isTreeLeaf() {
return left == null && right == null;
}
public boolean isComparator() {
return comparator != null;
}
}
因此,从这个例子中,我可以创建一个要求所有隐藏= false和X = 88SearchOperationRequest
"searchOperation": {
"left": {
"column": "Hidden",
"condition": "EQUALS",
"value": false
},
"comparator": "AND",
"right": {
"left": {
"column": "X",
"condition": "EQUALS",
"value": 88
},
"comparator": "AND"
}
}
此请求是使用通用规范生成器内置到规范中的。
public class GenericSpecificationsBuilder<U> {
public Specification<U> buildSpecificationFromSearchOperationRequest(SearchOperationRequest root, Function<SpecificationSearchCriteria, Specification<U>> converter) {
Stack<SearchOperationRequest> stack = new Stack<>();
Stack<SearchOperationRequest> comparatorStack = new Stack<>();
Deque<Specification<U>> specStack = new LinkedList<>();
SearchOperationRequest pointer = root;
while (pointer != null || !stack.empty()) {
if (pointer.isTreeLeaf()) {
specStack.push(converter.apply(new SpecificationSearchCriteria(pointer.getColumn(), pointer.getCondition(), pointer.getValue())));
}
if (pointer.isComparator()) {
comparatorStack.push(pointer);
}
if (pointer.getRight() != null) {
stack.push(pointer.getRight());
}
if (pointer.getLeft() != null) {
pointer.setRight(pointer.getLeft());
pointer.setLeft(null);
} else if (!stack.empty()) {
SearchOperationRequest temp = stack.pop();
pointer.setRight(temp);
}
pointer = pointer.getRight();
}
while (specStack.size() <= comparatorStack.size()) {
comparatorStack.pop();
}
while (!comparatorStack.empty()) {
SearchOperationRequest searchOperationRequest = comparatorStack.pop();
Specification<U> operand1 = specStack.pop();
Specification<U> operand2 = specStack.pop();
if (searchOperationRequest.getComparator().equals(SearchComparator.AND)) {
specStack.push(Specification.where(operand1)
.and(operand2));
} else if (searchOperationRequest.getComparator().equals(SearchComparator.OR)) {
specStack.push(Specification.where(operand1)
.or(operand2));
}
}
return specStack.pop();
}
}
我目前的工作非常适合右重树。含义查询,例如:
WHERE X = 6 AND X = 9
WHERE Z = 5 OR T=9
WHERE Z = 5 OR T=9 OR H=6
但它不适用于构建更复杂的树,其中大括号中的标准应优先并首先执行。
WHERE (X = 6 OR Z = 9) AND (T=6 OR H=8)
这种更复杂的模型是:SearchOperationRequest
"searchOperation": {
"left": {
"left": {
"column": "X",
"condition": "EQUALS",
"value": 6
},
"comparator": "AND",
"right": {
"column": "Z",
"condition": "EQUALS",
"value": 9
}
},
"comparator": "AND",
"right": {
"left": {
"column": "T",
"condition": "EQUALS",
"value": 6
},
"comparator": "AND",
"right": {
"column": "H",
"condition": "EQUALS",
"value": 8
}
}
}
如何修改 my 以能够处理更复杂的树?GenericSpecificationsBuilder
SearchOperationRequest