diff --git a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java index 6eda47fd70221..663c237fe55ab 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/ShardingTableMetaData.java @@ -22,13 +22,9 @@ import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.rule.ShardingRule; import io.shardingsphere.core.rule.TableRule; -import lombok.Setter; -import java.sql.Connection; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; -import java.util.LinkedList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -41,15 +37,14 @@ */ public class ShardingTableMetaData { + private final TableLoader tableLoader; + private final TableMetaDataLoader tableMetaDataLoader; private final Map tableMetaDataMap = new ConcurrentHashMap<>(); - @Setter - private TableMetaDataExecutorAdapter executorAdapter; - public ShardingTableMetaData(final ListeningExecutorService executorService, final TableMetaDataExecutorAdapter executorAdapter) { - this.executorAdapter = executorAdapter; + tableLoader = new TableLoader(executorAdapter); tableMetaDataLoader = new TableMetaDataLoader(executorService, executorAdapter); } @@ -76,25 +71,14 @@ private void initLogicTables(final ShardingRule shardingRule) { private void initDefaultTables(final ShardingRule shardingRule) throws SQLException { Optional actualDefaultDataSourceName = shardingRule.findActualDefaultDataSourceName(); if (actualDefaultDataSourceName.isPresent()) { - for (String each : getAllTableNames(actualDefaultDataSourceName.get())) { + for (String each : tableLoader.getAllTableNames(actualDefaultDataSourceName.get())) { tableMetaDataMap.put(each, tableMetaDataLoader.loadTableMetaData(each, shardingRule)); } } } - private Collection getAllTableNames(final String dataSourceName) throws SQLException { - Collection result = new LinkedList<>(); - try (Connection connection = executorAdapter.getConnection(dataSourceName); - ResultSet resultSet = connection.getMetaData().getTables(null, null, null, null)) { - while (resultSet.next()) { - result.add(resultSet.getString("TABLE_NAME")); - } - } - return result; - } - /** - * Put table meta data. + * Add table meta data. * * @param logicTableName logic table name * @param tableMetaData table meta data diff --git a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableLoader.java b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableLoader.java new file mode 100644 index 0000000000000..bd79f2bc5ad1f --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableLoader.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.core.metadata.table; + +import lombok.RequiredArgsConstructor; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collection; +import java.util.LinkedList; + +/** + * Table loader. + * + * @author zhangliang + */ +@RequiredArgsConstructor +public final class TableLoader { + + private final TableMetaDataExecutorAdapter executorAdapter; + + /** + * Get all table names. + * + * @param dataSourceName data source name + * @return table names + * @throws SQLException SQL exception + */ + public Collection getAllTableNames(final String dataSourceName) throws SQLException { + Collection result = new LinkedList<>(); + try (Connection connection = executorAdapter.getConnection(dataSourceName); + ResultSet resultSet = connection.getMetaData().getTables(null, null, null, null)) { + while (resultSet.next()) { + result.add(resultSet.getString("TABLE_NAME")); + } + } + return result; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableMetaData.java b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableMetaData.java index 44cc870a9af46..850dec98830e1 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableMetaData.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/metadata/table/TableMetaData.java @@ -41,7 +41,7 @@ public final class TableMetaData { /** * Get all column names. * - * @return column names. + * @return column names */ public Collection getAllColumnNames() { Collection result = new LinkedList<>();