diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java b/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java index 629cd60..c3fa4b2 100644 --- a/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java +++ b/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java @@ -110,7 +110,7 @@ public class TypeInfoCache implements TypeInfo { {"smalldatetime", Oid.SMALLDATETIME, Types.TIMESTAMP, "java.lang.Timestamp", Oid.SMALLDATETIME_ARRAY}, {"timestamptz", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMPTZ_ARRAY}, - {"json", Oid.JSON, Types.OTHER, "org.postgresql.util.PGobject", Oid.JSON_ARRAY}, + {"json", Oid.JSON, Types.VARCHAR, "java.lang.String", Oid.JSON_ARRAY}, {"point", Oid.POINT, Types.OTHER, "org.postgresql.geometric.PGpoint", Oid.POINT_ARRAY}, {"blob", Oid.BLOB, Types.BLOB, "org.postgresql.util.PGobject", -1}, {"clob", Oid.CLOB, Types.CLOB, "org.postgresql.util.PGobject", -1}, diff --git a/pgjdbc/src/test/java/org/postgresql/test/dolphintest/BinaryTest.java b/pgjdbc/src/test/java/org/postgresql/test/dolphintest/BinaryTest.java new file mode 100644 index 0000000..b26f07c --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/dolphintest/BinaryTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.postgresql.test.dolphintest; + +import org.junit.Test; +import org.postgresql.test.TestUtil; +import org.postgresql.test.jdbc2.BaseTest4B; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.junit.Assert.assertEquals; + +/** + * test binary + * + * @author zhangting + * @since 2024-08-20 + */ +public class BinaryTest extends BaseTest4B { + @Test + public void testBinary1() throws SQLException { + TestUtil.createTable(con, "test_binary_b", "id int, c1 binary(5)"); + try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_binary_b VALUES (?,?)")) { + pstmt.setInt(1, 1); + try (InputStream data = new ByteArrayInputStream("abcde".getBytes(StandardCharsets.UTF_8))) { + pstmt.setBinaryStream(2, data); + pstmt.execute(); + } catch (IOException e) { + e.printStackTrace(); + } + } + String sql = "INSERT INTO test_binary_b VALUES (2,'abcde'::binary)"; + try (PreparedStatement pstmt = con.prepareStatement(sql)) { + pstmt.execute(); + } + + try (Statement statement = con.createStatement(); + ResultSet rs = statement.executeQuery("SELECT * FROM test_binary_b")) { + while (rs.next()) { + assertEquals("\\x6162636465", new String(rs.getBytes(2), + StandardCharsets.UTF_8)); + } + } + TestUtil.dropTable(con, "test_binary_b"); + } + + @Test + public void testBinary2() throws SQLException { + try (Statement stat = con.createStatement()) { + try (ResultSet rs = stat.executeQuery("SELECT '10101'::binary(5)")) { + while (rs.next()) { + assertEquals("\\x3130313031", new String(rs.getBytes(1), + StandardCharsets.UTF_8)); + } + } + try (ResultSet rs = stat.executeQuery("SELECT cast('abc' as binary)")) { + while (rs.next()) { + assertEquals("\\x616263", rs.getString(1)); + } + } + + stat.execute("set bytea_output=escape;"); + try (ResultSet rs = stat.executeQuery("SELECT '10101'::binary(5)")) { + while (rs.next()) { + assertEquals("10101", new String(rs.getBytes(1), + StandardCharsets.UTF_8)); + } + } + try (ResultSet rs = stat.executeQuery("SELECT cast('abc' as binary)")) { + while (rs.next()) { + assertEquals("abc", rs.getString(1)); + } + } + } + } +} diff --git a/pgjdbc/src/test/java/org/postgresql/test/dolphintest/NumericTest.java b/pgjdbc/src/test/java/org/postgresql/test/dolphintest/NumericTest.java new file mode 100644 index 0000000..4e8b0c9 --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/dolphintest/NumericTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.postgresql.test.dolphintest; + +import org.junit.Test; +import org.postgresql.test.TestUtil; +import org.postgresql.test.jdbc2.BaseTest4B; + +import java.math.BigDecimal; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Types; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +/** + * test numeric + * + * @author zhangting + * @since 2024-08-20 + */ +public class NumericTest extends BaseTest4B { + /* + * test numeric type + */ + @Test + public void testNumeric() throws Exception { + TestUtil.createTable(con, "test_numeric", "c1 numeric,c2 numeric(8,4),c3 float," + + "c4 float(8,4),c5 double,c6 double(8,4),c7 real,c8 real(8,4),c9 decimal(8,4)"); + + String sql = "INSERT INTO test_numeric VALUES (?,?,?,?,?,?,?,?,?)"; + try (PreparedStatement pstmt = con.prepareStatement(sql)) { + for (int i = 1; i <= 9; i++) { + pstmt.setDouble(i, 92.456739023); + } + pstmt.executeUpdate(); + } + + try (Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM test_numeric")) { + assertTrue(rs.next()); + ResultSetMetaData rsmd = rs.getMetaData(); + assertEquals(9, rsmd.getColumnCount()); + assertEquals(Types.NUMERIC, rsmd.getColumnType(1)); + assertEquals(Types.NUMERIC, rsmd.getColumnType(2)); + assertEquals(Types.REAL, rsmd.getColumnType(3)); + assertEquals(Types.NUMERIC, rsmd.getColumnType(4)); + assertEquals(Types.DOUBLE, rsmd.getColumnType(5)); + assertEquals(Types.NUMERIC, rsmd.getColumnType(6)); + assertEquals(Types.REAL, rsmd.getColumnType(7)); + assertEquals(Types.NUMERIC, rsmd.getColumnType(8)); + assertEquals(Types.NUMERIC, rsmd.getColumnType(9)); + + assertEquals(new BigDecimal(92), rs.getObject(1)); + assertEquals(new String("92.4567"), rs.getString(2)); + assertEquals(new String("92.4567"), rs.getString(4)); + assertEquals(new String("92.4567"), rs.getString(6)); + assertEquals(new String("92.4567"), rs.getString(8)); + assertEquals(new String("92.4567"), rs.getString(9)); + } + TestUtil.dropTable(con, "test_numeric"); + } +} diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc4/ArrayTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc4/ArrayTest.java index 1d262d3..4790369 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc4/ArrayTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc4/ArrayTest.java @@ -160,14 +160,7 @@ public class ArrayTest extends BaseTest4 { return; } PreparedStatement pstmt = con.prepareStatement("SELECT ?::json[]"); - PGobject p1 = new PGobject(); - p1.setType("json"); - p1.setValue("{\"x\": 10}"); - - PGobject p2 = new PGobject(); - p2.setType("json"); - p2.setValue("{\"x\": 20}"); - PGobject[] in = new PGobject[] { p1, p2 }; + String[] in = new String[]{"{\"x\": 10}", "{\"x\": 20}"}; pstmt.setArray(1, con.createArrayOf("json", in)); ResultSet rs = pstmt.executeQuery(); @@ -175,10 +168,10 @@ public class ArrayTest extends BaseTest4 { Array arr = rs.getArray(1); ResultSet arrRs = arr.getResultSet(); Assert.assertTrue(arrRs.next()); - Assert.assertEquals(in[0], arrRs.getObject(2)); + Assert.assertEquals(in[0], arrRs.getString(2)); Assert.assertTrue(arrRs.next()); - Assert.assertEquals(in[1], arrRs.getObject(2)); + Assert.assertEquals(in[1], arrRs.getString(2)); } @Test diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc4/JsonTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc4/JsonTest.java new file mode 100644 index 0000000..5a18010 --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc4/JsonTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.postgresql.test.jdbc4; + +import org.junit.Test; +import org.postgresql.test.TestUtil; +import org.postgresql.test.jdbc2.BaseTest4; + +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Types; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * test json + * + * @author zhangting + * @since 2024-08-20 + */ +public class JsonTest extends BaseTest4 { + /* + * test json to string + */ + @Test + public void testJsonToString() throws Exception { + TestUtil.createTable(con, "test_json", "id json"); + String sql = "INSERT INTO test_json VALUES ('{\"k1\":\"v1\",\"k2\":\"v2\"}')"; + try (PreparedStatement pstmt = con.prepareStatement(sql)) { + pstmt.executeUpdate(); + } + + try (Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM test_json")) { + assertTrue(rs.next()); + ResultSetMetaData rsmd = rs.getMetaData(); + assertEquals(1, rsmd.getColumnCount()); + assertEquals(Types.VARCHAR, rsmd.getColumnType(1)); + + assertEquals("{\"k1\":\"v1\",\"k2\":\"v2\"}", rs.getString(1)); + } + TestUtil.dropTable(con, "test_json"); + } +}