Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WwYEQ-0006ln-Tn for pgsql-jdbc@arkaria.postgresql.org; Mon, 16 Jun 2014 14:49:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WwYEQ-00057U-D0 for pgsql-jdbc@arkaria.postgresql.org; Mon, 16 Jun 2014 14:49:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WwYEP-00057C-C4 for pgsql-jdbc@postgresql.org; Mon, 16 Jun 2014 14:49:57 +0000 Received: from [212.199.51.242] (helo=mail.UniCell.local) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WwYEG-0005uk-QL for pgsql-jdbc@postgresql.org; Mon, 16 Jun 2014 14:49:55 +0000 Received: from [192.168.251.97] ([192.168.251.97]) by mail.UniCell.local with Microsoft SMTPSVC(6.0.3790.3959); Mon, 16 Jun 2014 17:49:46 +0300 Message-ID: <539F040A.1060105@unicell.co.il> Date: Mon, 16 Jun 2014 17:49:46 +0300 From: Herouth Maoz User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: Unable to create triggers via jdbc. Tried so many anythings. Please help. References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------000407080306080106070009" X-OriginalArrivalTime: 16 Jun 2014 14:49:46.0482 (UTC) FILETIME=[3757ED20:01CF8972] X-Host-Lookup-Failed: Reverse DNS lookup failed for 212.199.51.242 (failed) X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------000407080306080106070009 Content-Type: text/plain; charset=windows-1255; format=flowed Content-Transfer-Encoding: 8bit If the problem is with the $ quoting, you can use plain quoting. After all, the main reason for using $ quoting is to avoid the need to escape all the quotation marks within the function body. And JDBC can do that for you if you use a prepared statement. I would try using a "full" $ quoting first, that is, instead of $$ use $func$ or something like that. But if that fails, use a prepared statement just with CREATE FUNCTION trigger_update_dates() RETURNS trigger LANGUAGE plpgsql AS ? And then use setString() to add the body of the function, and execute() the statement. áúàøéê 16/06/14, 16:15, öéèåè Dave Cramer: > You can have a look here > > http://stackoverflow.com/questions/10460542/create-postgresql-trigger-using-jdbc > > I suspect the parser is trying to parse the $$ but it appears you > aren't the first to try to solve this problem > > Dave Cramer > > dave.cramer(at)credativ(dot)ca > http://www.credativ.ca > > > On 16 June 2014 00:41, Coffee GoesToSchool > > > wrote: > > > Hi > > I am unable to create functions via postgres jdbc. It seems that > the driver strips out newlines and semi-colons, resulting in > syntax errors rejected by the db. I'm trying to setup dbdeploy > for the project I'm working on. All db artifacts (tables, > sequence, views) are successfully managed except for triggers. > > db version: postgres (PostgreSQL) 9.3.4 > jdbc version: org.postgresql:postgresql:9.3-1100-jdbc41 > platform: OS X 10.9.3 > > *Error in postgres log:* > ERROR: syntax error at or near "$" at character 89 > STATEMENT: CREATE FUNCTION trigger_update_dates() RETURNS > trigger LANGUAGE plpgsql AS $ BEGIN IF TG_OP = 'INSERT' THEN > NEW.CREATE_DATE := LOCALTIMESTAMP > > *Stacktrace**:* > Caused by: org.postgresql.util.PSQLException: ERROR: syntax error > at or near "$" > Position: 89 > at > org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161) > at > org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890) > at > org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:395) > at org.apache.tools.ant.taskdefs.SQLExec.execSQL(SQLExec.java:773) > at > org.apache.tools.ant.taskdefs.SQLExec.runStatements(SQLExec.java:743) > at > org.apache.tools.ant.taskdefs.SQLExec$Transaction.runTransaction(SQLExec.java:1053) > at > org.apache.tools.ant.taskdefs.SQLExec$Transaction.access$000(SQLExec.java:983) > at org.apache.tools.ant.taskdefs.SQLExec.execute(SQLExec.java:651) > > *Trigger:* > CREATE FUNCTION trigger_update_dates() RETURNS trigger > LANGUAGE plpgsql > AS $$ > BEGIN > IF TG_OP = 'INSERT' THEN > NEW.CREATE_DATE := LOCALTIMESTAMP; > NEW.UPDATE_DATE := LOCALTIMESTAMP; > END IF; > IF TG_OP = 'UPDATE' THEN > NEW.UPDATE_DATE := LOCALTIMESTAMP; > END IF; > RETURN NEW; > END > $$; > > > ---CoffeeGoesToSchool > > > -- > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org > ) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-jdbc > > --------------000407080306080106070009 Content-Type: text/html; charset=windows-1255 Content-Transfer-Encoding: 8bit If the problem is with the $ quoting, you can use plain quoting. After all, the main reason for using $ quoting is to avoid the need to escape all the quotation marks within the function body. And JDBC can do that for you if you use a prepared statement.

I would try using a "full" $ quoting first, that is, instead of $$ use $func$ or something like that. But if that fails, use a prepared statement just with

CREATE FUNCTION trigger_update_dates() RETURNS trigger LANGUAGE plpgsql AS ?

And then use setString() to add the body of the function, and execute() the statement.

áúàøéê 16/06/14, 16:15, öéèåè Dave Cramer:
You can have a look here


I suspect the parser is trying to parse the $$ but it appears you aren't the first to try to solve this problem

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On 16 June 2014 00:41, Coffee GoesToSchool <coffeegoestoschool@gmail.com> wrote:

Hi

I am unable to create functions via postgres jdbc. It seems that the driver strips out newlines and semi-colons, resulting in syntax errors rejected by the db.  I'm trying to setup dbdeploy for the project I'm working on. All db artifacts (tables, sequence, views) are successfully managed except for triggers.

db version:     postgres (PostgreSQL) 9.3.4
jdbc version:  org.postgresql:postgresql:9.3-1100-jdbc41
platform:        OS X 10.9.3

Error in postgres log:
ERROR:  syntax error at or near "$" at character 89
STATEMENT:    CREATE FUNCTION trigger_update_dates() RETURNS trigger LANGUAGE plpgsql AS $ BEGIN IF TG_OP = 'INSERT' THEN NEW.CREATE_DATE := LOCALTIMESTAMP

Stacktrace:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"
  Position: 89
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:395)
at org.apache.tools.ant.taskdefs.SQLExec.execSQL(SQLExec.java:773)
at org.apache.tools.ant.taskdefs.SQLExec.runStatements(SQLExec.java:743)
at org.apache.tools.ant.taskdefs.SQLExec$Transaction.runTransaction(SQLExec.java:1053)
at org.apache.tools.ant.taskdefs.SQLExec$Transaction.access$000(SQLExec.java:983)
at org.apache.tools.ant.taskdefs.SQLExec.execute(SQLExec.java:651)

Trigger:
CREATE FUNCTION trigger_update_dates() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
NEW.CREATE_DATE := LOCALTIMESTAMP;
NEW.UPDATE_DATE := LOCALTIMESTAMP;
END IF;
IF TG_OP = 'UPDATE' THEN
NEW.UPDATE_DATE := LOCALTIMESTAMP;
END IF;
RETURN NEW;
END
$$;


---CoffeeGoesToSchool


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



--------------000407080306080106070009--