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

jianglongtao 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 7fef251e1b8 password support special characters for DistSql (#24221)
7fef251e1b8 is described below

commit 7fef251e1b8f7f92a8677cb3bf91e2fb660c24b0
Author: skai <[email protected]>
AuthorDate: Fri Feb 24 00:18:25 2023 +0800

    password support special characters for DistSql (#24221)
    
    * password support special characters for DistSql
    
    * remove redundant comments
---
 .../core/kernel/KernelDistSQLStatementVisitor.java |  3 +-
 .../parser/rdl/RegisterStorageUnitTest.java        | 50 ++++++++++++++++++++++
 .../value/literal/impl/StringLiteralValue.java     | 11 +++++
 3 files changed, 63 insertions(+), 1 deletion(-)

diff --git 
a/distsql/parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
 
b/distsql/parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index 540d89d7a16..512081dfc64 100644
--- 
a/distsql/parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++ 
b/distsql/parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -102,6 +102,7 @@ import 
org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.StringLiteralValue;
 
 import java.util.Collection;
 import java.util.Properties;
@@ -139,7 +140,7 @@ public final class KernelDistSQLStatementVisitor extends 
KernelDistSQLStatementB
     }
     
     private String getPassword(final PasswordContext ctx) {
-        return getIdentifierValue(ctx);
+        return null == ctx ? null : 
StringLiteralValue.getStandardEscapesStringLiteralValue(ctx.getText()).getValue();
     }
     
     @Override
diff --git 
a/distsql/parser/src/test/java/org/apache/shardingsphere/distsql/parser/rdl/RegisterStorageUnitTest.java
 
b/distsql/parser/src/test/java/org/apache/shardingsphere/distsql/parser/rdl/RegisterStorageUnitTest.java
new file mode 100644
index 00000000000..ff09e10eb8c
--- /dev/null
+++ 
b/distsql/parser/src/test/java/org/apache/shardingsphere/distsql/parser/rdl/RegisterStorageUnitTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.distsql.parser.rdl;
+
+import 
org.apache.shardingsphere.distsql.parser.engine.api.DistSQLStatementParserEngine;
+import 
org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.RegisterStorageUnitStatement;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RegisterStorageUnitTest {
+    
+    @Test
+    public void assertRegisterStorageUnitSpecialCharacters() {
+        String sql = "REGISTER STORAGE UNIT test_db ("
+                + "URL='jdbc:mysql://127.0.0.1:3306/test_db',"
+                + "USER='root',PASSWORD='\\'\\\"r\\[oo]t');";
+        RegisterStorageUnitStatement distSQLStatement = 
getRegisterStorageUnitStatement(sql);
+        URLBasedDataSourceSegment sqlSegment = (URLBasedDataSourceSegment) 
distSQLStatement.getStorageUnits().iterator().next();
+        assertThat(sqlSegment.getUrl(), 
is("jdbc:mysql://127.0.0.1:3306/test_db"));
+        assertThat(sqlSegment.getName(), is("test_db"));
+        assertThat(sqlSegment.getUser(), is("root"));
+        assertThat(sqlSegment.getPassword(), is("'\"r\\[oo]t"));
+    }
+    
+    private RegisterStorageUnitStatement getRegisterStorageUnitStatement(final 
String sql) {
+        DistSQLStatementParserEngine distSQLStatementParserEngine = new 
DistSQLStatementParserEngine();
+        return (RegisterStorageUnitStatement) 
distSQLStatementParserEngine.parse(sql);
+    }
+}
diff --git 
a/sql-parser/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/StringLiteralValue.java
 
b/sql-parser/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/StringLiteralValue.java
index 8ca17fc44cc..b41f3b54078 100644
--- 
a/sql-parser/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/StringLiteralValue.java
+++ 
b/sql-parser/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/StringLiteralValue.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl;
 
 import lombok.Getter;
+import org.apache.groovy.parser.antlr4.util.StringUtils;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.LiteralValue;
 
 /**
@@ -31,4 +32,14 @@ public final class StringLiteralValue implements 
LiteralValue<String> {
     public StringLiteralValue(final String value) {
         this.value = value.substring(1, value.length() - 1);
     }
+    
+    /**
+     * Get special escape sequences string.
+     *
+     * @param value string text
+     * @return String literal value
+     */
+    public static StringLiteralValue 
getStandardEscapesStringLiteralValue(final String value) {
+        return new 
StringLiteralValue(StringUtils.replaceStandardEscapes(value));
+    }
 }

Reply via email to