You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/12/30 07:08:57 UTC

[shardingsphere] branch master updated: Add alias to SQL hint keys. (#23189)

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

duanzhengqiang 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 e7336e1dd73 Add alias to SQL hint keys. (#23189)
e7336e1dd73 is described below

commit e7336e1dd7362333e274044aecc14dbf23fa92fa
Author: Raigor <ra...@gmail.com>
AuthorDate: Fri Dec 30 15:08:50 2022 +0800

    Add alias to SQL hint keys. (#23189)
---
 .../infra/hint/SQLHintPropertiesKey.java           | 18 +++---
 .../infra/hint/SQLHintTokenEnum.java               | 43 +++++++++++++
 .../shardingsphere/infra/hint/SQLHintUtils.java    | 72 ++++++++++++++--------
 .../infra/hint/SQLHintExtractorTest.java           |  9 +++
 .../infra/hint/SQLHintUtilsTest.java               | 14 +++++
 5 files changed, 123 insertions(+), 33 deletions(-)

diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java
index 64753a87bc4..ad2e3327c77 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java
@@ -31,45 +31,47 @@ public enum SQLHintPropertiesKey implements TypedPropertyKey {
     /**
      * Hint data source name.
      */
-    DATASOURCE_NAME_KEY("DATA_SOURCE_NAME", "", String.class),
+    DATASOURCE_NAME_KEY("DATA_SOURCE_NAME", "dataSourceName", "", String.class),
     
     /**
      * Whether hint route write data source or not.
      */
-    WRITE_ROUTE_ONLY_KEY("WRITE_ROUTE_ONLY", String.valueOf(Boolean.FALSE), boolean.class),
+    WRITE_ROUTE_ONLY_KEY("WRITE_ROUTE_ONLY", "writeRouteOnly", String.valueOf(Boolean.FALSE), boolean.class),
     
     /**
      * Whether to use traffic or not.
      */
-    USE_TRAFFIC_KEY("USE_TRAFFIC", String.valueOf(Boolean.FALSE), boolean.class),
+    USE_TRAFFIC_KEY("USE_TRAFFIC", "useTraffic", String.valueOf(Boolean.FALSE), boolean.class),
     
     /**
      * Whether hint skip encrypt rewrite or not.
      */
-    SKIP_ENCRYPT_REWRITE_KEY("SKIP_ENCRYPT_REWRITE", String.valueOf(Boolean.FALSE), boolean.class),
+    SKIP_ENCRYPT_REWRITE_KEY("SKIP_ENCRYPT_REWRITE", "skipEncryptRewrite", String.valueOf(Boolean.FALSE), boolean.class),
     
     /**
      * Hint disable audit names.
      */
-    DISABLE_AUDIT_NAMES_KEY("DISABLE_AUDIT_NAMES", "", String.class),
+    DISABLE_AUDIT_NAMES_KEY("DISABLE_AUDIT_NAMES", "disableAuditNames", "", String.class),
     
     /**
      * Hint sharding database value.
      */
-    SHARDING_DATABASE_VALUE_KEY("SHARDING_DATABASE_VALUE", "", Comparable.class),
+    SHARDING_DATABASE_VALUE_KEY("SHARDING_DATABASE_VALUE", "shardingDatabaseValue", "", Comparable.class),
     
     /**
      * Hint sharding table value.
      */
-    SHARDING_TABLE_VALUE_KEY("SHARDING_TABLE_VALUE", "", Comparable.class),
+    SHARDING_TABLE_VALUE_KEY("SHARDING_TABLE_VALUE", "shardingTableValue", "", Comparable.class),
     
     /**
      * Whether to use shadow or not.
      */
-    SHADOW_KEY("SHADOW", String.valueOf(Boolean.FALSE), boolean.class);
+    SHADOW_KEY("SHADOW", "shadow", String.valueOf(Boolean.FALSE), boolean.class);
     
     private final String key;
     
+    private final String alias;
+    
     private final String defaultValue;
     
     private final Class<?> type;
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintTokenEnum.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintTokenEnum.java
new file mode 100644
index 00000000000..258724d3f88
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintTokenEnum.java
@@ -0,0 +1,43 @@
+/*
+ * 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.hint;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+/**
+ * SQL hint token enum.
+ */
+@RequiredArgsConstructor
+@Getter
+public enum SQLHintTokenEnum {
+    
+    /**
+     * SQL start hint token.
+     */
+    SQL_START_HINT_TOKEN("/* SHARDINGSPHERE_HINT:", "/* ShardingSphere hint:"),
+    
+    /**
+     * SQL hint token.
+     */
+    SQL_HINT_TOKEN("shardingsphere_hint:", "shardingsphere hint:");
+    
+    private final String key;
+    
+    private final String alias;
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
index a1f38e70bf8..7d46d2c9d82 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
@@ -38,10 +38,6 @@ public final class SQLHintUtils {
     
     private static final String SQL_COMMENT_SUFFIX = "*/";
     
-    private static final String SQL_START_HINT_TOKEN = "/* SHARDINGSPHERE_HINT:";
-    
-    private static final String SQL_HINT_TOKEN = "shardingsphere_hint:";
-    
     private static final String SQL_HINT_SPLIT = ",";
     
     private static final String SQL_HINT_VALUE_SPLIT = "=";
@@ -50,6 +46,16 @@ public final class SQLHintUtils {
     
     private static final int SQL_HINT_VALUE_SIZE = 2;
     
+    /**
+     * Whether the SQL statement starts with the hint prefix.
+     *
+     * @param sql SQL statement
+     * @return whether starts with hint prefix
+     */
+    public static boolean startWithHint(final String sql) {
+        return sql.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getKey()) || sql.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getAlias());
+    }
+    
     /**
      * Get SQL hint props.
      *
@@ -58,17 +64,20 @@ public final class SQLHintUtils {
      */
     public static Properties getSQLHintProps(final String comment) {
         Properties result = new Properties();
-        int startIndex = comment.toLowerCase().indexOf(SQL_HINT_TOKEN);
+        String lowerCaseComment = comment.toLowerCase();
+        int startIndex = lowerCaseComment.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getAlias().toLowerCase())
+                ? lowerCaseComment.indexOf(SQLHintTokenEnum.SQL_HINT_TOKEN.getAlias())
+                : lowerCaseComment.indexOf(SQLHintTokenEnum.SQL_HINT_TOKEN.getKey());
         if (startIndex < 0) {
             return result;
         }
-        startIndex = startIndex + SQL_HINT_TOKEN.length();
+        startIndex = startIndex + SQLHintTokenEnum.SQL_HINT_TOKEN.getKey().length();
         int endIndex = comment.endsWith(SQL_COMMENT_SUFFIX) ? comment.indexOf(SQL_COMMENT_SUFFIX) : comment.length();
         Collection<String> sqlHints = Splitter.on(SQL_HINT_SPLIT).trimResults().splitToList(comment.substring(startIndex, endIndex).trim());
         for (String each : sqlHints) {
             List<String> hintValues = Splitter.on(SQL_HINT_VALUE_SPLIT).trimResults().splitToList(each);
             if (SQL_HINT_VALUE_SIZE == hintValues.size()) {
-                result.put(hintValues.get(0).toUpperCase(), convert(hintValues.get(1)));
+                result.put(hintValues.get(0), convert(hintValues.get(1)));
             }
         }
         return result;
@@ -100,41 +109,54 @@ public final class SQLHintUtils {
      */
     public static HintValueContext extractHint(final String sql) {
         HintValueContext result = new HintValueContext();
-        if (null == sql || !sql.startsWith(SQL_START_HINT_TOKEN)) {
+        if (null == sql || !startWithHint(sql)) {
             return result;
         }
         String hintText = sql.substring(0, sql.indexOf(SQL_COMMENT_SUFFIX) + 2);
         Properties hintProperties = SQLHintUtils.getSQLHintProps(hintText);
-        if (hintProperties.containsKey(SQLHintPropertiesKey.DATASOURCE_NAME_KEY.getKey())) {
-            result.setDataSourceName(hintProperties.getProperty(SQLHintPropertiesKey.DATASOURCE_NAME_KEY.getKey()));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.DATASOURCE_NAME_KEY)) {
+            result.setDataSourceName(getProperty(hintProperties, SQLHintPropertiesKey.DATASOURCE_NAME_KEY));
         }
-        if (hintProperties.containsKey(SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY.getKey())) {
-            result.setWriteRouteOnly(Boolean.parseBoolean(hintProperties.getProperty(SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY.getKey())));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY)) {
+            result.setWriteRouteOnly(Boolean.parseBoolean(getProperty(hintProperties, SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY)));
         }
-        if (hintProperties.containsKey(SQLHintPropertiesKey.USE_TRAFFIC_KEY.getKey())) {
-            result.setUseTraffic(Boolean.parseBoolean(hintProperties.getProperty(SQLHintPropertiesKey.USE_TRAFFIC_KEY.getKey())));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.USE_TRAFFIC_KEY)) {
+            result.setUseTraffic(Boolean.parseBoolean(getProperty(hintProperties, SQLHintPropertiesKey.USE_TRAFFIC_KEY)));
         }
-        if (hintProperties.containsKey(SQLHintPropertiesKey.SKIP_ENCRYPT_REWRITE_KEY.getKey())) {
-            result.setSkipEncryptRewrite(Boolean.parseBoolean(hintProperties.getProperty(SQLHintPropertiesKey.SKIP_ENCRYPT_REWRITE_KEY.getKey())));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.SKIP_ENCRYPT_REWRITE_KEY)) {
+            result.setSkipEncryptRewrite(Boolean.parseBoolean(getProperty(hintProperties, SQLHintPropertiesKey.SKIP_ENCRYPT_REWRITE_KEY)));
         }
-        if (hintProperties.containsKey(SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY.getKey())) {
-            result.setDisableAuditNames(hintProperties.getProperty(SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY.getKey()));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY)) {
+            result.setDisableAuditNames(getProperty(hintProperties, SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY));
         }
-        if (hintProperties.containsKey(SQLHintPropertiesKey.SHADOW_KEY.getKey())) {
-            result.setShadow(Boolean.parseBoolean(hintProperties.getProperty(SQLHintPropertiesKey.SHADOW_KEY.getKey())));
+        if (containsPropertyKey(hintProperties, SQLHintPropertiesKey.SHADOW_KEY)) {
+            result.setShadow(Boolean.parseBoolean(getProperty(hintProperties, SQLHintPropertiesKey.SHADOW_KEY)));
         }
         for (Entry<Object, Object> entry : hintProperties.entrySet()) {
             Comparable<?> value = entry.getValue() instanceof Comparable ? (Comparable<?>) entry.getValue() : Objects.toString(entry.getValue());
-            if (Objects.toString(entry.getKey()).contains(SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY.getKey())) {
-                result.getShardingDatabaseValues().put(Objects.toString(entry.getKey()), value);
+            if (containsPropertyKey(Objects.toString(entry.getKey()), SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY)) {
+                result.getShardingDatabaseValues().put(Objects.toString(entry.getKey()).toUpperCase(), value);
             }
-            if (Objects.toString(entry.getKey()).contains(SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY.getKey())) {
-                result.getShardingTableValues().put(Objects.toString(entry.getKey()), value);
+            if (containsPropertyKey(Objects.toString(entry.getKey()), SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY)) {
+                result.getShardingTableValues().put(Objects.toString(entry.getKey()).toUpperCase(), value);
             }
         }
         return result;
     }
     
+    private static boolean containsPropertyKey(final Properties hintProperties, final SQLHintPropertiesKey sqlHintPropertiesKey) {
+        return hintProperties.containsKey(sqlHintPropertiesKey.getKey()) || hintProperties.containsKey(sqlHintPropertiesKey.getAlias());
+    }
+    
+    private static boolean containsPropertyKey(final String hintPropertyKey, final SQLHintPropertiesKey sqlHintPropertiesKey) {
+        return hintPropertyKey.contains(sqlHintPropertiesKey.getKey()) || hintPropertyKey.contains(sqlHintPropertiesKey.getAlias());
+    }
+    
+    private static String getProperty(final Properties hintProperties, final SQLHintPropertiesKey sqlHintPropertiesKey) {
+        String result = hintProperties.getProperty(sqlHintPropertiesKey.getKey());
+        return null == result ? hintProperties.getProperty(sqlHintPropertiesKey.getAlias()) : result;
+    }
+    
     /**
      * Remove SQL hint.
      *
@@ -142,7 +164,7 @@ public final class SQLHintUtils {
      * @return SQL after remove hint
      */
     public static String removeHint(final String sql) {
-        if (sql.startsWith(SQL_START_HINT_TOKEN)) {
+        if (startWithHint(sql)) {
             return sql.substring(sql.indexOf(SQL_COMMENT_SUFFIX) + 2);
         }
         return sql;
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
index c9c02806d86..5bf9eb201a3 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
@@ -122,6 +122,15 @@ public final class SQLHintExtractorTest {
         assertThat(dataSourceName.get(), is("ds_1"));
     }
     
+    @Test
+    public void assertFindHintDataSourceNameAliasExist() {
+        AbstractSQLStatement statement = mock(AbstractSQLStatement.class);
+        when(statement.getCommentSegments()).thenReturn(Collections.singletonList(new CommentSegment("/* ShardingSphere hint: dataSourceName=ds_1 */", 0, 0)));
+        Optional<String> dataSourceName = new SQLHintExtractor(statement).findHintDataSourceName();
+        assertTrue(dataSourceName.isPresent());
+        assertThat(dataSourceName.get(), is("ds_1"));
+    }
+    
     @Test
     public void assertFindHintDataSourceNameNotExist() {
         AbstractSQLStatement statement = mock(AbstractSQLStatement.class);
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
index a58d093ce33..c4c98a43e5a 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
@@ -63,4 +63,18 @@ public final class SQLHintUtilsTest {
         assertThat(actual.size(), is(2));
         assertTrue(actual.containsAll(Arrays.asList("sharding_audit1", "sharding_audit2")));
     }
+    
+    @Test
+    public void assertGetSQLHintPropsWithDataSourceName() {
+        Properties actual = SQLHintUtils.getSQLHintProps("/* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=ds_0 */");
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get("DATA_SOURCE_NAME"), is("ds_0"));
+    }
+    
+    @Test
+    public void assertGetSQLHintPropsWithDataSourceNameAlias() {
+        Properties actual = SQLHintUtils.getSQLHintProps("/* ShardingSphere hint: dataSourceName=ds_0 */");
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get("dataSourceName"), is("ds_0"));
+    }
 }