This is an automated email from the ASF dual-hosted git repository.
panjuan 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 b7e7ad6 fix table context init tables error (#11507)
b7e7ad6 is described below
commit b7e7ad6c64ca2c7bd3f5d615824587bd1f70cfe8
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Jul 26 20:56:08 2021 +0800
fix table context init tables error (#11507)
---
.../infra/binder/segment/table/TablesContext.java | 11 +++++-
.../binder/segment/table/TablesContextTest.java | 40 ++++++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
index 157fc6b..aa797d9 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
@@ -50,10 +50,19 @@ public final class TablesContext {
public TablesContext(final Collection<SimpleTableSegment> tableSegments) {
Collection<SimpleTableSegment> actualTables = new
LinkedList<>(tableSegments);
Set<String> tableSets = new HashSet<>(actualTables.size(), 1);
- actualTables.removeIf(each ->
!tableSets.add(each.getTableName().getIdentifier().getValue()));
+ actualTables.removeIf(each ->
!tableSets.add(getTableNameWithOwner(each)));
tables = actualTables;
}
+ private String getTableNameWithOwner(final SimpleTableSegment
tableSegment) {
+ StringBuilder builder = new StringBuilder();
+ if (tableSegment.getOwner().isPresent()) {
+ builder.append(tableSegment.getOwner().get()).append(".");
+ }
+ builder.append(tableSegment.getTableName().getIdentifier().getValue());
+ return builder.toString();
+ }
+
/**
* Get table names.
*
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
index c6449f2..710221b 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
@@ -111,4 +111,44 @@ public final class TablesContextTest {
private ColumnSegment createColumnSegment() {
return new ColumnSegment(0, 0, new IdentifierValue("col"));
}
+
+ @Test
+ public void assertGetSchemaNameWithSameSchemaAndSameTable() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ tableSegment1.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ SimpleTableSegment tableSegment2 = createTableSegment("table_1",
"tbl_1");
+ tableSegment2.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ TablesContext tablesContext = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2));
+ assertTrue(tablesContext.getSchemaName().isPresent());
+ assertThat(tablesContext.getSchemaName().get(), is("sharding_db_1"));
+ }
+
+ @Test
+ public void assertGetSchemaNameWithSameSchemaAndDifferentTable() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ tableSegment1.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ SimpleTableSegment tableSegment2 = createTableSegment("table_2",
"tbl_2");
+ tableSegment2.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ TablesContext tablesContext = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2));
+ assertTrue(tablesContext.getSchemaName().isPresent());
+ assertThat(tablesContext.getSchemaName().get(), is("sharding_db_1"));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void assertGetSchemaNameWithDifferentSchemaAndSameTable() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ tableSegment1.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ SimpleTableSegment tableSegment2 = createTableSegment("table_1",
"tbl_1");
+ tableSegment2.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_2")));
+ new TablesContext(Arrays.asList(tableSegment1,
tableSegment2)).getSchemaName();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void assertGetSchemaNameWithDifferentSchemaAndDifferentTable() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ tableSegment1.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_1")));
+ SimpleTableSegment tableSegment2 = createTableSegment("table_2",
"tbl_2");
+ tableSegment2.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("sharding_db_2")));
+ new TablesContext(Arrays.asList(tableSegment1,
tableSegment2)).getSchemaName();
+ }
}