From 41c917414fe3d8c3c6209c7e1347fb2f27579a61 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 10 Dec 2024 11:58:43 +0800 Subject: [PATCH] Refactor GenericSchemaBuilder --- .../schema/builder/GenericSchemaBuilder.java | 20 +++---------------- .../schema/model/ShardingSphereColumn.java | 12 +++++++++++ .../model/ShardingSphereConstraint.java | 7 +++++++ .../schema/model/ShardingSphereIndex.java | 7 +++++++ .../metadata/data/model/TableMetaData.java | 2 +- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java index c030d214bd6e2..323c34ee156e2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java @@ -134,28 +134,14 @@ private static Collection convertToTables(final Collection< } private static Collection convertToColumns(final Collection columnMetaDataList) { - Collection result = new LinkedList<>(); - for (ColumnMetaData each : columnMetaDataList) { - result.add(new ShardingSphereColumn(each.getName(), each.getDataType(), each.isPrimaryKey(), each.isGenerated(), each.isCaseSensitive(), each.isVisible(), each.isUnsigned(), - each.isNullable())); - } - return result; + return columnMetaDataList.stream().map(ShardingSphereColumn::new).collect(Collectors.toList()); } private static Collection convertToIndexes(final Collection indexMetaDataList) { - Collection result = new LinkedList<>(); - for (IndexMetaData each : indexMetaDataList) { - ShardingSphereIndex index = new ShardingSphereIndex(each.getName(), each.getColumns(), each.isUnique()); - result.add(index); - } - return result; + return indexMetaDataList.stream().map(ShardingSphereIndex::new).collect(Collectors.toList()); } private static Collection convertToConstraints(final Collection constraintMetaDataList) { - Collection result = new LinkedList<>(); - for (ConstraintMetaData each : constraintMetaDataList) { - result.add(new ShardingSphereConstraint(each.getName(), each.getReferencedTableName())); - } - return result; + return constraintMetaDataList.stream().map(ShardingSphereConstraint::new).collect(Collectors.toList()); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereColumn.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereColumn.java index da4d44ccbb832..35c69931b7725 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereColumn.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereColumn.java @@ -21,6 +21,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; +import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; /** * ShardingSphere column. @@ -46,4 +47,15 @@ public final class ShardingSphereColumn { private final boolean unsigned; private final boolean nullable; + + public ShardingSphereColumn(final ColumnMetaData columnMetaData) { + name = columnMetaData.getName(); + dataType = columnMetaData.getDataType(); + primaryKey = columnMetaData.isPrimaryKey(); + generated = columnMetaData.isGenerated(); + caseSensitive = columnMetaData.isCaseSensitive(); + visible = columnMetaData.isVisible(); + unsigned = columnMetaData.isUnsigned(); + nullable = columnMetaData.isNullable(); + } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraint.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraint.java index 3178bda7c5138..914a6621b8540 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraint.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraint.java @@ -21,6 +21,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; +import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData; /** * ShardingSphere constraint. @@ -34,4 +35,10 @@ public final class ShardingSphereConstraint { private final String name; private final String referencedTableName; + + public ShardingSphereConstraint(final ConstraintMetaData constraintMetaData) { + name = constraintMetaData.getName(); + referencedTableName = constraintMetaData.getReferencedTableName(); + + } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereIndex.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereIndex.java index e0e0f40e269f4..9ab59867ad4b3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereIndex.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereIndex.java @@ -21,6 +21,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; +import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import java.util.Collection; @@ -38,4 +39,10 @@ public final class ShardingSphereIndex { private final Collection columns; private final boolean unique; + + public ShardingSphereIndex(final IndexMetaData indexMetaData) { + name = indexMetaData.getName(); + columns = indexMetaData.getColumns(); + unique = indexMetaData.isUnique(); + } } diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/TableMetaData.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/TableMetaData.java index 792a0a850c6c1..7652306dbba37 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/TableMetaData.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/TableMetaData.java @@ -46,9 +46,9 @@ public final class TableMetaData { public TableMetaData(final String name, final Collection columns, final Collection indexes, final Collection constraints) { this.name = name; - type = TableType.TABLE; this.columns = columns; this.indexes = indexes; this.constraints = constraints; + type = TableType.TABLE; } }