Bug in setClob() ?

From: Giuseppe Sacco <giuseppe(at)eppesuigoccas(dot)homedns(dot)org>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Bug in setClob() ?
Date: 2014-09-11 17:06:28
Message-ID: 5411D694.206@eppesuigoccas.homedns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello all,
I found a problem in org/postgresql/jdbc2/AbstractJdbc2Statement on the
setClob(int i, Clob x) method.

I created a clob implementation in my application and then passed it to
postgresql via this method, but the correct value is not stored in the
database.

What happen is that this jdbc implementation read all data from my clob
using two methods, getAsciiStream() and length(), this way:

InputStream l_inStream = x.getAsciiStream();
int l_length = (int) x.length();
LargeObjectManager lom = connection.getLargeObjectAPI();
long oid = lom.createLO();
LargeObject lob = lom.open(oid);
OutputStream los = lob.getOutputStream();
try
{
int c = l_inStream.read();
int p = 0;
while (c > -1 && p < l_length)
{
los.write(c);
c = l_inStream.read();
p++;
}
los.close();
[...]

So, it does read from an ASCII stream using a counter based on the
number of characters present in the clob.

My clob does not contain ASCII characters, but multibyte ones. So, I
think I should probably fail on getAsciiStream(), but this driver does
not use any other way to get the data.

I tried to implement getAsciiStream() as:
return new ByteArrayInputStream( data.getBytes() );

but of course the byte array size is in bytes, so if you only read
length() bytes, you will not store the complete string.

Now, I think the problem is clear, but I don't know what is the best way
to fix it. Let see a couple of proposal for changing the setClob
implementation in this driver:

1. remove the usage of lenth() and just loop until the l_inStream.read()
fail.
2. use getCharacterStream(), when available, with precedence on
getAsciiStream()

what do you suggest? Will the "los" object accept non ASCII data?

Thanks,
Giuseppe

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Giuseppe Sacco 2014-09-12 08:28:23 Re: Bug in setClob() ?
Previous Message Dave Cramer 2014-09-04 20:21:45 Re: Cursors removed with commit