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/02/25 05:34:21 UTC

[shardingsphere] branch master updated: Fix set character sql parser (#15619)

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 635ba0c  Fix set character sql parser (#15619)
635ba0c is described below

commit 635ba0c1c6e252d79e6678576aaecda72254d178
Author: gin <ja...@163.com>
AuthorDate: Fri Feb 25 13:33:23 2022 +0800

    Fix set character sql parser (#15619)
    
    * Fix set character sql parser
    
    * Add test for set character sql parser
    
    * Remove final
---
 .../db/protocol/mysql/constant/MySQLCharacterSet.java         |  6 +++++-
 .../visitor/statement/impl/MySQLDALStatementSQLVisitor.java   |  4 +++-
 .../statement/impl/PostgreSQLDALStatementSQLVisitor.java      | 11 ++++++++++-
 .../src/main/resources/case/dal/set.xml                       | 10 ++++++++++
 .../src/main/resources/sql/supported/dal/set.xml              |  2 ++
 5 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
index b7f6525..d0dd3e2 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
@@ -370,7 +370,11 @@ public enum MySQLCharacterSet {
      * @return MySQL character set
      */
     public static Optional<Charset> findByValue(final String charsetName) {
-        MySQLCharacterSet result = CHARACTER_NAME_SET_MAP.get(charsetName.toLowerCase(Locale.ROOT));
+        String key = charsetName.toLowerCase(Locale.ROOT);
+        if ("default".equals(key)) {
+            return Optional.of(MySQLServerInfo.DEFAULT_CHARSET.getCharset());
+        }
+        MySQLCharacterSet result = CHARACTER_NAME_SET_MAP.get(key);
         return null == result || null == result.getCharset() ? Optional.empty() : Optional.of(result.getCharset());
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLDALStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLDALStatementSQLVisitor.java
index 1a016b1..4c5de68 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLDALStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLDALStatementSQLVisitor.java
@@ -974,13 +974,15 @@ public final class MySQLDALStatementSQLVisitor extends MySQLStatementSQLVisitor
     
     @Override
     public ASTNode visitSetCharacter(final SetCharacterContext ctx) {
-        MySQLSetStatement result = new MySQLSetStatement();
         VariableAssignSegment characterSet = new VariableAssignSegment();
         VariableSegment variable = new VariableSegment();
         String variableName = (null != ctx.CHARSET()) ? ctx.CHARSET().getText() : "charset";
         variable.setVariable(variableName);
+        characterSet.setVariable(variable);
         String assignValue = (null != ctx.DEFAULT()) ? ctx.DEFAULT().getText() : ctx.charsetName().getText();
         characterSet.setAssignValue(assignValue);
+        MySQLSetStatement result = new MySQLSetStatement();
+        result.getVariableAssigns().add(characterSet);
         return result;
     }
     
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDALStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDALStatementSQLVisitor.java
index a9fe861..db645cb 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDALStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDALStatementSQLVisitor.java
@@ -77,8 +77,17 @@ public final class PostgreSQLDALStatementSQLVisitor extends PostgreSQLStatementS
                 variableAssignSegment.getVariable().setScope(ctx.runtimeScope().getText());
             }
             variableAssigns.add(variableAssignSegment);
-            result.getVariableAssigns().addAll(variableAssigns);
         }
+        if (null != ctx.encoding()) {
+            VariableAssignSegment variableAssignSegment = new VariableAssignSegment();
+            VariableSegment variableSegment = new VariableSegment();
+            variableSegment.setVariable("charset");
+            variableAssignSegment.setVariable(variableSegment);
+            String value = ctx.encoding().getText();
+            variableAssignSegment.setAssignValue(value);
+            variableAssigns.add(variableAssignSegment);
+        }
+        result.getVariableAssigns().addAll(variableAssigns);
         return result;
     }
     
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dal/set.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dal/set.xml
index a953f2f..8072b00 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dal/set.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dal/set.xml
@@ -83,4 +83,14 @@
     <set-resource-group sql-case-id="set_resource_group">
         <group name="rg" />
     </set-resource-group>
+    <set-parameter sql-case-id="set_charset" >
+        <parameter-assign value="'UTF8'" >
+            <parameter name="charset" />
+        </parameter-assign>
+    </set-parameter>
+    <set-parameter sql-case-id="set_client_encoding" >
+        <parameter-assign value="'UTF8'" >
+            <parameter name="CLIENT_ENCODING" />
+        </parameter-assign>
+    </set-parameter>
 </sql-parser-test-cases>
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dal/set.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dal/set.xml
index 7d8940f..47693d4 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dal/set.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dal/set.xml
@@ -37,4 +37,6 @@
     <sql-case id="set_parameter_equal_number_with_signal" value="SET extra_float_digits = -10.5" db-types="PostgreSQL,openGauss" />
     <sql-case id="set_names" value="SET NAMES 'utf8' COLLATE 'utf8_general_ci'" db-types="MySQL" />
     <sql-case id="set_resource_group" value="SET RESOURCE GROUP rg" db-types="MySQL"/>
+    <sql-case id="set_charset" value="SET NAMES 'UTF8'" db-types="MySQL,PostgreSQL" />
+    <sql-case id="set_client_encoding" value="SET CLIENT_ENCODING TO 'UTF8'" db-types="PostgreSQL" />
 </sql-cases>