refactor: 重构逻辑删除默认实现类。

This commit is contained in:
Suomm 2023-06-20 21:52:41 +08:00
parent 46a25cde62
commit 7e504299f3
4 changed files with 122 additions and 24 deletions

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.logicdelete;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.table.TableInfo;
import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
/**
* 逻辑删除处理器抽象类
*
* @author 王帅
* @since 2023-06-20
*/
public abstract class AbstractLogicDeleteProcessor implements LogicDeleteProcessor {
@Override
public String buildLogicNormalCondition(String logicColumn, IDialect dialect) {
return dialect.wrap(logicColumn) + EQUALS + getLogicNormalValue();
}
@Override
public String buildLogicDeletedSet(String logicColumn, IDialect dialect) {
return dialect.wrap(logicColumn) + EQUALS + getLogicDeletedValue();
}
@Override
public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) {
queryWrapper.and(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()
, EQUALS
, getLogicNormalValue()));
}
/**
* 获取逻辑删除列未删除标记值
*
* @return 未删除标记值
*/
protected abstract Object getLogicNormalValue();
/**
* 获取逻辑删除列删除时标记值
*
* @return 删除时标记值
*/
protected abstract Object getLogicDeletedValue();
}

View File

@ -15,23 +15,41 @@
*/
package com.mybatisflex.core.logicdelete;
import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessorImpl;
import java.util.function.Supplier;
/**
* 逻辑删除管理器
*/
public class LogicDeleteManager {
private LogicDeleteManager() {
}
private static LogicDeleteProcessor processor = new DefaultLogicDeleteProcessorImpl();
private static final ThreadLocal<Boolean> skipFlags = new ThreadLocal<>();
/**
* 获取逻辑删除处理器
*
* @return 逻辑删除处理器
*/
public static LogicDeleteProcessor getProcessor() {
return processor;
}
/**
* 设置逻辑删除处理器
*
* @param processor 逻辑删除处理器
*/
public static void setProcessor(LogicDeleteProcessor processor) {
LogicDeleteManager.processor = processor;
}
/**
* 跳过逻辑删除字段处理直接进行数据库物理操作
* 跳过逻辑删除字段处理直接进行数据库物理操作
*/
public static <T> T execWithoutLogicDelete(Supplier<T> supplier) {
try {
@ -43,21 +61,25 @@ public class LogicDeleteManager {
}
/**
* 跳过逻辑删除字段处理
* 跳过逻辑删除字段处理
*/
public static void skipLogicDelete() {
skipFlags.set(Boolean.TRUE);
}
/**
* 恢复逻辑删除字段处理
* 恢复逻辑删除字段处理
*/
public static void restoreLogicDelete() {
skipFlags.remove();
}
/**
* 获取逻辑删除列返回 {@code null} 表示跳过逻辑删除
*
* @param logicDeleteColumn 逻辑删除列
* @return 逻辑删除列
*/
public static String getLogicDeleteColumn(String logicDeleteColumn) {
if (logicDeleteColumn == null) {
return null;
@ -66,7 +88,7 @@ public class LogicDeleteManager {
if (skipFlag == null) {
return logicDeleteColumn;
}
return skipFlag ? null : logicDeleteColumn;
}
}

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.logicdelete;
package com.mybatisflex.core.logicdelete.impl;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.table.TableInfo;
@ -25,27 +24,18 @@ import com.mybatisflex.core.table.TableInfo;
import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
import static com.mybatisflex.core.constant.SqlConsts.SINGLE_QUOTE;
public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor {
@Override
public String buildLogicNormalCondition(String logicColumn, IDialect dialect) {
return dialect.wrap(logicColumn) + EQUALS + getLogicNormalValue();
}
@Override
public String buildLogicDeletedSet(String logicColumn, IDialect dialect) {
return dialect.wrap(logicColumn) + EQUALS + getLogicDeletedValue();
}
/**
* 默认逻辑删除处理器
*/
public class DefaultLogicDeleteProcessorImpl extends AbstractLogicDeleteProcessor {
@Override
public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) {
queryWrapper.and(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()
, SqlConsts.EQUALS
, EQUALS
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
}
protected Object getLogicNormalValue() {
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
if (normalValueOfLogicDelete instanceof Number
@ -55,7 +45,6 @@ public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor {
return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE;
}
protected Object getLogicDeletedValue() {
Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete();
if (deletedValueOfLogicDelete instanceof Number
@ -64,6 +53,7 @@ public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor {
}
return SINGLE_QUOTE + deletedValueOfLogicDelete + SINGLE_QUOTE;
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 逻辑删除处理器实现
*/
package com.mybatisflex.core.logicdelete.impl;