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 2021/12/03 03:24:26 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #13905: add sql hint for write route only.

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



##########
File path: shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/SQLHintExtractor.java
##########
@@ -17,54 +17,87 @@
 
 package org.apache.shardingsphere.sql.parser.sql.common.extractor;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Optional;
 
 /**
  * SQL hint extractor.
  */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class SQLHintExtractor {
     
     private static final String SQL_COMMENT_SUFFIX = "*/";
     
-    private static final String SQL_HINT_TOKEN = "sql hint:";
+    private static final String SQL_HINT_TOKEN = "shardingsphere hint:";
     
     private static final String SQL_HINT_SPLIT = "=";
     
     private static final String SQL_HINT_DATASOURCE_NAME_KEY = "datasourcename";
     
+    private static final String SQL_HINT_WRITE_ROUTE_ONLY_KEY = "writerouteonly";
+    
+    private final Map<String, String> sqlHintMap;
+    
+    public SQLHintExtractor(final SQLStatement sqlStatement) {
+        if (sqlStatement instanceof AbstractSQLStatement) {
+            sqlHintMap = extract((AbstractSQLStatement) sqlStatement);
+        } else {
+            sqlHintMap = Collections.emptyMap();
+        }
+    }
+    
     /**
-     * Find hint data source name.
+     * Extract from statement.
      *
      * @param statement statement
-     * @return data source name
+     * @return sql hint map
      */
-    public static Optional<String> findHintDataSourceName(final AbstractSQLStatement statement) {
+    public static Map<String, String> extract(final AbstractSQLStatement statement) {
         for (CommentSegment each : statement.getCommentSegments()) {
-            Optional<String> result = findDataSourceNameFromComment(each.getText());
-            if (result.isPresent()) {
-                return result;
+            Optional<Map<String, String>> map = extractFromComment(each.getText());
+            if (map.isPresent()) {
+                return map.get();
             }
         }
-        return Optional.empty();
+        return Collections.emptyMap();
     }
     
-    private static Optional<String> findDataSourceNameFromComment(final String comment) {
+    private static Optional<Map<String, String>> extractFromComment(final String comment) {

Review comment:
       @tuichenchuxin Can we remove Optional here? 




-- 
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