Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 933E4632F1C for ; Sat, 1 May 2010 21:26:31 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 13466-04 for ; Sun, 2 May 2010 00:26:22 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 4084F632F37 for ; Sat, 1 May 2010 21:26:23 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id C7C61AE37C; Sun, 2 May 2010 08:26:18 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id cXYDJxZCeevl; Sun, 2 May 2010 08:26:13 +0800 (WST) Received: from [10.1.1.7] (unknown [124.148.207.159]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 75A52AE37B; Sun, 2 May 2010 08:26:13 +0800 (WST) Message-ID: <4BDCC68F.8000003@postnewspapers.com.au> Date: Sun, 02 May 2010 08:25:51 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: =?ISO-8859-1?Q?Maxime_L=E9vesque?= CC: pgsql-jdbc@postgresql.org Subject: Re: sending a block through jdbc References: <741db998-8a4f-4442-930f-46821ff785d2@b18g2000yqb.googlegroups.com> In-Reply-To: <741db998-8a4f-4442-930f-46821ff785d2@b18g2000yqb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.425 tagged_above=-10 required=5 tests=AWL=0.475, BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201005/20 X-Sequence-Number: 20504 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