Browse Source

formatting; refactoring

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@551166 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 18 years ago
parent
commit
f8e71b5033
1 changed files with 43 additions and 72 deletions
  1. +43
    -72
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 43
- 72
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -15,7 +15,6 @@
* limitations under the License. * limitations under the License.
* *
*/ */

package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
@@ -387,15 +386,15 @@ public class SQLExec extends JDBCTask {
if (srcFile == null && sqlCommand.length() == 0 if (srcFile == null && sqlCommand.length() == 0
&& resources.size() == 0) { && resources.size() == 0) {
if (transactions.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 " + "transactions or sql statement "
+ "must be set!", getLocation()); + "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 // deal with the resources
@@ -422,13 +421,9 @@ public class SQLExec extends JDBCTask {
PrintStream out = System.out; PrintStream out = System.out;
try { try {
if (output != null) { 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 // Process all transactions
@@ -442,9 +437,7 @@ public class SQLExec extends JDBCTask {
} }
} }
} finally { } finally {
if (out != null && out != System.out) {
out.close();
}
FileUtils.close(out);
} }
} catch (IOException e) { } catch (IOException e) {
closeQuietly(); closeQuietly();
@@ -457,6 +450,10 @@ public class SQLExec extends JDBCTask {
if (statement != null) { if (statement != null) {
statement.close(); statement.close();
} }
} catch (SQLException ex) {
// ignore
}
try {
if (conn != null) { if (conn != null) {
conn.close(); conn.close();
} }
@@ -465,8 +462,7 @@ public class SQLExec extends JDBCTask {
} }
} }


log(goodSql + " of " + totalSql
+ " SQL statements executed successfully");
log(goodSql + " of " + totalSql + " SQL statements executed successfully");
} finally { } finally {
transactions = savedTransaction; transactions = savedTransaction;
sqlCommand = savedSqlCommand; sqlCommand = savedSqlCommand;
@@ -510,29 +506,17 @@ public class SQLExec extends JDBCTask {
} }
} }


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 // SQL defines "--" as a comment to EOL
// and in Oracle it may contain a hint // and in Oracle it may contain a hint
// so we cannot just remove it, instead we must end it // 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(), ""); sql.replace(0, sql.length(), "");
} }
} }
@@ -542,7 +526,6 @@ public class SQLExec extends JDBCTask {
} }
} }



/** /**
* Exec the sql statement. * Exec the sql statement.
* @param sql the SQL statement to execute * @param sql the SQL statement to execute
@@ -571,10 +554,8 @@ public class SQLExec extends JDBCTask {
if (updateCount != -1) { if (updateCount != -1) {
updateCountTotal += updateCount; updateCountTotal += updateCount;
} }
} else {
if (print) {
printResults(resultSet, out);
}
} else if (print) {
printResults(resultSet, out);
} }
ret = statement.getMoreResults(); ret = statement.getMoreResults();
if (ret) { if (ret) {
@@ -583,13 +564,11 @@ public class SQLExec extends JDBCTask {
} }
} while (ret); } while (ret);


log(updateCountTotal + " rows affected",
Project.MSG_VERBOSE);
log(updateCountTotal + " rows affected", Project.MSG_VERBOSE);


if (print && showtrailers) { if (print && showtrailers) {
out.println(updateCountTotal + " rows affected"); out.println(updateCountTotal + " rows affected");
} }

SQLWarning warning = conn.getWarnings(); SQLWarning warning = conn.getWarnings();
while (warning != null) { while (warning != null) {
log(warning + " sql warning", Project.MSG_VERBOSE); log(warning + " sql warning", Project.MSG_VERBOSE);
@@ -605,7 +584,11 @@ public class SQLExec extends JDBCTask {
log(e.toString(), Project.MSG_ERR); log(e.toString(), Project.MSG_ERR);
} finally { } finally {
if (resultSet != null) { if (resultSet != null) {
resultSet.close();
try {
resultSet.close();
} catch (SQLException e) {
//ignore
}
} }
} }
} }
@@ -636,39 +619,28 @@ public class SQLExec extends JDBCTask {
* @throws SQLException on SQL problems. * @throws SQLException on SQL problems.
* @since Ant 1.6.3 * @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) { if (rs != null) {
log("Processing new result set.", Project.MSG_VERBOSE); log("Processing new result set.", Project.MSG_VERBOSE);
ResultSetMetaData md = rs.getMetaData(); ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); 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(); out.println();
@@ -774,8 +746,7 @@ public class SQLExec extends JDBCTask {
Reader reader = null; Reader reader = null;
try { try {
is = tSrcResource.getInputStream(); is = tSrcResource.getInputStream();
reader =
(encoding == null) ? new InputStreamReader(is)
reader = (encoding == null) ? new InputStreamReader(is)
: new InputStreamReader(is, encoding); : new InputStreamReader(is, encoding);
runStatements(reader, out); runStatements(reader, out);
} finally { } finally {


Loading…
Cancel
Save