pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kris Jurka <books@ejurka.com>
To: Jie Liang <jie@stbernard.com>
Cc: pgsql-sql@postgresql.org
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Prepare Statement
Date: Fri, 18 Jun 2004 00:47:19 -0500 (EST)
Message-ID: <Pine.BSO.4.56.0406180044340.30102@leary.csoft.net> (raw)
In-Reply-To: <E7E213858379814A9AE48CA6754F5ECB0345194D@mail01.stbernard.com>
References: <E7E213858379814A9AE48CA6754F5ECB0345194D@mail01.stbernard.com>



On Thu, 17 Jun 2004, Jie Liang wrote:

> Kris,
> I have another question, I saw some discussion regarding
> PreparedStatement work with array argument, I get a error when I try to
> play with it.
> E.g.
> I have myfunction(int[]),
> So, 
> PrepareStatement st = conn.prepareStatment("SELECT myfunction(?)");
> String arr="{1,2,3}";
> St.setString(1,arr};
> Result rs = st.executeQuery();
> 
> Then it will complaint when it run:
> Myfuntion(text) does not exist!
> 


This is actually a case where prepared statements actually cause trouble.  
With the directly executed SELECT myfunction('{1,2,3}'); The backend can
determine that there is only one version of myfunction so it can convert
the unkown argument type to it, but note that this won't work if 
myfunction is overloaded.

With the prepared case, you must tell it what types to use when doing the
prepare.  The JDBC driver doesn't have a whole lot of information to work
with, so it takes what it knows (that you called setString) and says the
argument is of type text, issuing a prepare like this:  PREPARE
JDBC_STATEMENT_1(text) AS SELECT myfunction($1);  At this time (before it
actually calls EXECUTE) it tries to lookup myfunction that takes a text
argument and determines there isn't one.  In this case it doesn't have the
opportunity to apply any casts because we were quite clear in specifying
that it should take a text argument, not one of unknown type.

Ideally you should be using setArray, but there is no existing way to 
create Array objects and I'm not sure that code would work even if there 
was.

Kris Jurka




view thread (19+ messages)  latest in thread

Message-ID: <Pine.BSO.4.56.0406180044340.30102@leary.csoft.net>
Permalink:  ../Pine.BSO.4.56.0406180044340.30102@leary.csoft.net/
Also on:    postgresql.org/message-id/Pine.BSO.4.56.0406180044340.30102@leary.csoft.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: books@ejurka.com, jie@stbernard.com, pgsql-jdbc@postgresql.org
  Subject: Re: Prepare Statement
  In-Reply-To: <Pine.BSO.4.56.0406180044340.30102@leary.csoft.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