|
Here's a patch to to the SQLExec.java which adds
the ability to pipe the results of SQL statements executed with <sql/> to
a file or the screen. It simply prints the output as comma delimited values,
optionally printing the headers. I wrote it thinking of using it to write
queries which generate sql statements which can then be fed back into
<sql/> directive a second time.
Here is sample usage:
<sql
driver="${db.driver}" url="" userid="${db.userid}" password="${db.password}" print="yes" showheaders="no" output="output.txt"> select count(*) from footable
</sql>
Julian.
|
Index: SQLExec.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v
retrieving revision 1.5
diff -u -r1.5 SQLExec.java
--- SQLExec.java 2000/08/02 12:18:25 1.5
+++ SQLExec.java 2000/09/22 08:17:25
@@ -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();
+ }
+ }
}
}
