pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthias Böhm <fliegenblues@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: Bug with PreparedStatements using EXTRACT function
Date: Thu, 29 Mar 2012 11:00:45 +0200
Message-ID: <000401cd0d8a$6dbe04f0$493a0ed0$@net> (raw)



> -----Ursprüngliche Nachricht-----
> Von: Maciek Sakrejda [mailto:msakrejda@truviso.com]
> Gesendet: Mittwoch, 28. März 2012 22:54
> An: Matthias Böhm
> Cc: pgsql-jdbc@postgresql.org
> Betreff: Re: [JDBC] Bug with PreparedStatements using EXTRACT function
> 
> That's because the 'date' is not a cast but some sort of type
> specification inherent to the expression. I don't understand the
> details of the grammar, but you can see this if you try to run it via
> PREPARE (the closest language-level thing you can use to JDBC's
> parameterized statements):
> 
> cqdb=# prepare foo(date) as select extract (year from date '2000-01-
> 01');
> PREPARE
> cqdb=# prepare bar(date) as select extract (year from date $1);
> ERROR:  syntax error at or near "$1"
> LINE 1: prepare bar(date) as select extract (year from date $1);
>                                                             ^
> cqdb=# prepare baz(date) as select extract (year from $1);
> PREPARE
> cqdb=#

OK, I found a solution:

Whereas the last prepare statement works on the sql console it doesn't work
with jdbc: I've tried to construct the PreparedStatement as follows:

PreparedStatement stmt2 = conn.prepareStatement(
		"SELECT EXTRACT (YEAR FROM ?)");

And now the exception I get is (translated roughly from German):

Function pg_catalog.date_part(unknown, unknown) is ambiguous. 
Hint: Couldn't choose best candidate function. You may have to add an
explicit cast.

Adding this explicit cast makes the thing work:

PreparedStatement stmt2 = conn.prepareStatement(
 		"SELECT EXTRACT (YEAR FROM CAST (? AS DATE))");


It is not obvious though at once that this cast is needed. 

Thanks for taking a look at this issue and kind regards, 
Matthias Böhm





view thread (6+ messages)  latest in thread

Message-ID: <000401cd0d8a$6dbe04f0$493a0ed0$@net>
Permalink:  ../000401cd0d8a$6dbe04f0$493a0ed0$@net/
Also on:    postgresql.org/message-id/000401cd0d8a$6dbe04f0$493a0ed0$@net

 · 

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: fliegenblues@gmx.net
  Subject: Re: Bug with PreparedStatements using EXTRACT function
  In-Reply-To: <000401cd0d8a$6dbe04f0$493a0ed0$@net>
  References: <000401cd0d8a$6dbe04f0$493a0ed0$@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