This is an automated email from the ASF dual-hosted git repository.
xingfudeshi pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new 44dd9d65fe optimize: optimize sql parsing logic (#7219)
44dd9d65fe is described below
commit 44dd9d65fe13602d76bb8583a49c8d1053fe50a9
Author: YoWuwuuuw <[email protected]>
AuthorDate: Fri Mar 14 16:33:23 2025 +0800
optimize: optimize sql parsing logic (#7219)
---
changes/en-us/2.x.md | 1 +
changes/zh-cn/2.x.md | 1 +
.../exec/kingbase/KingbaseInsertExecutor.java | 2 +-
.../seata/sqlparser/druid/BaseRecognizer.java | 16 ---
.../druid/DruidSQLRecognizerFactoryImpl.java | 10 ++
.../druid/kingbase/BaseKingbaseRecognizer.java | 25 +---
.../druid/oracle/BaseOracleRecognizer.java | 16 ---
.../sqlparser/druid/oscar/BaseOscarRecognizer.java | 19 +--
.../druid/postgresql/BasePostgresqlRecognizer.java | 16 ---
.../druid/sqlserver/BaseSqlServerRecognizer.java | 16 ---
.../SqlServerOperateRecognizerHolder.java | 11 +-
.../druid/DruidSQLRecognizerFactoryTest.java | 131 ++++++++++++++++-----
.../druid/mysql/MySQLDeleteRecognizerTest.java | 16 +++
13 files changed, 147 insertions(+), 133 deletions(-)
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 7473672f81..aa57ee6732 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -30,6 +30,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#7150](https://github.com/apache/incubator-seata/pull/7150)] The time
difference between the raft node and the follower node cannot synchronize data
- [[#7102](https://github.com/apache/incubator-seata/pull/7150)] bugfix:
modify XA mode pre commit transaction from commit phase to before close phase
- [[#7188](https://github.com/apache/incubator-seata/pull/7188)] bugfix: Fix
missing branchType in BusinessActionContext
+- [[#7219](https://github.com/apache/incubator-seata/pull/7219)] bugfix:
NotSupportExc can't be thrown out in some cases
### optimize:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index f6a2fcc7d8..e7299e0f57 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -29,6 +29,7 @@
- [[#7150](https://github.com/apache/incubator-seata/pull/7150)]
raft节点之前时间差,follower节点无法同步数据
- [[#7102](https://github.com/apache/incubator-seata/pull/7150)]
将XA模式预提交事务从提交阶段修改为关闭前阶段
- [[#7188](https://github.com/apache/incubator-seata/pull/7188)] 修复
BusinessActionContext 中缺少的 branchType
+- [[#7219](https://github.com/apache/incubator-seata/pull/7219)] 修复
NotSupportExc 有些情况下不能被正确抛出
### optimize:
diff --git
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/kingbase/KingbaseInsertExecutor.java
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/kingbase/KingbaseInsertExecutor.java
index ecafc44269..cc7925aaa9 100644
---
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/kingbase/KingbaseInsertExecutor.java
+++
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/kingbase/KingbaseInsertExecutor.java
@@ -40,7 +40,7 @@ import java.util.Map;
import java.util.Set;
/**
- * The type Postgresql insert executor.
+ * The type Kingbase insert executor.
*
*/
@LoadLevel(name = JdbcConstants.KINGBASE, scope = Scope.PROTOTYPE)
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/BaseRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/BaseRecognizer.java
index 71afd72658..cacb6e727d 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/BaseRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/BaseRecognizer.java
@@ -27,8 +27,6 @@ import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
@@ -133,20 +131,6 @@ public abstract class BaseRecognizer implements
SQLRecognizer {
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
@Override
public boolean visit(SQLInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryImpl.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryImpl.java
index a212112e7a..b4eeb1aada 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryImpl.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryImpl.java
@@ -20,8 +20,10 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
+import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
+import org.apache.seata.common.exception.NotSupportYetException;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.sqlparser.SQLRecognizer;
import org.apache.seata.sqlparser.SQLRecognizerFactory;
@@ -58,6 +60,14 @@ class DruidSQLRecognizerFactoryImpl implements
SQLRecognizerFactory {
} else if (ast instanceof SQLSelectStatement) {
recognizer =
recognizerHolder.getSelectForUpdateRecognizer(sql, ast);
}
+
+ // When recognizer is null, it indicates that recognizerHolder
cannot allocate unsupported syntax, like merge and replace
+ if (ast instanceof SQLReplaceStatement) {
+ //just like:replace into t (id,dr) values (1,'2'), (2,'3')
+ throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + ast +
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
+ }
+
if (recognizer != null && recognizer.isSqlSyntaxSupports()) {
if (recognizers == null) {
recognizers = new ArrayList<>();
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/kingbase/BaseKingbaseRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/kingbase/BaseKingbaseRecognizer.java
index 022be300ab..6264310211 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/kingbase/BaseKingbaseRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/kingbase/BaseKingbaseRecognizer.java
@@ -17,15 +17,14 @@
package org.apache.seata.sqlparser.druid.kingbase;
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
-import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
-import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleUpdateStatement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
+import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
+import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
+import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin;
import
com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
@@ -131,7 +130,7 @@ public abstract class BaseKingbaseRecognizer extends
BaseRecognizer {
}
@Override
- public boolean visit(OracleUpdateStatement x) {
+ public boolean visit(SQLUpdateStatement x) {
if (x.getTableSource() instanceof
OracleSelectSubqueryTableSource) {
//just like: "update (select a.id,a.name from a inner join
b on a.id = b.id) t set t.name = 'xxx'"
throw new NotSupportYetException("not support the sql
syntax with join table:" + x
@@ -156,26 +155,12 @@ public abstract class BaseKingbaseRecognizer extends
BaseRecognizer {
}
@Override
- public boolean visit(OracleSelectSubqueryTableSource x) {
+ public boolean visit(SQLSubqueryTableSource x) {
//just like: select * from (select * from t) for update
throw new NotSupportYetException("not support the sql syntax
with SubQuery:" + x
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
@Override
public boolean visit(SQLInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oracle/BaseOracleRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oracle/BaseOracleRecognizer.java
index 417566fd0a..6616dc4447 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oracle/BaseOracleRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oracle/BaseOracleRecognizer.java
@@ -28,8 +28,6 @@ import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin;
import
com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
@@ -160,20 +158,6 @@ public abstract class BaseOracleRecognizer extends
BaseRecognizer {
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
@Override
public boolean visit(SQLInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oscar/BaseOscarRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oscar/BaseOscarRecognizer.java
index a0a3b50535..f2a7a87312 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oscar/BaseOscarRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/oscar/BaseOscarRecognizer.java
@@ -22,8 +22,7 @@ import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
+import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin;
import
com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource;
@@ -156,26 +155,12 @@ public abstract class BaseOscarRecognizer extends
BaseRecognizer {
}
@Override
- public boolean visit(OracleSelectSubqueryTableSource x) {
+ public boolean visit(SQLSubqueryTableSource x) {
//just like: select * from (select * from t) for update
throw new NotSupportYetException("not support the sql syntax
with SubQuery:" + x
+ "\nplease see the doc about SQL restrictions
https://seata.io/zh-cn/docs/user/sqlreference/dml.html");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.io/zh-cn/docs/user/sqlreference/dml.html");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.io/zh-cn/docs/user/sqlreference/dml.html");
- }
-
@Override
public boolean visit(SQLInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/postgresql/BasePostgresqlRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/postgresql/BasePostgresqlRecognizer.java
index 2e93eec5ae..c725105c60 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/postgresql/BasePostgresqlRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/postgresql/BasePostgresqlRecognizer.java
@@ -21,8 +21,6 @@ import com.alibaba.druid.sql.ast.SQLLimit;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGUpdateStatement;
@@ -103,20 +101,6 @@ public abstract class BasePostgresqlRecognizer extends
BaseRecognizer {
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
@Override
public boolean visit(PGInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/BaseSqlServerRecognizer.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/BaseSqlServerRecognizer.java
index 75bb50204f..b7f25c97bf 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/BaseSqlServerRecognizer.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/BaseSqlServerRecognizer.java
@@ -25,8 +25,6 @@ import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
-import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
-import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import
com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
@@ -123,20 +121,6 @@ public abstract class BaseSqlServerRecognizer extends
BaseRecognizer {
+ "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
}
- @Override
- public boolean visit(SQLReplaceStatement x) {
- //just like: replace into t (id,dr) values (1,'2'), (2,'3')
- throw new NotSupportYetException("not support the sql syntax
with ReplaceStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
- @Override
- public boolean visit(SQLMergeStatement x) {
- //just like: merge into ... WHEN MATCHED THEN ...
- throw new NotSupportYetException("not support the sql syntax
with MergeStatement:" + x
- + "\nplease see the doc about SQL restrictions
https://seata.apache.org/zh-cn/docs/user/sqlreference/dml");
- }
-
@Override
public boolean visit(SQLInsertStatement x) {
if (null != x.getQuery()) {
diff --git
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/SqlServerOperateRecognizerHolder.java
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/SqlServerOperateRecognizerHolder.java
index 3af2c98522..59c564fb7e 100644
---
a/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/SqlServerOperateRecognizerHolder.java
+++
b/sqlparser/seata-sqlparser-druid/src/main/java/org/apache/seata/sqlparser/druid/sqlserver/SqlServerOperateRecognizerHolder.java
@@ -26,6 +26,8 @@ import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprHint;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
+import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
+import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.sqlparser.SQLRecognizer;
@@ -55,7 +57,13 @@ public class SqlServerOperateRecognizerHolder implements
SQLOperateRecognizerHol
@Override
public SQLRecognizer getSelectForUpdateRecognizer(String sql, SQLStatement
ast) {
- List<SQLHint> hints = ((SQLSelectStatement)
ast).getSelect().getFirstQueryBlock().getFrom().getHints();
+ SQLTableSource tableSource = ((SQLSelectStatement)
ast).getSelect().getFirstQueryBlock().getFrom();
+
+ if (tableSource instanceof SQLSubqueryTableSource) {
+ return new SqlServerSelectForUpdateRecognizer(sql, ast);
+ }
+
+ List<SQLHint> hints = tableSource.getHints();
if (CollectionUtils.isNotEmpty(hints)) {
List<String> hintsTexts = hints
.stream()
@@ -72,6 +80,7 @@ public class SqlServerOperateRecognizerHolder implements
SQLOperateRecognizerHol
return new SqlServerSelectForUpdateRecognizer(sql, ast);
}
}
+
return null;
}
}
diff --git
a/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryTest.java
b/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryTest.java
index 5356c83fc6..dbfa5f0bab 100644
---
a/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryTest.java
+++
b/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/DruidSQLRecognizerFactoryTest.java
@@ -48,6 +48,21 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertEquals(recognizers.size(), 1);
Assertions.assertEquals(SQLType.DELETE,
recognizers.get(0).getSQLType());
+ recognizers = recognizerFactory.create("delete from t1",
JdbcConstants.DM);
+ Assertions.assertNotNull(recognizers);
+ Assertions.assertEquals(recognizers.size(), 1);
+ Assertions.assertEquals(SQLType.DELETE,
recognizers.get(0).getSQLType());
+
+ recognizers = recognizerFactory.create("delete from t1",
JdbcConstants.KINGBASE);
+ Assertions.assertNotNull(recognizers);
+ Assertions.assertEquals(recognizers.size(), 1);
+ Assertions.assertEquals(SQLType.DELETE,
recognizers.get(0).getSQLType());
+
+ recognizers = recognizerFactory.create("delete from t1",
JdbcConstants.OSCAR);
+ Assertions.assertNotNull(recognizers);
+ Assertions.assertEquals(recognizers.size(), 1);
+ Assertions.assertEquals(SQLType.DELETE,
recognizers.get(0).getSQLType());
+
//test sql syntax
String sql = "update d.t set d.t.a = ?, d.t.b = ?, d.t.c = ?";
Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.MYSQL));
@@ -55,20 +70,11 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.POLARDBX));
Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.ORACLE));
Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.POSTGRESQL));
+ Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.DM));
+ Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.KINGBASE));
+ Assertions.assertNotNull(recognizerFactory.create(sql,
JdbcConstants.OSCAR));
- String sql1 = "update a set a.id = (select id from b where a.pid =
b.pid)";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.ORACLE));
- String sql2 = "update (select a.id,a.name from a inner join b on a.id
= b.id) t set t.name = 'xxx'";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.ORACLE));
- String sql3 = "update a set id = b.pid from b where a.id = b.id";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql3, JdbcConstants.POSTGRESQL));
- String sql4 = "update t set id = 1 where id in (select id from b)";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql4, JdbcConstants.MYSQL));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql4, JdbcConstants.MARIADB));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql4, JdbcConstants.POLARDBX));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql4, JdbcConstants.ORACLE));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql4, JdbcConstants.POSTGRESQL));
String sql5 = "insert into a values (1, 2)";
Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.MYSQL));
@@ -76,6 +82,9 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.POLARDBX));
Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.ORACLE));
Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.POSTGRESQL));
+ Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.DM));
+ Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.KINGBASE));
+ Assertions.assertNotNull(recognizerFactory.create(sql5,
JdbcConstants.OSCAR));
String sql6 = "insert into a (id, name) values (1, 2), (3, 4)";
Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.MYSQL));
@@ -83,13 +92,11 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.POLARDBX));
Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.ORACLE));
Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.POSTGRESQL));
+ Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.DM));
+ Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.KINGBASE));
+ Assertions.assertNotNull(recognizerFactory.create(sql6,
JdbcConstants.OSCAR));
+
- String sql7 = "insert into a select * from b";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.MYSQL));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.MARIADB));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.POLARDBX));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.ORACLE));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.POSTGRESQL));
String sql8 = "delete from t where id = ?";
Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.MYSQL));
@@ -97,13 +104,11 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.POLARDBX));
Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.ORACLE));
Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.POSTGRESQL));
+ Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.DM));
+ Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.KINGBASE));
+ Assertions.assertNotNull(recognizerFactory.create(sql8,
JdbcConstants.OSCAR));
+
- String sql9 = "delete from t where id in (select id from b)";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.MYSQL));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.MARIADB));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.POLARDBX));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.ORACLE));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.POSTGRESQL));
String sql10 = "select * from t for update";
Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.MYSQL));
@@ -111,12 +116,78 @@ public class DruidSQLRecognizerFactoryTest {
Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.POLARDBX));
Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.ORACLE));
Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.POSTGRESQL));
+ Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.KINGBASE));
+ Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.DM));
+ Assertions.assertNotNull(recognizerFactory.create(sql10,
JdbcConstants.OSCAR));
+
+
+
- String sql11 = "select * from (select * from t) for update";
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql11, JdbcConstants.MYSQL));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql11, JdbcConstants.MARIADB));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql11, JdbcConstants.POLARDBX));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql11, JdbcConstants.ORACLE));
- Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql11, JdbcConstants.POSTGRESQL));
+ }
+
+ @Test
+ public void testIsSqlSyntaxSupports() {
+ SQLRecognizerFactory recognizerFactory =
EnhancedServiceLoader.load(SQLRecognizerFactory.class,
SqlParserType.SQL_PARSER_TYPE_DRUID);
+
+ String sql1 = "delete from t where id in (select id from b)";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.MYSQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.MARIADB));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.POLARDBX));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.DM));
+
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.SQLSERVER));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.KINGBASE));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.OSCAR));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.POSTGRESQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql1, JdbcConstants.ORACLE));
+
+ String sql2 = "select * from (select * from t) for update";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.MYSQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.MARIADB));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.POLARDBX));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.DM));
+
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.KINGBASE));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.OSCAR));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.POSTGRESQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql2, JdbcConstants.ORACLE));
+
+ String sql3 = "replace into t (id,dr) values (1,'2'), (2,'3')";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql3, JdbcConstants.MYSQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql3, JdbcConstants.MARIADB));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql3, JdbcConstants.POLARDBX));
+
+ // When dbtype are DM and SQLSERVER, druid cannot parse the sql syntax
'replace'
+ try {
+ recognizerFactory.create(sql3, JdbcConstants.DM);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ String sql5 = "insert into a select * from b";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.MYSQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.MARIADB));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.POLARDBX));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.DM));
+
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.SQLSERVER));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.KINGBASE));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.OSCAR));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.POSTGRESQL));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql5, JdbcConstants.ORACLE));
+
+ String sql6 = "select * from (select * from t)";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql6, JdbcConstants.SQLSERVER));
+
+ String sql7 = "update a set id = b.pid from b where a.id = b.id";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql7, JdbcConstants.POSTGRESQL));
+
+ String sql8 = "update a set a.id = (select id from b where a.pid =
b.pid)";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql8, JdbcConstants.ORACLE));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql8, JdbcConstants.KINGBASE));
+
+ String sql9 = "update (select a.id,a.name from a inner join b on a.id
= b.id) t set t.name = 'xxx'";
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.ORACLE));
+ Assertions.assertThrows(NotSupportYetException.class, () ->
recognizerFactory.create(sql9, JdbcConstants.KINGBASE));
}
}
diff --git
a/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/mysql/MySQLDeleteRecognizerTest.java
b/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/mysql/MySQLDeleteRecognizerTest.java
index 5ae300ded0..1f5ebd75a6 100644
---
a/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/mysql/MySQLDeleteRecognizerTest.java
+++
b/sqlparser/seata-sqlparser-druid/src/test/java/org/apache/seata/sqlparser/druid/mysql/MySQLDeleteRecognizerTest.java
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
+import org.apache.seata.common.exception.NotSupportYetException;
import org.apache.seata.sqlparser.ParametersHolder;
import org.apache.seata.sqlparser.SQLType;
import org.apache.seata.sqlparser.druid.AbstractRecognizerTest;
@@ -196,6 +197,12 @@ public class MySQLDeleteRecognizerTest extends
AbstractRecognizerTest {
MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql,
asts.get(0));
Assertions.assertNull(recognizer.getTableAlias());
+
+ sql = "DELETE t1 FROM t t1 WHERE t1.id = ?";
+ asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
+
+ recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
+ Assertions.assertEquals("t1", recognizer.getTableAlias());
}
@Test
@@ -329,6 +336,15 @@ public class MySQLDeleteRecognizerTest extends
AbstractRecognizerTest {
});
}
+ @Test
+ public void testGetTableName() {
+ String sql = "DELETE t1 FROM t1 INNER JOIN t2 ON t1.id = t2.t1_id
WHERE t1.id BETWEEN ? AND ?;";
+ SQLStatement statement = getSQLStatement(sql);
+
+ MySQLDeleteRecognizer mySQLDeleteRecognizer1 = new
MySQLDeleteRecognizer(sql, statement);
+ Assertions.assertThrows(NotSupportYetException.class,
mySQLDeleteRecognizer1::getTableName);
+ }
+
@Override
public String getDbType() {
return JdbcConstants.MYSQL;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]