|
@@ -3,9 +3,13 @@ package com.baomidou.mybatisplus.extension.plugins.tenant;
|
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.LongValue;
|
|
|
+import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
|
|
+import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
|
+import net.sf.jsqlparser.schema.Column;
|
|
|
import net.sf.jsqlparser.statement.Statements;
|
|
|
import net.sf.jsqlparser.statement.select.Select;
|
|
|
+import net.sf.jsqlparser.statement.update.Update;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
@@ -21,7 +25,14 @@ public class TenantSqlParserTest {
|
|
|
.setTenantHandler(new TenantHandler() {
|
|
|
@Override
|
|
|
public Expression getTenantId(boolean where) {
|
|
|
- return new LongValue(1);
|
|
|
+ if (!where) {
|
|
|
+ return new LongValue(1);
|
|
|
+ }
|
|
|
+ final InExpression inExpression = new InExpression();
|
|
|
+ inExpression.setLeftExpression(new Column(getTenantIdColumn()));
|
|
|
+ final ExpressionList itemsList = new ExpressionList(new LongValue(1), new LongValue(2));
|
|
|
+ inExpression.setRightItemsList(itemsList);
|
|
|
+ return inExpression;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -38,13 +49,18 @@ public class TenantSqlParserTest {
|
|
|
@Test
|
|
|
public void processSelectBody() throws JSQLParserException {
|
|
|
select("select * from user",
|
|
|
- "select * from user where user.t_id = 1");
|
|
|
+ "select * from user where t_id = 1");
|
|
|
+ select("select * from user u",
|
|
|
+ "select * from user u where u.t_id = 1");
|
|
|
select("select * from user where id in (select id from user)",
|
|
|
- "select * from user where id in (select id from user where user.t_id = 1) and user.t_id = 1");
|
|
|
+ "select * from user where id in (select id from user where t_id = 1) and t_id = 1");
|
|
|
select("select * from user where id = 1 and id in (select id from user)",
|
|
|
- "select * from user where id = 1 and id in (select id from user where user.t_id = 1) and user.t_id = 1");
|
|
|
+ "select * from user where id = 1 and id in (select id from user where t_id = 1) and t_id = 1");
|
|
|
select("select * from user where id = 1 or id in (select id from user)",
|
|
|
- "select * from user where (id = 1 or id in (select id from user where user.t_id = 1)) and user.t_id = 1");
|
|
|
+ "select * from user where (id = 1 or id in (select id from user where t_id = 1)) and t_id = 1");
|
|
|
+
|
|
|
+ update("update user set age = 1",
|
|
|
+ "update user set age = 1 where t_id = 1");
|
|
|
}
|
|
|
|
|
|
private void select(String sql, String target) throws JSQLParserException {
|
|
@@ -53,4 +69,11 @@ public class TenantSqlParserTest {
|
|
|
parser.processSelectBody(select.getSelectBody());
|
|
|
assertThat(select.toString().toLowerCase()).isEqualTo(target);
|
|
|
}
|
|
|
+
|
|
|
+ private void update(String sql, String target) throws JSQLParserException {
|
|
|
+ Statements statement = CCJSqlParserUtil.parseStatements(sql);
|
|
|
+ Update update = (Update) statement.getStatements().get(0);
|
|
|
+ parser.processUpdate(update);
|
|
|
+ assertThat(update.toString().toLowerCase()).isEqualTo(target);
|
|
|
+ }
|
|
|
}
|