|
@@ -41,14 +41,7 @@ import org.apache.ibatis.session.RowBounds;
|
|
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
import java.sql.SQLException;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.Collection;
|
|
|
|
-import java.util.Collections;
|
|
|
|
-import java.util.Deque;
|
|
|
|
-import java.util.LinkedList;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Properties;
|
|
|
|
|
|
+import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -106,9 +99,9 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
processSelectBody(withItem.getSubSelect().getSelectBody());
|
|
processSelectBody(withItem.getSubSelect().getSelectBody());
|
|
} else {
|
|
} else {
|
|
SetOperationList operationList = (SetOperationList) selectBody;
|
|
SetOperationList operationList = (SetOperationList) selectBody;
|
|
- List<SelectBody> selectBodys = operationList.getSelects();
|
|
|
|
- if (CollectionUtils.isNotEmpty(selectBodys)) {
|
|
|
|
- selectBodys.forEach(this::processSelectBody);
|
|
|
|
|
|
+ List<SelectBody> selectBodyList = operationList.getSelects();
|
|
|
|
+ if (CollectionUtils.isNotEmpty(selectBodyList)) {
|
|
|
|
+ selectBodyList.forEach(this::processSelectBody);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -280,9 +273,7 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
// 无 join 时的处理逻辑
|
|
// 无 join 时的处理逻辑
|
|
if (fromItem instanceof Table) {
|
|
if (fromItem instanceof Table) {
|
|
Table fromTable = (Table) fromItem;
|
|
Table fromTable = (Table) fromItem;
|
|
- if (!tenantLineHandler.ignoreTable(fromTable.getName())) {
|
|
|
|
- mainTables.add(fromTable);
|
|
|
|
- }
|
|
|
|
|
|
+ mainTables.add(fromTable);
|
|
} else if (fromItem instanceof SubJoin) {
|
|
} else if (fromItem instanceof SubJoin) {
|
|
// SubJoin 类型则还需要添加上 where 条件
|
|
// SubJoin 类型则还需要添加上 where 条件
|
|
List<Table> tables = processSubJoin((SubJoin) fromItem);
|
|
List<Table> tables = processSubJoin((SubJoin) fromItem);
|
|
@@ -397,7 +388,7 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
processSelectBody(subSelect.getSelectBody());
|
|
processSelectBody(subSelect.getSelectBody());
|
|
}
|
|
}
|
|
} else if (fromItem instanceof ValuesList) {
|
|
} else if (fromItem instanceof ValuesList) {
|
|
- logger.debug("Perform a subquery, if you do not give us feedback");
|
|
|
|
|
|
+ logger.debug("Perform a subQuery, if you do not give us feedback");
|
|
} else if (fromItem instanceof LateralSubSelect) {
|
|
} else if (fromItem instanceof LateralSubSelect) {
|
|
LateralSubSelect lateralSubSelect = (LateralSubSelect) fromItem;
|
|
LateralSubSelect lateralSubSelect = (LateralSubSelect) fromItem;
|
|
if (lateralSubSelect.getSubSelect() != null) {
|
|
if (lateralSubSelect.getSubSelect() != null) {
|
|
@@ -433,15 +424,14 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
* @return List<Table> 右连接查询的 Table 列表
|
|
* @return List<Table> 右连接查询的 Table 列表
|
|
*/
|
|
*/
|
|
private List<Table> processJoins(List<Table> mainTables, List<Join> joins) {
|
|
private List<Table> processJoins(List<Table> mainTables, List<Join> joins) {
|
|
- if (mainTables == null) {
|
|
|
|
- mainTables = new ArrayList<>();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// join 表达式中最终的主表
|
|
// join 表达式中最终的主表
|
|
Table mainTable = null;
|
|
Table mainTable = null;
|
|
// 当前 join 的左表
|
|
// 当前 join 的左表
|
|
Table leftTable = null;
|
|
Table leftTable = null;
|
|
- if (mainTables.size() == 1) {
|
|
|
|
|
|
+
|
|
|
|
+ if (mainTables == null) {
|
|
|
|
+ mainTables = new ArrayList<>();
|
|
|
|
+ } else if (mainTables.size() == 1) {
|
|
mainTable = mainTables.get(0);
|
|
mainTable = mainTables.get(0);
|
|
leftTable = mainTable;
|
|
leftTable = mainTable;
|
|
}
|
|
}
|
|
@@ -471,19 +461,16 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
|
|
|
// 当前表是否忽略
|
|
// 当前表是否忽略
|
|
Table joinTable = joinTables.get(0);
|
|
Table joinTable = joinTables.get(0);
|
|
- boolean joinTableNeedIgnore = tenantLineHandler.ignoreTable(joinTable.getName());
|
|
|
|
|
|
|
|
List<Table> onTables = null;
|
|
List<Table> onTables = null;
|
|
// 如果不要忽略,且是右连接,则记录下当前表
|
|
// 如果不要忽略,且是右连接,则记录下当前表
|
|
if (join.isRight()) {
|
|
if (join.isRight()) {
|
|
- mainTable = joinTableNeedIgnore ? null : joinTable;
|
|
|
|
|
|
+ mainTable = joinTable;
|
|
if (leftTable != null) {
|
|
if (leftTable != null) {
|
|
onTables = Collections.singletonList(leftTable);
|
|
onTables = Collections.singletonList(leftTable);
|
|
}
|
|
}
|
|
} else if (join.isLeft()) {
|
|
} else if (join.isLeft()) {
|
|
- if (!joinTableNeedIgnore) {
|
|
|
|
- onTables = Collections.singletonList(joinTable);
|
|
|
|
- }
|
|
|
|
|
|
+ onTables = Collections.singletonList(joinTable);
|
|
} else if (join.isInner()) {
|
|
} else if (join.isInner()) {
|
|
if (mainTable == null) {
|
|
if (mainTable == null) {
|
|
onTables = Collections.singletonList(joinTable);
|
|
onTables = Collections.singletonList(joinTable);
|
|
@@ -492,6 +479,7 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
}
|
|
}
|
|
mainTable = null;
|
|
mainTable = null;
|
|
}
|
|
}
|
|
|
|
+
|
|
mainTables = new ArrayList<>();
|
|
mainTables = new ArrayList<>();
|
|
if (mainTable != null) {
|
|
if (mainTable != null) {
|
|
mainTables.add(mainTable);
|
|
mainTables.add(mainTable);
|
|
@@ -527,7 +515,6 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
processOtherFromItem(joinItem);
|
|
processOtherFromItem(joinItem);
|
|
leftTable = null;
|
|
leftTable = null;
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return mainTables;
|
|
return mainTables;
|
|
@@ -545,8 +532,14 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
|
|
Expression tenantId = tenantLineHandler.getTenantId();
|
|
Expression tenantId = tenantLineHandler.getTenantId();
|
|
// 构造每张表的条件
|
|
// 构造每张表的条件
|
|
List<EqualsTo> equalsTos = tables.stream()
|
|
List<EqualsTo> equalsTos = tables.stream()
|
|
|
|
+ .filter(x -> !tenantLineHandler.ignoreTable(x.getName()))
|
|
.map(item -> new EqualsTo(getAliasColumn(item), tenantId))
|
|
.map(item -> new EqualsTo(getAliasColumn(item), tenantId))
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ if(CollectionUtils.isEmpty(equalsTos)){
|
|
|
|
+ return currentExpression;
|
|
|
|
+ }
|
|
|
|
+
|
|
// 注入的表达式
|
|
// 注入的表达式
|
|
Expression injectExpression = equalsTos.get(0);
|
|
Expression injectExpression = equalsTos.get(0);
|
|
// 如果有多表,则用 and 连接
|
|
// 如果有多表,则用 and 连接
|