Browse Source

[Fix-15787] Reuse code and solve the problem of complex SQL parsing exceptions in… (#15833)

* Reuse code and solve the problem of complex SQL parsing exceptions in druid, corresponding to issue #15787

* Code Format

* Enhanced adaptability to SQL formatting
3.2.2-release-bak
XinXing 7 months ago committed by GitHub
parent
commit
5ad7b1509f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/main/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessor.java
  2. 13
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java

11
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/main/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessor.java

@ -41,8 +41,7 @@ import java.util.stream.Collectors;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.google.auto.service.AutoService;
@AutoService(DataSourceProcessor.class)
@ -149,9 +148,11 @@ public class OracleDataSourceProcessor extends AbstractDataSourceProcessor {
@Override
public List<String> splitAndRemoveComment(String sql) {
SQLStatementParser parser = new OracleStatementParser(sql, SQLParserFeature.KeepComments);
List<SQLStatement> statementList = parser.parseStatementList();
return statementList.stream().map(SQLStatement::toString).collect(Collectors.toList());
if (sql.toUpperCase().contains("BEGIN") && sql.toUpperCase().contains("END")) {
return new OracleStatementParser(sql).parseStatementList().stream().map(SQLStatement::toString)
.collect(Collectors.toList());
}
return SQLParserUtils.splitAndRemoveComment(sql, com.alibaba.druid.DbType.oracle);
}
private String transformOther(Map<String, String> otherMap) {

13
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java

@ -155,13 +155,16 @@ public class OracleDataSourceProcessorTest {
@Test
void splitAndRemoveComment_MultipleSql() {
String plSql = "select * from test;select * from test2;";
String plSql =
"select a,a-a as b from (select 1 as a,2 as b from dual) union all select 1 as a,2 as b from dual;select * from dual; -- this comment";
List<String> sqls = oracleDatasourceProcessor.splitAndRemoveComment(plSql);
// We will not split the plsql
Assertions.assertEquals(2, sqls.size());
Assertions.assertEquals("SELECT *\n" +
"FROM test;", sqls.get(0));
Assertions.assertEquals("SELECT *\n" +
"FROM test2;", sqls.get(1));
System.out.println(sqls.get(0));
System.out.println(sqls.get(1));
Assertions.assertEquals(
"select a,a-a as b from (select 1 as a,2 as b from dual) union all select 1 as a,2 as b from dual",
sqls.get(0));
Assertions.assertEquals("select * from dual", sqls.get(1));
}
}

Loading…
Cancel
Save