From: Oliver Jowett <oliver@opencloud.com>
To: j.random.programmer <javadesigner@yahoo.com>
Cc: pg@fastcrypt.com
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Bug: Driver(8.2dev-500.jdbc3) does not handle boolean->BIT
Date: Wed, 21 Dec 2005 07:22:52 +0000
Message-ID: <43A902CC.7020404@opencloud.com> (raw)
In-Reply-To: <20051221044129.56245.qmail@web32013.mail.mud.yahoo.com>
References: <20051221044129.56245.qmail@web32013.mail.mud.yahoo.com>
j.random.programmer wrote:
> Ok. This while BIT thing is very non-intuitive from my
> perspective.
It sure is. You're trying to deal with a type that's not directly
supported by the JDBC spec, using only spec-provided mechanisms. Having
a JDBC type called "BIT" which actually maps to a single boolean type is
very confusing. If you assume that JDBC's BIT has *nothing* to do with
the server type called "bit", and that it's just a coincidence that they
have the same name, then things should be clearer.
> In PSQL:
> --------------------------------------
> test=# create table foo (bit_val bit(5));
> CREATE TABLE
> test=# insert into foo (bit_val) values ('B10101');
> INSERT 0 1
> test=# insert into foo (bit_val) values ('11111');
> INSERT 0 1
The equivalent queries if you are using the JDBC driver and
PreparedStatement.setString() look like this:
>> test=# create table foo (bit_val bit(5));
>> CREATE TABLE
>> test=# insert into foo (bit_val) values ('B10101'::varchar);
>> ERROR: column "bit_val" is of type bit but expression is of type character varying
>> HINT: You will need to rewrite or cast the expression.
>> test=# insert into foo (bit_val) values ('11111'::varchar);
>> ERROR: column "bit_val" is of type bit but expression is of type character varying
>> HINT: You will need to rewrite or cast the expression.
Does that error look familiar? :)
> With val = '11111'
> ------------------------------------
> PreparedStatement pstmt =
> con.prepareStatement(
> "insert into foo (bit_val) values (?)"
> );
> pstmt.setString(1, val);
> pstmt.executeUpdate();
> -----------------------------------
>
> The same with val = 'B11111'
> The same with val = true
> ...etc...
>
> SQLException: SQLState(42804)
> Exception in thread "main"
> org.postgresql.util.PSQLException: ERROR: column
> "bit_val" is of type bit but expression is of type
> character varying
It is a *type* issue, not a representation issue. That error is
occurring before the server even looks at the parameter value you've
passed -- the problem is that you're passing a varchar parameter
(courtesy of using setString()) in a context where the server is
expecting something that can be implicitly cast to a bit(n) value, and
there is no such implicit conversion from varchar.
Use something like this to get an explicit type conversion from varchar
to bit(5):
INSERT INTO foo(bit_val) VALUES (CAST (? AS bit(5)))
or (nonstandard):
INSERT INTO foo(bit_val) VALUES (?::bit(5))
Or turn on autocasting for string parameters (see the development driver
docs).
If you want to support bit(n) directly, you could write an extension
type (subclass of PGobject). AFAIK noone has written this yet.
Another possible mapping would be to/from a Java boolean array. This has
the problem that a Java array of booleans passed to setObject() should
probably be mapped to an array of booleans on the server side, not to a
bit(n) type.
-O
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: oliver@opencloud.com, javadesigner@yahoo.com, pg@fastcrypt.com
Subject: Re: Bug: Driver(8.2dev-500.jdbc3) does not handle boolean->BIT
In-Reply-To: <43A902CC.7020404@opencloud.com>
References: <20051221044129.56245.qmail@web32013.mail.mud.yahoo.com> <43A902CC.7020404@opencloud.com>
* 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