You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Jeff Martin <je...@synamic.co.uk> on 2000/07/25 15:31:25 UTC

PATCH: SQLExec

I've made some of the changes recommended. I have removed the setSQL method
and replaced it with addText so the sql task can now be used as <sql>select
* from monkey; select...</sql>.

Added autocommit flag to allow the autocommit to be set on the connection
and SQL warnings are now logged in verbose mode.

Think that's it.



Index: src/main/org/apache/tools/ant/taskdefs/SQLExec.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.j
ava,v
retrieving revision 1.2
diff -r1.2 SQLExec.java
73a74,87
>     /**
>      * Database connection
>      */
>     private Connection conn = null;
> 
>     /**
>      * Autocommit flag. Default value is false
>      */
>     private boolean autocommit=false;
> 
>     /**
>      * SQL statement
>      */
>     private Statement statement = null;
98c112
<     private File inputFile = null;
---
>     private File srcFile = null;
108,109c122,123
<     public void setInputfile(File inputFile) {
<         this.inputFile = inputFile;
---
>     public void setSrc(File srcFile) {
>         this.srcFile = srcFile;
115c129
<     public void setSQL(String sql) {
---
>     public void addText(String sql) {
147a162,168
>      * Set the autocommit flag for the DB connection.
>      */
>     public void setAutocommit(boolean autocommit) {
>         this.autocommit = autocommit;
>     }
>     
>     /**
151d171
<         Connection conn = null;
153c173
<         if (inputFile == null && sqlCommand == null) {
---
>         if (srcFile == null && sqlCommand == null) {
168,169c188,189
<         if (inputFile != null && !inputFile.exists()) {
<             throw new BuildException("Input file does not exist!");
---
>         if (srcFile != null && !srcFile.exists()) {
>             throw new BuildException("Source file does not exist!");
178,180d197
<         String line = "";
<         String sql = "";
<         Statement statement = null;
184a202,203
> 	    conn.setAutoCommit(autocommit);
> 
188c207
<                 execSQL(statement, sqlCommand);
---
>                 runStatements(new StringReader(sqlCommand));
191,192c210,211
<             if (inputFile != null) {
<                 BufferedReader in = new BufferedReader(new
FileReader(inputFile));
---
>             if (srcFile != null) {
>                 runStatements(new FileReader(srcFile));
194,204d212
<                 while ((line=in.readLine()) != null){
<                     if (line.trim().startsWith("//")) continue;
<                     if (line.trim().startsWith("--")) continue;
<  
<                     sql += " " + line;
<                     if (sql.trim().endsWith(";")){
<                         log("SQL: " + sql, Project.MSG_VERBOSE);
<                         execSQL(statement, sql.substring(0,
sql.length()-1));
<                         sql = "";
<                     }
<                 }
211d218
<             log("Failed to execute: " + sql, Project.MSG_ERR);
228a236,264
>     private void runStatements(Reader reader) throws SQLException,
IOException {
>         String sql = "";
>         String line = "";
> 
> 	BufferedReader in = new BufferedReader(reader);
> 
> 	try{
>             while ((line=in.readLine()) != null){
>                 if (line.trim().startsWith("//")) continue;
>                 if (line.trim().startsWith("--")) continue;
>      
>                 sql += " " + line;
>                 if (sql.trim().endsWith(";")){
>                      log("SQL: " + sql, Project.MSG_VERBOSE);
>                      execSQL(sql.substring(0, sql.length()-1));
>                      sql = "";
>                 }
>             }
> 
> 	    // Catch any statements not followed by ;
> 	    if(!sql.trim().equals("")){
> 	    	execSQL(sql);
> 	    }
> 	}catch(SQLException e){
>             log("Failed to execute: " + sql, Project.MSG_ERR);
> 	    throw e;
> 	}
>     }
> 
232c268
<     private void execSQL(Statement statement, String sql) throws
SQLException{
---
>     private void execSQL(String sql) throws SQLException{
235a272,278
> 
> 	    SQLWarning warning = conn.getWarnings();
>             while(warning!=null){
> 	    	log(warning + " sql warnging", Project.MSG_VERBOSE);
> 		warning=warning.getNextWarning();
> 	    }
> 	    conn.clearWarnings();


Re: PATCH: SQLExec

Posted by Stefan Bodewig <bo...@bost.de>.
>>>>> "JM" == Jeff Martin <je...@synamic.co.uk> writes:

 JM> Think that's it.

Pending documentation, yes 8^). Want to submit it as well?

I've made some minor modifications to your patch. One takes into
account that addText might be called multiple times. 

The other modification is that the code only call Connection.commit or
rollback if not in autocommit mode. There are some ($%&!/ยง(%) JDBC
drivers (namely Interbase again, but I think DB2 is the same) that
will throw an exception if you try to commit an autocommit
transaction.

Stefan