pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sending a block through jdbc
3+ messages / 3 participants
[nested] [flat]

* sending a block through jdbc
@ 2010-05-01 03:20  Maxime Lévesque <maxime.levesque@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Maxime Lévesque @ 2010-05-01 03:20 UTC (permalink / raw)
  To: pgsql-jdbc

I want to drop a constraint if it exists, and not get an error if it
doesn't exist,
ideally I'd get a jdbc error code, and silence the 'consrtaint does
not exist' exception,
but since postgress doesn't send error codes to jdbc, I'd like to put
the statement in
a block and silence the exception like this

    sb.append("begin \n");
    sb.append("alter table " + foreingKeyTable.name + " drop
constraint " + fkName + ";\n");
    sb.append("exception when true then \n");
    sb.append("end;");

 statement.execute(sb.toString)

It seems that this is either unsupported or I have bad syntax....

Any clues on ho to do this ?



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

* Re: sending a block through jdbc
@ 2010-05-01 14:02  Kris Jurka <books@ejurka.com>
  parent: Maxime Lévesque <maxime.levesque@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Kris Jurka @ 2010-05-01 14:02 UTC (permalink / raw)
  To: Maxime Lévesque <maxime.levesque@gmail.com>; +Cc: pgsql-jdbc



On Fri, 30 Apr 2010, Maxime L?vesque wrote:

> I want to drop a constraint if it exists, and not get an error if it 
> doesn't exist, ideally I'd get a jdbc error code, and silence the 
> 'consrtaint does not exist' exception, but since postgress doesn't send 
> error codes to jdbc, I'd like to put the statement in a block and 
> silence the exception like this

PG sets SQLState in the SQLException since it is standardized and not 
error code which is vendor specific.

>    sb.append("begin \n");
>    sb.append("alter table " + foreingKeyTable.name + " drop
> constraint " + fkName + ";\n");
>    sb.append("exception when true then \n");
>    sb.append("end;");
>
> It seems that this is either unsupported or I have bad syntax....

PG will only support anonymous blocks in the upcoming 9.0 release, and 
then with a different syntax.

http://developer.postgresql.org/pgdocs/postgres/sql-do.html

Kris Jurka



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

* Re: sending a block through jdbc
@ 2010-05-02 00:25  Craig Ringer <craig@postnewspapers.com.au>
  parent: Maxime Lévesque <maxime.levesque@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Craig Ringer @ 2010-05-02 00:25 UTC (permalink / raw)
  To: Maxime Lévesque <maxime.levesque@gmail.com>; +Cc: pgsql-jdbc

On 1/05/2010 11:20 AM, Maxime Lévesque wrote:
> I want to drop a constraint if it exists, and not get an error if it
> doesn't exist

Check the system tables / INFORMATION SCHEMA to test if it exists before 
dropping it.

> ideally I'd get a jdbc error code, and silence the 'consrtaint does
> not exist' exception,
> but since postgress doesn't send error codes to jdbc, I'd like to put
> the statement in

Yes, it does. As Kris noted, it's in SQLException. Use the "getSQLState" 
method. It's often useful to test for prefixes.

http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html#JDBC209

http://www.postgresql.org/docs/current/interactive/errcodes-appendix.html


> a block and silence the exception like this
>
>      sb.append("begin \n");
>      sb.append("alter table " + foreingKeyTable.name + " drop
> constraint " + fkName + ";\n");
>      sb.append("exception when true then \n");

Exception handlers are a part of the PL/PgSQL language. They're not 
supported in SQL, and don't make much sense there since there's no 
procedural logic flow.

Kris pointed out that something similar is possible in 9.0. Consider 
whether it's actually a good idea, though. DO blocks won't be cheap to 
execute - especially compared to plain old SQL.

--
Craig Ringer



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


end of thread, other threads:[~2010-05-02 00:25 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-05-01 03:20 sending a block through jdbc Maxime Lévesque <maxime.levesque@gmail.com>
2010-05-01 14:02 ` Kris Jurka <books@ejurka.com>
2010-05-02 00:25 ` Craig Ringer <craig@postnewspapers.com.au>

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