修复blob类型调getBytes方法报错问题
This commit is contained in:
@ -2655,8 +2655,9 @@ public class PgResultSet implements ResultSet, org.postgresql.PGRefCursorResultS
|
|||||||
int oid = fields[columnIndex - 1].getOID();
|
int oid = fields[columnIndex - 1].getOID();
|
||||||
if (oid == Oid.BYTEA) {
|
if (oid == Oid.BYTEA) {
|
||||||
return trimBytes(columnIndex, PGbytea.toBytes(this_row[columnIndex - 1]));
|
return trimBytes(columnIndex, PGbytea.toBytes(this_row[columnIndex - 1]));
|
||||||
} else if (oid == Oid.BLOB) {
|
} else if (oid == Oid.BLOB || blobSet.contains(getPGType(columnIndex))) {
|
||||||
return toBytes(getString(columnIndex));
|
String result = new String(this_row[columnIndex - 1]);
|
||||||
|
return toBytes(result);
|
||||||
} else if (oid == Oid.BIT && connection.getPgDatabase().isDec()) {
|
} else if (oid == Oid.BIT && connection.getPgDatabase().isDec()) {
|
||||||
return toDecBytes(fields[columnIndex - 1].getMod(), getString(columnIndex));
|
return toDecBytes(fields[columnIndex - 1].getMod(), getString(columnIndex));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import java.sql.Statement;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class BlobTest extends BaseTest4B {
|
public class BlobTest extends BaseTest4B {
|
||||||
|
|
||||||
@ -107,4 +108,54 @@ public class BlobTest extends BaseTest4B {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToBlob() throws SQLException {
|
||||||
|
String sql = "INSERT INTO test_blob_b VALUES (2,'1234'::tinyblob,"
|
||||||
|
+ "'1234'::blob,'1234'::mediumblob,'1234'::longblob)";
|
||||||
|
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||||
|
pstmt.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Statement statement = con.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery("SELECT * FROM test_blob_b")) {
|
||||||
|
while (rs.next()) {
|
||||||
|
assertEquals("1234", new String(rs.getBlob(2).getBytes(1, 4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("1234", new String(rs.getBlob(3).getBytes(1, 4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("1234", new String(rs.getBlob(4).getBytes(1, 4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("1234", new String(rs.getBlob(5).getBytes(1, 4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test blob by getBytes
|
||||||
|
*
|
||||||
|
* @throws SQLException sql exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testBlobToBytes() throws SQLException {
|
||||||
|
String sql = "INSERT INTO test_blob_b VALUES (1,'abcd'::tinyblob,"
|
||||||
|
+ "'abcd'::blob,'abcd'::mediumblob,'abcd'::longblob)";
|
||||||
|
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||||
|
pstmt.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Statement statement = con.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery("SELECT * FROM test_blob_b")) {
|
||||||
|
assertTrue(rs.next());
|
||||||
|
assertEquals("abcd", new String(rs.getBytes(2),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("abcd", new String(rs.getBytes(3),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("abcd", new String(rs.getBytes(4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("abcd", new String(rs.getBytes(5),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
63
pgjdbc/src/test/java/org/postgresql/test/jdbc4/BlobTest.java
Normal file
63
pgjdbc/src/test/java/org/postgresql/test/jdbc4/BlobTest.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.types.PGBlob;
|
||||||
|
import org.postgresql.test.TestUtil;
|
||||||
|
import org.postgresql.test.jdbc2.BaseTest4;
|
||||||
|
|
||||||
|
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 blob
|
||||||
|
*
|
||||||
|
* @author zhangting
|
||||||
|
* @since 2024-08-23
|
||||||
|
*/
|
||||||
|
public class BlobTest extends BaseTest4 {
|
||||||
|
@Test
|
||||||
|
public void testStringToBlob() throws SQLException {
|
||||||
|
TestUtil.createTable(con, "test_blob_a", "id int, c1 blob");
|
||||||
|
try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_blob_a VALUES (?,?)")) {
|
||||||
|
pstmt.setInt(1, 1);
|
||||||
|
PGBlob blob = new PGBlob();
|
||||||
|
blob.setBytes(1, "1234".getBytes(StandardCharsets.UTF_8));
|
||||||
|
pstmt.setBlob(2, blob);
|
||||||
|
pstmt.execute();
|
||||||
|
}
|
||||||
|
String sql = "INSERT INTO test_blob_a VALUES (2,'31323334'::blob)";
|
||||||
|
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||||
|
pstmt.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Statement statement = con.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery("SELECT * FROM test_blob_a")) {
|
||||||
|
while (rs.next()) {
|
||||||
|
assertEquals("1234", new String(rs.getBlob(2).getBytes(1, 4),
|
||||||
|
StandardCharsets.UTF_8));
|
||||||
|
assertEquals("1234", new String(rs.getBytes(2), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TestUtil.dropTable(con, "test_blob_a");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user