Author: bodewig
Date: Mon Jul 14 04:59:12 2008
New Revision: 676560
URL: http://svn.apache.org/viewvc?rev=676560&view=rev
Log:
Allow more control over CSV output. PR 35627.
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/docs/manual/CoreTasks/sql.html
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=676560&r1=676559&r2=676560&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Mon Jul 14 04:59:12 2008
@@ -155,6 +155,10 @@
* Ant now supports local properties. Bugzilla report 23942.
+ * <sql>'s CSV output can be controlled via the new attributes
+ csvColumnSeparator and csvQuoteCharacter.
+ Bugzilla report 35627.
+
Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================
Modified: ant/core/trunk/docs/manual/CoreTasks/sql.html
URL:
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/sql.html?rev=676560&r1=676559&r2=676560&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/sql.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/sql.html Mon Jul 14 04:59:12 2008
@@ -226,10 +226,32 @@
<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/>
+ <em>Since Ant 1.8.0</em>.</td>
<td width="10%" valign="top">No, default <em>false</em></td>
</tr>
+<tr>
+ <td width="12%" valign="top">csvColumnSeparator</td>
+ <td width="78%" valign="top">The column separator used when printing
+ the results.
+ <em>Since Ant 1.8.0</em>.</td>
+ <td width="10%" valign="top">No, default <em>','</em></td>
+</tr>
+
+<tr>
+ <td width="12%" valign="top">csvQuoteCharacter</td>
+ <td width="78%" valign="top">The character used to quote column
+ values.<br/>
+ If set, columns that contain either the column separator or the
+ quote character itself will be surrounded by the quote character.
+ The quote character itself will be doubled if it appears inside of
+ the column's value.<br/>
+ <b>Note:</b> BLOB values will never be quoted.
+ <em>Since Ant 1.8.0</em>.</td>
+ <td width="10%" valign="top">No, default is not set (i.e. no quoting
+ ever occurs)</td>
+</tr>
+
</table>
<h3>Parameters specified as nested elements</h3>
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java?rev=676560&r1=676559&r2=676560&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java Mon Jul
14 04:59:12 2008
@@ -212,6 +212,35 @@
private boolean showWarnings = false;
/**
+ * The column separator used when printing the results.
+ *
+ * <p>Defaults to ","</p>
+ *
+ * @since Ant 1.8.0
+ */
+ private String csvColumnSep = ",";
+
+ /**
+ * The character used to quote column values.
+ *
+ * <p>If set, columns that contain either the column separator or
+ * the quote character itself will be surrounded by the quote
+ * character. The quote character itself will be doubled if it
+ * appears inside of the column's value.</p>
+ *
+ * <p>If this value is not set (the default), no column values
+ * will be quoted, not even if they contain the column
+ * separator.</p>
+ *
+ * <p><b>Note:<b> BLOB values will never be quoted.</p>
+ *
+ * <p>Defaults to "not set"</p>
+ *
+ * @since Ant 1.8.0
+ */
+ private String csvQuoteChar = null;
+
+ /**
* Whether a warning is an error - in which case onError aplies.
* @since Ant 1.8.0
*/
@@ -442,6 +471,43 @@
}
/**
+ * The column separator used when printing the results.
+ *
+ * <p>Defaults to ","</p>
+ *
+ * @since Ant 1.8.0
+ */
+ public void setCsvColumnSeparator(String s) {
+ csvColumnSep = s;
+ }
+
+ /**
+ * The character used to quote column values.
+ *
+ * <p>If set, columns that contain either the column separator or
+ * the quote character itself will be surrounded by the quote
+ * character. The quote character itself will be doubled if it
+ * appears inside of the column's value.</p>
+ *
+ * <p>If this value is not set (the default), no column values
+ * will be quoted, not even if they contain the column
+ * separator.</p>
+ *
+ * <p><b>Note:<b> BLOB values will never be quoted.</p>
+ *
+ * <p>Defaults to "not set"</p>
+ *
+ * @since Ant 1.8.0
+ */
+ public void setCsvQuoteCharacter(String s) {
+ if (s != null && s.length() > 1) {
+ throw new BuildException("The quote character must be a single"
+ + " character.");
+ }
+ csvQuoteChar = s;
+ }
+
+ /**
* Load the sql file and then execute it
* @throws BuildException on error.
*/
@@ -703,15 +769,15 @@
if (showheaders) {
out.print(md.getColumnName(1));
for (int col = 2; col <= columnCount; col++) {
- out.write(',');
- out.print(md.getColumnName(col));
+ out.print(csvColumnSep);
+ out.print(maybeQuote(md.getColumnName(col)));
}
out.println();
}
while (rs.next()) {
printValue(rs, 1, out);
for (int col = 2; col <= columnCount; col++) {
- out.write(',');
+ out.print(csvColumnSep);
printValue(rs, col, out);
}
out.println();
@@ -727,8 +793,27 @@
if (rawBlobs && rs.getMetaData().getColumnType(col) == Types.BLOB) {
new StreamPumper(rs.getBlob(col).getBinaryStream(), out).run();
} else {
- out.print(rs.getString(col));
+ out.print(maybeQuote(rs.getString(col)));
+ }
+ }
+
+ private String maybeQuote(String s) {
+ if (csvQuoteChar == null || s == null
+ || (s.indexOf(csvColumnSep) == -1 && s.indexOf(csvQuoteChar) == -1)
+ ) {
+ return s;
+ }
+ StringBuffer sb = new StringBuffer(csvQuoteChar);
+ int len = s.length();
+ char q = csvQuoteChar.charAt(0);
+ for (int i = 0; i < len; i++) {
+ char c = s.charAt(i);
+ if (c == q) {
+ sb.append(q);
+ }
+ sb.append(c);
}
+ return sb.append(csvQuoteChar).toString();
}
/*