conor 00/09/24 02:32:00
Modified: src/main/org/apache/tools/ant/taskdefs SQLExec.java
Log:
Allow sql task to write results to a file.
Submitted by: Julian M. Savage <[EMAIL PROTECTED]>
Revision Changes Path
1.6 +86 -1
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SQLExec.java 2000/08/02 12:18:25 1.5
+++ SQLExec.java 2000/09/24 09:31:59 1.6
@@ -116,6 +116,21 @@
* SQL input command
*/
private String sqlCommand = "";
+
+ /**
+ * Print SQL results.
+ */
+ private boolean print = false;
+
+ /**
+ * Print header columns.
+ */
+ private boolean showheaders = true;
+
+ /**
+ * Results Output file.
+ */
+ private File output = null;
/**
* Set the name of the sql file to be run.
@@ -165,6 +180,27 @@
public void setAutocommit(boolean autocommit) {
this.autocommit = autocommit;
}
+
+ /**
+ * Set the print flag.
+ */
+ public void setPrint(boolean print) {
+ this.print = print;
+ }
+
+ /**
+ * Set the showheaders flag.
+ */
+ public void setShowheaders(boolean showheaders) {
+ this.showheaders = showheaders;
+ }
+
+ /**
+ * Set the output file.
+ */
+ public void setOutput(File output) {
+ this.output = output;
+ }
/**
* Load the sql file and then execute it
@@ -282,18 +318,67 @@
/**
* Exec the sql statement.
*/
- protected void execSQL(String sql) throws SQLException{
+ protected void execSQL(String sql) throws SQLException {
if (!statement.execute(sql)) {
log(statement.getUpdateCount()+" rows affected",
Project.MSG_VERBOSE);
}
+ if (print) {
+ printResults();
+ }
+
SQLWarning warning = conn.getWarnings();
while(warning!=null){
log(warning + " sql warning", Project.MSG_VERBOSE);
warning=warning.getNextWarning();
}
conn.clearWarnings();
+ }
+
+ /**
+ * print any results in the statement.
+ */
+ protected void printResults() throws java.sql.SQLException {
+ ResultSet rs = null;
+ PrintStream out = System.out;
+ try {
+ if (output != null) {
+ out = new PrintStream(new BufferedOutputStream(new
FileOutputStream(output)));
+ }
+ while ((rs = statement.getResultSet()) != null) {
+ 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);
+ }
+ while (rs.next()) {
+ for (int col = 1; col < columnCount; col++) {
+ line.append(rs.getString(col).trim());
+ line.append(",");
+ }
+ line.append(rs.getString(columnCount).trim());
+ out.println(line);
+ line.setLength(0);
+ }
+ statement.getMoreResults();
+ }
+ }
+ catch (IOException ioe) {
+ throw new BuildException("Error writing " +
output.getAbsolutePath(), ioe, location);
+ }
+ finally {
+ if (out != null) {
+ out.close();
+ }
+ }
}
}