fixed: 子查询时,逻辑删除字段的处理有错误 close #I6X4U8

This commit is contained in:
开源海哥 2023-04-20 10:52:00 +08:00
parent a8023de0a8
commit 8cad41d0a6
11 changed files with 138 additions and 21 deletions

View File

@ -420,12 +420,14 @@ public interface BaseMapper<T> {
* @return page 数据
*/
default Page<T> paginate(@Param("page") Page<T> page, @Param("query") QueryWrapper queryWrapper) {
List<QueryColumn> groupByColumns = CPI.getGroupByColumns(queryWrapper);
List<QueryColumn> groupByColumns = null;
// 只有 totalRow 小于 0 的时候才会去查询总量
// 这样方便用户做总数缓存而非每次都要去查询总量
// 一般的分页场景中只有第一页的时候有必要去查询总量第二页以后是不需要的
if (page.getTotalRow() < 0) {
groupByColumns = CPI.getGroupByColumns(queryWrapper);
//清除group by 去查询数据
CPI.setGroupByColumns(queryWrapper, null);
long count = selectCountByQuery(queryWrapper);
@ -437,7 +439,10 @@ public interface BaseMapper<T> {
}
//恢复数量查询清除的 groupBy
CPI.setGroupByColumns(queryWrapper, groupByColumns);
if (groupByColumns != null) {
CPI.setGroupByColumns(queryWrapper, groupByColumns);
}
int offset = page.getPageSize() * (page.getPageNumber() - 1);
queryWrapper.limit(offset, page.getPageSize());
List<T> rows = selectListByQuery(queryWrapper);

View File

@ -16,12 +16,11 @@
package com.mybatisflex.core.query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
public class BaseQueryWrapper<T> implements Serializable {
protected List<QueryTable> queryTables;
protected String datasource;
@ -38,6 +37,8 @@ public class BaseQueryWrapper<T> implements Serializable {
protected Integer limitOffset;
protected Integer limitRows;
protected Map<String, Object> context;
protected T addSelectColumn(QueryColumn queryColumn) {
if (selectColumns == null) {
@ -208,4 +209,23 @@ public class BaseQueryWrapper<T> implements Serializable {
protected void setLimitRows(Integer limitRows) {
this.limitRows = limitRows;
}
}
protected Map<String, Object> getContext() {
return context;
}
protected void setContext(Map<String, Object> context) {
this.context = context;
}
protected void putContext(String key, Object value){
if (context == null){
context = new HashMap<>();
}
context.put(key,value);
}
protected <R> R getContext(String key){
return context == null ? null : (R) context.get(key);
}
}

View File

@ -20,6 +20,7 @@ import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.StringUtil;
import java.util.List;
import java.util.Map;
/**
* Cross Package Invoke
@ -34,6 +35,9 @@ public class CPI {
return queryWrapper.getValueArray();
}
public static List<QueryWrapper> getChildSelect(QueryWrapper queryWrapper) {
return queryWrapper.getChildSelect();
}
public static List<QueryTable> getQueryTables(QueryWrapper queryWrapper) {
return queryWrapper.getQueryTables();
@ -130,6 +134,23 @@ public class CPI {
queryWrapper.setLimitRows(limitRows);
}
public static Map<String, Object> getContext(QueryWrapper queryWrapper) {
return queryWrapper.getContext();
}
public static void setContext(QueryWrapper queryWrapper, Map<String, Object> context) {
queryWrapper.setContext(context);
}
public static void putContext(QueryWrapper queryWrapper, String key, Object value) {
queryWrapper.putContext(key, value);
}
public static <R> R getContext(QueryWrapper queryWrapper, String key) {
return queryWrapper.getContext(key);
}
public static String toConditionSql(QueryColumn queryColumn, List<QueryTable> queryTables, IDialect dialect) {
return queryColumn.toConditionSql(queryTables, dialect);

View File

@ -23,6 +23,7 @@ import com.mybatisflex.core.util.StringUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
@ -363,4 +364,17 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
List<QueryWrapper> getChildSelect() {
List<QueryWrapper> childQueryWrappers = new ArrayList<>();
List<QueryWrapper> whereChildQuery= WrapperUtil.getChildSelect(whereQueryCondition);
List<QueryWrapper> havingChildQuery = WrapperUtil.getChildSelect(havingQueryCondition);
childQueryWrappers.addAll(whereChildQuery);
childQueryWrappers.addAll(havingChildQuery);
return childQueryWrappers;
}
}

View File

@ -20,8 +20,9 @@ import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.StringUtil;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Collections;
import java.util.List;
class WrapperUtil {
@ -33,12 +34,42 @@ class WrapperUtil {
static final Object[] NULL_PARA_ARRAY = new Object[0];
static List<QueryWrapper> getChildSelect(QueryCondition condition) {
List<QueryWrapper> list = null;
while (condition != null) {
if (condition.checkEffective()) {
Object value = condition.getValue();
if (value instanceof QueryWrapper) {
if (list == null) {
list = new ArrayList<>();
}
list.add((QueryWrapper) value);
list.addAll(((QueryWrapper) value).getChildSelect());
} else if (value != null && value.getClass().isArray()) {
for (int i = 0; i < Array.getLength(value); i++) {
Object arrayValue = Array.get(value, i);
if (arrayValue instanceof QueryWrapper) {
if (list == null) {
list = new ArrayList<>();
}
list.add((QueryWrapper) arrayValue);
list.addAll(((QueryWrapper) arrayValue).getChildSelect());
}
}
}
}
condition = condition.next;
}
return list == null ? Collections.emptyList() : list;
}
static Object[] getValues(QueryCondition condition) {
if (condition == null) {
return NULL_PARA_ARRAY;
}
List<Object> paras = new LinkedList<>();
List<Object> paras = new ArrayList<>();
getValues(condition, paras);
return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray();
@ -58,7 +89,6 @@ class WrapperUtil {
return;
}
if (value.getClass().isArray()) {
Object[] values = (Object[]) value;
for (Object object : values) {
@ -120,4 +150,5 @@ class WrapperUtil {
return queryTable;
}
}

View File

@ -482,6 +482,13 @@ public class TableInfo {
public void appendConditions(Object entity, QueryWrapper queryWrapper) {
Object appendConditions = CPI.getContext(queryWrapper, "appendConditions");
if (Boolean.TRUE.equals(appendConditions)) {
return;
} else {
CPI.putContext(queryWrapper, "appendConditions", Boolean.TRUE);
}
//添加乐观锁条件只有在 update 的时候进行处理
if (StringUtil.isNotBlank(versionColumn) && entity != null) {
Object versionValue = buildColumnSqlArg(entity, versionColumn);
@ -493,7 +500,6 @@ public class TableInfo {
//逻辑删除条件已删除的数据不能被修改
if (StringUtil.isNotBlank(logicDeleteColumn)) {
// queryWrapper.and(QueryCondition.create(tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS, FlexConsts.LOGIC_DELETE_NORMAL));
queryWrapper.and(QueryCondition.create(tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
}
@ -508,6 +514,20 @@ public class TableInfo {
}
}
//子查询
List<QueryWrapper> childSelects = CPI.getChildSelect(queryWrapper);
if (CollectionUtil.isNotEmpty(childSelects)) {
for (QueryWrapper childQueryWrapper : childSelects) {
List<QueryTable> queryTables = CPI.getQueryTables(childQueryWrapper);
for (QueryTable queryTable : queryTables) {
TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName());
if (tableInfo != null) {
tableInfo.appendConditions(entity, childQueryWrapper);
}
}
}
}
//union
List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
if (CollectionUtil.isNotEmpty(unions)) {

View File

@ -27,6 +27,8 @@ public class Account extends BaseAccount implements Serializable {
@Column(typeHandler = Fastjson2TypeHandler.class,isLarge = true)
private Map<String, Object> options;
@Column(isLogicDelete = true)
private Boolean isDelete;
public Long getId() {
@ -85,6 +87,7 @@ public class Account extends BaseAccount implements Serializable {
", age=" + age +
", birthday=" + birthday +
", options=" + options +
", isDelete=" + isDelete +
'}';
}
}

View File

@ -5,7 +5,7 @@ import com.mybatisflex.annotation.SetListener;
public class AccountOnSetListener implements SetListener {
@Override
public Object onSet(Object entity, String property, Object value) {
System.out.println(">>>>>>> property: " + property +" value:" + value);
// System.out.println(">>>>>>> property: " + property +" value:" + value);
return value;
}
}

View File

@ -26,9 +26,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.List;
import static com.mybatisflex.core.query.QueryMethods.count;
import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.test.table.Tables.ACCOUNT;
public class EntityTestStarter {
@ -55,12 +53,16 @@ public class EntityTestStarter {
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.DEFAULT_COLUMNS).select(count()).from(ACCOUNT)
.groupBy(ACCOUNT.ID);
List<Account> accounts = accountMapper.selectListByQuery(wrapper);
// QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.DEFAULT_COLUMNS).select(count()).from(ACCOUNT)
// .groupBy(ACCOUNT.ID);
// List<Account> accounts = accountMapper.selectListByQuery(wrapper);
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.where(ACCOUNT.ID.in(
select(ACCOUNT.ID).from(ACCOUNT)
));
Page<Account> paginate = accountMapper.paginate(new Page<>(1,10), new QueryWrapper());
Page<Account> paginate = accountMapper.paginate(new Page<>(1,10),queryWrapper);
System.out.println(paginate);

View File

@ -1,3 +1,3 @@
INSERT INTO tb_account
VALUES (1, '张三', 18, 0,'2020-01-11', null),
(2, '王麻子叔叔', 19, 1, '2021-03-21', null);
VALUES (1, '张三', 18, 0,'2020-01-11', null,0),
(2, '王麻子叔叔', 19, 1, '2021-03-21', null,0);

View File

@ -5,5 +5,6 @@ CREATE TABLE IF NOT EXISTS `tb_account`
`age` Integer,
`sex` Integer,
`birthday` DATETIME,
`options` VARCHAR(1024)
`options` VARCHAR(1024),
`is_delete` Integer
);