You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/04/03 12:44:16 UTC
[shardingsphere] branch master updated: get index info via catalog
(#9909)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 9cdf9e7 get index info via catalog (#9909)
9cdf9e7 is described below
commit 9cdf9e7504d23e26a07a261ae446762939809b5d
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Sat Apr 3 20:43:57 2021 +0800
get index info via catalog (#9909)
---
.../shardingsphere/sharding/rule/ShardingRule.java | 9 +++++++++
.../infra/rule/type/DataNodeContainedRule.java | 9 +++++++++
.../fixture/rule/DataNodeContainedFixtureRule.java | 5 +++++
.../metadata/ShardingSphereDatabaseMetaData.java | 23 +++++++++++++---------
4 files changed, 37 insertions(+), 9 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 772baf6..97d1a93 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -512,4 +512,13 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained
public Collection<String> getTables() {
return tableRules.stream().map((Function<TableRule, String>) TableRule::getLogicTable).collect(Collectors.toList());
}
+
+ @Override
+ public Optional<String> findActualTableByCatalog(final String catalog, final String logicTable) {
+ return findTableRule(logicTable).flatMap(tableRule -> findActualTableFromActualDataNode(catalog, tableRule.getActualDataNodes()));
+ }
+
+ private Optional<String> findActualTableFromActualDataNode(final String catalog, final List<DataNode> actualDataNodes) {
+ return actualDataNodes.stream().filter(each -> each.getDataSourceName().equalsIgnoreCase(catalog)).findFirst().map(DataNode::getTableName);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/type/DataNodeContainedRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/type/DataNodeContainedRule.java
index 6c33005..2d7ad45 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/type/DataNodeContainedRule.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/type/DataNodeContainedRule.java
@@ -66,4 +66,13 @@ public interface DataNodeContainedRule extends ShardingSphereRule {
* @return logic table name
*/
Optional<String> findLogicTableByActualTable(String actualTable);
+
+ /**
+ * Find actual table name via catelog.
+ *
+ * @param catalog catalog
+ * @param logicTable logic table name
+ * @return actual table name
+ */
+ Optional<String> findActualTableByCatalog(String catalog, String logicTable);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
index 7b871cc..8c263fc 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
@@ -67,4 +67,9 @@ public final class DataNodeContainedFixtureRule implements DataNodeContainedRule
public Collection<String> getTables() {
return new HashSet<>(actualTableNameMaps.values());
}
+
+ @Override
+ public Optional<String> findActualTableByCatalog(final String catalog, final String logicTable) {
+ return Optional.empty();
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java
index cf16598..af97f94 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.driver.jdbc.core.datasource.metadata;
+import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.driver.jdbc.adapter.AdaptedDatabaseMetaData;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
@@ -128,7 +129,7 @@ public final class ShardingSphereDatabaseMetaData extends AdaptedDatabaseMetaDat
@Override
public ResultSet getColumnPrivileges(final String catalog, final String schema, final String table, final String columnNamePattern) throws SQLException {
return createDatabaseMetaDataResultSet(
- getDatabaseMetaData().getColumnPrivileges(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table), columnNamePattern));
+ getDatabaseMetaData().getColumnPrivileges(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table), columnNamePattern));
}
@Override
@@ -138,27 +139,27 @@ public final class ShardingSphereDatabaseMetaData extends AdaptedDatabaseMetaDat
@Override
public ResultSet getBestRowIdentifier(final String catalog, final String schema, final String table, final int scope, final boolean nullable) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getBestRowIdentifier(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table), scope, nullable));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getBestRowIdentifier(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table), scope, nullable));
}
@Override
public ResultSet getVersionColumns(final String catalog, final String schema, final String table) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getVersionColumns(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table)));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getVersionColumns(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table)));
}
@Override
public ResultSet getPrimaryKeys(final String catalog, final String schema, final String table) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getPrimaryKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table)));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getPrimaryKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table)));
}
@Override
public ResultSet getImportedKeys(final String catalog, final String schema, final String table) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getImportedKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table)));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getImportedKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table)));
}
@Override
public ResultSet getExportedKeys(final String catalog, final String schema, final String table) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getExportedKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table)));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getExportedKeys(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table)));
}
@Override
@@ -175,7 +176,7 @@ public final class ShardingSphereDatabaseMetaData extends AdaptedDatabaseMetaDat
@Override
public ResultSet getIndexInfo(final String catalog, final String schema, final String table, final boolean unique, final boolean approximate) throws SQLException {
- return createDatabaseMetaDataResultSet(getDatabaseMetaData().getIndexInfo(getActualCatalog(catalog), getActualSchema(schema), getActualTable(table), unique, approximate));
+ return createDatabaseMetaDataResultSet(getDatabaseMetaData().getIndexInfo(getActualCatalog(catalog), getActualSchema(schema), getActualTable(catalog, table), unique, approximate));
}
@Override
@@ -215,11 +216,15 @@ public final class ShardingSphereDatabaseMetaData extends AdaptedDatabaseMetaDat
return tableNamePattern;
}
- private String getActualTable(final String table) {
+ private String getActualTable(final String catalog, final String table) {
if (null == table) {
return null;
}
- return findDataNodeContainedRule().map(each -> each.findFirstActualTable(table).orElse(table)).orElse(table);
+ return findDataNodeContainedRule().map(each -> findActualTable(each, catalog, table).orElse(table)).orElse(table);
+ }
+
+ private Optional<String> findActualTable(final DataNodeContainedRule dataNodeContainedRule, final String catalog, final String table) {
+ return Strings.isNullOrEmpty(catalog) ? dataNodeContainedRule.findFirstActualTable(table) : dataNodeContainedRule.findActualTableByCatalog(catalog, table);
}
private Optional<DataNodeContainedRule> findDataNodeContainedRule() {