Re: SQL Script using JDBC - Mailing list pgsql-jdbc

From Csaba Nagy
Subject Re: SQL Script using JDBC
Date
Msg-id 1139220066.24321.188.camel@coppola.muc.ecircle.de
Whole thread Raw
In response to SQL Script using JDBC  (Ravi Chaddha <ravi_chaddha@yahoo.co.in>)
List pgsql-jdbc
Ravi,

AFAIK, there's no easy way to execute an SQL script directly through
JDBC, you will have to write your own parser to split the script in
individual statements and execute them one by one.

Actually the splitting in statements is the biggest challenge here, it's
not as simple as for e.g. using a regular expression. We do this kind of
stuff here (i.e. executing SQL scripts through JDBC, with the added
condition that the same script must execute fine in psql), and first we
used the simple rule of "end of statement = a semicolon at the end of
the line". This works fine until your script doesn't contain stored
procedures, in that case the stored procedure body is to be sent as one
statement, but it surely will contain lines ending in semicolon. So
either you start tracking quoted strings, or you use some other
statement delimiter. You could use an SQL comment as statement
delimiter, something like:

-- execute

This will be ignored by psql, and used by our parser code to execute the
current statement...

We actually still use the "line ended with semicolon" delimiter, and put
some comments at the end of line in stored procedures to not have the
semicolons at line ends... kind of awkward, but it works...

HTH,
Csaba.

On Mon, 2006-02-06 at 10:41, Ravi Chaddha wrote:
> Please let me know how can we execute a SQL file using
> JDBC from a remote computer. The script creates
> tables, functions in PostgreSQL database.
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>                http://archives.postgresql.org


pgsql-jdbc by date:

Previous
From: Ravi Chaddha
Date:
Subject: SQL Script using JDBC
Next
From: Csaba Nagy
Date:
Subject: Re: SQL Script using JDBC