pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: EBIHARA, Yuichiro <ebihara@iplocks.co.jp>
To: pgsql-jdbc@postgresql.org
Subject: how to handle data containing '\0'
Date: Wed, 17 May 2006 21:01:30 +0900
Message-ID: <001f01c679a9$a3627350$360aa8c0@ipljp.local> (raw)

Hi experts,

I need to handle String data containing '\0' in my Java + JDBC program. Such data can happen at other systems and be sent to
PostgreSQL. Here is a sample to simulate it in Java code.

        char c = '\0';
        String val = "abc" + c + "def";

Now I have to support two versions of drivers, 7.4 and 8.1, but the behavior of driver depends on versions when I insert this String
value.

=== pg74.216.jdbc3.jar ===
java.lang.IllegalArgumentException: \0 not allowed
	at org.postgresql.jdbc1.AbstractJdbc1Statement.escapeString(AbstractJdbc1Statement.java:1152)
	at org.postgresql.jdbc1.AbstractJdbc1Statement.setString(AbstractJdbc1Statement.java:1133)
	at org.postgresql.jdbc1.AbstractJdbc1Statement.setString(AbstractJdbc1Statement.java:1116)
	at Bug5391.main(Bug5391.java:24)

=== postgresql-8.1-405.jdbc3.jar ===
Successfully finished without exception but the string is truncated at '\0' and only the first three characters, "abc", is inserted.
(I verified this with psql on the server side)

I understand the current protocol does not allow '\0' in String and basically I should remove '\0' before I try to insert but I and
team members may forget it somewhere. I'm feeling the 8.1 behavior is worse than 7.4 because user data is truncated without notice.
For the sample above, I expect "abcdef" rather than "abc".

What do you think about this?
My idea is to introduce new connection parameter to determine how to behave against '\0'.
Options may be

 - throws an exception
 - truncates at '\0'
 - removes '\0'

Thanks,

ebi





view thread (7+ messages)  latest in thread

Message-ID: <001f01c679a9$a3627350$360aa8c0@ipljp.local>
Permalink:  ../001f01c679a9$a3627350$360aa8c0@ipljp.local/
Also on:    postgresql.org/message-id/001f01c679a9$a3627350$360aa8c0@ipljp.local

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: ebihara@iplocks.co.jp
  Subject: Re: how to handle data containing '\0'
  In-Reply-To: <001f01c679a9$a3627350$360aa8c0@ipljp.local>
  References: <001f01c679a9$a3627350$360aa8c0@ipljp.local>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox