From 8d80383df6e16c642282f1515988c6117bc69b8a Mon Sep 17 00:00:00 2001 From: zhangting Date: Sun, 22 Sep 2024 20:59:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0utf8Decode=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pgjdbc/src/main/java/org/postgresql/PGProperty.java | 7 ++++++- pgjdbc/src/main/java/org/postgresql/core/Encoding.java | 10 ++++++++++ pgjdbc/src/main/java/org/postgresql/core/PGStream.java | 9 ++++++++- .../org/postgresql/core/v3/ConnectionFactoryImpl.java | 5 +++++ .../java/org/postgresql/test/jdbc2/PGPropertyTest.java | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pgjdbc/src/main/java/org/postgresql/PGProperty.java b/pgjdbc/src/main/java/org/postgresql/PGProperty.java index c62a936..1f71544 100644 --- a/pgjdbc/src/main/java/org/postgresql/PGProperty.java +++ b/pgjdbc/src/main/java/org/postgresql/PGProperty.java @@ -622,7 +622,12 @@ public enum PGProperty { * Determine whether SAVEPOINTS used in AUTOSAVE will be released per query or not */ CLEANUP_SAVEPOINTS("cleanupSavepoints", "false", "Determine whether SAVEPOINTS " - + "used in AUTOSAVE will be released per query or not", false, new String[]{"true", "false"}) + + "used in AUTOSAVE will be released per query or not", false, new String[]{"true", "false"}), + + /** + * Determine whether use utf8 encoding + */ + UTF8_DECODE("UTF8Decode", "false", "Use utf8 encoding.") ; private String _name; diff --git a/pgjdbc/src/main/java/org/postgresql/core/Encoding.java b/pgjdbc/src/main/java/org/postgresql/core/Encoding.java index fe1c51f..1f06c32 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/Encoding.java +++ b/pgjdbc/src/main/java/org/postgresql/core/Encoding.java @@ -127,6 +127,16 @@ public class Encoding { } } + /** + * Construct utf8 Encoding for a given JVM encoding. + * + * @param jvmEncoding the name of the JVM encoding + * @return an Encoding instance for the specified encoding. + */ + public static Encoding getUtf8Encoding(String jvmEncoding) { + return new UTF8Encoding(jvmEncoding); + } + /** * Construct an Encoding for a given database encoding. * diff --git a/pgjdbc/src/main/java/org/postgresql/core/PGStream.java b/pgjdbc/src/main/java/org/postgresql/core/PGStream.java index a57c13b..fe75416 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/PGStream.java +++ b/pgjdbc/src/main/java/org/postgresql/core/PGStream.java @@ -49,6 +49,9 @@ public class PGStream implements Closeable, Flushable { private Writer encodingWriter; private final int MAX_PARAMS_NUM = 65535; + + private boolean isUtf8Encode = false; + /** * Constructor: Connect to the PostgreSQL back end and return a stream connection. * @@ -103,6 +106,10 @@ public class PGStream implements Closeable, Flushable { return socketFactory; } + public void setUtf8Encode(boolean isUtf8Encode) { + this.isUtf8Encode = isUtf8Encode; + } + /** * Check for pending backend messages without blocking. Might return false when there actually are * messages waiting, depending on the characteristics of the underlying socket. This is used to @@ -162,7 +169,7 @@ public class PGStream implements Closeable, Flushable { * @throws IOException if something goes wrong */ public void setEncoding(Encoding encoding) throws IOException { - if (this.encoding != null && this.encoding.name().equals(encoding.name())) { + if (!isUtf8Encode && this.encoding != null && this.encoding.name().equals(encoding.name())) { return; } // Close down any old writer. diff --git a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java index 8f9f4b8..3c4494d 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -18,6 +18,7 @@ import org.postgresql.core.SetupQueryRunner; import org.postgresql.core.SocketFactoryFactory; import org.postgresql.core.Utils; import org.postgresql.core.Version; +import org.postgresql.core.Encoding; import org.postgresql.hostchooser.*; import org.postgresql.jdbc.SslMode; import org.postgresql.QueryCNListUtils; @@ -111,6 +112,10 @@ public class ConnectionFactoryImpl extends ConnectionFactory { } PGStream newStream = new PGStream(socketFactory, hostSpec, connectTimeout); + if (Boolean.valueOf(PGProperty.UTF8_DECODE.get(info))) { + newStream.setUtf8Encode(true); + newStream.setEncoding(Encoding.getUtf8Encoding("UTF-8")); + } // Construct and send an ssl startup packet if requested. newStream = enableSSL(newStream, sslMode, info, connectTimeout); diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PGPropertyTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PGPropertyTest.java index f1d0f2d..330d6c0 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PGPropertyTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PGPropertyTest.java @@ -154,6 +154,7 @@ public class PGPropertyTest { excluded.add("writeDataSourceAddress"); excluded.add("BCmptMode"); excluded.add("bitOutput"); + excluded.add("UTF8Decode"); // index PropertyDescriptors by name Map propertyDescriptors = -- Gitee