diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/main/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessor.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/main/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessor.java index c3b73580dd22..687d853fffae 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/main/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessor.java +++ b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/main/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessor.java @@ -35,6 +35,7 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import com.alibaba.druid.sql.parser.SQLParserUtils; import com.google.auto.service.AutoService; @@ -128,8 +129,10 @@ public DataSourceProcessor create() { @Override public List splitAndRemoveComment(String sql) { - String cleanSQL = SQLParserUtils.removeComment(sql, com.alibaba.druid.DbType.sqlserver); - return SQLParserUtils.split(cleanSQL, com.alibaba.druid.DbType.sqlserver); + return SQLParserUtils.splitAndRemoveComment(sql, com.alibaba.druid.DbType.sqlserver) + .stream() + .map(subSql -> subSql.concat(";")) + .collect(Collectors.toList()); } private String transformOther(Map otherMap) { diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java index de30e27e0bf9..7d34755d6539 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java +++ b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java @@ -17,12 +17,15 @@ package org.apache.dolphinscheduler.plugin.datasource.sqlserver.param; +import static com.google.common.truth.Truth.assertThat; + import org.apache.dolphinscheduler.common.constants.DataSourceConstants; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.plugin.datasource.api.utils.PasswordUtils; import org.apache.dolphinscheduler.spi.enums.DbType; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.jupiter.api.Assertions; @@ -95,4 +98,32 @@ public void testGetValidationQuery() { Assertions.assertEquals(DataSourceConstants.SQLSERVER_VALIDATION_QUERY, sqlServerDatasourceProcessor.getValidationQuery()); } + + @Test + void splitAndRemoveComment_singleSelect() { + String sql = "select * from table;"; + List subSqls = sqlServerDatasourceProcessor.splitAndRemoveComment(sql); + assertThat(subSqls).hasSize(1); + assertThat(subSqls.get(0)).isEqualTo("select * from table;"); + } + + @Test + void splitAndRemoveComment_singleMerge() { + String sql = "MERGE\n" + + " [ TOP ( expression ) [ PERCENT ] ]\n" + + " [ INTO ] [ WITH ( ) ] [ [ AS ] table_alias ]\n" + + " USING [ [ AS ] table_alias ]\n" + + " ON \n" + + " [ WHEN MATCHED [ AND ]\n" + + " THEN ] [ ...n ]\n" + + " [ WHEN NOT MATCHED [ BY TARGET ] [ AND ]\n" + + " THEN ]\n" + + " [ WHEN NOT MATCHED BY SOURCE [ AND ]\n" + + " THEN ] [ ...n ]\n" + + " [ ]\n" + + " [ OPTION ( [ ,...n ] ) ];"; + List subSqls = sqlServerDatasourceProcessor.splitAndRemoveComment(sql); + assertThat(subSqls).hasSize(1); + assertThat(subSqls.get(0)).isEqualTo(sql); + } }