Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 4883E6335D5 for ; Thu, 25 Mar 2010 18:44:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 81339-06 for ; Thu, 25 Mar 2010 21:43:52 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id 0BDE2632DAC for ; Thu, 25 Mar 2010 18:43:57 -0300 (ADT) Received: by vws8 with SMTP id 8so1016967vws.19 for ; Thu, 25 Mar 2010 14:43:56 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.124.76 with SMTP id t12mr4205165vcr.33.1269553436363; Thu, 25 Mar 2010 14:43:56 -0700 (PDT) In-Reply-To: <895e58dd1003251402t1d2a5ec9j6c3fe6dff87b7a0a@mail.gmail.com> References: <7a8b7ba31003251319y391cef47sff3c5ee3bba5eb4c@mail.gmail.com> <895e58dd1003251402t1d2a5ec9j6c3fe6dff87b7a0a@mail.gmail.com> Date: Thu, 25 Mar 2010 14:43:56 -0700 Message-ID: <7a8b7ba31003251443v74fe6df1mc412e0c03ba4cf3c@mail.gmail.com> Subject: Re: Problems with semicolon trying to create a trigger function via jdbc From: Collin Peters To: Maciek Sakrejda 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/60 X-Sequence-Number: 20400 Thanks for the replies Tom and Maciek, I wasn't able to get the DriverManager.getConnection() approach to work (still worked in the unit test, but not the running webapp), but reverting to the old 'non-dollar' quoting approach worked. Thanks Collin On Thu, Mar 25, 2010 at 2:02 PM, Maciek Sakrejda wr= ote: > 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 $B= ODY$ >> 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.receiveErrorRespon= se(QueryExecutorImpl.java:1591) >> =A0=A0=A0 at org.postgresql.core.v3.QueryExecutorImpl.processResults(Que= ryExecutorImpl.java:1340) >> =A0=A0=A0 at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecu= torImpl.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 >> >