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 1e80bdcfddb Parsing CharacterSet to exclude single and double quotes 
for postgres (#23954)
1e80bdcfddb is described below

commit 1e80bdcfddb88026da8f52a72faf2e9f50900a08
Author: jyotisharma7 <[email protected]>
AuthorDate: Fri Feb 3 14:12:00 2023 +0530

    Parsing CharacterSet to exclude single and double quotes for postgres 
(#23954)
    
    * fix character encoding issues when enclosed in single quotes
    
    * Add Method level comments
    
    * Address PR review comments
    
    * Refactor the code
    
    ---------
    
    Co-authored-by: santhosh <>
    Co-authored-by: surukonda <[email protected]>
---
 .../handler/admin/postgresql/PostgreSQLCharacterSets.java        | 9 +++++++--
 .../admin/postgresql/executor/PostgreSQLSetCharsetExecutor.java  | 7 +------
 .../handler/admin/postgresql/PostgreSQLCharacterSetsTest.java    | 5 +++++
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSets.java
 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSets.java
index 7a1e634e5cc..88ad3f74cfd 100644
--- 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSets.java
+++ 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSets.java
@@ -107,7 +107,12 @@ public enum PostgreSQLCharacterSets {
      * @return corresponding {@link Charset}
      */
     public static Charset findCharacterSet(final String charsetName) {
-        PostgreSQLCharacterSets result = 
CHARACTER_SETS_MAP.get(charsetName.toUpperCase());
-        return null != result && null != result.charset ? result.charset : 
Charset.forName(charsetName);
+        String formattedCharsetName = formatValue(charsetName);
+        PostgreSQLCharacterSets result = 
CHARACTER_SETS_MAP.get(formattedCharsetName.toUpperCase());
+        return null != result && null != result.charset ? result.charset : 
Charset.forName(formattedCharsetName);
+    }
+    
+    private static String formatValue(final String value) {
+        return value.startsWith("'") && value.endsWith("'") || 
value.startsWith("\"") && value.endsWith("\"") ? value.substring(1, 
value.length() - 1) : value.trim();
     }
 }
diff --git 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/PostgreSQLSetCharsetExecutor.java
 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/PostgreSQLSetCharsetExecutor.java
index cf1bf6d6197..e5236fb8885 100644
--- 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/PostgreSQLSetCharsetExecutor.java
+++ 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/PostgreSQLSetCharsetExecutor.java
@@ -34,12 +34,7 @@ public final class PostgreSQLSetCharsetExecutor implements 
PostgreSQLSessionVari
     
     @Override
     public void handle(final ConnectionSession connectionSession, final String 
variableName, final String assignValue) {
-        String value = formatValue(assignValue.trim());
-        
connectionSession.getAttributeMap().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(parseCharset(value));
-    }
-    
-    private String formatValue(final String value) {
-        return value.startsWith("'") && value.endsWith("'") ? 
value.substring(1, value.length() - 1).trim() : value;
+        
connectionSession.getAttributeMap().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(parseCharset(assignValue.trim()));
     }
     
     private Charset parseCharset(final String value) {
diff --git 
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSetsTest.java
 
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSetsTest.java
index 6fcab5d4551..25a9d21fb77 100644
--- 
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSetsTest.java
+++ 
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLCharacterSetsTest.java
@@ -37,6 +37,11 @@ public final class PostgreSQLCharacterSetsTest {
         assertThat(PostgreSQLCharacterSets.findCharacterSet("utf8"), 
is(StandardCharsets.UTF_8));
     }
     
+    @Test
+    public void assertFindUTF8WithQuotes() {
+        assertThat(PostgreSQLCharacterSets.findCharacterSet("'utf-8'"), 
is(StandardCharsets.UTF_8));
+    }
+    
     @Test(expected = UnsupportedCharsetException.class)
     public void assertFindUnsupportedCharset() {
         PostgreSQLCharacterSets.findCharacterSet("unknown_charset");

Reply via email to