Skip to content

Commit

Permalink
fix: issues#6075 多租户查询出现问题 (#6091)
Browse files Browse the repository at this point in the history
#6075

Co-authored-by: wangqibo <wangqibo@51etc.com>
  • Loading branch information
sirius19 and wangqibo committed Apr 24, 2024
1 parent 8383e11 commit 3673c7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.sf.jsqlparser.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.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
Expand Down Expand Up @@ -105,7 +106,7 @@ protected void processPlainSelect(final PlainSelect plainSelect, final String wh
// 处理 join
List<Join> joins = plainSelect.getJoins();
if (CollectionUtils.isNotEmpty(joins)) {
processJoins(mainTables, joins, whereSegment);
processJoins(mainTables, joins, whereSegment);
}

// 当有 mainTable 时,进行 where 条件追加
Expand All @@ -125,7 +126,7 @@ private List<Table> processFromItem(FromItem fromItem, final String whereSegment
if (fromItem instanceof Table) {
Table fromTable = (Table) fromItem;
mainTables.add(fromTable);
} else if (fromItem instanceof ParenthesedFromItem ) {
} else if (fromItem instanceof ParenthesedFromItem) {
// SubJoin 类型则还需要添加上 where 条件
List<Table> tables = processSubJoin((ParenthesedFromItem) fromItem, whereSegment);
mainTables.addAll(tables);
Expand Down Expand Up @@ -219,6 +220,13 @@ protected void processFunction(Function function, final String whereSegment) {
processSelectBody(((Select) expression), whereSegment);
} else if (expression instanceof Function) {
processFunction((Function) expression, whereSegment);
} else if (expression instanceof EqualsTo) {
if (((EqualsTo) expression).getLeftExpression() instanceof Select) {
processSelectBody(((Select) ((EqualsTo) expression).getLeftExpression()), whereSegment);
}
if (((EqualsTo) expression).getRightExpression() instanceof Select) {
processSelectBody(((Select) ((EqualsTo) expression).getRightExpression()), whereSegment);
}
}
});
}
Expand Down Expand Up @@ -289,8 +297,8 @@ private List<Table> processJoins(List<Table> mainTables, List<Join> joins, final
if (joinItem instanceof Table) {
joinTables = new ArrayList<>();
joinTables.add((Table) joinItem);
} else if (joinItem instanceof ParenthesedFromItem ) {
joinTables = processSubJoin((ParenthesedFromItem ) joinItem, whereSegment);
} else if (joinItem instanceof ParenthesedFromItem) {
joinTables = processSubJoin((ParenthesedFromItem) joinItem, whereSegment);
}

if (joinTables != null) {
Expand Down Expand Up @@ -373,9 +381,9 @@ protected Expression builderExpression(Expression currentExpression, List<Table>
}
// 构造每张表的条件
List<Expression> expressions = tables.stream()
.map(item -> buildTableExpression(item, currentExpression, whereSegment))
.filter(Objects::nonNull)
.collect(Collectors.toList());
.map(item -> buildTableExpression(item, currentExpression, whereSegment))
.filter(Objects::nonNull)
.collect(Collectors.toList());

// 没有表需要处理直接返回
if (CollectionUtils.isEmpty(expressions)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@ void testDuplicateKeyUpdate() {
"INSERT INTO entity (name, age, tenant_id) VALUES ('秋秋', 18, 1), ('秋秋', '22', 1) ON DUPLICATE KEY UPDATE age = 18, tenant_id = 1");
}

@Test
void test6075() {
String sql = "Select a.*, b.*, IF((Select c.value From C as c Where c.code = \"1\") = 1, c.type, a.type) as type from A as a, B as b Where a.b_id = b.id";
System.out.println(interceptor.parserSingle(sql, null));
}

void assertSql(String sql, String targetSql) {
assertThat(interceptor.parserSingle(sql, null)).isEqualTo(targetSql);
}
Expand Down

0 comments on commit 3673c7a

Please sign in to comment.