cn.handyplus.lib.db.Db Maven / Gradle / Ivy
package cn.handyplus.lib.db;
import cn.handyplus.lib.annotation.TableField;
import cn.handyplus.lib.annotation.TableName;
import cn.handyplus.lib.core.CollUtil;
import cn.handyplus.lib.core.StrUtil;
import cn.handyplus.lib.db.func.DbFunction;
import cn.handyplus.lib.db.param.FieldInfoParam;
import cn.handyplus.lib.db.param.TableInfoParam;
import lombok.Getter;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/**
* 处理器
*
* @author handy
* @since 1.4.8
*/
public class Db {
/**
* 类
*/
private final Class clazz;
/**
* DbSql
*/
@Getter
private DbSql dbSql;
/**
* 构造
*
* @param clazz 类
*/
public Db(Class clazz) {
this.clazz = clazz;
init();
}
/**
* 初始化使用
*
* @param clazz 类
* @param T
* @return T
*/
public static Db use(Class clazz) {
return new Db<>(clazz);
}
/**
* 查询
*
* @param fn 字段
* @param R
*/
@SafeVarargs
public final void select(DbFunction... fn) {
List fieldList = new ArrayList<>();
for (DbFunction rDbFunction : fn) {
fieldList.add(DbColumnUtil.getFieldName(rDbFunction));
}
this.dbSql.setField(CollUtil.listToStr(fieldList));
this.dbSql.getFieldInfoMap().entrySet().removeIf(entry -> !fieldList.contains(entry.getKey()));
}
/**
* 条件构造器
*
* @return 条件构造器
*/
public Compare where() {
return new Compare<>(dbSql);
}
/**
* 更新构造器
*
* @return 更新构造器
*/
public UpdateCondition update() {
return new UpdateCondition<>(dbSql);
}
/**
* 执行器
*
* @return 执行器
*/
public DbExecution execution() {
return new DbExecution<>(dbSql, clazz);
}
/**
* 创建表
*
* @since 3.10.9
*/
public void createTable() {
new DbExecution<>(dbSql, clazz, false).create();
}
/**
* 执行器
*
* @param storageMethod 存储方式
* @return 执行器
*/
public DbExecution execution(String storageMethod) {
return new DbExecution<>(dbSql, clazz, storageMethod);
}
/**
* 执行器
*
* @param connection 链接
* @return 执行器
* @since 3.10.8
*/
public DbExecution execution(Connection connection) {
return new DbExecution<>(dbSql, clazz, connection);
}
/**
* 构建基础条件
*/
private void init() {
TableName tableName = this.clazz.getAnnotation(TableName.class);
if (tableName == null) {
throw new RuntimeException("tableName 为空");
}
// 表信息
TableInfoParam tableInfoParam = TableInfoParam.builder().tableName(tableName.value()).tableComment(tableName.comment()).build();
Field[] fields = this.clazz.getDeclaredFields();
List fieldList = new ArrayList<>();
LinkedHashMap fieldInfoMap = new LinkedHashMap<>();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null && StrUtil.isNotEmpty(tableField.value())) {
fieldList.add(DbConstant.POINT + tableField.value() + DbConstant.POINT);
FieldInfoParam build = FieldInfoParam.builder()
.fieldName(tableField.value())
.fieldRealName(field.getName())
.fieldType(field.getGenericType().getTypeName())
.fieldComment(tableField.comment())
.fieldNotNull(tableField.notNull())
.fieldDefault(tableField.fieldDefault())
.fieldIndex(i + 1)
.fieldLength(tableField.length())
.indexEnum(tableField.indexEnum())
.build();
fieldInfoMap.put(tableField.value(), build);
}
}
if (CollUtil.isEmpty(fieldList)) {
throw new RuntimeException("fieldList 为空");
}
this.dbSql = DbSql.builder()
.tableName(DbConstant.POINT + tableName.value() + DbConstant.POINT)
.tableInfoParam(tableInfoParam)
.field(CollUtil.listToStr(fieldList))
.fieldInfoMap(fieldInfoMap)
.where(DbConstant.DEFAULT_WHERE)
.updatefieldList(new ArrayList<>())
.updateFieldMap(new LinkedHashMap<>())
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy