From a20c41951b850644b2b2cc3cbf17c696e6e0529c Mon Sep 17 00:00:00 2001 From: Francesco Steccanella Date: Mon, 6 Feb 2017 10:57:51 +0100 Subject: [PATCH] Added option to force the csv quote char --- .../org/apache/tools/ant/taskdefs/SQLExec.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 6d1e5148c..d91356665 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -273,6 +273,11 @@ public class SQLExec extends JDBCTask { */ private String rowCountProperty = null; + /** + * The name of the property to force the csv quote character + */ + private boolean forceCsvQuoteChar = false; + /** * Set the name of the SQL file to be run. * Required unless statements are enclosed in the build file @@ -586,6 +591,13 @@ public class SQLExec extends JDBCTask { this.rowCountProperty = rowCountProperty; } + /** + * Force the csv quote character + */ + public void setForceCsvQuoteChar(boolean forceCsvQuoteChar) { + this.forceCsvQuoteChar = forceCsvQuoteChar; + } + /** * Load the sql file and then execute it * @throws BuildException on error. @@ -878,7 +890,7 @@ public class SQLExec extends JDBCTask { int columnCount = md.getColumnCount(); if (columnCount > 0) { if (showheaders) { - out.print(md.getColumnName(1)); + out.print(maybeQuote(md.getColumnName(1))); for (int col = 2; col <= columnCount; col++) { out.print(csvColumnSep); out.print(maybeQuote(md.getColumnName(col))); @@ -912,9 +924,7 @@ public class SQLExec extends JDBCTask { } private String maybeQuote(String s) { - if (csvQuoteChar == null || s == null - || (s.indexOf(csvColumnSep) == -1 && s.indexOf(csvQuoteChar) == -1) - ) { + if (csvQuoteChar == null || s == null || (!forceCsvQuoteChar && s.indexOf(csvColumnSep) == -1 && s.indexOf(csvQuoteChar) == -1)) { return s; } StringBuffer sb = new StringBuffer(csvQuoteChar);