conor 00/11/01 20:39:57
Modified: src/main/org/apache/tools/ant/taskdefs SQLExec.java
Log:
Make SQLExec print its output at the task level rather than the single
statement level. All output goes to the output file.
Revision Changes Path
1.12 +64 -64
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
Index: SQLExec.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- SQLExec.java 2000/11/01 05:59:16 1.11
+++ SQLExec.java 2000/11/02 04:39:56 1.12
@@ -360,16 +360,30 @@
statement = conn.createStatement();
- // Process all transactions
- for (Enumeration e = transactions.elements();
- e.hasMoreElements();) {
- ((Transaction) e.nextElement()).runTransaction();
- if (!autocommit) {
- log("Commiting transaction", Project.MSG_VERBOSE);
- conn.commit();
+
+ 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)));
+ }
+
+ // Process all transactions
+ for (Enumeration e = transactions.elements();
+ e.hasMoreElements();) {
+
+ ((Transaction) e.nextElement()).runTransaction(out);
+ if (!autocommit) {
+ log("Commiting transaction", Project.MSG_VERBOSE);
+ conn.commit();
+ }
}
}
-
+ finally {
+ if (out != null && out != System.out) {
+ out.close();
+ }
+ }
} catch(IOException e){
if (!autocommit && conn != null && onError.equals("abort")) {
try {
@@ -401,7 +415,7 @@
" SQL statements executed successfully");
}
- protected void runStatements(Reader reader) throws SQLException,
IOException {
+ protected void runStatements(Reader reader, PrintStream out) throws
SQLException, IOException {
String sql = "";
String line = "";
@@ -422,14 +436,14 @@
if (sql.endsWith(";")){
log("SQL: " + sql, Project.MSG_VERBOSE);
- execSQL(sql.substring(0, sql.length()-1));
+ execSQL(sql.substring(0, sql.length()-1), out);
sql = "";
}
}
// Catch any statements not followed by ;
if(!sql.equals("")){
- execSQL(sql);
+ execSQL(sql, out);
}
}catch(SQLException e){
throw e;
@@ -481,7 +495,7 @@
/**
* Exec the sql statement.
*/
- protected void execSQL(String sql) throws SQLException {
+ protected void execSQL(String sql, PrintStream out) throws SQLException {
// Check and ignore empty statements
if ("".equals(sql.trim())) return;
@@ -493,7 +507,7 @@
}
if (print) {
- printResults();
+ printResults(out);
}
SQLWarning warning = conn.getWarnings();
@@ -514,61 +528,47 @@
/**
* print any results in the statement.
*/
- protected void printResults() throws java.sql.SQLException {
+ protected void printResults(PrintStream out) throws
java.sql.SQLException {
ResultSet rs = null;
- 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)));
- }
- 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.setLength(0);
+ 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(",");
}
- 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);
+ line.append(md.getColumnName(columnCount));
+ out.println(line);
+ line.setLength(0);
+ }
+ 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(",");
}
- out.println(line);
- line.setLength(0);
+ line.append(columnValue);
}
+ out.println(line);
+ line.setLength(0);
}
}
- while (statement.getMoreResults());
- }
- catch (IOException ioe) {
- throw new BuildException("Error writing " +
output.getAbsolutePath(), ioe, location);
- }
- finally {
- if (out != null && out != System.out) {
- out.close();
- }
}
+ while (statement.getMoreResults());
+ out.println();
}
/**
@@ -599,16 +599,16 @@
this.tSqlCommand += sql;
}
- private void runTransaction() throws IOException, SQLException {
+ private void runTransaction(PrintStream out) throws IOException,
SQLException {
if (tSqlCommand.length() != 0) {
log("Executing commands", Project.MSG_INFO);
- runStatements(new StringReader(tSqlCommand));
+ runStatements(new StringReader(tSqlCommand), out);
}
if (tSrcFile != null) {
log("Executing file: " + tSrcFile.getAbsolutePath(),
Project.MSG_INFO);
- runStatements(new FileReader(tSrcFile));
+ runStatements(new FileReader(tSrcFile), out);
}
}
}