From: Matthias Böhm <fliegenblues@gmx.net>
To: 'Maciek Sakrejda' <msakrejda@truviso.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Bug with PreparedStatements using EXTRACT function
Date: Fri, 30 Mar 2012 12:13:47 +0200
Message-ID: <000001cd0e5d$cf737490$6e5a5db0$@net> (raw)
In-Reply-To: <CAH_hXRazAorz=Cp_byageUYmJct5wPA2qWsOgXgUW8JtpEKy0g@mail.gmail.com>
References: <000401cd0d8a$6dbe04f0$493a0ed0$@net>
<CAH_hXRazAorz=Cp_byageUYmJct5wPA2qWsOgXgUW8JtpEKy0g@mail.gmail.com>
> If you use one of the type-specific setters, it should work.
No, it doesn't: I'm using setDate(int, java.util.sql.Date) or
setTimestamp(int, java.util.sql.Timestamp) for setting the parameter, but
the information about the parameter type seems to get lost:
A legal query without using a parameter is:
SELECT EXTRACT (YEAR FROM DATE '1990-06-07 +02:00:00')
But when I prepare a statement with "SELECT EXTRACT (YEAR FROM ?)" and use
the setDate method to set the parameter and after this, use the toString
method of the PreparedStatement, I get the following:
SELECT EXTRACT (YEAR FROM '1991-09-07 +02:00:00')
In this query the obviously required DATE is missing!
On the contrary, trying to prepare a statement with "SELECT EXTRACT (YEAR
FROM DATE ?)" results, after calling toString on the prepared statement, in
the following, seemingly correct query:
SELECT EXTRACT (YEAR FROM DATE '1991-09-07 +02:00:00')
But when I try to execute the query I get a "syntax error at $1".
So there seems no way to prepare such a query without using the explicit
cast.
> The driver does attempt to do
> some type-guessing based on the Java "source" parameter type, but
> there is something of a disconnect between Java and Postgres types, so
> this does *not* occur for java.util.Date, and for java.sql.Timestamp,
> it occurs in such a way as to break for your use case
Yes, I took a look at the code, and the problem seems to be that bindString
method is called with Oid.UNSPECIFIED, so that the information about the
type is lost.
Still it is not clear to me why preparing a statement with "SELECT EXTRACT
(YEAR FROM DATE ?)" doesn't work as well, because it yields, as already
mentioned above, a seemingly valid query.
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: fliegenblues@gmx.net, msakrejda@truviso.com
Subject: Re: Bug with PreparedStatements using EXTRACT function
In-Reply-To: <000001cd0e5d$cf737490$6e5a5db0$@net>
References: <000401cd0d8a$6dbe04f0$493a0ed0$@net> <CAH_hXRazAorz=Cp_byageUYmJct5wPA2qWsOgXgUW8JtpEKy0g@mail.gmail.com> <000001cd0e5d$cf737490$6e5a5db0$@net>
* 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