diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index fbdacd3afd..3849f43800 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -490,12 +490,42 @@ public interface Result extends List, Attachable { /** * Get a simple formatted representation of this result as CSV. *

- * This is the same as calling formatCSV(',', "") + * This is the same as calling formatCSV(true, ',', "") * * @return The formatted result */ String formatCSV(); + /** + * Get a simple formatted representation of this result as CSV. + *

+ * This is the same as calling formatCSV(true, delimiter, "") + * + * @param delimiter The delimiter to use between records + * @return The formatted result + */ + String formatCSV(char delimiter); + + /** + * Get a simple formatted representation of this result as CSV. + *

+ * This is the same as calling formatCSV(true, delimiter, nullString) + * + * @param delimiter The delimiter to use between records + * @param nullString A special string for encoding NULL values. + * @return The formatted result + */ + String formatCSV(char delimiter, String nullString); + + /** + * Get a simple formatted representation of this result as CSV. + *

+ * This is the same as calling formatCSV(',', "") + * + * @return The formatted result + */ + String formatCSV(boolean header); + /** * Get a simple formatted representation of this result as CSV. *

@@ -504,7 +534,7 @@ public interface Result extends List, Attachable { * @param delimiter The delimiter to use between records * @return The formatted result */ - String formatCSV(char delimiter); + String formatCSV(boolean header, char delimiter); /** * Get a simple formatted representation of this result as CSV. @@ -513,7 +543,7 @@ public interface Result extends List, Attachable { * @param nullString A special string for encoding NULL values. * @return The formatted result */ - String formatCSV(char delimiter, String nullString); + String formatCSV(boolean header, char delimiter, String nullString); /** * Get a simple formatted representation of this result as a JSON array of @@ -601,6 +631,27 @@ public interface Result extends List, Attachable { */ void formatCSV(OutputStream stream, char delimiter, String nullString) throws IOException; + /** + * Like {@link #formatCSV(boolean)}, but the data is output onto an {@link OutputStream}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(OutputStream stream, boolean header) throws IOException; + + /** + * Like {@link #formatCSV(boolean, char)}, but the data is output onto an {@link OutputStream}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(OutputStream stream, boolean header, char delimiter) throws IOException; + + /** + * Like {@link #formatCSV(boolean, char, String)}, but the data is output onto an {@link OutputStream}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(OutputStream stream, boolean header, char delimiter, String nullString) throws IOException; + /** * Like {@link #formatJSON()}, but the data is output onto an {@link OutputStream}. * @@ -671,6 +722,27 @@ public interface Result extends List, Attachable { */ void formatCSV(Writer writer, char delimiter, String nullString) throws IOException; + /** + * Like {@link #formatCSV(boolean)}, but the data is output onto a {@link Writer}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(Writer writer, boolean header) throws IOException; + + /** + * Like {@link #formatCSV(boolean, char)}, but the data is output onto a {@link Writer}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(Writer writer, boolean header, char delimiter) throws IOException; + + /** + * Like {@link #formatCSV(boolean, char, String)}, but the data is output onto a {@link Writer}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatCSV(Writer writer, boolean header, char delimiter, String nullString) throws IOException; + /** * Like {@link #formatJSON()}, but the data is output onto a {@link Writer}. * diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index b32fb2926c..3bdaf58038 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -660,23 +660,43 @@ class ResultImpl implements Result, AttachableInternal { @Override public final String formatCSV() { + return formatCSV(true); + } + + @Override + public final String formatCSV(boolean header) { StringWriter writer = new StringWriter(); - formatCSV(writer); + formatCSV(writer, header); return writer.toString(); } @Override public final void formatCSV(OutputStream stream) { - formatCSV(new OutputStreamWriter(stream)); + formatCSV(stream, true); + } + + @Override + public final void formatCSV(OutputStream stream, boolean header) { + formatCSV(new OutputStreamWriter(stream), header); } @Override public final void formatCSV(Writer writer) { - formatCSV(writer, ',', ""); + formatCSV(writer, true); + } + + @Override + public final void formatCSV(Writer writer, boolean header) { + formatCSV(writer, header, ',', ""); } @Override public final String formatCSV(char delimiter) { + return formatCSV(true, delimiter); + } + + @Override + public final String formatCSV(boolean header, char delimiter) { StringWriter writer = new StringWriter(); formatCSV(writer, delimiter); return writer.toString(); @@ -684,39 +704,66 @@ class ResultImpl implements Result, AttachableInternal { @Override public final void formatCSV(OutputStream stream, char delimiter) { + formatCSV(stream, true, delimiter); + } + + @Override + public final void formatCSV(OutputStream stream, boolean header, char delimiter) { formatCSV(new OutputStreamWriter(stream), delimiter); } @Override public final void formatCSV(Writer writer, char delimiter) { - formatCSV(writer, delimiter, ""); + formatCSV(writer, true, delimiter); + } + + @Override + public final void formatCSV(Writer writer, boolean header, char delimiter) { + formatCSV(writer, header, delimiter, ""); } @Override public final String formatCSV(char delimiter, String nullString) { + return formatCSV(true, delimiter, nullString); + } + + @Override + public final String formatCSV(boolean header, char delimiter, String nullString) { StringWriter writer = new StringWriter(); - formatCSV(writer, delimiter, nullString); + formatCSV(writer, header, delimiter, nullString); return writer.toString(); } @Override public final void formatCSV(OutputStream stream, char delimiter, String nullString) { - formatCSV(new OutputStreamWriter(stream), delimiter, nullString); + formatCSV(stream, true, delimiter, nullString); + } + + @Override + public final void formatCSV(OutputStream stream, boolean header, char delimiter, String nullString) { + formatCSV(new OutputStreamWriter(stream), header, delimiter, nullString); } @Override public final void formatCSV(Writer writer, char delimiter, String nullString) { + formatCSV(writer, true, delimiter, nullString); + } + + @Override + public final void formatCSV(Writer writer, boolean header, char delimiter, String nullString) { try { - String sep1 = ""; - for (Field field : fields.fields) { - writer.append(sep1); - writer.append(formatCSV0(field.getName(), "")); + if (header) { + String sep1 = ""; + for (Field field : fields.fields) { + writer.append(sep1); + writer.append(formatCSV0(field.getName(), "")); - sep1 = Character.toString(delimiter); + sep1 = Character.toString(delimiter); + } + + writer.append("\n"); } - writer.append("\n"); - for (Record record : this) { String sep2 = "";