Skip to content

Commit

Permalink
for #2900, use TableMetaData on EncryptRuntimeContext
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 21, 2019
1 parent 57b8764 commit 2f0965b
Showing 1 changed file with 1 addition and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

package org.apache.shardingsphere.shardingjdbc.jdbc.core.context;

import com.google.common.base.Optional;
import lombok.Getter;
import org.apache.shardingsphere.core.metadata.table.ColumnMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetas;
import org.apache.shardingsphere.core.metadata.table.sharding.ShardingTableMetaData;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.spi.database.DatabaseType;

Expand All @@ -37,7 +35,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
* Runtime context for encrypt.
Expand All @@ -59,7 +56,7 @@ private TableMetas createEncryptTableMetas(final DataSource dataSource, final En
try (Connection connection = dataSource.getConnection()) {
for (String each : encryptRule.getEncryptTableNames()) {
if (isTableExist(connection, each)) {
tables.put(each, new ShardingTableMetaData(getColumnMetaDataList(connection, each), getLogicIndexes(connection, each)));
tables.put(each, new TableMetaData(getColumnMetaDataList(connection, each)));
}
}
}
Expand Down Expand Up @@ -94,25 +91,4 @@ private Collection<String> getPrimaryKeys(final Connection connection, final Str
}
return result;
}

private Set<String> getLogicIndexes(final Connection connection, final String actualTableName) throws SQLException {
Set<String> result = new HashSet<>();
try (ResultSet resultSet = connection.getMetaData().getIndexInfo(connection.getCatalog(), connection.getCatalog(), actualTableName, false, false)) {
while (resultSet.next()) {
Optional<String> logicIndex = getLogicIndex(resultSet.getString("INDEX_NAME"), actualTableName);
if (logicIndex.isPresent()) {
result.add(logicIndex.get());
}
}
}
return result;
}

private Optional<String> getLogicIndex(final String actualIndexName, final String actualTableName) {
String indexNameSuffix = "_" + actualTableName;
if (actualIndexName.contains(indexNameSuffix)) {
return Optional.of(actualIndexName.replace(indexNameSuffix, ""));
}
return Optional.absent();
}
}

0 comments on commit 2f0965b

Please sign in to comment.