perf: 简化类型校验代码
This commit is contained in:
@@ -271,48 +271,42 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
if (ObjectHelper.isNotEmpty(queryable.getLike())) {
|
if (ObjectHelper.isNotEmpty(queryable.getLike())) {
|
||||||
queryable.getLike().forEach((column, value) -> {
|
queryable.getLike().forEach((column, value) -> {
|
||||||
var path = column(root, column);
|
var path = column(root, column);
|
||||||
checkString(path, column);
|
checkString(path, value, column);
|
||||||
checkString(value, column);
|
|
||||||
predicates.add(builder.like(column(root, column), value));
|
predicates.add(builder.like(column(root, column), value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getNotLike())) {
|
if (ObjectHelper.isNotEmpty(queryable.getNotLike())) {
|
||||||
queryable.getNotLike().forEach((column, value) -> {
|
queryable.getNotLike().forEach((column, value) -> {
|
||||||
var path = column(root, column);
|
var path = column(root, column);
|
||||||
checkString(path, column);
|
checkString(path, value, column);
|
||||||
checkString(value, column);
|
|
||||||
predicates.add(builder.notLike(column(root, column), value));
|
predicates.add(builder.notLike(column(root, column), value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getGreat())) {
|
if (ObjectHelper.isNotEmpty(queryable.getGreat())) {
|
||||||
queryable.getGreat().forEach((column, value) -> {
|
queryable.getGreat().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value, column);
|
|
||||||
predicates.add(builder.greaterThan(path, (Comparable<Object>) value));
|
predicates.add(builder.greaterThan(path, (Comparable<Object>) value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getLess())) {
|
if (ObjectHelper.isNotEmpty(queryable.getLess())) {
|
||||||
queryable.getLess().forEach((column, value) -> {
|
queryable.getLess().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value, column);
|
|
||||||
predicates.add(builder.lessThan(path, (Comparable<Object>) value));
|
predicates.add(builder.lessThan(path, (Comparable<Object>) value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getGreatEqual())) {
|
if (ObjectHelper.isNotEmpty(queryable.getGreatEqual())) {
|
||||||
queryable.getGreatEqual().forEach((column, value) -> {
|
queryable.getGreatEqual().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value, column);
|
|
||||||
predicates.add(builder.greaterThanOrEqualTo(path, (Comparable<Object>) value));
|
predicates.add(builder.greaterThanOrEqualTo(path, (Comparable<Object>) value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getLessEqual())) {
|
if (ObjectHelper.isNotEmpty(queryable.getLessEqual())) {
|
||||||
queryable.getLessEqual().forEach((column, value) -> {
|
queryable.getLessEqual().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value, column);
|
|
||||||
predicates.add(builder.lessThanOrEqualTo(path, (Comparable<Object>) value));
|
predicates.add(builder.lessThanOrEqualTo(path, (Comparable<Object>) value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -325,18 +319,14 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
if (ObjectHelper.isNotEmpty(queryable.getBetween())) {
|
if (ObjectHelper.isNotEmpty(queryable.getBetween())) {
|
||||||
queryable.getBetween().forEach((column, value) -> {
|
queryable.getBetween().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value.getStart(), column);
|
|
||||||
checkComparable(value.getEnd(), column);
|
|
||||||
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()));
|
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (ObjectHelper.isNotEmpty(queryable.getNotBetween())) {
|
if (ObjectHelper.isNotEmpty(queryable.getNotBetween())) {
|
||||||
queryable.getNotBetween().forEach((column, value) -> {
|
queryable.getNotBetween().forEach((column, value) -> {
|
||||||
var path = this.<Comparable<Object>>column(root, column);
|
var path = this.<Comparable<Object>>column(root, column);
|
||||||
checkComparable(path, column);
|
checkComparable(path, value, column);
|
||||||
checkComparable(value.getStart(), column);
|
|
||||||
checkComparable(value.getEnd(), column);
|
|
||||||
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()).not());
|
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()).not());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -353,21 +343,8 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
* @param column 字段名称
|
* @param column 字段名称
|
||||||
* @throws NotComparableException 当字段类型不可比较时抛出
|
* @throws NotComparableException 当字段类型不可比较时抛出
|
||||||
*/
|
*/
|
||||||
private void checkComparable(Path<?> path, String column) {
|
private void checkComparable(Path<?> path, Object value, String column) {
|
||||||
if (!ObjectHelper.isComparable(path.getJavaType())) {
|
if (!ObjectHelper.isComparable(path.getJavaType()) || !ObjectHelper.isComparable(value)) {
|
||||||
throw new NotComparableException(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查值是否可比较
|
|
||||||
*
|
|
||||||
* @param value 值对象
|
|
||||||
* @param column 字段名称
|
|
||||||
* @throws NotComparableException 当值不可比较时抛出
|
|
||||||
*/
|
|
||||||
private void checkComparable(Object value, String column) {
|
|
||||||
if (!ObjectHelper.isComparable(value)) {
|
|
||||||
throw new NotComparableException(column);
|
throw new NotComparableException(column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -379,9 +356,9 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
* @param column 字段名称
|
* @param column 字段名称
|
||||||
* @throws NotComparableException 当区间值不可比较时抛出
|
* @throws NotComparableException 当区间值不可比较时抛出
|
||||||
*/
|
*/
|
||||||
private void checkComparable(Query.Queryable.Between value, String column) {
|
private void checkComparable(Path<?> path, Query.Queryable.Between value, String column) {
|
||||||
checkComparable(value.getStart(), column);
|
checkComparable(path, value.getStart(), column);
|
||||||
checkComparable(value.getEnd(), column);
|
checkComparable(path, value.getEnd(), column);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -417,21 +394,8 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
* @param column 字段名称
|
* @param column 字段名称
|
||||||
* @throws NotStringException 当字段类型不是字符串时抛出
|
* @throws NotStringException 当字段类型不是字符串时抛出
|
||||||
*/
|
*/
|
||||||
private void checkString(Path<?> path, String column) {
|
private void checkString(Path<?> path, Object value, String column) {
|
||||||
if (!ObjectHelper.isString(path.getJavaType())) {
|
if (!ObjectHelper.isString(path.getJavaType()) || !ObjectHelper.isString(value)) {
|
||||||
throw new NotStringException(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查值是否为字符串
|
|
||||||
*
|
|
||||||
* @param value 值对象
|
|
||||||
* @param column 字段名称
|
|
||||||
* @throws NotStringException 当值不是字符串时抛出
|
|
||||||
*/
|
|
||||||
private void checkString(Object value, String column) {
|
|
||||||
if (!ObjectHelper.isString(value)) {
|
|
||||||
throw new NotStringException(column);
|
throw new NotStringException(column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user