Browse Source

Allow more control over CSV output. PR 35627.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@676560 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
f97926a108
3 changed files with 116 additions and 5 deletions
  1. +4
    -0
      WHATSNEW
  2. +23
    -1
      docs/manual/CoreTasks/sql.html
  3. +89
    -4
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 4
- 0
WHATSNEW View File

@@ -155,6 +155,10 @@ Other changes:

* 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
=============================================



+ 23
- 1
docs/manual/CoreTasks/sql.html View File

@@ -226,10 +226,32 @@ and <b>abort</b> execution and transaction and fail task.</p>
<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>


+ 89
- 4
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -211,6 +211,35 @@ public class SQLExec extends JDBCTask {
*/
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
@@ -441,6 +470,43 @@ public class SQLExec extends JDBCTask {
treatWarningsAsErrors = b;
}

/**
* 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 @@ public class SQLExec extends JDBCTask {
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 @@ public class SQLExec extends JDBCTask {
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();
}

/*


Loading…
Cancel
Save