This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 754e5582c2f Refactor OracleMetaDataLoader. (#31041)
754e5582c2f is described below

commit 754e5582c2f691e26cab8de69a09aebe3becdb1a
Author: Cong Hu <[email protected]>
AuthorDate: Sun Apr 28 13:47:44 2024 +0800

    Refactor OracleMetaDataLoader. (#31041)
---
 .../metadata/data/loader/OracleMetaDataLoader.java | 40 +++++++++++++---------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git 
a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java
 
b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java
index 456af3c70c9..3ada32a495f 100644
--- 
a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java
+++ 
b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java
@@ -82,28 +82,34 @@ public final class OracleMetaDataLoader implements 
DialectMetaDataLoader {
     
     @Override
     public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial 
material) throws SQLException {
-        Collection<String> viewNames = new LinkedList<>();
-        Map<String, Collection<ColumnMetaData>> columnMetaDataMap = new 
HashMap<>(material.getActualTableNames().size(), 1F);
-        Map<String, Collection<IndexMetaData>> indexMetaDataMap = new 
HashMap<>(material.getActualTableNames().size(), 1F);
+        Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
         try (Connection connection = new 
MetaDataLoaderConnection(TypedSPILoader.getService(DatabaseType.class, 
"Oracle"), material.getDataSource().getConnection())) {
-            for (List<String> each : Lists.partition(new 
ArrayList<>(material.getActualTableNames()), MAX_EXPRESSION_SIZE)) {
-                viewNames.addAll(loadViewNames(connection, each));
-                columnMetaDataMap.putAll(loadColumnMetaDataMap(connection, 
each));
-                indexMetaDataMap.putAll(loadIndexMetaData(connection, each));
-            }
+            tableMetaDataList.addAll(getTableMetaDataList(connection, 
connection.getSchema(), material.getActualTableNames()));
         }
-        Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
+        return Collections.singletonList(new 
SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
+    }
+    
+    private Collection<TableMetaData> getTableMetaDataList(final Connection 
connection, final String schema, final Collection<String> tableNames) throws 
SQLException {
+        Collection<String> viewNames = new LinkedList<>();
+        Map<String, Collection<ColumnMetaData>> columnMetaDataMap = new 
HashMap<>(tableNames.size(), 1F);
+        Map<String, Collection<IndexMetaData>> indexMetaDataMap = new 
HashMap<>(tableNames.size(), 1F);
+        for (List<String> each : Lists.partition(new ArrayList<>(tableNames), 
MAX_EXPRESSION_SIZE)) {
+            viewNames.addAll(loadViewNames(connection, each, schema));
+            columnMetaDataMap.putAll(loadColumnMetaDataMap(connection, each, 
schema));
+            indexMetaDataMap.putAll(loadIndexMetaData(connection, each, 
schema));
+        }
+        Collection<TableMetaData> result = new LinkedList<>();
         for (Entry<String, Collection<ColumnMetaData>> entry : 
columnMetaDataMap.entrySet()) {
-            tableMetaDataList.add(new TableMetaData(entry.getKey(), 
entry.getValue(), indexMetaDataMap.getOrDefault(entry.getKey(), 
Collections.emptyList()), Collections.emptyList(),
+            result.add(new TableMetaData(entry.getKey(), entry.getValue(), 
indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()), 
Collections.emptyList(),
                     viewNames.contains(entry.getKey()) ? TableType.VIEW : 
TableType.TABLE));
         }
-        return Collections.singletonList(new 
SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
+        return result;
     }
     
-    private Collection<String> loadViewNames(final Connection connection, 
final Collection<String> tables) throws SQLException {
+    private Collection<String> loadViewNames(final Connection connection, 
final Collection<String> tables, final String schema) throws SQLException {
         Collection<String> result = new LinkedList<>();
         try (PreparedStatement preparedStatement = 
connection.prepareStatement(getViewMetaDataSQL(tables))) {
-            preparedStatement.setString(1, connection.getSchema());
+            preparedStatement.setString(1, schema);
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
                 while (resultSet.next()) {
                     result.add(resultSet.getString(1));
@@ -117,12 +123,12 @@ public final class OracleMetaDataLoader implements 
DialectMetaDataLoader {
         return String.format(VIEW_META_DATA_SQL, tableNames.stream().map(each 
-> String.format("'%s'", each)).collect(Collectors.joining(",")));
     }
     
-    private Map<String, Collection<ColumnMetaData>> 
loadColumnMetaDataMap(final Connection connection, final Collection<String> 
tables) throws SQLException {
+    private Map<String, Collection<ColumnMetaData>> 
loadColumnMetaDataMap(final Connection connection, final Collection<String> 
tables, final String schema) throws SQLException {
         Map<String, Collection<ColumnMetaData>> result = new 
HashMap<>(tables.size(), 1F);
         try (PreparedStatement preparedStatement = 
connection.prepareStatement(getTableMetaDataSQL(tables, 
connection.getMetaData()))) {
             Map<String, Integer> dataTypes = new 
DataTypeLoader().load(connection.getMetaData(), getType());
             Map<String, Collection<String>> tablePrimaryKeys = 
loadTablePrimaryKeys(connection, tables);
-            preparedStatement.setString(1, connection.getSchema());
+            preparedStatement.setString(1, schema);
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
                 while (resultSet.next()) {
                     String tableName = resultSet.getString("TABLE_NAME");
@@ -181,10 +187,10 @@ public final class OracleMetaDataLoader implements 
DialectMetaDataLoader {
         return databaseMetaData.getDatabaseMajorVersion() >= 
COLLATION_START_MAJOR_VERSION && databaseMetaData.getDatabaseMinorVersion() >= 
IDENTITY_COLUMN_START_MINOR_VERSION;
     }
     
-    private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final 
Connection connection, final Collection<String> tableNames) throws SQLException 
{
+    private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final 
Connection connection, final Collection<String> tableNames, final String 
schema) throws SQLException {
         Map<String, Collection<IndexMetaData>> result = new 
HashMap<>(tableNames.size(), 1F);
         try (PreparedStatement preparedStatement = 
connection.prepareStatement(getIndexMetaDataSQL(tableNames))) {
-            preparedStatement.setString(1, connection.getSchema());
+            preparedStatement.setString(1, schema);
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
                 while (resultSet.next()) {
                     String indexName = resultSet.getString("INDEX_NAME");

Reply via email to