Commit 794edf56 authored by liaozan's avatar liaozan 🏀

Polish

parent 8db1d48e
...@@ -17,15 +17,15 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> { ...@@ -17,15 +17,15 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> {
columnMeta.setTableName(resultSet.getString("TABLE_NAME")); columnMeta.setTableName(resultSet.getString("TABLE_NAME"));
columnMeta.setColumnName(resultSet.getString("COLUMN_NAME")); columnMeta.setColumnName(resultSet.getString("COLUMN_NAME"));
columnMeta.setDataType(resultSet.getString("DATA_TYPE")); columnMeta.setDataType(resultSet.getString("DATA_TYPE"));
columnMeta.setNullable(isNullable(resultSet.getString("IS_NULLABLE"))); columnMeta.setNullable(toBoolean(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")); columnMeta.setIndexName(resultSet.getString("INDEX_NAME"));
return columnMeta; return columnMeta;
} }
private boolean isNullable(String value) { private boolean toBoolean(String value) {
return "YES".equalsIgnoreCase(value); return "YES".equalsIgnoreCase(value);
} }
} }
\ No newline at end of file
...@@ -11,7 +11,6 @@ import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConst ...@@ -11,7 +11,6 @@ import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConst
* @author liaozan * @author liaozan
* @since 2022/8/30 * @since 2022/8/30
*/ */
@SuppressWarnings("DuplicatedCode")
public class DefaultTableConstraintChecker implements TableConstraintChecker { public class DefaultTableConstraintChecker implements TableConstraintChecker {
@Override @Override
...@@ -33,24 +32,25 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker { ...@@ -33,24 +32,25 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
if (bizColumnField == null) { if (bizColumnField == null) {
return; return;
} }
ColumnMeta columnMeta = table.getColumnMeta(bizColumnField.getColumnName());
if (columnMeta == null) { ColumnMeta bizColumnMeta = getColumnMeta(table, bizColumnField.getColumnName());
addMissingFieldError(table, bizColumnField.getColumnName()); if (bizColumnMeta == null) {
return; return;
} }
if (columnMeta.getIndexName() == null) {
addError(table, columnMeta, "BizId field should create an index"); checkNotNull(table, bizColumnMeta);
if (bizColumnMeta.getIndexName() == null) {
addError(table, bizColumnMeta, "BizId field should create an index");
} }
} }
protected void checkIdField(Table table) { protected void checkIdField(Table table) {
ColumnMeta idColumnMeta = table.getColumnMeta(ID); ColumnMeta idColumnMeta = getColumnMeta(table, ID);
if (idColumnMeta == null) { if (idColumnMeta == null) {
addMissingFieldError(table, ID);
return; return;
} }
checkNullable(table, idColumnMeta); checkNotNull(table, idColumnMeta);
if (!MysqlType.BIGINT.getName().equalsIgnoreCase(idColumnMeta.getDataType())) { if (!MysqlType.BIGINT.getName().equalsIgnoreCase(idColumnMeta.getDataType())) {
addError(table, idColumnMeta, "should be type of 'bigint'"); addError(table, idColumnMeta, "should be type of 'bigint'");
} }
...@@ -60,61 +60,47 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker { ...@@ -60,61 +60,47 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
} }
protected void checkCreateTimeField(Table table) { protected void checkCreateTimeField(Table table) {
ColumnMeta createTimeColumnMeta = table.getColumnMeta(CREATE_TIME); ColumnMeta createTimeColumnMeta = getColumnMeta(table, CREATE_TIME);
if (createTimeColumnMeta == null) { if (createTimeColumnMeta == null) {
addMissingFieldError(table, CREATE_TIME);
return; return;
} }
checkNullable(table, createTimeColumnMeta); checkNotNull(table, createTimeColumnMeta);
if (!MysqlType.DATETIME.getName().equalsIgnoreCase(createTimeColumnMeta.getDataType())) { checkDatetimeField(table, createTimeColumnMeta);
addError(table, createTimeColumnMeta, "should be type of 'datetime'");
}
if (!CURRENT_TIMESTAMP.equalsIgnoreCase(createTimeColumnMeta.getColumnDefault())) {
addError(table, createTimeColumnMeta, "default value should be 'current_timestamp'");
}
} }
protected void checkModifyTimeField(Table table) { protected void checkModifyTimeField(Table table) {
ColumnMeta modifyTimeColumnMeta = table.getColumnMeta(MODIFY_TIME); ColumnMeta modifyTimeColumnMeta = getColumnMeta(table, MODIFY_TIME);
if (modifyTimeColumnMeta == null) { if (modifyTimeColumnMeta == null) {
addMissingFieldError(table, MODIFY_TIME);
return; return;
} }
checkNullable(table, modifyTimeColumnMeta); checkNotNull(table, modifyTimeColumnMeta);
if (!MysqlType.DATETIME.getName().equalsIgnoreCase(modifyTimeColumnMeta.getDataType())) { checkDatetimeField(table, modifyTimeColumnMeta);
addError(table, modifyTimeColumnMeta, "should be type of 'datetime'");
}
if (!CURRENT_TIMESTAMP.equalsIgnoreCase(modifyTimeColumnMeta.getColumnDefault())) {
addError(table, modifyTimeColumnMeta, "default value should be 'current_timestamp'");
}
if (!modifyTimeColumnMeta.getExtra().toLowerCase().contains(UPDATE_WITH_CURRENT_TIMESTAMP)) { if (!modifyTimeColumnMeta.getExtra().toLowerCase().contains(UPDATE_WITH_CURRENT_TIMESTAMP)) {
addError(table, modifyTimeColumnMeta, "need set to 'on update current_timestamp'"); addError(table, modifyTimeColumnMeta, "need set to 'on update current_timestamp'");
} }
} }
protected void checkDeletedField(Table table) { protected void checkDeletedField(Table table) {
ColumnMeta deletedColumnMeta = table.getColumnMeta(DELETED); ColumnMeta deletedColumnMeta = getColumnMeta(table, DELETED);
if (deletedColumnMeta == null) { if (deletedColumnMeta == null) {
addMissingFieldError(table, DELETED);
return; return;
} }
checkNullable(table, deletedColumnMeta); checkNotNull(table, deletedColumnMeta);
if (!MysqlType.TINYINT.getName().equalsIgnoreCase(deletedColumnMeta.getDataType())) { if (!MysqlType.TINYINT.getName().equalsIgnoreCase(deletedColumnMeta.getDataType())) {
addError(table, deletedColumnMeta, "should be type of 'tinyint'"); addError(table, deletedColumnMeta, "should be type of 'tinyint'");
} }
} }
protected void checkDeleteVersionField(Table table) { protected void checkDeleteVersionField(Table table) {
ColumnMeta deleteVersionColumnMeta = table.getColumnMeta(DELETE_VERSION); ColumnMeta deleteVersionColumnMeta = getColumnMeta(table, DELETE_VERSION);
if (deleteVersionColumnMeta == null) { if (deleteVersionColumnMeta == null) {
addMissingFieldError(table, DELETE_VERSION);
return; return;
} }
checkNullable(table, deleteVersionColumnMeta); checkNotNull(table, deleteVersionColumnMeta);
if (!MysqlType.BIGINT.getName().equalsIgnoreCase(deleteVersionColumnMeta.getDataType())) { if (!MysqlType.BIGINT.getName().equalsIgnoreCase(deleteVersionColumnMeta.getDataType())) {
addError(table, deleteVersionColumnMeta, "should be type of 'bigint'"); addError(table, deleteVersionColumnMeta, "should be type of 'bigint'");
} }
...@@ -123,18 +109,36 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker { ...@@ -123,18 +109,36 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
} }
} }
protected void checkNullable(Table table, ColumnMeta columnMeta) { protected void checkNotNull(Table table, ColumnMeta columnMeta) {
if (columnMeta.isNullable()) { if (columnMeta.isNullable()) {
addError(table, columnMeta, "should be 'not null'"); addError(table, columnMeta, "should be 'not null'");
} }
} }
private void addError(Table table, ColumnMeta columnMeta, String errorMsg) { private ColumnMeta getColumnMeta(Table table, String columnName) {
addError(table, columnMeta.getColumnName(), errorMsg); ColumnMeta columnMeta = table.getColumnMeta(columnName);
if (columnMeta == null) {
addMissingFieldError(table, columnName);
return null;
}
return columnMeta;
}
private void checkDatetimeField(Table table, ColumnMeta columnMeta) {
if (!MysqlType.DATETIME.getName().equalsIgnoreCase(columnMeta.getDataType())) {
addError(table, columnMeta, "should be type of 'datetime'");
}
if (!CURRENT_TIMESTAMP.equalsIgnoreCase(columnMeta.getColumnDefault())) {
addError(table, columnMeta, "default value should be 'current_timestamp'");
}
} }
private void addMissingFieldError(Table table, String columnName) { private void addMissingFieldError(Table table, String columnName) {
addError(table, columnName, "not exist"); table.addError(TableConstraintException.ofColumnNotExist(table.getTableName(), columnName));
}
private void addError(Table table, ColumnMeta columnMeta, String errorMsg) {
addError(table, columnMeta.getColumnName(), errorMsg);
} }
private void addError(Table table, String columnName, String errorMsg) { private void addError(Table table, String columnName, String errorMsg) {
......
...@@ -17,4 +17,4 @@ public class TableConstraintCheckFailureAnalyzer extends AbstractFailureAnalyzer ...@@ -17,4 +17,4 @@ public class TableConstraintCheckFailureAnalyzer extends AbstractFailureAnalyzer
return new FailureAnalysis(cause.getMessage(), null, cause); return new FailureAnalysis(cause.getMessage(), null, cause);
} }
} }
\ No newline at end of file
...@@ -3,7 +3,6 @@ package com.schbrain.framework.autoconfigure.mybatis.exception; ...@@ -3,7 +3,6 @@ package com.schbrain.framework.autoconfigure.mybatis.exception;
import cn.hutool.core.text.StrFormatter; import cn.hutool.core.text.StrFormatter;
import com.schbrain.common.exception.BaseException; import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.StreamUtils; import com.schbrain.common.util.StreamUtils;
import lombok.Getter;
import java.util.List; import java.util.List;
...@@ -11,7 +10,6 @@ import java.util.List; ...@@ -11,7 +10,6 @@ import java.util.List;
* @author liaozan * @author liaozan
* @since 2022/8/30 * @since 2022/8/30
*/ */
@Getter
public class TableConstraintException extends BaseException { public class TableConstraintException extends BaseException {
private static final long serialVersionUID = -3139175416089223586L; private static final long serialVersionUID = -3139175416089223586L;
...@@ -21,11 +19,15 @@ public class TableConstraintException extends BaseException { ...@@ -21,11 +19,15 @@ public class TableConstraintException extends BaseException {
} }
public TableConstraintException(String tableName, String column, String message) { public TableConstraintException(String tableName, String column, String message) {
super("Table: '" + tableName + "' , Column: '" + column + "' " + message); super("Table: '" + tableName + "', Column: '" + column + "' : " + message);
} }
public TableConstraintException(List<TableConstraintException> errors) { public TableConstraintException(List<TableConstraintException> errors) {
super(StreamUtils.join(StreamUtils.toList(errors, Throwable::getMessage), System.lineSeparator())); super(StreamUtils.join(StreamUtils.toList(errors, Throwable::getMessage), System.lineSeparator()));
} }
} public static TableConstraintException ofColumnNotExist(String tableName, String column) {
\ No newline at end of file return new TableConstraintException(tableName, column, "not exist");
}
}
...@@ -64,8 +64,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B ...@@ -64,8 +64,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
List<TableMetaDataLoader> metaDataLoaders = beanFactory.getBeanProvider(TableMetaDataLoader.class).orderedStream().collect(toList()); List<TableMetaDataLoader> metaDataLoaders = beanFactory.getBeanProvider(TableMetaDataLoader.class).orderedStream().collect(toList());
if (CollectionUtils.isEmpty(metaDataLoaders)) { if (CollectionUtils.isEmpty(metaDataLoaders)) {
JdbcTemplate jdbcTemplate = beanFactory.getBean(JdbcTemplate.class); JdbcTemplate jdbcTemplate = beanFactory.getBean(JdbcTemplate.class);
// Avoid add to a immutable collection metaDataLoaders.add(new DefaultTableMetaDataLoader(jdbcTemplate));
metaDataLoaders = List.of(new DefaultTableMetaDataLoader(jdbcTemplate));
} }
Map<String, List<ColumnMeta>> tableMetadata = loadTableMetadata(metaDataLoaders); Map<String, List<ColumnMeta>> tableMetadata = loadTableMetadata(metaDataLoaders);
...@@ -76,8 +75,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B ...@@ -76,8 +75,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
List<TableConstraintChecker> checkers = beanFactory.getBeanProvider(TableConstraintChecker.class).orderedStream().collect(toList()); List<TableConstraintChecker> checkers = beanFactory.getBeanProvider(TableConstraintChecker.class).orderedStream().collect(toList());
if (CollectionUtils.isEmpty(checkers)) { if (CollectionUtils.isEmpty(checkers)) {
// Avoid add to a immutable collection checkers.add(new DefaultTableConstraintChecker());
checkers = List.of(new DefaultTableConstraintChecker());
} }
List<TableConstraintException> errors = new ArrayList<>(); List<TableConstraintException> errors = new ArrayList<>();
...@@ -172,9 +170,9 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B ...@@ -172,9 +170,9 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
private void checkAllFieldExist(Table table) { private void checkAllFieldExist(Table table) {
for (FieldInfo fieldInfo : table.getFieldInfoList()) { for (FieldInfo fieldInfo : table.getFieldInfoList()) {
if (!table.containsColumn(fieldInfo.getColumn())) { if (!table.containsColumn(fieldInfo.getColumn())) {
table.addError(new TableConstraintException(table.getTableName(), fieldInfo.getColumn(), "not exist")); table.addError(TableConstraintException.ofColumnNotExist(table.getTableName(), fieldInfo.getColumn()));
} }
} }
} }
} }
\ No newline at end of file
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