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

wuweijie 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 e8154619f28 Fix sonar could issue of PostgreSQLServerInfo (#25532)
e8154619f28 is described below

commit e8154619f28e9819d22a94e764b7bcab5f482e16
Author: Liang Zhang <[email protected]>
AuthorDate: Tue May 9 12:56:35 2023 +0800

    Fix sonar could issue of PostgreSQLServerInfo (#25532)
    
    * Fix sonar could issue of PostgreSQLServerInfo
    
    * Fix sonar could issue of PostgreSQLServerInfo
    
    * Fix sonar could issue of PostgreSQLServerInfo
---
 .../protocol/mysql/constant/MySQLServerInfo.java   |  6 +++---
 .../postgresql/constant/PostgreSQLServerInfo.java  | 24 ++++++++++++++++------
 .../constant/PostgreSQLServerInfoTest.java         | 13 ++++++++----
 .../opengauss/OpenGaussFrontendEngine.java         |  2 +-
 .../OpenGaussAuthenticationEngine.java             |  2 +-
 .../postgresql/PostgreSQLFrontendEngine.java       |  2 +-
 .../PostgreSQLAuthenticationEngine.java            |  2 +-
 7 files changed, 34 insertions(+), 17 deletions(-)

diff --git 
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
 
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
index 553fddc8d88..5608aca7641 100644
--- 
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
+++ 
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
@@ -54,7 +54,7 @@ public final class MySQLServerInfo {
     }
     
     /**
-     * Get current server version by schemaName.
+     * Get server version.
      * 
      * @param databaseName database name
      * @return server version
@@ -64,9 +64,9 @@ public final class MySQLServerInfo {
     }
     
     /**
-     * Set default mysql version.
+     * Set default MySQL version.
      * 
-     * @param defaultMysqlVersion default mysql version
+     * @param defaultMysqlVersion default MySQL version
      */
     public static void setDefaultMysqlVersion(final String 
defaultMysqlVersion) {
         MySQLServerInfo.defaultMysqlVersion = defaultMysqlVersion;
diff --git 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfo.java
 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfo.java
index 40782fa4aef..b7ee67f9b14 100644
--- 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfo.java
+++ 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfo.java
@@ -21,6 +21,9 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.db.protocol.constant.CommonConstants;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 /**
  * ShardingSphere-Proxy's information for PostgreSQL.
  */
@@ -31,23 +34,32 @@ public final class PostgreSQLServerInfo {
     
     private static final String SERVER_VERSION_PATTERN = 
"%s-ShardingSphere-Proxy %s";
     
-    private static volatile String serverVersion;
+    private static final Map<String, String> SERVER_VERSIONS = new 
ConcurrentHashMap<>();
     
     /**
      * Set server version.
      *
+     * @param databaseName database name
      * @param serverVersion server version
      */
-    public static synchronized void setServerVersion(final String 
serverVersion) {
-        PostgreSQLServerInfo.serverVersion = null == serverVersion ? null : 
String.format(SERVER_VERSION_PATTERN, serverVersion, 
CommonConstants.PROXY_VERSION.get());
+    public static void setServerVersion(final String databaseName, final 
String serverVersion) {
+        // TODO check when the serverVersion is null value
+        if (null != serverVersion) {
+            SERVER_VERSIONS.put(databaseName, 
String.format(SERVER_VERSION_PATTERN, serverVersion, 
CommonConstants.PROXY_VERSION.get()));
+        }
     }
     
     /**
-     * Get current server version.
+     * Get server version.
      *
+     * @param databaseName database name
      * @return server version
      */
-    public static String getServerVersion() {
-        return null == serverVersion ? String.format(SERVER_VERSION_PATTERN, 
DEFAULT_POSTGRESQL_VERSION, CommonConstants.PROXY_VERSION.get()) : 
serverVersion;
+    public static String getServerVersion(final String databaseName) {
+        return null == databaseName ? getDefaultServerVersion() : 
SERVER_VERSIONS.getOrDefault(databaseName, getDefaultServerVersion());
+    }
+    
+    private static String getDefaultServerVersion() {
+        return String.format(SERVER_VERSION_PATTERN, 
DEFAULT_POSTGRESQL_VERSION, CommonConstants.PROXY_VERSION.get());
     }
 }
diff --git 
a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfoTest.java
 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfoTest.java
index 0fbe1c79557..a15387f524c 100644
--- 
a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfoTest.java
+++ 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLServerInfoTest.java
@@ -28,14 +28,19 @@ class PostgreSQLServerInfoTest {
     @Test
     void assertSetServerVersion() {
         CommonConstants.PROXY_VERSION.set("5.0.0");
-        PostgreSQLServerInfo.setServerVersion("13.2");
-        assertThat(PostgreSQLServerInfo.getServerVersion(), 
is("13.2-ShardingSphere-Proxy 5.0.0"));
+        PostgreSQLServerInfo.setServerVersion("foo_db", "13.2");
+        assertThat(PostgreSQLServerInfo.getServerVersion("foo_db"), 
is("13.2-ShardingSphere-Proxy 5.0.0"));
     }
     
     @Test
     void assertSetServerVersionForNull() {
         CommonConstants.PROXY_VERSION.set("5.0.0");
-        PostgreSQLServerInfo.setServerVersion(null);
-        assertThat(PostgreSQLServerInfo.getServerVersion(), 
is("12.3-ShardingSphere-Proxy 5.0.0"));
+        PostgreSQLServerInfo.setServerVersion("foo_db", null);
+        assertThat(PostgreSQLServerInfo.getServerVersion("foo_db"), 
is("12.3-ShardingSphere-Proxy 5.0.0"));
+    }
+    
+    @Test
+    void assertGetServerVersionWithoutDatabase() {
+        assertThat(PostgreSQLServerInfo.getServerVersion(null), 
is("12.3-ShardingSphere-Proxy 5.0.0"));
     }
 }
diff --git 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
index 9e760686df9..dfec2fb6253 100644
--- 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
+++ 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
@@ -45,7 +45,7 @@ public final class OpenGaussFrontendEngine implements 
DatabaseProtocolFrontendEn
     
     @Override
     public void setDatabaseVersion(final String databaseName, final String 
databaseVersion) {
-        PostgreSQLServerInfo.setServerVersion(databaseVersion);
+        PostgreSQLServerInfo.setServerVersion(databaseName, databaseVersion);
     }
     
     @Override
diff --git 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
index 9c346c1a311..4488ff242eb 100644
--- 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
+++ 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
@@ -119,7 +119,7 @@ public final class OpenGaussAuthenticationEngine implements 
AuthenticationEngine
         PostgreSQLPasswordMessagePacket passwordMessagePacket = new 
PostgreSQLPasswordMessagePacket(payload);
         login(rule, passwordMessagePacket.getDigest());
         context.write(new PostgreSQLAuthenticationOKPacket());
-        context.write(new PostgreSQLParameterStatusPacket("server_version", 
PostgreSQLServerInfo.getServerVersion()));
+        context.write(new PostgreSQLParameterStatusPacket("server_version", 
PostgreSQLServerInfo.getServerVersion(currentAuthResult.getDatabase())));
         context.write(new PostgreSQLParameterStatusPacket("client_encoding", 
clientEncoding));
         context.write(new PostgreSQLParameterStatusPacket("server_encoding", 
"UTF8"));
         context.write(new PostgreSQLParameterStatusPacket("integer_datetimes", 
"on"));
diff --git 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index 906f7123fbd..a23842ec89a 100644
--- 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++ 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -45,7 +45,7 @@ public final class PostgreSQLFrontendEngine implements 
DatabaseProtocolFrontendE
     
     @Override
     public void setDatabaseVersion(final String databaseName, final String 
databaseVersion) {
-        PostgreSQLServerInfo.setServerVersion(databaseVersion);
+        PostgreSQLServerInfo.setServerVersion(databaseName, databaseVersion);
     }
     
     @Override
diff --git 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
index 6651058cebf..0e21d08a5be 100644
--- 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
+++ 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
@@ -108,7 +108,7 @@ public final class PostgreSQLAuthenticationEngine 
implements AuthenticationEngin
         login(currentAuthResult.getDatabase(), 
currentAuthResult.getUsername(), md5Salt, passwordMessagePacket.getDigest(), 
rule);
         // TODO implement PostgreSQLServerInfo like MySQLServerInfo
         context.write(new PostgreSQLAuthenticationOKPacket());
-        context.write(new PostgreSQLParameterStatusPacket("server_version", 
PostgreSQLServerInfo.getServerVersion()));
+        context.write(new PostgreSQLParameterStatusPacket("server_version", 
PostgreSQLServerInfo.getServerVersion(currentAuthResult.getDatabase())));
         context.write(new PostgreSQLParameterStatusPacket("client_encoding", 
clientEncoding));
         context.write(new PostgreSQLParameterStatusPacket("server_encoding", 
"UTF8"));
         context.write(new PostgreSQLParameterStatusPacket("integer_datetimes", 
"on"));

Reply via email to