Browse Source

allow SQLWarnings to stop <sql>. PR 41836.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@675968 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
eaffd9d71b
3 changed files with 43 additions and 16 deletions
  1. +4
    -0
      WHATSNEW
  2. +9
    -0
      docs/manual/CoreTasks/sql.html
  3. +30
    -16
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 4
- 0
WHATSNEW View File

@@ -146,6 +146,10 @@ Other changes:
* A new showWarnings attribute of <sql> allows warnings to be logged.
Bugzilla Report 41836.

* A new treatWarningsAsErrors attribute of <sql> can be used to fail
a build if a warning occurs.
Bugzilla Report 41836.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 9
- 0
docs/manual/CoreTasks/sql.html View File

@@ -222,6 +222,15 @@ and <b>abort</b> execution and transaction and fail task.</p>
<td width="10%" valign="top">No, default <em>false</em></td>
</tr>

<tr>
<td width="12%" valign="top">treatWarningsAsErrors</td>
<td width="78%" valign="top">If true, SQLWarnings will be treated
like errors - and the logic selected via the onError attribute
applies.
<em>Since Ant 1.8.0</em>.<br/>
<td width="10%" valign="top">No, default <em>false</em></td>
</tr>

</table>

<h3>Parameters specified as nested elements</h3>


+ 30
- 16
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -211,6 +211,12 @@ public class SQLExec extends JDBCTask {
*/
private boolean showWarnings = false;

/**
* Whether a warning is an error - in which case onError aplies.
* @since Ant 1.8.0
*/
private boolean treatWarningsAsErrors = false;

/**
* Set the name of the SQL file to be run.
* Required unless statements are enclosed in the build file
@@ -427,6 +433,14 @@ public class SQLExec extends JDBCTask {
showWarnings = b;
}

/**
* Whether a warning is an error - in which case onError aplies.
* @since Ant 1.8.0
*/
public void setTreatWarningsAsErrors(boolean b) {
treatWarningsAsErrors = b;
}

/**
* Load the sql file and then execute it
* @throws BuildException on error.
@@ -613,10 +627,7 @@ public class SQLExec extends JDBCTask {
}
if (ret) {
resultSet = getStatement().getResultSet();
if (showWarnings) {
printWarnings(resultSet.getWarnings(),
Project.MSG_WARN);
}
printWarnings(resultSet.getWarnings(), false);
resultSet.clearWarnings();
if (print) {
printResults(resultSet, out);
@@ -626,9 +637,7 @@ public class SQLExec extends JDBCTask {
updateCount = getStatement().getUpdateCount();
} while (ret || updateCount != -1);

if (showWarnings) {
printWarnings(getStatement().getWarnings(), Project.MSG_WARN);
}
printWarnings(getStatement().getWarnings(), false);
getStatement().clearWarnings();

log(updateCountTotal + " rows affected", Project.MSG_VERBOSE);
@@ -637,8 +646,7 @@ public class SQLExec extends JDBCTask {
out.println(updateCountTotal + " rows affected");
}
SQLWarning warning = getConnection().getWarnings();
printWarnings(warning, showWarnings
? Project.MSG_WARN : Project.MSG_VERBOSE);
printWarnings(warning, true);
getConnection().clearWarnings();
goodSql++;
} catch (SQLException e) {
@@ -707,9 +715,7 @@ public class SQLExec extends JDBCTask {
printValue(rs, col, out);
}
out.println();
if (showWarnings) {
printWarnings(rs.getWarnings(), Project.MSG_WARN);
}
printWarnings(rs.getWarnings(), false);
}
}
}
@@ -917,10 +923,18 @@ public class SQLExec extends JDBCTask {
}
}

private void printWarnings(SQLWarning warning, int level) {
while (warning != null) {
log(warning + " sql warning", level);
warning = warning.getNextWarning();
private void printWarnings(SQLWarning warning, boolean force)
throws SQLException {
SQLWarning initialWarning = warning;
if (showWarnings || force) {
while (warning != null) {
log(warning + " sql warning",
showWarnings ? Project.MSG_WARN : Project.MSG_VERBOSE);
warning = warning.getNextWarning();
}
}
if (treatWarningsAsErrors && initialWarning != null) {
throw initialWarning;
}
}
}

Loading…
Cancel
Save