!278 fix:用户拼接的 SQL 条件无法判断是否用到 Join 连接的表

Merge pull request !278 from 王帅/main
This commit is contained in:
Michael Yang 2023-08-13 04:53:08 +00:00 committed by Gitee
commit 601b2cf39a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 108 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.ObjectUtil;
import com.mybatisflex.core.util.StringUtil;
import java.lang.reflect.Array;
import java.util.List;
@ -290,7 +291,8 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
return nextContainsTable(tables);
}
for (String table : tables) {
if (column.table != null && table.equals(column.table.name)) {
String tableName = StringUtil.getTableNameWithAlisa(table)[0];
if (column.table != null && tableName.equals(column.table.name)) {
return true;
}
}

View File

@ -17,6 +17,7 @@ package com.mybatisflex.core.query;
import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.util.StringUtil;
import java.util.List;
@ -38,6 +39,17 @@ public class RawFragment extends QueryCondition {
this.setValue(paras);
}
@Override
boolean containsTable(String... tables) {
for (String table : tables) {
String[] tableNameWithAlisa = StringUtil.getTableNameWithAlisa(table);
if (content.contains(tableNameWithAlisa[1])
|| content.contains(tableNameWithAlisa[0])) {
return true;
}
}
return false;
}
@Override
public String toSql(List<QueryTable> queryTables, IDialect dialect) {

View File

@ -120,8 +120,13 @@ public class MapperUtil {
List<String> joinTables = new ArrayList<>();
joins.forEach(join -> {
QueryTable joinQueryTable = CPI.getJoinQueryTable(join);
if (joinQueryTable != null && StringUtil.isNotBlank(joinQueryTable.getName())) {
joinTables.add(joinQueryTable.getName());
if (joinQueryTable != null) {
String tableName = joinQueryTable.getName();
if (StringUtil.isNotBlank(joinQueryTable.getAlias())) {
joinTables.add(tableName + "." + joinQueryTable.getAlias());
} else {
joinTables.add(tableName);
}
}
});

View File

@ -283,6 +283,12 @@ public class StringUtil {
: new String[]{tableNameWithSchema.substring(0, index).trim(), tableNameWithSchema.substring(index + 1).trim()};
}
public static String[] getTableNameWithAlisa(String tableNameWithAlisa) {
int index = tableNameWithAlisa.indexOf(".");
return index <= 0 ? new String[]{tableNameWithAlisa, null}
: new String[]{tableNameWithAlisa.substring(0, index), tableNameWithAlisa.substring(index + 1)};
}
public static String tryTrim(String string) {
return string != null ? string.trim() : null;
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.mybatisflex.coretest;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.MapperUtil;
import org.junit.Test;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
/**
* @author 王帅
* @since 2023-08-13
*/
public class CountSqlTest {
@Test
public void test01() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
.leftJoin(ARTICLE).as("a1").on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
.leftJoin(ARTICLE).as("a2").on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
.where(ARTICLE.ACCOUNT_ID.in(1, 2, 3));
System.out.println(queryWrapper.toSQL());
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL());
}
@Test
public void test02() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
.leftJoin(ARTICLE).as("a1").on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
.leftJoin(ARTICLE).as("a2").on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
.where("a1.account_id IN (1, 2, 3)");
System.out.println(queryWrapper.toSQL());
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL());
}
@Test
public void test03() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT.as("a"))
.leftJoin("tb_article").as("a1").on("a1.account_id = a.id")
.leftJoin("tb_article").as("a2").on("a1.account_id = a.id")
.where("a1.account_id IN (1, 2, 3)");
System.out.println(queryWrapper.toSQL());
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL());
}
}