From 7e504299f3ff4923b7c31a4ba44a270f02e0e545 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:52:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E9=BB=98=E8=AE=A4=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLogicDeleteProcessor.java | 66 +++++++++++++++++++ .../core/logicdelete/LogicDeleteManager.java | 34 ++++++++-- .../DefaultLogicDeleteProcessorImpl.java | 26 +++----- .../core/logicdelete/impl/package-info.java | 20 ++++++ 4 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java rename mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/{ => impl}/DefaultLogicDeleteProcessorImpl.java (77%) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java new file mode 100644 index 00000000..e3b6d32f --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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(); + +} + + diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java index 8464e8c6..2a0e2235 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java @@ -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 skipFlags = new ThreadLocal<>(); + /** + * 获取逻辑删除处理器。 + * + * @return 逻辑删除处理器 + */ public static LogicDeleteProcessor getProcessor() { return processor; } + /** + * 设置逻辑删除处理器。 + * + * @param processor 逻辑删除处理器 + */ public static void setProcessor(LogicDeleteProcessor processor) { LogicDeleteManager.processor = processor; } /** - * 跳过逻辑删除字段处理,直接进行数据库物理操作 + * 跳过逻辑删除字段处理,直接进行数据库物理操作。 */ public static T execWithoutLogicDelete(Supplier 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; } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java similarity index 77% rename from mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java rename to mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java index 4e95ba95..acae4807 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java @@ -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; } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java new file mode 100644 index 00000000..0e5d3e57 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; \ No newline at end of file