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

zhaojinchao 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 af77556f930 Add ColumnReviser to help decorate schema metadata (#24006)
af77556f930 is described below

commit af77556f930b81195ce19d2368a65c869276b48d
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 5 07:17:18 2023 +0800

    Add ColumnReviser to help decorate schema metadata (#24006)
---
 .../metadata/EncryptSchemaMetaDataDecorator.java   | 48 ++++--------------
 .../metadata/reviser/EncryptColumnNameReviser.java | 59 ++++++++++++++++++++++
 .../decorator/reviser/ColumnNameReviser.java       | 40 +++++++++++++++
 .../decorator/reviser/ColumnReviseEngine.java      | 54 ++++++++++++++++++++
 .../schema/decorator/reviser/ColumnReviser.java    | 36 +++++++++++++
 5 files changed, 199 insertions(+), 38 deletions(-)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptSchemaMetaDataDecorator.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptSchemaMetaDataDecorator.java
index 2dfb48c062b..4b0d98aa59f 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptSchemaMetaDataDecorator.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/EncryptSchemaMetaDataDecorator.java
@@ -18,20 +18,23 @@
 package org.apache.shardingsphere.encrypt.metadata;
 
 import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
+import 
org.apache.shardingsphere.encrypt.metadata.reviser.EncryptColumnNameReviser;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.rule.EncryptTable;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.ColumnReviseEngine;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.ColumnReviser;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.spi.RuleBasedSchemaMetaDataDecorator;
-import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ColumnMetaData;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.SchemaMetaData;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.TableMetaData;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 /**
  * Schema meta data decorator for encrypt.
@@ -52,43 +55,12 @@ public final class EncryptSchemaMetaDataDecorator 
implements RuleBasedSchemaMeta
     }
     
     private TableMetaData decorate(final String tableName, final TableMetaData 
tableMetaData, final EncryptRule encryptRule) {
-        return encryptRule.findEncryptTable(tableName).map(optional -> new 
TableMetaData(tableName,
-                getEncryptColumnMetaDataList(optional, 
tableMetaData.getColumns()), tableMetaData.getIndexes(), 
tableMetaData.getConstrains())).orElse(tableMetaData);
-    }
-    
-    private Collection<ColumnMetaData> getEncryptColumnMetaDataList(final 
EncryptTable encryptTable, final Collection<ColumnMetaData> 
originalColumnMetaDataList) {
-        Collection<ColumnMetaData> result = new LinkedHashSet<>();
-        Collection<String> plainColumns = encryptTable.getPlainColumns();
-        Collection<String> assistedQueryColumns = 
encryptTable.getAssistedQueryColumns();
-        Collection<String> likeQueryColumns = 
encryptTable.getLikeQueryColumns();
-        for (ColumnMetaData each : originalColumnMetaDataList) {
-            String columnName = each.getName();
-            if (plainColumns.contains(columnName)) {
-                result.add(createMetaDataByPlainColumn(encryptTable, 
columnName, each));
-                continue;
-            }
-            if (encryptTable.isCipherColumn(columnName)) {
-                result.add(createMetaDataByCipherColumn(encryptTable, 
columnName, each));
-                continue;
-            }
-            if (!assistedQueryColumns.contains(columnName) && 
!likeQueryColumns.contains(columnName)) {
-                result.add(each);
-            }
+        Optional<EncryptTable> encryptTable = 
encryptRule.findEncryptTable(tableName);
+        if (!encryptTable.isPresent()) {
+            return tableMetaData;
         }
-        return result;
-    }
-    
-    private ColumnMetaData createMetaDataByPlainColumn(final EncryptTable 
encryptTable, final String columnName, final ColumnMetaData columnMetaData) {
-        return 
createColumnMetaData(encryptTable.getLogicColumnByPlainColumn(columnName), 
columnMetaData);
-    }
-    
-    private ColumnMetaData createMetaDataByCipherColumn(final EncryptTable 
encryptTable, final String columnName, final ColumnMetaData columnMetaData) {
-        return 
createColumnMetaData(encryptTable.getLogicColumnByCipherColumn(columnName), 
columnMetaData);
-    }
-    
-    private ColumnMetaData createColumnMetaData(final String columnName, final 
ColumnMetaData columnMetaData) {
-        return new ColumnMetaData(columnName, columnMetaData.getDataType(),
-                columnMetaData.isPrimaryKey(), columnMetaData.isGenerated(), 
columnMetaData.isCaseSensitive(), columnMetaData.isVisible(), 
columnMetaData.isUnsigned());
+        Collection<ColumnReviser> revisers = Collections.singleton(new 
EncryptColumnNameReviser(encryptTable.get()));
+        return new TableMetaData(tableName, new 
ColumnReviseEngine().revise(tableMetaData.getColumns(), revisers), 
tableMetaData.getIndexes(), tableMetaData.getConstrains());
     }
     
     @Override
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/EncryptColumnNameReviser.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/EncryptColumnNameReviser.java
new file mode 100644
index 00000000000..ef64c068e6e
--- /dev/null
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/EncryptColumnNameReviser.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.metadata.reviser;
+
+import org.apache.shardingsphere.encrypt.rule.EncryptTable;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.ColumnNameReviser;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Encrypt column name reviser.
+ */
+public final class EncryptColumnNameReviser extends ColumnNameReviser {
+    
+    private final EncryptTable encryptTable;
+    
+    private final Collection<String> plainColumns;
+    
+    private final Collection<String> assistedQueryColumns;
+    
+    private final Collection<String> likeQueryColumns;
+    
+    public EncryptColumnNameReviser(final EncryptTable encryptTable) {
+        this.encryptTable = encryptTable;
+        plainColumns = encryptTable.getPlainColumns();
+        assistedQueryColumns = encryptTable.getAssistedQueryColumns();
+        likeQueryColumns = encryptTable.getLikeQueryColumns();
+    }
+    
+    @Override
+    protected Optional<String> getColumnName(final String originalName) {
+        if (plainColumns.contains(originalName)) {
+            return 
Optional.of(encryptTable.getLogicColumnByPlainColumn(originalName));
+        }
+        if (encryptTable.isCipherColumn(originalName)) {
+            return 
Optional.of(encryptTable.getLogicColumnByCipherColumn(originalName));
+        }
+        if (!assistedQueryColumns.contains(originalName) && 
!likeQueryColumns.contains(originalName)) {
+            return Optional.of(originalName);
+        }
+        return Optional.empty();
+    }
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnNameReviser.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnNameReviser.java
new file mode 100644
index 00000000000..4dc6dc68ed7
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnNameReviser.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ColumnMetaData;
+
+import java.util.Optional;
+
+/**
+ * Column name reviser.
+ */
+public abstract class ColumnNameReviser implements ColumnReviser {
+    
+    @Override
+    public final Optional<ColumnMetaData> revise(final ColumnMetaData 
originalMetaData) {
+        return getColumnName(originalMetaData.getName()).map(optional -> 
createColumnMetaData(optional, originalMetaData));
+    }
+    
+    private ColumnMetaData createColumnMetaData(final String name, final 
ColumnMetaData metaData) {
+        return new ColumnMetaData(name, metaData.getDataType(),
+                metaData.isPrimaryKey(), metaData.isGenerated(), 
metaData.isCaseSensitive(), metaData.isVisible(), metaData.isUnsigned());
+    }
+    
+    protected abstract Optional<String> getColumnName(String originalName);
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviseEngine.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviseEngine.java
new file mode 100644
index 00000000000..8c033699969
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviseEngine.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ColumnMetaData;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Column revise engine.
+ */
+public final class ColumnReviseEngine {
+    
+    /**
+     * Revise column meta data.
+     * 
+     * @param originalMetaDataList original column meta data list
+     * @param revisers column revisers
+     * @return revised column meta data
+     */
+    public Collection<ColumnMetaData> revise(final Collection<ColumnMetaData> 
originalMetaDataList, final Collection<ColumnReviser> revisers) {
+        return originalMetaDataList.stream().map(each -> revise(each, 
revisers)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toCollection(LinkedHashSet::new));
+    }
+    
+    private Optional<ColumnMetaData> revise(final ColumnMetaData 
originalMetaData, final Collection<ColumnReviser> revisers) {
+        ColumnMetaData result = originalMetaData;
+        for (ColumnReviser each : revisers) {
+            Optional<ColumnMetaData> revisedMetaData = each.revise(result);
+            if (!revisedMetaData.isPresent()) {
+                return Optional.empty();
+            }
+            result = revisedMetaData.get();
+        }
+        return Optional.of(result);
+    }
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviser.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviser.java
new file mode 100644
index 00000000000..c9cf34c508a
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/reviser/ColumnReviser.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ColumnMetaData;
+
+import java.util.Optional;
+
+/**
+ * Column reviser.
+ */
+public interface ColumnReviser {
+    
+    /**
+     * Revise column meta data.
+     * 
+     * @param originalMetaData original column meta data
+     * @return revised column meta data
+     */
+    Optional<ColumnMetaData> revise(ColumnMetaData originalMetaData);
+}

Reply via email to