Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:多租户插件的查询使用索引的优化#6687 #6688

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ default String getTenantIdColumn() {
return "tenant_id";
}

/**
* where语句租户字段名是否前置
* <p>
* 默认为不前置 where id=? and name=? and tenant_id=?
* 前置,可以使用索引 where tenant_id=? and id=? and name=?
*
* @return 是否前置
*/
default boolean tenantIdColumnFirst() {
return false;
}

/**
* 根据表名判断是否忽略拼接多租户条件
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.baomidou.mybatisplus.extension.toolkit.PropertyMapper;
import lombok.*;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
Expand Down Expand Up @@ -261,4 +263,16 @@ public Expression buildTableExpression(final Table table, final Expression where
}
return new EqualsTo(getAliasColumn(table), tenantLineHandler.getTenantId());
}

@Override
public Expression builderExpression(Expression currentExpression, List<Table> tables, final String whereSegment) {
Expression expression = super.builderExpression(currentExpression, tables, whereSegment);
if (tenantLineHandler.tenantIdColumnFirst() && expression instanceof AndExpression) {
AndExpression andExpression = (AndExpression) expression;
Expression leftExpression = andExpression.getLeftExpression();
andExpression.setLeftExpression(andExpression.getRightExpression());
andExpression.setRightExpression(leftExpression);
}
return expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @since 2020-07-30
*/
class TenantLineInnerInterceptorTest {
private boolean tenantIdColumnFirst = false;

private final TenantLineInnerInterceptor interceptor = new TenantLineInnerInterceptor(new TenantLineHandler() {
private boolean ignoreFirst;// 需要执行 getTenantId 前必须先执行 ignoreTable
Expand All @@ -29,6 +30,11 @@ public boolean ignoreTable(String tableName) {
ignoreFirst = true;
return tableName.startsWith("with_as");
}

@Override
public boolean tenantIdColumnFirst() {
return tenantIdColumnFirst;
}
});

@Test
Expand Down Expand Up @@ -109,6 +115,28 @@ void selectSingle() {
"SELECT * FROM entity u WHERE NOT (u.id = ? OR u.name = ?) AND u.tenant_id = 1");
}

@Test
void selectSingleTenantIdColumnFirst() {
tenantIdColumnFirst = true;
// 单表
assertSql("select * from entity where id = ?",
"SELECT * FROM entity WHERE tenant_id = 1 AND id = ?");

assertSql("select * from entity where id = ? or name = ?",
"SELECT * FROM entity WHERE tenant_id = 1 AND (id = ? OR name = ?)");

assertSql("SELECT * FROM entity WHERE (id = ? OR name = ?)",
"SELECT * FROM entity WHERE tenant_id = 1 AND (id = ? OR name = ?)");

/* not */
assertSql("SELECT * FROM entity WHERE not (id = ? OR name = ?)",
"SELECT * FROM entity WHERE tenant_id = 1 AND NOT (id = ? OR name = ?)");

assertSql("SELECT * FROM entity u WHERE not (u.id = ? OR u.name = ?)",
"SELECT * FROM entity u WHERE u.tenant_id = 1 AND NOT (u.id = ? OR u.name = ?)");
tenantIdColumnFirst = false;
}

@Test
void selectSubSelectIn() {
/* in */
Expand Down