This is an automated email from the ASF dual-hosted git repository.
terrymanu 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 a0375f4d1f5 Refactor H2MetaDataLoader (#39785)
a0375f4d1f5 is described below
commit a0375f4d1f5a409ea3100fd829c0de28d505fc27
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 10 11:23:47 2026 +0800
Refactor H2MetaDataLoader (#39785)
* Raise anonymous inner class length limit
* Set variable declaration usage distance to 7
* Refactor H2MetaDataLoader
---
.../h2/metadata/data/loader/H2MetaDataLoader.java | 72 ++++++++++++----------
1 file changed, 38 insertions(+), 34 deletions(-)
diff --git
a/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/data/loader/H2MetaDataLoader.java
b/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/data/loader/H2MetaDataLoader.java
index b5ab19aec3f..289f4f01989 100644
---
a/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/data/loader/H2MetaDataLoader.java
+++
b/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/data/loader/H2MetaDataLoader.java
@@ -73,48 +73,31 @@ public final class H2MetaDataLoader implements
DialectMetaDataLoader {
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial
material) throws SQLException {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
try (Connection connection = material.getDataSource().getConnection())
{
- Map<String, Collection<ColumnMetaData>> columnMetaDataMap =
loadColumnMetaDataMap(connection, material.getActualTableNames());
+ Collection<String> tables = material.getActualTableNames();
+ Map<String, Collection<ColumnMetaData>> columnMetaDataMap =
loadColumnMetaDataMap(connection, tables, loadTablePrimaryKeys(connection,
tables), loadTableGenerated(connection, tables));
Collection<String> viewNames = columnMetaDataMap.isEmpty() ?
Collections.emptySet() : loadViewNames(connection, columnMetaDataMap.keySet());
Map<String, Collection<IndexMetaData>> indexMetaDataMap =
columnMetaDataMap.isEmpty() ? Collections.emptyMap() :
loadIndexMetaData(connection, columnMetaDataMap.keySet());
for (Entry<String, Collection<ColumnMetaData>> entry :
columnMetaDataMap.entrySet()) {
Collection<IndexMetaData> indexMetaDataList =
indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList());
- tableMetaDataList.add(new TableMetaData(entry.getKey(),
entry.getValue(), indexMetaDataList, Collections.emptyList(),
- viewNames.contains(entry.getKey()) ? TableType.VIEW :
TableType.TABLE));
+ tableMetaDataList.add(
+ new TableMetaData(entry.getKey(), entry.getValue(),
indexMetaDataList, Collections.emptyList(), viewNames.contains(entry.getKey())
? TableType.VIEW : TableType.TABLE));
}
}
return Collections.singleton(new
SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
}
- private Collection<String> loadViewNames(final Connection connection,
final Collection<String> tableNames) throws SQLException {
- Collection<String> result = new LinkedList<>();
- try (PreparedStatement preparedStatement =
connection.prepareStatement(getViewMetaDataSQL(tableNames))) {
- preparedStatement.setString(1, connection.getCatalog());
- preparedStatement.setString(2, "PUBLIC");
- try (ResultSet resultSet = preparedStatement.executeQuery()) {
- while (resultSet.next()) {
- result.add(resultSet.getString("TABLE_NAME"));
- }
- }
- }
- return result;
- }
-
- private String getViewMetaDataSQL(final Collection<String> tableNames) {
- return String.format(VIEW_META_DATA_SQL, tableNames.stream().map(each
-> String.format("'%s'", each).toUpperCase()).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 Map<String, Collection<String>> tablePrimaryKeys,
+
final Map<String, Map<String, Boolean>> tableGenerated) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>();
try (PreparedStatement preparedStatement =
connection.prepareStatement(getTableMetaDataSQL(tables))) {
- Map<String, Collection<String>> tablePrimaryKeys =
loadTablePrimaryKeys(connection, tables);
- Map<String, Map<String, Boolean>> tableGenerated =
loadTableGenerated(connection, tables);
preparedStatement.setString(1, connection.getCatalog());
preparedStatement.setString(2, "PUBLIC");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
- ColumnMetaData columnMetaData =
- loadColumnMetaData(resultSet,
tablePrimaryKeys.getOrDefault(tableName, Collections.emptyList()),
tableGenerated.getOrDefault(tableName, new HashMap<>()));
+ ColumnMetaData columnMetaData = getColumnMetaData(
+ resultSet,
tablePrimaryKeys.getOrDefault(tableName, Collections.emptyList()),
tableGenerated.getOrDefault(tableName, new HashMap<>()));
if (!result.containsKey(tableName)) {
result.put(tableName, new LinkedList<>());
}
@@ -125,19 +108,38 @@ public final class H2MetaDataLoader implements
DialectMetaDataLoader {
return result;
}
- private ColumnMetaData loadColumnMetaData(final ResultSet resultSet, final
Collection<String> primaryKeys, final Map<String, Boolean> tableGenerated)
throws SQLException {
+ private String getTableMetaDataSQL(final Collection<String> tables) {
+ return tables.isEmpty()
+ ? TABLE_META_DATA_SQL
+ : String.format(TABLE_META_DATA_SQL_IN_TABLES,
tables.stream().map(each -> String.format("'%s'",
each).toUpperCase()).collect(Collectors.joining(",")));
+ }
+
+ private ColumnMetaData getColumnMetaData(final ResultSet resultSet, final
Collection<String> primaryKeys, final Map<String, Boolean> tableGenerated)
throws SQLException {
String columnName = resultSet.getString("COLUMN_NAME");
- String dataType = resultSet.getString("DATA_TYPE");
+ Integer dataType = DataTypeRegistry.getDataType(getDatabaseType(),
resultSet.getString("DATA_TYPE")).orElse(Types.OTHER);
boolean primaryKey = primaryKeys.contains(columnName);
boolean generated = tableGenerated.getOrDefault(columnName,
Boolean.FALSE);
boolean isVisible = resultSet.getBoolean("IS_VISIBLE");
boolean isNullable = "YES".equals(resultSet.getString("IS_NULLABLE"));
- return new ColumnMetaData(columnName,
DataTypeRegistry.getDataType(getDatabaseType(), dataType).orElse(Types.OTHER),
primaryKey, generated, false, isVisible, false, isNullable);
+ return new ColumnMetaData(columnName, dataType, primaryKey, generated,
false, isVisible, false, isNullable);
}
- private String getTableMetaDataSQL(final Collection<String> tables) {
- return tables.isEmpty() ? TABLE_META_DATA_SQL
- : String.format(TABLE_META_DATA_SQL_IN_TABLES,
tables.stream().map(each -> String.format("'%s'",
each).toUpperCase()).collect(Collectors.joining(",")));
+ private Collection<String> loadViewNames(final Connection connection,
final Collection<String> tableNames) throws SQLException {
+ Collection<String> result = new LinkedList<>();
+ try (PreparedStatement preparedStatement =
connection.prepareStatement(getViewMetaDataSQL(tableNames))) {
+ preparedStatement.setString(1, connection.getCatalog());
+ preparedStatement.setString(2, "PUBLIC");
+ try (ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ result.add(resultSet.getString("TABLE_NAME"));
+ }
+ }
+ }
+ return result;
+ }
+
+ private String getViewMetaDataSQL(final Collection<String> tableNames) {
+ return String.format(VIEW_META_DATA_SQL, tableNames.stream().map(each
-> String.format("'%s'", each).toUpperCase()).collect(Collectors.joining(",")));
}
private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final
Connection connection, final Collection<String> tableNames) throws SQLException
{
@@ -168,7 +170,8 @@ public final class H2MetaDataLoader implements
DialectMetaDataLoader {
}
private String getPrimaryKeyMetaDataSQL(final Collection<String> tables) {
- return tables.isEmpty() ? PRIMARY_KEY_META_DATA_SQL
+ return tables.isEmpty()
+ ? PRIMARY_KEY_META_DATA_SQL
: String.format(PRIMARY_KEY_META_DATA_SQL_IN_TABLES,
tables.stream().map(each -> String.format("'%s'",
each).toUpperCase()).collect(Collectors.joining(",")));
}
@@ -189,7 +192,8 @@ public final class H2MetaDataLoader implements
DialectMetaDataLoader {
}
private String getGeneratedInfoSQL(final Collection<String> tables) {
- return tables.isEmpty() ? GENERATED_INFO_SQL
+ return tables.isEmpty()
+ ? GENERATED_INFO_SQL
: String.format(GENERATED_INFO_SQL_IN_TABLES,
tables.stream().map(each -> String.format("'%s'",
each).toUpperCase()).collect(Collectors.joining(",")));
}