You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/07/19 15:34:11 UTC
[shardingsphere] branch master updated: Split DataSourceSegment to URL based and hostname+port based. (#19312)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 566479a1e4d Split DataSourceSegment to URL based and hostname+port based. (#19312)
566479a1e4d is described below
commit 566479a1e4ddf92d37ebbddaa866c5618c045bb6
Author: yx9o <ya...@163.com>
AuthorDate: Tue Jul 19 23:34:04 2022 +0800
Split DataSourceSegment to URL based and hostname+port based. (#19312)
* Split DataSourceSegment.
* Update.
---
.../core/kernel/KernelDistSQLStatementVisitor.java | 20 ++++++--------
.../distsql/parser/segment/DataSourceSegment.java | 12 ++------
... => HostnameAndPortBasedDataSourceSegment.java} | 22 ++++++---------
...Segment.java => URLBasedDataSourceSegment.java} | 24 ++++------------
.../rdl/resource/AlterResourceBackendHandler.java | 17 ++++++++----
.../rdl/resource/ResourceSegmentsConverter.java | 19 +++++++++----
.../resource/AddResourceBackendHandlerTest.java | 8 ++++--
.../resource/AlterResourceBackendHandlerTest.java | 8 ++++--
.../resource/ResourceSegmentsConverterTest.java | 8 ++++--
.../asserts/segment/distsql/DataSourceAssert.java | 32 ++++++++++++----------
10 files changed, 82 insertions(+), 88 deletions(-)
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index d4dfa25b96a..0ab38d09d81 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -80,8 +80,10 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.distsql.parser.segment.CacheOptionSegment;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
import org.apache.shardingsphere.distsql.parser.segment.TransactionProviderSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.statement.ral.hint.ClearHintStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ExportDatabaseConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
@@ -156,21 +158,17 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
@Override
public ASTNode visitDataSource(final DataSourceContext ctx) {
- String url = null;
- String hostname = null;
- String port = null;
- String dbName = null;
+ String password = null == ctx.password() ? "" : getPassword(ctx.password());
+ Properties props = getProperties(ctx.propertiesDefinition());
+ DataSourceSegment result = null;
if (null != ctx.urlSource()) {
- url = new IdentifierValue(ctx.urlSource().url().getText()).getValue();
+ result = new URLBasedDataSourceSegment(getIdentifierValue(ctx.dataSourceName()), getIdentifierValue(ctx.urlSource().url()), ctx.user().getText(), password, props);
}
if (null != ctx.simpleSource()) {
- hostname = ctx.simpleSource().hostname().getText();
- port = ctx.simpleSource().port().getText();
- dbName = ctx.simpleSource().dbName().getText();
+ result = new HostnameAndPortBasedDataSourceSegment(getIdentifierValue(ctx.dataSourceName()), ctx.simpleSource().hostname().getText(), ctx.simpleSource().port().getText(),
+ ctx.simpleSource().dbName().getText(), ctx.user().getText(), password, props);
}
- String password = null == ctx.password() ? "" : getPassword(ctx.password());
- Properties props = getProperties(ctx.propertiesDefinition());
- return new DataSourceSegment(getIdentifierValue(ctx.dataSourceName()), url, hostname, port, dbName, ctx.user().getText(), password, props);
+ return result;
}
private String getPassword(final List<PasswordContext> passwordContexts) {
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
index 8636486b287..76b597ae0e6 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
@@ -28,21 +28,13 @@ import java.util.Properties;
*/
@RequiredArgsConstructor
@Getter
-public final class DataSourceSegment implements ASTNode {
+public class DataSourceSegment implements ASTNode {
private final String name;
- private final String url;
-
- private final String hostname;
-
- private final String port;
-
- private final String database;
-
private final String user;
private final String password;
- private final Properties properties;
+ private final Properties props;
}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/HostnameAndPortBasedDataSourceSegment.java
similarity index 68%
copy from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
copy to shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/HostnameAndPortBasedDataSourceSegment.java
index 8636486b287..78b0608b8d9 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/HostnameAndPortBasedDataSourceSegment.java
@@ -18,21 +18,14 @@
package org.apache.shardingsphere.distsql.parser.segment;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Properties;
/**
- * Data source segment.
+ * Hostname and port based data source segment.
*/
-@RequiredArgsConstructor
@Getter
-public final class DataSourceSegment implements ASTNode {
-
- private final String name;
-
- private final String url;
+public final class HostnameAndPortBasedDataSourceSegment extends DataSourceSegment {
private final String hostname;
@@ -40,9 +33,10 @@ public final class DataSourceSegment implements ASTNode {
private final String database;
- private final String user;
-
- private final String password;
-
- private final Properties properties;
+ public HostnameAndPortBasedDataSourceSegment(final String name, final String hostname, final String port, final String database, final String user, final String password, final Properties props) {
+ super(name, user, password, props);
+ this.hostname = hostname;
+ this.port = port;
+ this.database = database;
+ }
}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/URLBasedDataSourceSegment.java
similarity index 66%
copy from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
copy to shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/URLBasedDataSourceSegment.java
index 8636486b287..792d3e89139 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/URLBasedDataSourceSegment.java
@@ -18,31 +18,19 @@
package org.apache.shardingsphere.distsql.parser.segment;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Properties;
/**
- * Data source segment.
+ * URL based data source segment.
*/
-@RequiredArgsConstructor
@Getter
-public final class DataSourceSegment implements ASTNode {
-
- private final String name;
+public final class URLBasedDataSourceSegment extends DataSourceSegment {
private final String url;
- private final String hostname;
-
- private final String port;
-
- private final String database;
-
- private final String user;
-
- private final String password;
-
- private final Properties properties;
+ public URLBasedDataSourceSegment(final String name, final String url, final String user, final String password, final Properties props) {
+ super(name, user, password, props);
+ this.url = url;
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
index f891cf74853..533839b81d5 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterResourceStatement;
import org.apache.shardingsphere.infra.database.metadata.url.JdbcUrl;
import org.apache.shardingsphere.infra.database.metadata.url.StandardJdbcUrlParser;
@@ -109,11 +111,16 @@ public final class AlterResourceBackendHandler extends DatabaseRequiredBackendHa
}
private boolean isIdenticalDatabase(final DataSourceSegment segment, final DataSource dataSource) {
- String hostName = segment.getHostname();
- String port = segment.getPort();
- String database = segment.getDatabase();
- if (null != segment.getUrl() && (null == hostName || null == port || null == database)) {
- JdbcUrl segmentJdbcUrl = new StandardJdbcUrlParser().parse(segment.getUrl());
+ String hostName = null;
+ String port = null;
+ String database = null;
+ if (segment instanceof HostnameAndPortBasedDataSourceSegment) {
+ hostName = ((HostnameAndPortBasedDataSourceSegment) segment).getHostname();
+ port = ((HostnameAndPortBasedDataSourceSegment) segment).getPort();
+ database = ((HostnameAndPortBasedDataSourceSegment) segment).getDatabase();
+ }
+ if (segment instanceof URLBasedDataSourceSegment) {
+ JdbcUrl segmentJdbcUrl = new StandardJdbcUrlParser().parse(((URLBasedDataSourceSegment) segment).getUrl());
hostName = segmentJdbcUrl.getHostname();
port = String.valueOf(segmentJdbcUrl.getPort());
database = segmentJdbcUrl.getDatabase();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverter.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverter.java
index a3aec6fdef7..9374ee2be32 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverter.java
@@ -21,8 +21,10 @@ import com.zaxxer.hikari.HikariDataSource;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
-import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import java.util.Collection;
import java.util.LinkedHashMap;
@@ -55,16 +57,21 @@ public final class ResourceSegmentsConverter {
result.put("jdbcUrl", getURL(databaseType, segment));
result.put("username", segment.getUser());
result.put("password", segment.getPassword());
- if (null != segment.getProperties()) {
- result.putAll((Map) segment.getProperties());
+ if (null != segment.getProps()) {
+ result.putAll((Map) segment.getProps());
}
return result;
}
private static String getURL(final DatabaseType databaseType, final DataSourceSegment segment) {
- if (null != segment.getUrl()) {
- return segment.getUrl();
+ String result = null;
+ if (segment instanceof URLBasedDataSourceSegment) {
+ result = ((URLBasedDataSourceSegment) segment).getUrl();
}
- return String.format("%s//%s:%s/%s", databaseType.getJdbcUrlPrefixes().iterator().next(), segment.getHostname(), segment.getPort(), segment.getDatabase());
+ if (segment instanceof HostnameAndPortBasedDataSourceSegment) {
+ HostnameAndPortBasedDataSourceSegment actualSegment = (HostnameAndPortBasedDataSourceSegment) segment;
+ result = String.format("%s//%s:%s/%s", databaseType.getJdbcUrlPrefixes().iterator().next(), actualSegment.getHostname(), actualSegment.getPort(), actualSegment.getDatabase());
+ }
+ return result;
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
index 1ead6432393..7494507ca12 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.AddResourceStatement;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesValidator;
@@ -107,13 +109,13 @@ public final class AddResourceBackendHandlerTest extends ProxyContextRestorer {
}
private AddResourceStatement createAddResourceStatement() {
- return new AddResourceStatement(Collections.singleton(new DataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/test0", null, null, null, "root", "", new Properties())));
+ return new AddResourceStatement(Collections.singleton(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/test0", "root", "", new Properties())));
}
private AddResourceStatement createAlterResourceStatementWithDuplicateResourceNames() {
Collection<DataSourceSegment> result = new LinkedList<>();
- result.add(new DataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_0", null, null, null, "root", "", new Properties()));
- result.add(new DataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", null, null, null, "root", "", new Properties()));
+ result.add(new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()));
+ result.add(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties()));
return new AddResourceStatement(result);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
index 79051316838..fc825252bb4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterResourceStatement;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesValidator;
@@ -130,13 +132,13 @@ public final class AlterResourceBackendHandlerTest extends ProxyContextRestorer
}
private AlterResourceStatement createAlterResourceStatement(final String resourceName) {
- return new AlterResourceStatement(Collections.singleton(new DataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0", null, null, null, "root", "", new Properties())));
+ return new AlterResourceStatement(Collections.singleton(new URLBasedDataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0", "root", "", new Properties())));
}
private AlterResourceStatement createAlterResourceStatementWithDuplicateResourceNames() {
Collection<DataSourceSegment> result = new LinkedList<>();
- result.add(new DataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_0", null, null, null, "root", "", new Properties()));
- result.add(new DataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", null, null, null, "root", "", new Properties()));
+ result.add(new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()));
+ result.add(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties()));
return new AlterResourceStatement(result);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverterTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverterTest.java
index c579c3f2fb5..71bc5afa2a7 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverterTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/ResourceSegmentsConverterTest.java
@@ -18,8 +18,10 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
-import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import org.junit.Test;
import java.util.Arrays;
@@ -47,8 +49,8 @@ public final class ResourceSegmentsConverterTest {
Collection<DataSourceSegment> result = new LinkedList<>();
Properties customPoolProps = new Properties();
customPoolProps.setProperty("maxPoolSize", "30");
- result.add(new DataSourceSegment("ds0", null, "127.0.0.1", "3306", "demo_ds_0", "root0", "root0", customPoolProps));
- result.add(new DataSourceSegment("ds1", "jdbc:mysql://127.0.0.1:3306/demo_ds_1?useSSL=false", null, null, null, "root1", "root1", customPoolProps));
+ result.add(new HostnameAndPortBasedDataSourceSegment("ds0", "127.0.0.1", "3306", "demo_ds_0", "root0", "root0", customPoolProps));
+ result.add(new URLBasedDataSourceSegment("ds1", "jdbc:mysql://127.0.0.1:3306/demo_ds_1?useSSL=false", "root1", "root1", customPoolProps));
return result;
}
}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/DataSourceAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/DataSourceAssert.java
index 8d4bcdf12a1..5bb0911a093 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/DataSourceAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/DataSourceAssert.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
+import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.distsql.ExpectedDataSource;
@@ -46,21 +48,21 @@ public final class DataSourceAssert {
assertNull(assertContext.getText("Actual dataSource should not exist."), actual);
} else {
assertNotNull(assertContext.getText("Actual dataSource should exist."), actual);
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getName(), is(expected.getName()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getUrl(), is(expected.getUrl()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getHostname(), is(expected.getHostname()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getPort(), is(expected.getPort()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getDatabase(), is(expected.getDb()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getUser(), is(expected.getUser()));
- assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
- actual.getClass().getSimpleName())), actual.getPassword(), is(expected.getPassword()));
- PropertiesAssert.assertIs(assertContext, actual.getProperties(), expected.getProps());
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ", actual.getClass().getSimpleName())), actual.getName(), is(expected.getName()));
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ", actual.getClass().getSimpleName())), actual.getUser(), is(expected.getUser()));
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ", actual.getClass().getSimpleName())), actual.getPassword(), is(expected.getPassword()));
+ PropertiesAssert.assertIs(assertContext, actual.getProps(), expected.getProps());
+ if (actual instanceof URLBasedDataSourceSegment) {
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
+ actual.getClass().getSimpleName())), ((URLBasedDataSourceSegment) actual).getUrl(), is(expected.getUrl()));
+ }
+ if (actual instanceof HostnameAndPortBasedDataSourceSegment) {
+ HostnameAndPortBasedDataSourceSegment actualSegment = (HostnameAndPortBasedDataSourceSegment) actual;
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ",
+ actual.getClass().getSimpleName())), actualSegment.getHostname(), is(expected.getHostname()));
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ", actual.getClass().getSimpleName())), actualSegment.getPort(), is(expected.getPort()));
+ assertThat(assertContext.getText(String.format("`%s`'s datasource segment assertion error: ", actual.getClass().getSimpleName())), actualSegment.getDatabase(), is(expected.getDb()));
+ }
}
}
}