ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r551166 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
Date Wed, 27 Jun 2007 14:08:25 GMT
Author: mbenson
Date: Wed Jun 27 07:08:24 2007
New Revision: 551166

URL: http://svn.apache.org/viewvc?view=rev&rev=551166
Log:
formatting; refactoring

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java?view=diff&rev=551166&r1=551165&r2=551166
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java Wed Jun 27 07:08:24
2007
@@ -15,7 +15,6 @@
  *  limitations under the License.
  *
  */
-
 package org.apache.tools.ant.taskdefs;
 
 import org.apache.tools.ant.BuildException;
@@ -387,15 +386,15 @@
             if (srcFile == null && sqlCommand.length() == 0
                 && resources.size() == 0) {
                 if (transactions.size() == 0) {
-                    throw new BuildException("Source file or resource "
-                                             + "collection, "
+                    throw new BuildException("Source file or resource collection, "
                                              + "transactions or sql statement "
                                              + "must be set!", getLocation());
                 }
             }
 
-            if (srcFile != null && !srcFile.exists()) {
-                throw new BuildException("Source file does not exist!", getLocation());
+            if (srcFile != null && !srcFile.isFile()) {
+                throw new BuildException("Source file " + srcFile
+                        + " is not a file!", getLocation());
             }
 
             // deal with the resources
@@ -422,13 +421,9 @@
                 PrintStream out = System.out;
                 try {
                     if (output != null) {
-                        log("Opening PrintStream to output file " + output,
-                            Project.MSG_VERBOSE);
-                        out = new PrintStream(
-                                  new BufferedOutputStream(
-                                      new FileOutputStream(output
-                                                           .getAbsolutePath(),
-                                                           append)));
+                        log("Opening PrintStream to output file " + output, Project.MSG_VERBOSE);
+                        out = new PrintStream(new BufferedOutputStream(
+                                new FileOutputStream(output.getAbsolutePath(), append)));
                     }
 
                     // Process all transactions
@@ -442,9 +437,7 @@
                         }
                     }
                 } finally {
-                    if (out != null && out != System.out) {
-                        out.close();
-                    }
+                    FileUtils.close(out);
                 }
             } catch (IOException e) {
                 closeQuietly();
@@ -457,6 +450,10 @@
                     if (statement != null) {
                         statement.close();
                     }
+                } catch (SQLException ex) {
+                    // ignore
+                }
+                try {
                     if (conn != null) {
                         conn.close();
                     }
@@ -465,8 +462,7 @@
                 }
             }
 
-            log(goodSql + " of " + totalSql
-                + " SQL statements executed successfully");
+            log(goodSql + " of " + totalSql + " SQL statements executed successfully");
         } finally {
             transactions = savedTransaction;
             sqlCommand = savedSqlCommand;
@@ -510,29 +506,17 @@
                 }
             }
 
-            if (!keepformat) {
-                sql.append(" ");
-                sql.append(line);
-            } else {
-                sql.append("\n");
-                sql.append(line);
-            }
+            sql.append(keepformat ? "\n" : " ").append(line);
 
             // SQL defines "--" as a comment to EOL
             // and in Oracle it may contain a hint
             // so we cannot just remove it, instead we must end it
-            if (!keepformat) {
-                if (line.indexOf("--") >= 0) {
-                    sql.append("\n");
-                }
+            if (!keepformat && line.indexOf("--") >= 0) {
+                sql.append("\n");
             }
-            if ((delimiterType.equals(DelimiterType.NORMAL)
-                 && StringUtils.endsWith(sql, delimiter))
-                ||
-                (delimiterType.equals(DelimiterType.ROW)
-                 && line.equals(delimiter))) {
-                execSQL(sql.substring(0, sql.length() - delimiter.length()),
-                        out);
+            if ((delimiterType.equals(DelimiterType.NORMAL) && StringUtils.endsWith(sql,
delimiter))
+                    || (delimiterType.equals(DelimiterType.ROW) && line.equals(delimiter)))
{
+                execSQL(sql.substring(0, sql.length() - delimiter.length()), out);
                 sql.replace(0, sql.length(), "");
             }
         }
@@ -542,7 +526,6 @@
         }
     }
 
-
     /**
      * Exec the sql statement.
      * @param sql the SQL statement to execute
@@ -571,10 +554,8 @@
                     if (updateCount != -1) {
                         updateCountTotal += updateCount;
                     }
-                } else {
-                    if (print) {
-                        printResults(resultSet, out);
-                    }
+                } else if (print) {
+                    printResults(resultSet, out);
                 }
                 ret = statement.getMoreResults();
                 if (ret) {
@@ -583,13 +564,11 @@
                 }
             } while (ret);
 
-            log(updateCountTotal + " rows affected",
-                Project.MSG_VERBOSE);
+            log(updateCountTotal + " rows affected", Project.MSG_VERBOSE);
 
             if (print && showtrailers) {
                 out.println(updateCountTotal + " rows affected");
             }
-
             SQLWarning warning = conn.getWarnings();
             while (warning != null) {
                 log(warning + " sql warning", Project.MSG_VERBOSE);
@@ -605,7 +584,11 @@
             log(e.toString(), Project.MSG_ERR);
         } finally {
             if (resultSet != null) {
-                resultSet.close();
+                try {
+                    resultSet.close();
+                } catch (SQLException e) {
+                    //ignore
+                }
             }
         }
     }
@@ -636,39 +619,28 @@
      * @throws SQLException on SQL problems.
      * @since Ant 1.6.3
      */
-    protected void printResults(ResultSet rs, PrintStream out)
-        throws SQLException {
+    protected void printResults(ResultSet rs, PrintStream out) throws SQLException {
         if (rs != null) {
             log("Processing new result set.", Project.MSG_VERBOSE);
             ResultSetMetaData md = rs.getMetaData();
             int columnCount = md.getColumnCount();
-            StringBuffer line = new StringBuffer();
-            if (showheaders) {
-                for (int col = 1; col < columnCount; col++) {
-                     line.append(md.getColumnName(col));
-                     line.append(",");
-                }
-                line.append(md.getColumnName(columnCount));
-                out.println(line);
-                line = new StringBuffer();
-            }
-            while (rs.next()) {
-                boolean first = true;
-                for (int col = 1; col <= columnCount; col++) {
-                    String columnValue = rs.getString(col);
-                    if (columnValue != null) {
-                        columnValue = columnValue.trim();
+            if (columnCount > 0) {
+                if (showheaders) {
+                    out.print(md.getColumnName(1));
+                    for (int col = 2; col <= columnCount; col++) {
+                         out.write(',');
+                         out.print(md.getColumnName(col));
                     }
-
-                    if (first) {
-                        first = false;
-                    } else {
-                        line.append(",");
+                    out.println();
+                }
+                while (rs.next()) {
+                    out.print(rs.getString(1));
+                    for (int col = 2; col <= columnCount; col++) {
+                        out.write(',');
+                        out.print(rs.getString(col));
                     }
-                    line.append(columnValue);
+                    out.println();
                 }
-                out.println(line);
-                line = new StringBuffer();
             }
         }
         out.println();
@@ -774,8 +746,7 @@
                 Reader reader = null;
                 try {
                     is = tSrcResource.getInputStream();
-                    reader =
-                        (encoding == null) ? new InputStreamReader(is)
+                    reader = (encoding == null) ? new InputStreamReader(is)
                         : new InputStreamReader(is, encoding);
                     runStatements(reader, out);
                 } finally {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message