pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Prepared statement parameter and timestamp
3+ messages / 3 participants
[nested] [flat]

* Prepared statement parameter and timestamp
@ 2007-04-03 09:28  ste.buffaishere@tin.it <ste.buffaishere@tin.it>
  0 siblings, 1 reply; 3+ messages in thread

From: ste.buffaishere@tin.it @ 2007-04-03 09:28 UTC (permalink / raw)
  To: pgsql-jdbc

Why this code:

PreparedStatement ps = conn.prepareStatement(
				
"SELECT date_trunc('week', {ts ?})");
ps.setTimestamp(1, new java.sql.
Timestamp(
				new java.util.Date().getTime()));
ResultSet rs = ps.
executeQuery();							
while(rs.next()) {
	debug(rs.getString(1));
}

fails miserably with
org.postgresql.util.PSQLException: ERROR: syntax 
error at or near "$1"
        at org.postgresql.core.v3.
QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1531)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults
(QueryExecutorImpl.java:1313)
        at org.postgresql.core.v3.
QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
        at org.
postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.
java:452)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.
executeWithFlags(AbstractJdbc2Statement.java:354)
        at org.
postgresql.jdbc2.AbstractJdbc2Statement.executeQuery
(AbstractJdbc2Statement.java:258)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Prepared statement parameter and timestamp
@ 2007-04-09 16:14  Kris Jurka <books@ejurka.com>
  parent: ste.buffaishere@tin.it <ste.buffaishere@tin.it>
  0 siblings, 1 reply; 3+ messages in thread

From: Kris Jurka @ 2007-04-09 16:14 UTC (permalink / raw)
  To: ste.buffaishere@tin.it <ste.buffaishere@tin.it>; +Cc: pgsql-jdbc



On Tue, 3 Apr 2007, ste.buffaishere@tin.it wrote:

> PreparedStatement ps = conn.prepareStatement(
> "SELECT date_trunc('week', {ts ?})");
> ps.setTimestamp(1, new java.sql.
> Timestamp(
> 				new java.util.Date().getTime()));
>
> fails miserably with
> org.postgresql.util.PSQLException: ERROR: syntax
> error at or near "$1"


The JDBC driver translates the escape sequence to:

SELECT date_trunc('week', timestamp ?)

but the server can't handle the prefix timestamp cast for anything other 
than plain literals, so it doesn't work for the parameter.

I suppose the driver should instead try to rewrite this as:

SELECT date_trunc('week', ?::timestamp)

I'm not sure how much work that would be to do.

Kris Jurka




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Prepared statement parameter and timestamp
@ 2007-04-11 13:21  Xavier Poinsard <xpoinsard@free.fr>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Xavier Poinsard @ 2007-04-11 13:21 UTC (permalink / raw)
  To: ste.buffaishere@tin.it <ste.buffaishere@tin.it>; +Cc: pgsql-jdbc

Kris Jurka a écrit :
> On Tue, 3 Apr 2007, ste.buffaishere@tin.it wrote:
> 
>> PreparedStatement ps = conn.prepareStatement(
>> "SELECT date_trunc('week', {ts ?})");
>> ps.setTimestamp(1, new java.sql.
>> Timestamp(
>>                 new java.util.Date().getTime()));
>>
>> fails miserably with
>> org.postgresql.util.PSQLException: ERROR: syntax
>> error at or near "$1"

The purpose of the {ts 'XXX'} JDBC escape syntax is to have a standard
syntax to write timestamp literal values. But if you are using a
PreparedStatement you should directly write : SELECT date_trunc('week',?).

> 
> 
> The JDBC driver translates the escape sequence to:
> 
> SELECT date_trunc('week', timestamp ?)
> 
> but the server can't handle the prefix timestamp cast for anything other
> than plain literals, so it doesn't work for the parameter.
> 
> I suppose the driver should instead try to rewrite this as:
> 
> SELECT date_trunc('week', ?::timestamp)
> 
> I'm not sure how much work that would be to do.

This is a one line change.

> 
> Kris Jurka
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
> 






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2007-04-11 13:21 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-04-03 09:28 Prepared statement parameter and timestamp ste.buffaishere@tin.it <ste.buffaishere@tin.it>
2007-04-09 16:14 ` Kris Jurka <books@ejurka.com>
2007-04-11 13:21   ` Xavier Poinsard <xpoinsard@free.fr>

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