feat: 添加主键逻辑删除处理器。
This commit is contained in:
Suomm 2023-07-26 18:33:10 +08:00 committed by 王帅
parent 252a7fc6a5
commit 8b2b85d2e5

View File

@ -0,0 +1,67 @@
/*
* 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;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexAssert;
import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor;
import com.mybatisflex.core.table.IdInfo;
import com.mybatisflex.core.table.TableInfo;
import java.util.List;
import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
/**
* 主键逻辑删除处理器
*
* @author 王帅
* @see <a href="https://gitee.com/mybatis-flex/mybatis-flex/issues/I7O1VV">I7O1VV</a>
* @since 2023-07-26
*/
public class PrimaryKeyLogicDeleteProcessor extends AbstractLogicDeleteProcessor {
@Override
public String buildLogicDeletedSet(String logicColumn, TableInfo tableInfo, IDialect dialect) {
List<IdInfo> primaryKeyList = tableInfo.getPrimaryKeyList();
FlexAssert.notEmpty(primaryKeyList, "Must have one primary key.");
String column = primaryKeyList.get(0).getColumn();
return dialect.wrap(logicColumn) + EQUALS + dialect.wrap(column);
}
/**
* 正常未删除的值为 0 或者可配置为其他值
*/
@Override
public Object getLogicNormalValue() {
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
if (normalValueOfLogicDelete instanceof Number) {
return normalValueOfLogicDelete;
}
return 0;
}
/**
* 逻辑删除后则更新逻辑删除字段值为主键的值
*/
@Override
public Object getLogicDeletedValue() {
return null;
}
}