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

chengzhang 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 ff93c987d76 Refactor SQLHintUtils (#31837)
ff93c987d76 is described below

commit ff93c987d76ae0dadd44d90fa91bbefd2873c316
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jun 25 13:15:39 2024 +0800

    Refactor SQLHintUtils (#31837)
    
    * Fix javadoc on SQLHintTokenType
    
    * Refactor SQLHintUtils
---
 .../shardingsphere/infra/hint/SQLHintUtils.java    | 40 +++++++++++-----------
 1 file changed, 20 insertions(+), 20 deletions(-)

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 626f2112a12..3962cde25a2 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
@@ -53,7 +53,7 @@ public final class SQLHintUtils {
      * @return hint value context
      */
     public static HintValueContext extractHint(final String sql) {
-        if (!startWithHint(sql, SQLHintTokenType.SQL_START_HINT_TOKEN)) {
+        if (!startWithHint(sql)) {
             return new HintValueContext();
         }
         HintValueContext result = new HintValueContext();
@@ -87,19 +87,13 @@ public final class SQLHintUtils {
         return result;
     }
     
-    /**
-     * Remove SQL hint.
-     *
-     * @param sql SQL
-     * @return SQL after remove hint
-     */
-    public static String removeHint(final String sql) {
-        return startWithHint(sql, SQLHintTokenType.SQL_START_HINT_TOKEN) ? 
sql.substring(sql.indexOf(SQL_COMMENT_SUFFIX) + 2).trim() : sql;
+    private static boolean startWithHint(final String sql) {
+        return null != sql && 
(sql.startsWith(SQLHintTokenType.SQL_START_HINT_TOKEN.getKey()) || 
sql.startsWith(SQLHintTokenType.SQL_START_HINT_TOKEN.getAlias()));
     }
     
     private static Properties getSQLHintProps(final String comment) {
         Properties result = new Properties();
-        int startIndex = getStartIndex(comment, 
SQLHintTokenType.SQL_START_HINT_TOKEN, SQLHintTokenType.SQL_HINT_TOKEN);
+        int startIndex = getStartIndex(comment);
         if (startIndex < 0) {
             return result;
         }
@@ -114,13 +108,13 @@ public final class SQLHintUtils {
         return result;
     }
     
-    private static int getStartIndex(final String comment, final 
SQLHintTokenType sqlStartHintToken, final SQLHintTokenType sqlHintToken) {
+    private static int getStartIndex(final String comment) {
         String lowerCaseComment = comment.toLowerCase();
-        int result = 
lowerCaseComment.startsWith(sqlStartHintToken.getAlias().toLowerCase())
-                ? lowerCaseComment.indexOf(sqlHintToken.getAlias())
-                : lowerCaseComment.indexOf(sqlHintToken.getKey());
+        int result = 
lowerCaseComment.startsWith(SQLHintTokenType.SQL_START_HINT_TOKEN.getAlias().toLowerCase())
+                ? 
lowerCaseComment.indexOf(SQLHintTokenType.SQL_HINT_TOKEN.getAlias())
+                : 
lowerCaseComment.indexOf(SQLHintTokenType.SQL_HINT_TOKEN.getKey());
         if (result >= 0) {
-            return result + sqlHintToken.getKey().length();
+            return result + SQLHintTokenType.SQL_HINT_TOKEN.getKey().length();
         }
         return result;
     }
@@ -133,10 +127,6 @@ public final class SQLHintUtils {
         }
     }
     
-    private static boolean startWithHint(final String sql, final 
SQLHintTokenType sqlStartHintToken) {
-        return null != sql && (sql.startsWith(sqlStartHintToken.getKey()) || 
sql.startsWith(sqlStartHintToken.getAlias()));
-    }
-    
     private static boolean containsPropertyKey(final Properties hintProps, 
final SQLHintPropertiesKey sqlHintPropsKey) {
         return hintProps.containsKey(sqlHintPropsKey.getKey()) || 
hintProps.containsKey(sqlHintPropsKey.getAlias());
     }
@@ -151,6 +141,16 @@ public final class SQLHintUtils {
     }
     
     private static Collection<String> getSplitterSQLHintValue(final String 
property) {
-        return property.isEmpty() ? Collections.emptySet() : new 
HashSet<>(Splitter.on(SQLHintUtils.SQL_HINT_VALUE_COLLECTION_SPLIT).omitEmptyStrings().trimResults().splitToList(property));
+        return property.isEmpty() ? Collections.emptySet() : new 
HashSet<>(Splitter.on(SQL_HINT_VALUE_COLLECTION_SPLIT).omitEmptyStrings().trimResults().splitToList(property));
+    }
+    
+    /**
+     * Remove SQL hint.
+     *
+     * @param sql SQL
+     * @return SQL after remove hint
+     */
+    public static String removeHint(final String sql) {
+        return startWithHint(sql) ? 
sql.substring(sql.indexOf(SQL_COMMENT_SUFFIX) + 2).trim() : sql;
     }
 }

Reply via email to