mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
optimize tenant condition;close #I7IVCR
This commit is contained in:
parent
fec7422f1f
commit
c52c3654f8
@ -28,6 +28,7 @@ import com.mybatisflex.core.table.TableInfoFactory;
|
|||||||
import com.mybatisflex.core.update.RawValue;
|
import com.mybatisflex.core.update.RawValue;
|
||||||
import com.mybatisflex.core.util.ArrayUtil;
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@ -113,7 +114,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < rows.size(); i++) {
|
for (int i = 0; i < rows.size(); i++) {
|
||||||
questions.append(buildQuestion(attrs.size()));
|
questions.append(SqlUtil.buildSqlParamPlaceholder(attrs.size()));
|
||||||
if (i != rows.size() - 1) {
|
if (i != rows.size() - 1) {
|
||||||
questions.append(DELIMITER);
|
questions.append(DELIMITER);
|
||||||
}
|
}
|
||||||
@ -585,14 +586,11 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forDeleteEntityById(TableInfo tableInfo) {
|
public String forDeleteEntityById(TableInfo tableInfo) {
|
||||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||||
|
|
||||||
//正常删除
|
//正常删除
|
||||||
if (StringUtil.isBlank(logicDeleteColumn)) {
|
if (StringUtil.isBlank(logicDeleteColumn)) {
|
||||||
String deleteByIdSql = forDeleteById(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getPrimaryColumns());
|
String deleteByIdSql = forDeleteById(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getPrimaryColumns());
|
||||||
|
return tableInfo.buildTenantCondition(deleteByIdSql, tenantIdArgs, this);
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
|
||||||
deleteByIdSql += AND + wrap(tableInfo.getTenantIdColumn()) + IN + buildQuestion(tenantIdArgs.length);
|
|
||||||
}
|
|
||||||
return deleteByIdSql;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//逻辑删除
|
//逻辑删除
|
||||||
@ -612,10 +610,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||||
|
|
||||||
//租户ID
|
//租户ID
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
tableInfo.buildTenantCondition(sql, tenantIdArgs, this);
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,7 +627,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
//多租户
|
//多租户
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||||
deleteSQL = deleteSQL.replace(WHERE, WHERE + BRACKET_LEFT) + BRACKET_RIGHT;
|
deleteSQL = deleteSQL.replace(WHERE, WHERE + BRACKET_LEFT) + BRACKET_RIGHT;
|
||||||
deleteSQL += AND + wrap(tableInfo.getTenantIdColumn()) + IN + buildQuestion(tenantIdArgs.length);
|
deleteSQL = tableInfo.buildTenantCondition(deleteSQL, tenantIdArgs, this);
|
||||||
}
|
}
|
||||||
return deleteSQL;
|
return deleteSQL;
|
||||||
}
|
}
|
||||||
@ -674,9 +669,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||||
|
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
tableInfo.buildTenantCondition(sql, tenantIdArgs, this);
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
@ -766,13 +759,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
//租户ID字段
|
//租户ID字段
|
||||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
tableInfo.buildTenantCondition(sql, tenantIdArgs, this);
|
||||||
if (tenantIdArgs.length == 1) {
|
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(EQUALS_PLACEHOLDER);
|
|
||||||
} else {
|
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//乐观锁条件
|
//乐观锁条件
|
||||||
if (StringUtil.isNotBlank(versionColumn)) {
|
if (StringUtil.isNotBlank(versionColumn)) {
|
||||||
@ -923,9 +910,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
//多租户
|
//多租户
|
||||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
tableInfo.buildTenantCondition(sql, tenantIdArgs, this);
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
@ -980,9 +965,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
//多租户
|
||||||
sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length));
|
tableInfo.buildTenantCondition(sql, tenantIdArgs, this);
|
||||||
}
|
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
@ -1071,18 +1055,6 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String buildQuestion(int count) {
|
|
||||||
StringBuilder sb = new StringBuilder(BRACKET_LEFT);
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
sb.append(PLACEHOLDER);
|
|
||||||
if (i != count - 1) {
|
|
||||||
sb.append(DELIMITER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append(BRACKET_RIGHT);
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String buildLogicNormalCondition(String logicColumn) {
|
protected String buildLogicNormalCondition(String logicColumn) {
|
||||||
return LogicDeleteManager.getProcessor().buildLogicNormalCondition(logicColumn, this);
|
return LogicDeleteManager.getProcessor().buildLogicNormalCondition(logicColumn, this);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,11 +31,11 @@ public class DefaultLogicDeleteProcessor extends AbstractLogicDeleteProcessor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) {
|
public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) {
|
||||||
queryWrapper.and(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()
|
queryWrapper.where(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()
|
||||||
, EQUALS
|
, EQUALS, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||||
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getLogicNormalValue() {
|
protected Object getLogicNormalValue() {
|
||||||
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
|
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
|
||||||
@ -46,6 +46,7 @@ public class DefaultLogicDeleteProcessor extends AbstractLogicDeleteProcessor {
|
|||||||
return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE;
|
return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getLogicDeletedValue() {
|
protected Object getLogicDeletedValue() {
|
||||||
Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete();
|
Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete();
|
||||||
|
|||||||
@ -48,6 +48,8 @@ import java.util.*;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.constant.SqlConsts.*;
|
||||||
|
|
||||||
public class TableInfo {
|
public class TableInfo {
|
||||||
|
|
||||||
private String schema; //schema
|
private String schema; //schema
|
||||||
@ -683,6 +685,42 @@ public class TableInfo {
|
|||||||
return TenantManager.getTenantIds();
|
return TenantManager.getTenantIds();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String buildTenantCondition(String sql, Object[] tenantIdArgs, IDialect dialect) {
|
||||||
|
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||||
|
if (tenantIdArgs.length == 1) {
|
||||||
|
return sql + AND + dialect.wrap(tenantIdColumn) + EQUALS_PLACEHOLDER;
|
||||||
|
} else {
|
||||||
|
return sql + AND + dialect.wrap(tenantIdColumn) + IN + SqlUtil.buildSqlParamPlaceholder(tenantIdArgs.length);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void buildTenantCondition(StringBuilder sql, Object[] tenantIdArgs, IDialect dialect) {
|
||||||
|
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||||
|
if (tenantIdArgs.length == 1) {
|
||||||
|
sql.append(AND).append(dialect.wrap(tenantIdColumn)).append(EQUALS_PLACEHOLDER);
|
||||||
|
} else {
|
||||||
|
sql.append(AND).append(dialect.wrap(tenantIdColumn)).append(IN).append(SqlUtil.buildSqlParamPlaceholder(tenantIdArgs.length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void buildTenantCondition(QueryWrapper queryWrapper) {
|
||||||
|
Object[] tenantIdArgs = buildTenantIdArgs();
|
||||||
|
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||||
|
if (tenantIdArgs.length == 1) {
|
||||||
|
queryWrapper.where(QueryCondition.create(schema, tableName, tenantIdColumn, SqlConsts.EQUALS, tenantIdArgs[0]));
|
||||||
|
} else {
|
||||||
|
queryWrapper.where(QueryCondition.create(schema, tableName, tenantIdColumn, SqlConsts.IN, tenantIdArgs));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static final String APPEND_CONDITIONS_FLAG = "appendConditions";
|
private static final String APPEND_CONDITIONS_FLAG = "appendConditions";
|
||||||
|
|
||||||
public void appendConditions(Object entity, QueryWrapper queryWrapper) {
|
public void appendConditions(Object entity, QueryWrapper queryWrapper) {
|
||||||
@ -731,14 +769,8 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//多租户
|
//多租户
|
||||||
Object[] tenantIdArgs = buildTenantIdArgs();
|
buildTenantCondition(queryWrapper);
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
|
||||||
if (tenantIdArgs.length == 1) {
|
|
||||||
queryWrapper.and(QueryCondition.create(schema, tableName, tenantIdColumn, SqlConsts.EQUALS, tenantIdArgs[0]));
|
|
||||||
} else {
|
|
||||||
queryWrapper.and(QueryCondition.create(schema, tableName, tenantIdColumn, SqlConsts.IN, tenantIdArgs));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//子查询
|
//子查询
|
||||||
List<QueryWrapper> childSelects = CPI.getChildSelect(queryWrapper);
|
List<QueryWrapper> childSelects = CPI.getChildSelect(queryWrapper);
|
||||||
|
|||||||
@ -21,6 +21,9 @@ import java.util.Date;
|
|||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.constant.SqlConsts.*;
|
||||||
|
import static com.mybatisflex.core.constant.SqlConsts.BRACKET_RIGHT;
|
||||||
|
|
||||||
public class SqlUtil {
|
public class SqlUtil {
|
||||||
|
|
||||||
private SqlUtil() {
|
private SqlUtil() {
|
||||||
@ -127,4 +130,16 @@ public class SqlUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String buildSqlParamPlaceholder(int count) {
|
||||||
|
StringBuilder sb = new StringBuilder(BRACKET_LEFT);
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
sb.append(PLACEHOLDER);
|
||||||
|
if (i != count - 1) {
|
||||||
|
sb.append(DELIMITER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append(BRACKET_RIGHT);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,6 +68,15 @@ public class AccountTester {
|
|||||||
System.out.println(queryWrapper.toSQL());
|
System.out.println(queryWrapper.toSQL());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTenant() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create();
|
||||||
|
queryWrapper.where(Account::getId).ge(1);
|
||||||
|
List<Account> accounts = accountMapper.selectListByQuery(queryWrapper);
|
||||||
|
System.out.println(accounts);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectAsToDTO() {
|
public void testSelectAsToDTO() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user