From 7a897ad3cf4c3e8178b72b5828db17ef79ffe3e6 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 9 Dec 2024 22:47:05 +0800 Subject: [PATCH] Refactor StandardPipelineTableMetaDataLoader (#33980) --- .../StandardPipelineTableMetaDataLoader.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/loader/StandardPipelineTableMetaDataLoader.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/loader/StandardPipelineTableMetaDataLoader.java index c75f88041fa9a..75da750797e1c 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/loader/StandardPipelineTableMetaDataLoader.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/loader/StandardPipelineTableMetaDataLoader.java @@ -71,26 +71,24 @@ public PipelineTableMetaData getTableMetaData(final String schemaName, final Str return result; } - private void loadTableMetaData(final String schemaName, final String tableNamePattern) throws SQLException { + private void loadTableMetaData(final String schemaName, final String tableName) throws SQLException { try (Connection connection = dataSource.getConnection()) { DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(dataSource.getDatabaseType()).getDialectDatabaseMetaData(); - Map tableMetaDataMap = loadTableMetaData0(connection, dialectDatabaseMetaData.isSchemaAvailable() ? schemaName : null, tableNamePattern); - this.tableMetaDataMap.putAll(tableMetaDataMap); + tableMetaDataMap.putAll(loadTableMetaData(connection, dialectDatabaseMetaData.isSchemaAvailable() ? schemaName : null, tableName)); } } - private Map loadTableMetaData0(final Connection connection, final String schemaName, final String tableNamePattern) throws SQLException { + private Map loadTableMetaData(final Connection connection, final String schemaName, final String tableNamePattern) throws SQLException { Collection tableNames = new LinkedList<>(); try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), schemaName, tableNamePattern, null)) { while (resultSet.next()) { - String tableName = resultSet.getString("TABLE_NAME"); - tableNames.add(tableName); + tableNames.add(resultSet.getString("TABLE_NAME")); } } Map result = new LinkedHashMap<>(tableNames.size(), 1F); for (String each : tableNames) { Collection primaryKeys = loadPrimaryKeys(connection, schemaName, each); - Map> uniqueKeys = loadUniqueIndexesOfTable(connection, schemaName, each); + Map> uniqueKeys = loadUniqueKeys(connection, schemaName, each); Map columnMetaDataMap = new LinkedHashMap<>(); try (ResultSet resultSet = connection.getMetaData().getColumns(connection.getCatalog(), schemaName, each, "%")) { while (resultSet.next()) { @@ -104,8 +102,7 @@ private Map loadTableMetaData0( boolean primaryKey = primaryKeys.contains(columnName); boolean isNullable = "YES".equals(resultSet.getString("IS_NULLABLE")); boolean isUniqueKey = uniqueKeys.values().stream().anyMatch(names -> names.contains(columnName)); - PipelineColumnMetaData columnMetaData = new PipelineColumnMetaData(ordinalPosition, columnName.toString(), dataType, dataTypeName, isNullable, primaryKey, isUniqueKey); - columnMetaDataMap.put(columnName, columnMetaData); + columnMetaDataMap.put(columnName, new PipelineColumnMetaData(ordinalPosition, columnName.toString(), dataType, dataTypeName, isNullable, primaryKey, isUniqueKey)); } } Collection uniqueIndexMetaData = uniqueKeys.entrySet().stream() @@ -117,8 +114,18 @@ private Map loadTableMetaData0( return result; } - private Map> loadUniqueIndexesOfTable(final Connection connection, - final String schemaName, final String tableName) throws SQLException { + private Collection loadPrimaryKeys(final Connection connection, final String schemaName, final String tableName) throws SQLException { + SortedMap result = new TreeMap<>(); + try (ResultSet resultSet = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), schemaName, tableName)) { + while (resultSet.next()) { + result.put(resultSet.getShort("KEY_SEQ"), new ShardingSphereIdentifier(resultSet.getString("COLUMN_NAME"))); + } + } + return result.values(); + } + + private Map> loadUniqueKeys(final Connection connection, + final String schemaName, final String tableName) throws SQLException { Map> orderedColumnsOfIndexes = new LinkedHashMap<>(); // Set approximate=true to avoid Oracle driver 19 run `analyze table` try (ResultSet resultSet = connection.getMetaData().getIndexInfo(connection.getCatalog(), schemaName, tableName, true, true)) { @@ -127,8 +134,8 @@ private Map> load if (null == indexName) { continue; } - orderedColumnsOfIndexes.computeIfAbsent(indexName, unused -> new TreeMap<>()).put( - resultSet.getShort("ORDINAL_POSITION"), new ShardingSphereIdentifier(resultSet.getString("COLUMN_NAME"))); + orderedColumnsOfIndexes.computeIfAbsent(indexName, + unused -> new TreeMap<>()).put(resultSet.getShort("ORDINAL_POSITION"), new ShardingSphereIdentifier(resultSet.getString("COLUMN_NAME"))); } } Map> result = new LinkedHashMap<>(); @@ -138,14 +145,4 @@ private Map> load } return result; } - - private Collection loadPrimaryKeys(final Connection connection, final String schemaName, final String tableName) throws SQLException { - SortedMap result = new TreeMap<>(); - try (ResultSet resultSet = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), schemaName, tableName)) { - while (resultSet.next()) { - result.put(resultSet.getShort("KEY_SEQ"), new ShardingSphereIdentifier(resultSet.getString("COLUMN_NAME"))); - } - } - return result.values(); - } }