36 Star 101 Fork 107

openGauss / openGauss-connector-jdbc

 / 详情

在BLOB MODE打开的情况下无法通过setBinaryStream()方法插入blob数据

已验收
缺陷
创建于  
2023-05-19 16:22

【标题描述】: 在BLOB MODE打开的情况下无法通过setBinaryStream()方法插入blob数据
【测试类型:接口功能】
【测试版本:MASTER】
【操作步骤】(请填写详细的操作步骤):
1.在opengauss数据库上创建表:

/**
 * @author saxisuer
 * huawei=> create table test_lob (id varchar(64), col_blob blob, col_bytea bytea);
 * CREATE TABLE
 * huawei=> \d test_lob
 * Table "test_data.test_lob"
 * Column   |         Type          | Modifiers
 * -----------+-----------------------+-----------
 * id        | character varying(64) |
 * col_blob  | blob                  |
 * col_bytea | bytea                 |
 */
  1. 通过JDBC代码 插入一条数据
    /**
     * blobMode=on
     * 通过setBinaryStream的方式 插入数据到blob,bytea 字段中
     *
     * @throws ClassNotFoundException
     * @throws SQLException           org.opengauss.util.PSQLException: [127.0.0.1:53558/.0.0.0/0.0.0.0:55448] ERROR: column "col_blob" is of type blob but expression is of type bytea
     *                                建议:You will need to rewrite or cast the expression.
     *                                位置:67
     *                                在位置:referenced column: col_blob
     **/
    @Test
    public void testBlobModeOnfInsert() throws ClassNotFoundException, SQLException, IOException {
        Class.forName(org.opengauss.Driver.class.getName());
        blobMode = true;
        String url = buildJdbcUrl();
        System.out.println(url);
        Connection connection = DriverManager.getConnection(url, "dbuser", "dbpwd");
        InputStream inputStream = new ByteArrayInputStream(content.getBytes());
        InputStream inputStream1 = new ByteArrayInputStream(content.getBytes());
        PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
        preparedStatement.setString(1, UUID.randomUUID().toString());
        preparedStatement.setBinaryStream(2, inputStream);
        preparedStatement.setBinaryStream(3, inputStream1);
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();
    }

【预期输出】:
正常插入 col_blob 字段 但是 col_typea 字段报错
【实际输出】:
column "col_blob" is of type blob but expression is of type bytea
【原因分析】:

  1. org.postgresql.jdbc.PgPreparedStatement#setBinaryStream(int, java.io.InputStream) 方法没有实现blobMode 的特性

【日志信息】(请附上日志文件、截图、coredump信息):

【测试代码】:

评论 (5)

萨西摩尔 创建了缺陷

Hey @萨西摩尔, Welcome to openGauss Community.
All of the projects in openGauss Community are maintained by @opengauss-bot.
That means the developers can comment below every pull request or issue to trigger Bot Commands.
Please follow instructions at Here to find the details.

opengauss-bot 添加了
 
sig/connectors
标签

Hi @萨西摩尔,
if you want to get quick review about your issue, please contact the owner in first: @周斌 ,
and then any of the maintainers: @Pike
and then any of the committers: @Kamus , @vimiix , @汪伟 , @travelliu , @aaronwell , @Loong
if you have any question, please contact the SIG: Connectors.

jiexiao1413 优先级设置为不重要
jiexiao1413 负责人设置为周斌
周斌 任务状态待办的 修改为已确认
周斌 任务状态已确认 修改为修复中
萨西摩尔 通过opengauss/openGauss-connector-jdbc Pull Request !130任务状态修复中 修改为已完成

验收日期:2023-10-21(5.0.1 B006)
验收版本:compiled at 2023-10-19-10:19:59 build 6061008
验收结果:通过
输入图片说明

验收脚本

package PreparedStatement;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;

import org.opengauss.Driver;

/**
 * @作者 : pwx5328113 @时间 : 2023/9/27
 * @测试点 : blob字段类型setBinaryStream\setBlob\getBlob方法覆盖
 **/
public class JDBC_PreparedStatement_Case0002 {
	public static void main(String[] args) throws Exception {
		if (args.length < 4) {
			System.out.print("need para: host:ip,db_b,user,password");
			return;
		}
		System.out.println(Driver.getGSVersion());
		String t_name = "t_jdbc_preparedstatament_0027";
		String url1 = "jdbc:opengauss://" + args[0] + "/" + args[1] + "?LoggerLevel=off&blobMode=on";
		Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
		Statement statement = connection1.createStatement();
		String create_sql = "drop table if exists " + t_name + "; " + "create table " + t_name
				+ " (id varchar(64), col_blob blob);";
		statement.executeUpdate(create_sql);

		String insert_sql = "insert into " + t_name + " (id, col_blob) values (?, ?)";
		String select_sql = "select id,col_blob from " + t_name;
		String content = "通过setBinaryStream方法插入blob数据";
		InputStream inputStream1 = new ByteArrayInputStream(content.getBytes());
		PreparedStatement preparedStatement = connection1.prepareStatement(insert_sql);
		preparedStatement.setString(1, UUID.randomUUID().toString());
		preparedStatement.setBinaryStream(2, inputStream1);
		preparedStatement.execute();
		System.out.println("testcase1:通过setBinaryStream方法插入blob数据成功");
		content = "通过setBlob方法插入blob数据";
		inputStream1 = new ByteArrayInputStream(content.getBytes());
		preparedStatement.setString(1, UUID.randomUUID().toString());
		preparedStatement.setBlob(2, inputStream1);
		preparedStatement.execute();
		System.out.println("testcase2:通过setBlob方法插入blob数据成功");
		try {
			preparedStatement.setString(1, UUID.randomUUID().toString());
			preparedStatement.setBytes(2, content.getBytes());
			preparedStatement.execute();
		} catch (Exception e) {
			System.out.println("testcase3:通过setBytes方法插入blob数据报错 \n" + e.getMessage());
		}
		try (ResultSet resultSet = statement.executeQuery(select_sql)) {
			while (resultSet.next()) {
				String col1 = resultSet.getObject(1).toString();
				Object col2 = resultSet.getObject(2);
				Blob col3 = resultSet.getBlob(2);
				System.out.println("col1 = " + col1);
				System.out.println("testcase4:getObject获取blob字段 = " + new String((byte[]) col2)); // getObject获取结果以字符串方式显示
				System.out.println("testcase5:getBlob获取blob字段 = " + new String(col3.getBytes(1, (int) col3.length()))); // getBlob获取结果以字符串方式显示
			}
		}
		statement.executeUpdate("drop table " + t_name);
		preparedStatement.close();
		statement.close();
		statement.close();
	}
}

验收日期:2023-11-30
验收版本:compiled at 2023-11-28-19:27:06 build 2eed94f
验收结论:通过
输入图片说明
输入图片说明

wan005 任务状态已完成 修改为已验收

登录 后才可以发表评论

状态
负责人
项目
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
预计工期 (小时)
参与者(3)
5622128 opengauss bot 1581905080
Java
1
https://gitee.com/opengauss/openGauss-connector-jdbc.git
git@gitee.com:opengauss/openGauss-connector-jdbc.git
opengauss
openGauss-connector-jdbc
openGauss-connector-jdbc

搜索帮助