Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 57268632DAC for ; Thu, 25 Mar 2010 18:09:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 80260-07 for ; Thu, 25 Mar 2010 21:09:08 +0000 (UTC) X-Greylist: delayed 00:06:12.611775 by SQLgrey-1.7.6 Received: from mail-px0-f182.google.com (mail-px0-f182.google.com [209.85.216.182]) by mail.postgresql.org (Postfix) with ESMTP id 9BC41632511 for ; Thu, 25 Mar 2010 18:09:05 -0300 (ADT) Received: by pxi12 with SMTP id 12so2120198pxi.14 for ; Thu, 25 Mar 2010 14:09:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.114.186.40 with SMTP id j40mr7422830waf.93.1269550971933; Thu, 25 Mar 2010 14:02:51 -0700 (PDT) In-Reply-To: <7a8b7ba31003251319y391cef47sff3c5ee3bba5eb4c@mail.gmail.com> References: <7a8b7ba31003251319y391cef47sff3c5ee3bba5eb4c@mail.gmail.com> Date: Thu, 25 Mar 2010 14:02:51 -0700 Message-ID: <895e58dd1003251402t1d2a5ec9j6c3fe6dff87b7a0a@mail.gmail.com> Subject: Re: Problems with semicolon trying to create a trigger function via jdbc From: Maciek Sakrejda To: Collin Peters Cc: pgsql-jdbc@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 201003/59 X-Sequence-Number: 20399 The parseQuery() method in QueryExecutorImpl breaks up a query if you're executing more than one statement in a single JDBC query. However, it seems to take quotes (and comments) into account. I tried your simple test case (replacing DriverManager.getConnection() for ds.getConnection()), and it works fine. I have a feeling that the DataSource is wrapping the Connection (and Statement) in proxies that also try to break up the individual queries, but that do not take dollar-quotes into account. --- Maciek Sakrejda | Software Engineer | Truviso 1065 E. Hillsdale Blvd., Suite 230 Foster City, CA 94404 (650) 242-3500 Main (650) 242-3501 F msakrejda@truviso.com www.truviso.com On Thu, Mar 25, 2010 at 1:19 PM, Collin Peters wrote: > Hi all, > > I have some framework code that needs to dynamically generate a > function.=A0 There seems to be a problem where the SQL gets truncated at > the first semicolon encountered in the function.=A0 I have tried this > with a very simple function and duplicated it. > > The test trigger function is as follows: > CREATE OR REPLACE FUNCTION test() > =A0 RETURNS trigger AS > $BODY$ > DECLARE > =A0=A0=A0 foo integer; > BEGIN > =A0=A0=A0 foo =3D 4; > =A0=A0=A0 RAISE NOTICE 'Foo: %', foo; > END; > $BODY$ > LANGUAGE 'plpgsql' VOLATILE > COST 100; > ALTER FUNCTION test() OWNER TO mcrtdbms; > > The simple test code is: > String sql =3D "CREATE OR REPLACE FUNCTION > history.history_insert_trigger()=A0=A0=A0=A0 RETURNS trigger AS=A0=A0 $BO= DY$ > DECLARE=A0=A0 foo integer;=A0=A0 BEGIN=A0=A0=A0 foo =3D 4; RAISE NOTICE '= Foo: %', foo; > =A0=A0=A0=A0 END;=A0=A0 $BODY$=A0=A0=A0=A0 LANGUAGE 'plpgsql' VOLATILE; "= ; > =A0=A0=A0 =A0=A0=A0 DataSource ds =3D getDataSource(); > =A0=A0=A0 =A0=A0=A0 try > =A0=A0=A0 =A0=A0=A0 { > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 Connection conn =3D ds.getConnection(); > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 conn.setAutoCommit(true); > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 Statement st =3D conn.createStatement(); > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 st.executeUpdate(sql); > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 st.close(); > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 conn.close(); > =A0=A0=A0 =A0=A0=A0 } > =A0=A0=A0 =A0=A0=A0 catch (SQLException e) > =A0=A0=A0 =A0=A0=A0 { > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 e.printStackTrace(); > =A0=A0=A0 =A0=A0=A0 } > > When I try to run this via my webapp I get the error: > org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted > string at or near "$BODY$=A0=A0 DECLARE=A0=A0 foo integer" > =A0=A0=A0 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorRespons= e(QueryExecutorImpl.java:1591) > =A0=A0=A0 at org.postgresql.core.v3.QueryExecutorImpl.processResults(Quer= yExecutorImpl.java:1340) > =A0=A0=A0 at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecut= orImpl.java:192) > > > So it seems to be truncating the SQL at the first semicolon it > encounters which, of course, borks the whole thing.=A0 What is even > stranger in my quest to get this working is that the above code > actually WORKS when I run it through a JUnit test!!!=A0 I have made no > progress in trying to figure out what is different between the unit > test and the running webapp.=A0 At first I though it was my ORM so I > tried with the straight JDBC code used above and so eliminated that. > Now I am trying to determine if the Postgres JDBC driver is at fault. > > Here is the rest of the details > =A0* JDBC version 8.3-605 JDBC 3 > =A0* Postgres 8.3 > =A0* JUnit 4 > =A0* Application is built w/ Spring (but this manual query shouldn't be > affected by taht) > =A0* When the above query is run through the webapp, it is initially > triggered by a Quartz (scheduling api) trigger (which runs when the > webapp starts) > > Any help at all would be appreciated!! =A0I am about to give up and > write a function which will do the job of creating the trigger for > me!! > > Regards, > Collin > > -- > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-jdbc >