All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
shz.core.orm.OrmServiceHelper Maven / Gradle / Ivy
package shz.core.orm;
import shz.core.*;
import shz.core.id.IdProducer;
import shz.core.orm.annotation.Column;
import shz.core.orm.annotation.Id;
import shz.core.orm.annotation.Logic;
import shz.core.orm.annotation.OrderBy;
import shz.core.orm.annotation.Version;
import shz.core.queue.a.IArrayQueue;
import shz.core.transaction.TransactionDefinition;
import shz.core.type.TypeHelp;
import shz.core.orm.enums.DataType;
import shz.core.orm.sql.builder.SqlBuilder;
import shz.core.orm.sql.handler.SqlHandler;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
abstract class OrmServiceHelper {
protected SqlHandler sqlHandler;
public final SqlHandler getSqlHandler() {
return sqlHandler;
}
public final void setSqlHandler(SqlHandler sqlHandler) {
this.sqlHandler = sqlHandler;
}
public final SqlBuilder builder() {
return new SqlBuilder(sqlHandler);
}
public final SqlBuilder builder(int capacity) {
return new SqlBuilder(sqlHandler, capacity);
}
/**
* 实体域是否存在于数据库列中
*/
protected boolean existField(Field field) {
if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()) || field.isAnnotationPresent(OrderBy.class))
return false;
Column column = field.getAnnotation(Column.class);
return column == null || column.exist();
}
/**
* 获取实体域对应的列名
*/
protected String columnName(Field field) {
Id id = field.getAnnotation(Id.class);
if (id != null) return NullHelp.isBlank(id.value()) ? humpToUnderline(field.getName()) : id.value();
Column column = field.getAnnotation(Column.class);
return column == null || NullHelp.isBlank(column.value()) ? humpToUnderline(field.getName()) : column.value();
}
/**
* 实体域名转成数据库列名
*/
protected String humpToUnderline(String name) {
return StringHelp.humpToUnderlineLowerCase(name);
}
/**
* 是否主键
*/
protected boolean isIdField(Field field) {
return field.isAnnotationPresent(Id.class) || "id".equals(field.getName());
}
/**
* 主键是否自增
*/
protected boolean auto(Field field) {
Id id = field.getAnnotation(Id.class);
return id != null && id.auto();
}
/**
* id生成器
*/
protected IdProducer idProducer(Tnp tnp, Field field) {
if (auto(field)) return null;
Id id = field.getAnnotation(Id.class);
Class extends OrmIdProducerSupplier> generate;
if (id == null) generate = OrmIdProducerSupplier.class;
else generate = id.generate();
Supplier maxId = () -> NumberHelp.getLong(((OrmService) this).maxColumn(tnp, null, field.getName(), null));
if (generate == OrmIdProducerSupplier.class)
return defaultIdProducerSupplier().apply(new OrmIdProducerSupplier.Param(tnp, maxId));
OrmIdProducerSupplier> supplier = Container.get(generate, () -> AccessibleHelp.newInstance(generate));
NullHelp.requireNonNull(supplier, "id生成提供类:%s无法实例化", generate.getName());
return supplier.apply(new OrmIdProducerSupplier.Param(tnp, maxId));
}
protected OrmIdProducerSupplier> defaultIdProducerSupplier() {
return OrmSFIdProducerSupplier.DEFAULT;
}
/**
* 实体域是否对应逻辑删除字段
*/
protected boolean isLogicField(Field field) {
return field.isAnnotationPresent(Logic.class);
}
/**
* 该值代表没有逻辑删除
*/
protected Object logicVal(Field field) {
Logic logic = field.getAnnotation(Logic.class);
if (logic == null || NullHelp.isBlank(logic.value())) return null;
return Objects.requireNonNull(FieldSetter.cast(logic.value(), field.getType()));
}
/**
* 该值代表已经逻辑删除
*/
protected Object logicDelVal(Field field) {
Logic logic = field.getAnnotation(Logic.class);
if (logic == null || NullHelp.isBlank(logic.delValue())) return null;
return Objects.requireNonNull(FieldSetter.cast(logic.delValue(), field.getType()));
}
/**
* 实体域是否对应版本字段
*/
protected boolean isVersionField(Field field) {
return field.isAnnotationPresent(Version.class);
}
/**
* 插入时填充
*/
protected void insertFill(MetaObject metaObject) {
Object val = metaObject.getFieldValByName("createBy");
if (val == null) {
Long currentUser = currentUser();
if (currentUser != null) metaObject.setFieldValByName("createBy", currentUser);
}
val = metaObject.getFieldValByName("createTime");
if (val == null) metaObject.setFieldValByName("createTime", LocalDateTime.now());
//移除更新才设置的值
metaObject.removeFieldByName("updateBy");
metaObject.removeFieldByName("updateTime");
metaObject.removeFieldByName("delFlag");
metaObject.removeFieldByName("dataVersion");
}
/**
* 获取当前用户
*/
protected Long currentUser() {
return 0L;
}
/**
* 更新时填充
*/
protected void updateFill(MetaObject metaObject) {
Object val = metaObject.getFieldValByName("updateBy");
if (val == null) {
Long currentUser = currentUser();
if (currentUser != null) metaObject.setFieldValByName("updateBy", currentUser);
}
val = metaObject.getFieldValByName("updateTime");
if (val == null) metaObject.setFieldValByName("updateTime", LocalDateTime.now());
//移除插入才设置的值
metaObject.removeFieldByName("createBy");
metaObject.removeFieldByName("createTime");
metaObject.removeFieldByName("delFlag");
}
protected void backId(MetaObject metaObject) {
if (metaObject.classInfo.auto) metaObject.cp.remove(metaObject.classInfo.idName);
else if (metaObject.classInfo.idProducer != null) {
Object id = metaObject.cp.get(metaObject.classInfo.idName);
if (id == null) {
id = metaObject.classInfo.idProducer.next();
metaObject.cp.put(metaObject.classInfo.idName, id);
metaObject.setId(id);
}
}
}
protected DataType dataType(Type type) {
Class> cls = TypeHelp.toClass(type);
if (cls == null || Map.class.isAssignableFrom(cls)) return DataType.MAP;
if (Collection.class.isAssignableFrom(cls)) return DataType.LIST;
if (cls.isArray()) return DataType.ARRAY;
if (TypeHelp.likeCommon(cls)) return DataType.COMMON;
boolean nested = false;
for (Field f : AccessibleHelp.fields(cls)) {
Column column = f.getAnnotation(Column.class);
if (column != null && column.ignoreNested()) continue;
Class> fCls = TypeHelp.fieldClass(f, type);
if (Map.class.isAssignableFrom(fCls)
|| Collection.class.isAssignableFrom(fCls)
|| fCls.isArray()) return DataType.MERGE;
if (!nested && !TypeHelp.likeCommon(fCls)) nested = true;
}
return nested ? DataType.NESTED : DataType.DEFAULT;
}
protected int batchSize(int batchSize) {
if (batchSize == 0) return 2000;
if (batchSize < 0 || batchSize > 6000) return 6000;
return batchSize;
}
/**
* 事务控制 执行方法
*
* @param action 需要执行的方法
*/
public abstract T apply(Supplier extends T> action, TransactionDefinition definition);
public final T apply(Supplier extends T> action) {
return apply(action, TransactionDefinition.withDefaults());
}
public final void accept(Runnable action, TransactionDefinition definition) {
apply(() -> {
action.run();
return null;
}, definition);
}
public final void accept(Runnable action) {
accept(action, TransactionDefinition.withDefaults());
}
public static boolean fail(int row) {
return row != 1;
}
public static boolean batchFail(int[] rows) {
return Arrays.stream(rows).anyMatch(row -> row != 1);
}
public static int[] batchFailIdx(int[] rows) {
IArrayQueue queue = IArrayQueue.of();
int i = 0;
for (; i < rows.length; ++i) if (rows[i] != 1) queue.offer(i);
return queue.toArray();
}
public static Map> groupBatchResult(List entities, int[] rows, Function super T, ? extends R> mapper) {
int[] failIdx = batchFailIdx(rows);
int size = entities.size();
if (failIdx.length == 0)
return ToMap.get(1).put(Boolean.TRUE, ToList.explicitCollect(entities.stream().map(mapper), size)).build();
Map> result = ToMap.get(2).build();
List successList = new ArrayList<>(size - failIdx.length);
List failList = new ArrayList<>(failIdx.length);
result.put(Boolean.TRUE, successList);
result.put(Boolean.FALSE, failList);
for (int i = 0, j = 0; i < size; ++i) {
if (j < failIdx.length && i == failIdx[j]) {
failList.add(mapper.apply(entities.get(i)));
++j;
} else successList.add(mapper.apply(entities.get(i)));
}
return result;
}
}