Commit 10ab662e authored by liaozan's avatar liaozan 🏀

Support bizField index check

parent a04a174e
package com.schbrain.common.util; package com.schbrain.common.util;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Singleton; import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeansException;
import org.springframework.cglib.beans.BeanCopier; import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter; import org.springframework.cglib.core.Converter;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -20,12 +19,11 @@ import java.util.List; ...@@ -20,12 +19,11 @@ import java.util.List;
* @author liaozan * @author liaozan
* @since 2022/1/24 * @since 2022/1/24
*/ */
@Slf4j
public class BeanCopyUtils { public class BeanCopyUtils {
private static final Joiner CACHE_KEY_JOINER = Joiner.on("#"); private static final Joiner CACHE_KEY_JOINER = Joiner.on("#");
private static final ConversionServiceConverter CONVERTER = new ConversionServiceConverter();
/** /**
* copy object list * copy object list
*/ */
...@@ -54,7 +52,7 @@ public class BeanCopyUtils { ...@@ -54,7 +52,7 @@ public class BeanCopyUtils {
return null; return null;
} }
BeanCopier copier = getCopier(source.getClass(), target.getClass()); BeanCopier copier = getCopier(source.getClass(), target.getClass());
copier.copy(source, target, CONVERTER); copier.copy(source, target, DefaultConverter.INSTANCE);
return target; return target;
} }
...@@ -67,18 +65,9 @@ public class BeanCopyUtils { ...@@ -67,18 +65,9 @@ public class BeanCopyUtils {
return CACHE_KEY_JOINER.join(source.getName(), target.getName()); return CACHE_KEY_JOINER.join(source.getName(), target.getName());
} }
@SuppressWarnings("unchecked") private static class DefaultConverter implements Converter {
private static class ConversionServiceConverter implements Converter {
private ConversionService conversionService; private static final DefaultConverter INSTANCE = new DefaultConverter();
private ConversionServiceConverter() {
try {
this.conversionService = SpringUtil.getBean(ConversionService.class);
} catch (BeansException e) {
this.conversionService = null;
}
}
@Override @Override
public Object convert(Object value, Class targetType, Object context) { public Object convert(Object value, Class targetType, Object context) {
...@@ -88,10 +77,11 @@ public class BeanCopyUtils { ...@@ -88,10 +77,11 @@ public class BeanCopyUtils {
if (ClassUtils.isAssignableValue(targetType, value)) { if (ClassUtils.isAssignableValue(targetType, value)) {
return value; return value;
} }
if (conversionService != null && conversionService.canConvert(value.getClass(), targetType)) { Object converted = Convert.convertQuietly(targetType, value, null);
return conversionService.convert(value, targetType); if (converted == null) {
log.warn("Could not copy {} to {}, value type: {}", value, targetType, value.getClass());
} }
return value; return converted;
} }
} }
......
...@@ -39,4 +39,9 @@ public class ColumnMeta { ...@@ -39,4 +39,9 @@ public class ColumnMeta {
*/ */
private String extra; private String extra;
/**
* 索引名称
*/
private String indexName;
} }
\ No newline at end of file
...@@ -20,6 +20,7 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> { ...@@ -20,6 +20,7 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> {
columnMeta.setNullable(isNullable(resultSet.getString("IS_NULLABLE"))); columnMeta.setNullable(isNullable(resultSet.getString("IS_NULLABLE")));
columnMeta.setColumnDefault(resultSet.getString("COLUMN_DEFAULT")); columnMeta.setColumnDefault(resultSet.getString("COLUMN_DEFAULT"));
columnMeta.setExtra(resultSet.getString("EXTRA")); columnMeta.setExtra(resultSet.getString("EXTRA"));
columnMeta.setIndexName(resultSet.getString("INDEX_NAME"));
return columnMeta; return columnMeta;
} }
......
package com.schbrain.framework.autoconfigure.mybatis.constraint; package com.schbrain.framework.autoconfigure.mybatis.constraint;
import com.mysql.cj.MysqlType; import com.mysql.cj.MysqlType;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException; import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException;
import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConstants.*; import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConstants.*;
...@@ -25,6 +27,22 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker { ...@@ -25,6 +27,22 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
checkDeleteVersionField(table); checkDeleteVersionField(table);
} }
@Override
public void checkBizIdField(Table table) {
BizIdColumnField bizColumnField = BizIdHelper.getBizColumnField(table.getTableInfo().getEntityType());
if (bizColumnField == null) {
return;
}
ColumnMeta columnMeta = table.getColumnMeta(bizColumnField.getColumnName());
if (columnMeta == null) {
addMissingFieldError(table, bizColumnField.getColumnName());
return;
}
if (columnMeta.getIndexName() == null) {
addError(table, columnMeta, "BizId field should create an index");
}
}
protected void checkIdField(Table table) { protected void checkIdField(Table table) {
ColumnMeta idColumnMeta = table.getColumnMeta(ID); ColumnMeta idColumnMeta = table.getColumnMeta(ID);
if (idColumnMeta == null) { if (idColumnMeta == null) {
......
...@@ -11,7 +11,7 @@ import java.util.*; ...@@ -11,7 +11,7 @@ import java.util.*;
*/ */
public class DefaultTableMetaDataLoader implements TableMetaDataLoader { public class DefaultTableMetaDataLoader implements TableMetaDataLoader {
public static final String METADATA_QUERY = "SELECT `TABLE_NAME`,`COLUMN_NAME`,`COLUMN_DEFAULT`,`DATA_TYPE`,`EXTRA`,`IS_NULLABLE` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ?"; private static final String METADATA_QUERY = "SELECT c.`TABLE_NAME`,c.`COLUMN_NAME`,c.`COLUMN_DEFAULT`,c.`DATA_TYPE`,c.`EXTRA`,c.`IS_NULLABLE`,s.INDEX_NAME FROM INFORMATION_SCHEMA.COLUMNS c left join INFORMATION_SCHEMA.STATISTICS s on c.TABLE_SCHEMA = s.TABLE_SCHEMA and c.TABLE_NAME = s.TABLE_NAME and c.COLUMN_NAME = s.COLUMN_NAME WHERE c.TABLE_SCHEMA = ?";
private final RowMapper<ColumnMeta> rowMapper = new ColumnMetaRowMapper(); private final RowMapper<ColumnMeta> rowMapper = new ColumnMetaRowMapper();
......
...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; ...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.schbrain.common.util.StreamUtils; import com.schbrain.common.util.StreamUtils;
import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException; import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
...@@ -14,6 +15,7 @@ import java.util.*; ...@@ -14,6 +15,7 @@ import java.util.*;
* @author liaozan * @author liaozan
* @since 2022/9/2 * @since 2022/9/2
*/ */
@Getter
public class Table { public class Table {
private final TableInfo tableInfo; private final TableInfo tableInfo;
...@@ -43,18 +45,10 @@ public class Table { ...@@ -43,18 +45,10 @@ public class Table {
return columnMetaMap.containsKey(column); return columnMetaMap.containsKey(column);
} }
public List<FieldInfo> getFieldInfoList() {
return fieldInfoList;
}
public void addError(TableConstraintException error) { public void addError(TableConstraintException error) {
errors.add(error); errors.add(error);
} }
public List<TableConstraintException> getErrors() {
return errors;
}
@Data @Data
public static class FieldInfo { public static class FieldInfo {
......
...@@ -16,4 +16,9 @@ public interface TableConstraintChecker { ...@@ -16,4 +16,9 @@ public interface TableConstraintChecker {
*/ */
void checkLogicDeleteField(Table table); void checkLogicDeleteField(Table table);
/**
* 检查业务主键 id 字段
*/
void checkBizIdField(Table table);
} }
\ No newline at end of file
...@@ -127,6 +127,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B ...@@ -127,6 +127,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
if (ClassUtils.isAssignable(BaseEntityWithLogicDelete.class, entityClass)) { if (ClassUtils.isAssignable(BaseEntityWithLogicDelete.class, entityClass)) {
checker.checkLogicDeleteField(table); checker.checkLogicDeleteField(table);
} }
checker.checkBizIdField(table);
} }
errors.addAll(table.getErrors()); errors.addAll(table.getErrors());
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment