Skip to content

Commit

Permalink
add TableLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 1, 2018
1 parent 2d54305 commit cff9022
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,15 +37,14 @@
*/
public class ShardingTableMetaData {

private final TableLoader tableLoader;

private final TableMetaDataLoader tableMetaDataLoader;

private final Map<String, TableMetaData> 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);
}

Expand All @@ -76,25 +71,14 @@ private void initLogicTables(final ShardingRule shardingRule) {
private void initDefaultTables(final ShardingRule shardingRule) throws SQLException {
Optional<String> 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<String> getAllTableNames(final String dataSourceName) throws SQLException {
Collection<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

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<String> getAllTableNames(final String dataSourceName) throws SQLException {
Collection<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class TableMetaData {
/**
* Get all column names.
*
* @return column names.
* @return column names
*/
public Collection<String> getAllColumnNames() {
Collection<String> result = new LinkedList<>();
Expand Down

0 comments on commit cff9022

Please sign in to comment.