You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/04/23 05:25:05 UTC

[shardingsphere] branch master updated: Refactor system variable query in MySQLSaneQueryResultEngine (#25283)

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

panjuan 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 a7433ce3e0c Refactor system variable query in MySQLSaneQueryResultEngine (#25283)
a7433ce3e0c is described below

commit a7433ce3e0c57ff83442ca38c84635e03f4444e9
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Sun Apr 23 13:24:58 2023 +0800

    Refactor system variable query in MySQLSaneQueryResultEngine (#25283)
    
    * Refactor system variable query in MySQLSaneQueryResultEngine
    
    * Perform code format in MySQLSaneQueryResultEngine
---
 .../mysql/connector/sane/MySQLDefaultVariable.java | 76 ----------------------
 .../connector/sane/MySQLSaneQueryResultEngine.java | 12 +++-
 .../sane/MySQLSaneQueryResultEngineTest.java       |  3 +-
 3 files changed, 11 insertions(+), 80 deletions(-)

diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLDefaultVariable.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLDefaultVariable.java
deleted file mode 100644
index b88a5343fc4..00000000000
--- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLDefaultVariable.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.proxy.backend.mysql.connector.sane;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * MySQL default variable.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class MySQLDefaultVariable {
-    
-    private static final Map<String, String> VARIABLES = new HashMap<>(20, 1);
-    
-    static {
-        VARIABLES.put("auto_increment_increment", "1");
-        VARIABLES.put("character_set_client", "utf8");
-        VARIABLES.put("character_set_connection", "utf8");
-        VARIABLES.put("character_set_results", "utf8");
-        VARIABLES.put("character_set_server", "utf8");
-        VARIABLES.put("collation_server", "utf8_general_ci");
-        VARIABLES.put("collation_connection", "utf8_general_ci");
-        VARIABLES.put("init_connect", "");
-        VARIABLES.put("interactive_timeout", "28800");
-        VARIABLES.put("license", "GPL");
-        VARIABLES.put("lower_case_table_names", "2");
-        VARIABLES.put("max_allowed_packet", "4194304");
-        VARIABLES.put("net_buffer_length", "16384");
-        VARIABLES.put("net_write_timeout", "60");
-        VARIABLES.put("sql_mode", "STRICT_TRANS_TABLES");
-        VARIABLES.put("system_time_zone", "CST");
-        VARIABLES.put("time_zone", "SYSTEM");
-        VARIABLES.put("transaction_isolation", "REPEATABLE-READ");
-        VARIABLES.put("wait_timeout", "28800");
-        VARIABLES.put("@@session.transaction_read_only", "0");
-    }
-    
-    /**
-     * Judge whether contains variable.
-     * 
-     * @param variableName variable name
-     * @return contains variable or not
-     */
-    public static boolean containsVariable(final String variableName) {
-        return VARIABLES.containsKey(variableName);
-    }
-    
-    /**
-     * Get variable value.
-     *
-     * @param variableName variable name
-     * @return variable value
-     */
-    public static String getVariable(final String variableName) {
-        return VARIABLES.get(variableName);
-    }
-}
diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngine.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngine.java
index 0263e7674bc..5820c7ffdca 100644
--- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngine.java
+++ b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngine.java
@@ -25,6 +25,8 @@ import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.ra
 import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
 import org.apache.shardingsphere.proxy.backend.connector.sane.SaneQueryResultEngine;
+import org.apache.shardingsphere.proxy.backend.mysql.handler.admin.executor.sysvar.SystemVariable;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -71,10 +73,14 @@ public final class MySQLSaneQueryResultEngine implements SaneQueryResultEngine {
         List<Object> data = new ArrayList<>(sqlStatement.getProjections().getProjections().size());
         for (ProjectionSegment each : sqlStatement.getProjections().getProjections()) {
             if (each instanceof ExpressionProjectionSegment) {
-                String text = ((ExpressionProjectionSegment) each).getText();
-                String alias = ((ExpressionProjectionSegment) each).getAlias().orElse(((ExpressionProjectionSegment) each).getText());
+                ExpressionProjectionSegment expressionProjection = (ExpressionProjectionSegment) each;
+                String text = expressionProjection.getText();
+                String alias = expressionProjection.getAlias().orElse(expressionProjection.getText());
                 queryResultColumnMetaDataList.add(createRawQueryResultColumnMetaData(text, alias));
-                data.add(MySQLDefaultVariable.containsVariable(alias) ? MySQLDefaultVariable.getVariable(alias) : "1");
+                String value = expressionProjection.getExpr() instanceof VariableSegment
+                        ? SystemVariable.findSystemVariable(((VariableSegment) expressionProjection.getExpr()).getVariable()).map(SystemVariable::getDefaultValue).orElse("1")
+                        : "1";
+                data.add(value);
             }
         }
         return queryResultColumnMetaDataList.isEmpty()
diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngineTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngineTest.java
index cce06e66080..662db572076 100644
--- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngineTest.java
+++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/connector/sane/MySQLSaneQueryResultEngineTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.mysql.connector.sane;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.type.RawMemoryQueryResult;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
@@ -59,7 +60,7 @@ class MySQLSaneQueryResultEngineTest {
     void assertGetSaneQueryResultForSelectStatementWithoutFrom() {
         MySQLSelectStatement selectStatement = new MySQLSelectStatement();
         selectStatement.setProjections(new ProjectionsSegment(0, 0));
-        selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "@@session.transaction_read_only"));
+        selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "@@session.transaction_read_only", new VariableSegment(0, 0, "transaction_read_only")));
         selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "unknown_variable"));
         Optional<ExecuteResult> actual = new MySQLSaneQueryResultEngine().getSaneQueryResult(selectStatement, new SQLException());
         assertTrue(actual.isPresent());