Skip to content

Commit

Permalink
Merge 948480d into a0d6901
Browse files Browse the repository at this point in the history
  • Loading branch information
xbkaishui committed Sep 24, 2020
2 parents a0d6901 + 948480d commit 0c7d1ae
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 78 deletions.
Expand Up @@ -21,6 +21,7 @@
import lombok.Setter;
import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;

import java.util.Collection;
import java.util.Map;

/**
Expand All @@ -32,5 +33,5 @@ public final class YamlRuleSchemaMetaData implements YamlConfiguration {

private YamlSchemaMetaData configuredSchemaMetaData;

private Map<String, YamlSchemaMetaData> unconfiguredSchemaMetaDataMap;
private Map<String, Collection<String>> unconfiguredSchemaMetaDataMap;
}
Expand Up @@ -46,28 +46,21 @@ public final class RuleSchemaMetaDataYamlSwapper implements YamlSwapper<YamlRule
public YamlRuleSchemaMetaData swapToYamlConfiguration(final RuleSchemaMetaData metaData) {
YamlRuleSchemaMetaData result = new YamlRuleSchemaMetaData();
result.setConfiguredSchemaMetaData(convertYamlSchema(metaData.getConfiguredSchemaMetaData()));
Map<String, YamlSchemaMetaData> unconfigured = metaData.getUnconfiguredSchemaMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> convertUnconfiguredYamlSchema(entry.getValue())));
result.setUnconfiguredSchemaMetaDataMap(unconfigured);
result.setUnconfiguredSchemaMetaDataMap(metaData.getUnconfiguredSchemaMetaDataMap());
return result;
}

@Override
public RuleSchemaMetaData swapToObject(final YamlRuleSchemaMetaData yamlConfig) {
SchemaMetaData configured = Optional.ofNullable(yamlConfig.getConfiguredSchemaMetaData()).map(this::convertSchema).orElse(new SchemaMetaData());
Map<String, Collection<String>> unconfigured = Optional.ofNullable(yamlConfig.getUnconfiguredSchemaMetaDataMap()).map(e -> e.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> convertUnconfiguredSchema(entry.getValue())))).orElse(new HashMap<>());
Map<String, Collection<String>> unconfigured = Optional.ofNullable(yamlConfig.getUnconfiguredSchemaMetaDataMap()).orElse(new HashMap<>());
return new RuleSchemaMetaData(configured, unconfigured);
}

private SchemaMetaData convertSchema(final YamlSchemaMetaData schema) {
return new SchemaMetaData(schema.getTables().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> convertTable(entry.getValue()))));
}

private Collection<String> convertUnconfiguredSchema(final YamlSchemaMetaData schema) {
return schema.getTables().keySet().stream().collect(Collectors.toList());
}

private TableMetaData convertTable(final YamlTableMetaData table) {
return new TableMetaData(convertColumns(table.getColumns()), convertIndexes(table.getIndexes()));
}
Expand Down Expand Up @@ -95,12 +88,6 @@ private YamlSchemaMetaData convertYamlSchema(final SchemaMetaData schema) {
return result;
}

private YamlSchemaMetaData convertUnconfiguredYamlSchema(final Collection<String> tableNames) {
SchemaMetaData schemaMetaData = new SchemaMetaData();
tableNames.forEach(table -> schemaMetaData.put(table, new TableMetaData()));
return convertYamlSchema(schemaMetaData);
}

private YamlTableMetaData convertYamlTable(final TableMetaData table) {
YamlTableMetaData result = new YamlTableMetaData();
result.setColumns(convertYamlColumns(table.getColumns()));
Expand Down
Expand Up @@ -48,7 +48,7 @@ public void assertSwapToYamlRuleSchemaMetaData() {
assertThat(yamlRuleSchemaMetaData.getConfiguredSchemaMetaData().getTables().get("t_order").getIndexes().keySet(), is(Collections.singleton("primary")));
assertThat(yamlRuleSchemaMetaData.getConfiguredSchemaMetaData().getTables().get("t_order").getColumns().keySet(), is(Collections.singleton("id")));
assertThat(yamlRuleSchemaMetaData.getUnconfiguredSchemaMetaDataMap().keySet(), is(Collections.singleton("ds_0")));
assertThat(yamlRuleSchemaMetaData.getUnconfiguredSchemaMetaDataMap().get("ds_0").getTables().keySet(), is(Collections.singleton("t_user")));
assertThat(yamlRuleSchemaMetaData.getUnconfiguredSchemaMetaDataMap().get("ds_0"), is(Arrays.asList("t_user")));
}

@Test
Expand Down
Expand Up @@ -30,15 +30,4 @@ configuredSchemaMetaData:
name: PRIMARY
unconfiguredSchemaMetaDataMap:
ds_0:
tables:
t_user:
columns:
id:
caseSensitive: false
dataType: 0
generated: false
name: id
primaryKey: true
indexes:
primary:
name: PRIMARY
- t_user
Expand Up @@ -30,15 +30,4 @@ configuredSchemaMetaData:
name: PRIMARY
unconfiguredSchemaMetaDataMap:
ds_0:
tables:
t_user:
columns:
id:
caseSensitive: false
dataType: 0
generated: false
name: id
primaryKey: true
indexes:
primary:
name: PRIMARY
- t_user
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.infra.metadata.refresh.MetaDataRefreshStrategy;
import org.apache.shardingsphere.infra.metadata.refresh.TableMetaDataLoaderCallback;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaDataLoader;
import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateTableStatementContext;

import javax.sql.DataSource;
Expand All @@ -46,18 +45,13 @@ public void refreshMetaData(final ShardingSphereMetaData metaData, final Databas
if (tableMetaData.isPresent()) {
metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData().put(tableName, tableMetaData.get());
} else {
refreshUnconfiguredMetaData(metaData, databaseType, dataSourceMap, tableName);
refreshUnconfiguredMetaData(metaData, dataSourceMap, tableName);
}
}

private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData,
final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final String tableName) throws SQLException {
private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData, final Map<String, DataSource> dataSourceMap, final String tableName) throws SQLException {
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
Optional<TableMetaData> tableMetaData = TableMetaDataLoader.loadWithoutColumnMetaData(entry.getValue(), tableName, databaseType.getName());
if (tableMetaData.isPresent()) {
refreshUnconfiguredMetaData(metaData, tableName, entry.getKey());
return;
}
refreshUnconfiguredMetaData(metaData, tableName, entry.getKey());
}
}

Expand Down
Expand Up @@ -54,24 +54,6 @@ public static Optional<TableMetaData> load(final DataSource dataSource, final St
}
}

/**
* Load table without column and index meta data, this is for unconfigured table.
*
* @param dataSource data source
* @param tableNamePattern table name pattern
* @param databaseType database type
* @return table meta data
* @throws SQLException SQL exception
*/
public static Optional<TableMetaData> loadWithoutColumnMetaData(final DataSource dataSource, final String tableNamePattern, final String databaseType) throws SQLException {
try (MetaDataConnection connection = new MetaDataConnection(dataSource.getConnection())) {
if (!isTableExist(connection, tableNamePattern, databaseType)) {
return Optional.empty();
}
return Optional.of(new TableMetaData());
}
}

private static boolean isTableExist(final Connection connection, final String tableNamePattern, final String databaseType) throws SQLException {
try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), JdbcUtil.getSchema(connection, databaseType), tableNamePattern, null)) {
return resultSet.next();
Expand Down
Expand Up @@ -123,16 +123,6 @@ public void assertLoad() throws SQLException {
assertTrue(indexMetaDataMap.containsKey("my_index"));
}

@Test
public void assertLoadWithoutColumnMetaData() throws SQLException {
Optional<TableMetaData> actual = TableMetaDataLoader.loadWithoutColumnMetaData(dataSource, TEST_TABLE, "");
assertTrue(actual.isPresent());
Map<String, ColumnMetaData> columnMetaDataMap = actual.get().getColumns();
assertThat(columnMetaDataMap.size(), is(0));
Map<String, IndexMetaData> indexMetaDataMap = actual.get().getIndexes();
assertThat(indexMetaDataMap.size(), is(0));
}

@Test
public void assertTableNotExist() throws SQLException {
when(databaseMetaData.getTables(TEST_CATALOG, null, TEST_TABLE, null)).thenReturn(tableNotExistResultSet);
Expand Down
Expand Up @@ -60,4 +60,10 @@ public void assertIsPrimaryKey() {
assertTrue(tableMetaData.isPrimaryKey(0));
assertFalse(tableMetaData.isPrimaryKey(1));
}

@Test
public void assertEmptyColumnsWithDefaultConstructor() {
tableMetaData = new TableMetaData();
assertThat(tableMetaData.getColumns(), is(Collections.emptyMap()));
}
}

0 comments on commit 0c7d1ae

Please sign in to comment.