From 740ed5fbfbb7f59db596062f9e853fba40a65722 Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Wed, 16 Jul 2003 17:29:36 +0000 Subject: [PATCH] Allow Result Sets and Errors to be processed properly in SQL task with multiple statements PR: 21594 Submitted by: Jeff Bohanek (jeff dot bohanek at msi dot com) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274837 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 + .../apache/tools/ant/taskdefs/SQLExec.java | 101 +++++++++++------- 2 files changed, 67 insertions(+), 37 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index ab44cbcf3..3aa6b9f9c 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -356,6 +356,9 @@ Other changes: * has a new attribute to control escape processing. +* is able to display properly several resultsets if you are + running a compound sql statement. Bugzilla Report 21594. + * will invoke oldjavah on JDK 1.4.2. Bugzilla Report 18667. * A new selector has been added, that selects files diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index a981e4bb2..42311bf4c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -527,13 +527,43 @@ public class SQLExec extends JDBCTask { try { totalSql++; log("SQL: " + sql, Project.MSG_VERBOSE); - if (!statement.execute(sql)) { - log(statement.getUpdateCount() + " rows affected", - Project.MSG_VERBOSE); - } else { - if (print) { - printResults(out); + + boolean ret; + int updateCount = 0, updateCountTotal = 0; + ResultSet resultSet = null; + + ret = statement.execute(sql); + updateCount = statement.getUpdateCount(); + resultSet = statement.getResultSet(); + do + { + if (!ret) + { + if (updateCount != -1) + { + updateCountTotal += updateCount; + } } + else + { + if (print) + { + printResults(out); + } + } + ret = statement.getMoreResults(); + updateCount = statement.getUpdateCount(); + resultSet = statement.getResultSet(); + } while ((resultSet != null) || (updateCount != -1)); + + log(updateCountTotal + " rows affected", + Project.MSG_VERBOSE); + + if (print) + { + StringBuffer line = new StringBuffer(); + line.append(updateCountTotal + " rows affected"); + out.println(line); } SQLWarning warning = conn.getWarnings(); @@ -557,43 +587,40 @@ public class SQLExec extends JDBCTask { */ protected void printResults(PrintStream out) throws java.sql.SQLException { ResultSet rs = null; - do { - rs = statement.getResultSet(); - 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(); + rs = statement.getResultSet(); + 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(","); } - while (rs.next()) { - boolean first = true; - for (int col = 1; col <= columnCount; col++) { - String columnValue = rs.getString(col); - if (columnValue != null) { - columnValue = columnValue.trim(); - } + 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 (first) { - first = false; - } else { - line.append(","); - } - line.append(columnValue); + if (first) { + first = false; + } else { + line.append(","); } - out.println(line); - line = new StringBuffer(); + line.append(columnValue); } + out.println(line); + line = new StringBuffer(); } } - while (statement.getMoreResults()); out.println(); }