Browse Source

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
master
Antoine Levy-Lambert 22 years ago
parent
commit
740ed5fbfb
2 changed files with 67 additions and 37 deletions
  1. +3
    -0
      WHATSNEW
  2. +64
    -37
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 3
- 0
WHATSNEW View File

@@ -356,6 +356,9 @@ Other changes:

* <sql> has a new attribute to control escape processing.

* <sql> is able to display properly several resultsets if you are
running a compound sql statement. Bugzilla Report 21594.

* <javah> will invoke oldjavah on JDK 1.4.2. Bugzilla Report 18667.

* A new <containsregexp> selector has been added, that selects files


+ 64
- 37
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -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();
}



Loading…
Cancel
Save