提交 75dd870d 编写于 作者: M MaxKey

LogicDeleteProvider

上级 b139775f
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
* Copyright [2023] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -114,53 +114,5 @@ public class DeleteProvider <T extends JpaEntity>{
logger.trace("Delete SQL \n{}" , deleteSql);
return deleteSql;
}
@SuppressWarnings("unchecked")
public String logicDelete(Map<String, Object> parametersMap) {
Class<?> entityClass=(Class<?>)parametersMap.get(MapperMetadata.ENTITY_CLASS);
MapperMetadata.buildColumnList(entityClass);
String tableName = MapperMetadata.getTableName(entityClass);
ArrayList <String> idValues=(ArrayList<String>)parametersMap.get(MapperMetadata.PARAMETER_ID_LIST);
StringBuffer keyValue = new StringBuffer();
for(String value : idValues) {
if(value.trim().length() > 0) {
keyValue.append(",'").append(value).append("'");
logger.trace("logic delete by id {}" , value);
}
}
String keyValues = keyValue.substring(1).replaceAll(";", "");//remove ;
FieldColumnMapper logicColumnMapper = MapperMetadata.getLogicColumn((entityClass).getSimpleName());
String partitionKeyValue = (String) parametersMap.get(MapperMetadata.PARAMETER_PARTITION_KEY);
FieldColumnMapper partitionKeyColumnMapper = MapperMetadata.getPartitionKey((entityClass).getSimpleName());
FieldColumnMapper idFieldColumnMapper = MapperMetadata.getIdColumn(entityClass.getSimpleName());
SQL sql=new SQL()
.UPDATE(tableName)
.SET(" %s = %s ".formatted(
logicColumnMapper.getColumnName(),
logicColumnMapper.getColumnLogic().delete()
)
);
if(partitionKeyColumnMapper != null && partitionKeyValue != null) {
sql.WHERE("%s = #{%s} and %s in ( %s )"
.formatted(
partitionKeyColumnMapper.getColumnName() ,
partitionKeyValue,
idFieldColumnMapper.getColumnName(),
idFieldColumnMapper.getFieldName())
);
}else {
sql.WHERE(" %s in ( %s )".formatted(idFieldColumnMapper.getColumnName(),keyValues));
}
String deleteSql = sql.toString();
MapperMetadata.sqlsMap.put(tableName + SQL_TYPE.LOGICDELETE_SQL,deleteSql);
logger.trace("logic Delete SQL \n{}" , deleteSql);
return deleteSql;
}
}
/*
* Copyright [2023] [MaxKey of copyright http://www.maxkey.top]
*
* 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 org.dromara.mybatis.jpa.provider;
import java.util.ArrayList;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import org.dromara.mybatis.jpa.entity.JpaEntity;
import org.dromara.mybatis.jpa.metadata.FieldColumnMapper;
import org.dromara.mybatis.jpa.metadata.MapperMetadata;
import org.dromara.mybatis.jpa.metadata.MapperMetadata.SQL_TYPE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Crystal.Sea
*
*/
public class LogicDeleteProvider <T extends JpaEntity>{
private static final Logger logger = LoggerFactory.getLogger(LogicDeleteProvider.class);
@SuppressWarnings("unchecked")
public String logicDelete(Map<String, Object> parametersMap) {
Class<?> entityClass=(Class<?>)parametersMap.get(MapperMetadata.ENTITY_CLASS);
MapperMetadata.buildColumnList(entityClass);
String tableName = MapperMetadata.getTableName(entityClass);
ArrayList <String> idValues=(ArrayList<String>)parametersMap.get(MapperMetadata.PARAMETER_ID_LIST);
StringBuffer keyValue = new StringBuffer();
for(String value : idValues) {
if(value.trim().length() > 0) {
keyValue.append(",'").append(value).append("'");
logger.trace("logic delete by id {}" , value);
}
}
String keyValues = keyValue.substring(1).replaceAll(";", "");//remove ;
FieldColumnMapper logicColumnMapper = MapperMetadata.getLogicColumn((entityClass).getSimpleName());
String partitionKeyValue = (String) parametersMap.get(MapperMetadata.PARAMETER_PARTITION_KEY);
FieldColumnMapper partitionKeyColumnMapper = MapperMetadata.getPartitionKey((entityClass).getSimpleName());
FieldColumnMapper idFieldColumnMapper = MapperMetadata.getIdColumn(entityClass.getSimpleName());
SQL sql=new SQL()
.UPDATE(tableName)
.SET(" %s = %s ".formatted(
logicColumnMapper.getColumnName(),
logicColumnMapper.getColumnLogic().delete()
)
);
if(partitionKeyColumnMapper != null && partitionKeyValue != null) {
sql.WHERE("%s = #{%s} and %s in ( %s )"
.formatted(
partitionKeyColumnMapper.getColumnName() ,
partitionKeyValue,
idFieldColumnMapper.getColumnName(),
idFieldColumnMapper.getFieldName())
);
}else {
sql.WHERE(" %s in ( %s )".formatted(idFieldColumnMapper.getColumnName(),keyValues));
}
String deleteSql = sql.toString();
MapperMetadata.sqlsMap.put(tableName + SQL_TYPE.LOGICDELETE_SQL,deleteSql);
logger.trace("logic Delete SQL \n{}" , deleteSql);
return deleteSql;
}
}
......@@ -61,7 +61,7 @@ public class MapperSqlProvider <T extends JpaEntity>{
}
public String logicDelete(Map<String, Object> parametersMap) {
return new DeleteProvider().logicDelete(parametersMap);
return new LogicDeleteProvider().logicDelete(parametersMap);
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册