Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 75F0C13B3874 for ; Fri, 30 Mar 2012 07:14:07 -0300 (ADT) Received: from mailout-de.gmx.net ([213.165.64.23]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SDYqK-0005do-9A for pgsql-jdbc@postgresql.org; Fri, 30 Mar 2012 10:14:06 +0000 Received: (qmail invoked by alias); 30 Mar 2012 10:13:51 -0000 Received: from i577A8CD5.versanet.de (EHLO MatthiasLaptop) [87.122.140.213] by mail.gmx.net (mp031) with SMTP; 30 Mar 2012 12:13:51 +0200 X-Authenticated: #41751617 X-Provags-ID: V01U2FsdGVkX1/pdTpsCqLpvZX5fZEUODxW4gucesaUVbIHIcCaUT fEPAzgdHF9R9YZ From: =?iso-8859-1?Q?Matthias_B=F6hm?= To: "'Maciek Sakrejda'" Cc: References: <000401cd0d8a$6dbe04f0$493a0ed0$@net> In-Reply-To: Subject: Re: Bug with PreparedStatements using EXTRACT function Date: Fri, 30 Mar 2012 12:13:47 +0200 Message-ID: <000001cd0e5d$cf737490$6e5a5db0$@net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac0Ny2SLF2qKUe+zSlW/uLrSa3ly2AAisdww Content-Language: de X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/60 X-Sequence-Number: 22799 > 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.