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

zhonghongsheng 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 6f83adeb54f proxy support parse pg json parameter in bind phase 
(#25864)
6f83adeb54f is described below

commit 6f83adeb54f070804719553070092983a06a8e62
Author: 亥时 <[email protected]>
AuthorDate: Wed May 24 08:00:02 2023 +0800

    proxy support parse pg json parameter in bind phase (#25864)
    
    * proxy support parse pg json parameter in bind phase
    
    * improve according to cr
---
 .../extended/bind/PostgreSQLComBindPacket.java     |  3 ++
 .../bind/protocol/PostgreSQLTextJsonUtils.java     | 50 ++++++++++++++++++++++
 .../bind/protocol/PostgreSQLTextJsonUtilsTest.java | 35 +++++++++++++++
 3 files changed, 88 insertions(+)

diff --git 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
index 2335f3bc635..392bc08ab3f 100644
--- 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
+++ 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
@@ -25,6 +25,7 @@ import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQ
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValue;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValueFactory;
+import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextJsonUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimeUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimestampUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
@@ -131,6 +132,8 @@ public final class PostgreSQLComBindPacket extends 
PostgreSQLCommandPacket {
             case POSTGRESQL_TYPE_TIMESTAMP:
             case POSTGRESQL_TYPE_TIMESTAMPTZ:
                 return PostgreSQLTextTimestampUtils.parse(textValue);
+            case POSTGRESQL_TYPE_JSON:
+                return PostgreSQLTextJsonUtils.parse(textValue);
             default:
                 return textValue;
         }
diff --git 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtils.java
 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtils.java
new file mode 100644
index 00000000000..ab4bd705dc2
--- /dev/null
+++ 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtils.java
@@ -0,0 +1,50 @@
+/*
+ * 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.db.protocol.postgresql.packet.command.query.extended.bind.protocol;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.type.wrapper.SQLWrapperException;
+import org.postgresql.util.PGobject;
+
+import java.sql.SQLException;
+
+/**
+ * Text Json utility class of PostgreSQL.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PostgreSQLTextJsonUtils {
+    
+    /**
+     * Parse json in PostgreSQL text format.
+     *
+     * @param jsonText text value to be parsed
+     * @return json pgobject
+     * @throws SQLWrapperException thrown if value is invalid for json type
+     */
+    public static PGobject parse(final String jsonText) {
+        try {
+            PGobject result = new PGobject();
+            result.setType("json");
+            result.setValue(jsonText);
+            return result;
+        } catch (final SQLException ex) {
+            throw new SQLWrapperException(ex);
+        }
+    }
+}
diff --git 
a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtilsTest.java
 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtilsTest.java
new file mode 100644
index 00000000000..41c0228b1de
--- /dev/null
+++ 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextJsonUtilsTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.db.protocol.postgresql.packet.command.query.extended.bind.protocol;
+
+import org.junit.jupiter.api.Test;
+import org.postgresql.util.PGobject;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class PostgreSQLTextJsonUtilsTest {
+    
+    @Test
+    void assertParse() {
+        String textValue = "['input']";
+        PGobject actual = PostgreSQLTextJsonUtils.parse(textValue);
+        assertThat(actual.getType(), is("json"));
+        assertThat(actual.getValue(), is(textValue));
+    }
+}

Reply via email to