shz.core.orm.OrderByInfo Maven / Gradle / Ivy
package shz.core.orm;
import shz.core.*;
import shz.core.model.PageInfo;
import shz.core.model.Range;
import shz.core.reference.ZReference;
import shz.core.structure.limiter.Limiter;
import shz.core.type.TypeHelp;
import shz.core.orm.annotation.OrderBy;
import shz.core.orm.exception.OrmClassNoFieldException;
import shz.core.orm.param.OrmParam;
import shz.core.orm.sql.builder.SqlBuilder;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.util.*;
final class OrderByInfo {
private static final class OrderByFieldInfo {
final Field field;
final String columnName;
final String fieldName;
final int sort;
OrderByFieldInfo(Field field, String columnName, String fieldName, int sort) {
this.field = field;
this.columnName = columnName;
this.fieldName = fieldName;
this.sort = sort;
}
}
static void orderBy(ClassInfo classInfo, Object obj, SqlBuilder builder) {
if (NullHelp.isBlank(obj)) return;
if (obj instanceof Map, ?>) {
Map, ?> map = (Map, ?>) obj;
ZReference mark = new ZReference(true);
map.forEach((k, v) -> orderBy(classInfo, null, k, v, builder, mark));
return;
}
Class> cls;
if (!TypeHelp.likeModel(cls = obj.getClass())) return;
List orderByFields = orderByFields(classInfo, cls);
if (orderByFields.isEmpty()) return;
ZReference mark = new ZReference(true);
orderByFields.forEach(info -> orderBy(classInfo, info.columnName, null, AccessibleHelp.getField(info.field, obj), builder, mark));
}
private static void orderBy(ClassInfo classInfo, String column, Object fieldName, Object fieldValue, SqlBuilder builder, ZReference mark) {
if (fieldValue == null) return;
String columnName = column == null ? classInfo.toColumnName(ToString.normal(fieldName)) : column;
if (columnName == null) throw new OrmClassNoFieldException(classInfo.cls, ToString.normal(fieldName));
if (mark.get()) {
mark.set(false);
builder.orderBy();
} else builder.comma();
builder.wrap(columnName).space();
if (Help.isTrue(fieldValue)) builder.desc();
else builder.asc();
}
private static List orderByFields(ClassInfo classInfo, Class> cls) {
List list = new ArrayList<>();
AccessibleHelp.fields(cls, f -> {
OrderBy orderBy = f.getAnnotation(OrderBy.class);
if (orderBy == null) return false;
String fieldName = NullHelp.isBlank(orderBy.value()) ? f.getName() : orderBy.value();
String columnName = classInfo.toColumnName(fieldName);
if (columnName == null) throw new OrmClassNoFieldException(classInfo.cls, fieldName);
list.add(new OrderByFieldInfo(f, columnName, fieldName, orderBy.sort()));
return false;
});
list.sort(Comparator.comparingInt(a -> a.sort));
return list;
}
static void orderBy(ClassInfo classInfo, Parameter[] parameters, Object[] args, SqlBuilder builder) {
if (parameters.length == 0) return;
ZReference mark = new ZReference(true);
for (int i = 0; i < parameters.length; ++i) {
OrderBy orderBy = parameters[i].getAnnotation(OrderBy.class);
if (orderBy == null) continue;
Class> pt = parameters[i].getType();
if (PageInfo.class.isAssignableFrom(pt)
|| Limiter.class.isAssignableFrom(pt)
|| OrmParam.class.isAssignableFrom(pt)
|| Collection.class.isAssignableFrom(pt)
|| Range.class.isAssignableFrom(pt)
| pt.isArray())
continue;
if (args[i] != null && Map.class.isAssignableFrom(pt)) {
((Map, ?>) args[i]).forEach((k, v) -> orderBy(classInfo, null, k, v, builder, mark));
continue;
}
if (TypeHelp.likeCommon(pt)) {
orderBy(classInfo, null, orderBy.value(), args[i], builder, mark);
} else for (OrderByFieldInfo info : orderByFields(classInfo, pt))
orderBy(classInfo, info.columnName, null, AccessibleHelp.getField(info.field, args[i]), builder, mark);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy