You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/02/16 03:08:44 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #15431: refactor encrypt config extract data type

strongduanmu commented on a change in pull request #15431:
URL: https://github.com/apache/shardingsphere/pull/15431#discussion_r807476593



##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptDataTypeExtractor.java
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.spi;
+
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeAwareSPI;
+import org.apache.shardingsphere.spi.singleton.SingletonSPI;
+
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Encrypt data type extractor for SPI.
+ */
+public interface EncryptDataTypeExtractor extends DatabaseTypeAwareSPI, SingletonSPI {

Review comment:
       @tuichenchuxin Maybe `EncryptDataTypeConverter` may be better.

##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/extractor/MySQLEncryptDataTypeExtractor.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.rule.extractor;
+
+import org.apache.shardingsphere.encrypt.spi.EncryptDataTypeExtractor;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * MySQL encrypt data type extractor.
+ */
+public final class MySQLEncryptDataTypeExtractor implements EncryptDataTypeExtractor {
+    
+    private final Map<String, String> compatibleDbDataTypes = initCompatibleDataTypes();
+    
+    private Map<String, String> initCompatibleDataTypes() {
+        Map<String, String> result = new LinkedHashMap<>();
+        result.put("bool", "tinyint");
+        result.put("boolean", "tinyint");
+        result.put("character varying", "varchar");
+        result.put("fixed", "decimal");
+        result.put("float4", "float");
+        result.put("float8", "double");
+        result.put("int1", "tinyint");
+        result.put("int2", "smallint");
+        result.put("int3", "mediumint");
+        result.put("int4", "int");
+        result.put("int8", "bigint");
+        result.put("long varbinary", "mediumblob");
+        result.put("long varchar", "mediumtext");
+        result.put("long char varying", "mediumtext");
+        result.put("long", "mediumtext");
+        result.put("middleint", "mediumint");
+        result.put("numeric", "decimal");
+        result.put("nchar", "char");
+        result.put("national character", "char");
+        result.put("national char", "char");
+        result.put("national varchar", "varchar");
+        result.put("nvarchar", "varchar");
+        result.put("nchar varchar", "varchar");
+        result.put("national character varying", "varchar");
+        result.put("national char varying", "varchar");
+        result.put("nchar varying", "varchar");
+        result.put("serial", "bigint");
+        result.put("geometrycollection", "binary");
+        result.put("year", "date");
+        result.put("geometry", "binary");
+        result.put("json", "text");
+        result.put("point", "binary");
+        result.put("multipoint", "binary");
+        result.put("linestring", "binary");
+        result.put("multilinestring", "binary");
+        result.put("polygon", "binary");
+        result.put("multipolygon", "binary");
+        return result;
+    }
+    
+    @Override
+    public Optional<Integer> getDataType(final String typeName, final Map<String, Integer> dataTypes) {
+        return Optional.ofNullable(dataTypes.get(getRealDataTypeName(typeName)));
+    }
+    
+    private String getRealDataTypeName(final String fullDataTypeDefinition) {
+        String dataType = extractConfigDataType(fullDataTypeDefinition);
+        return compatibleDbDataTypes.getOrDefault(dataType, dataType);
+    }
+    
+    private String extractConfigDataType(final String fullDataTypeDefinition) {
+        String dataType = fullDataTypeDefinition.trim().toLowerCase();
+        Optional<String> multiWordDataType = findMultiWordDataType(dataType);
+        if (multiWordDataType.isPresent()) {
+            return multiWordDataType.get();
+        }
+        if (dataType.contains("(")) {
+            return dataType.substring(0, dataType.indexOf("("));
+        } 
+        if (dataType.contains(" ")) {
+            return dataType.substring(0, dataType.indexOf(" "));
+        } 
+        return dataType;

Review comment:
       Please rename it to result.

##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
##########
@@ -67,19 +69,27 @@
     public EncryptRule(final EncryptRuleConfiguration config, final Map<String, DataSource> dataSourceMap) {
         Preconditions.checkArgument(isValidRuleConfiguration(config), "Invalid encrypt column configurations in EncryptTableRuleConfigurations.");
         config.getEncryptors().forEach((key, value) -> encryptors.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, EncryptAlgorithm.class)));
-        Map<String, Integer> dataTypes = containsConfigDataTypeColumn(config.getTables()) ? getDataTypes(dataSourceMap) : Collections.emptyMap();
-        config.getTables().forEach(each -> tables.put(each.getName(), new EncryptTable(each, dataTypes)));
+        initEncryptTables(config.getTables(), dataSourceMap);

Review comment:
       createEncryptTables may be better.

##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/extractor/MySQLEncryptDataTypeExtractor.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.rule.extractor;
+
+import org.apache.shardingsphere.encrypt.spi.EncryptDataTypeExtractor;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * MySQL encrypt data type extractor.
+ */
+public final class MySQLEncryptDataTypeExtractor implements EncryptDataTypeExtractor {
+    
+    private final Map<String, String> compatibleDbDataTypes = initCompatibleDataTypes();

Review comment:
       Please add static modifier for compatibleDbDataTypes, and change this field for more meaningful name.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org