You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/06/24 11:33:19 UTC

[shardingsphere] branch master updated: Do SQL translation only when storage type is present (#18572)

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

zhangliang 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 69ce62f3250 Do SQL translation only when storage type is present (#18572)
69ce62f3250 is described below

commit 69ce62f32508397a607ee5808f9328a5cd332d26
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Fri Jun 24 19:33:13 2022 +0800

    Do SQL translation only when storage type is present (#18572)
    
    * Do SQL translation only when storage type is present
    
    Signed-off-by: 吴伟杰 <wu...@apache.org>
    
    * Revise ConvertToUpperCaseSQLTranslator
    
    Signed-off-by: 吴伟杰 <wu...@apache.org>
---
 .../sqltranslator/rule/SQLTranslatorRule.java      |  2 +-
 .../sqltranslator/rule/SQLTranslatorRuleTest.java  | 77 ++++++++++++++++++++++
 .../rule/fixture/AlwaysFailedSQLTranslator.java    | 36 ++++++++++
 .../fixture/ConvertToUpperCaseSQLTranslator.java   | 37 +++++++++++
 ....shardingsphere.sqltranslator.spi.SQLTranslator | 19 ++++++
 5 files changed, 170 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
index 35fd11874a8..70f3e58445e 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
@@ -55,7 +55,7 @@ public final class SQLTranslatorRule implements GlobalRule {
      * @return translated SQL
      */
     public String translate(final String sql, final SQLStatement sqlStatement, final DatabaseType protocolType, final DatabaseType storageType) {
-        if (protocolType.equals(storageType)) {
+        if (protocolType.equals(storageType) || null == storageType) {
             return sql;
         }
         try {
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
new file mode 100644
index 00000000000..364ac7bc65c
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.sqltranslator.rule;
+
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Locale;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class SQLTranslatorRuleTest {
+    
+    @Test
+    public void assertTranslateWhenProtocolSameAsStorage() {
+        String expected = "select 1";
+        PostgreSQLDatabaseType databaseType = new PostgreSQLDatabaseType();
+        String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(expected, null, databaseType, databaseType);
+        assertThat(actual, is(expected));
+    }
+    
+    @Test
+    public void assertTranslateWhenNoStorage() {
+        String expected = "select 1";
+        String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(expected, null, new PostgreSQLDatabaseType(), null);
+        assertThat(actual, is(expected));
+    }
+    
+    @Test
+    public void assertTranslateWithProtocolDifferentWithStorage() {
+        String input = "select 1";
+        String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(input, null, new PostgreSQLDatabaseType(), new MySQLDatabaseType());
+        assertThat(actual, is(input.toUpperCase(Locale.ROOT)));
+    }
+    
+    @Test
+    public void assertUseOriginalSQLWhenTranslatingFailed() {
+        String expected = "select 1";
+        String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", true)).translate(expected, null, new PostgreSQLDatabaseType(), new MySQLDatabaseType());
+        assertThat(actual, is(expected));
+    }
+    
+    @Test(expected = ShardingSphereException.class)
+    public void assertNotUseOriginalSQLWhenTranslatingFailed() {
+        new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", false)).translate("", null, new PostgreSQLDatabaseType(), new MySQLDatabaseType());
+    }
+    
+    @Test
+    public void assertGetConfiguration() {
+        SQLTranslatorRuleConfiguration expected = new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false);
+        assertThat(new SQLTranslatorRule(expected).getConfiguration(), is(expected));
+    }
+    
+    @Test
+    public void assertGetType() {
+        assertThat(new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).getType(), is(SQLTranslatorRule.class.getSimpleName()));
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java
new file mode 100644
index 00000000000..ed5e9867fd6
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.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.sqltranslator.rule.fixture;
+
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
+import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
+
+public final class AlwaysFailedSQLTranslator implements SQLTranslator {
+    
+    @Override
+    public String translate(final String sql, final SQLStatement sqlStatement, final DatabaseType protocolType, final DatabaseType storageType) throws SQLTranslationException {
+        throw new SQLTranslationException("Always failed");
+    }
+    
+    @Override
+    public String getType() {
+        return "ALWAYS_FAILED";
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/ConvertToUpperCaseSQLTranslator.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/ConvertToUpperCaseSQLTranslator.java
new file mode 100644
index 00000000000..eafe17e92e0
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/ConvertToUpperCaseSQLTranslator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.sqltranslator.rule.fixture;
+
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
+
+import java.util.Locale;
+
+public final class ConvertToUpperCaseSQLTranslator implements SQLTranslator {
+    
+    @Override
+    public String translate(final String sql, final SQLStatement sqlStatement, final DatabaseType protocolType, final DatabaseType storageType) {
+        return sql.toUpperCase(Locale.ROOT);
+    }
+    
+    @Override
+    public String getType() {
+        return "CONVERT_TO_UPPER_CASE";
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
new file mode 100644
index 00000000000..a9b1a113bd4
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sqltranslator.rule.fixture.AlwaysFailedSQLTranslator
+org.apache.shardingsphere.sqltranslator.rule.fixture.ConvertToUpperCaseSQLTranslator