From 14d30146a14fe9b6d66fbcaef73a5fb9643d0ee7 Mon Sep 17 00:00:00 2001 From: Renny Koshy Date: Sun, 12 Feb 2017 02:00:48 -0500 Subject: [PATCH] [SQLLINE-57] Respect 'showheader=false' in file output (Renny Koshy and Sergey Nuyanzin) File output also respects showHeader. (Renny Koshy) Handle csv/tsv/table output, rollback non-related to showheader changes, add tests. (Sergey Nuyanzin) --- .../java/sqlline/AbstractOutputFormat.java | 5 ++-- .../sqlline/SeparatedValuesOutputFormat.java | 22 ++++++++-------- src/main/java/sqlline/TableOutputFormat.java | 15 +++++------ src/test/java/sqlline/SqlLineArgsTest.java | 25 +++++++++++++++++++ 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main/java/sqlline/AbstractOutputFormat.java b/src/main/java/sqlline/AbstractOutputFormat.java index 64c9242a..a24a59be 100644 --- a/src/main/java/sqlline/AbstractOutputFormat.java +++ b/src/main/java/sqlline/AbstractOutputFormat.java @@ -25,8 +25,9 @@ public int print(Rows rows) { int count = 0; Rows.Row header = rows.next(); - printHeader(header); - + if (sqlLine.getOpts().getShowHeader()) { + printHeader(header); + } while (rows.hasNext()) { printRow(rows, header, rows.next()); count++; diff --git a/src/main/java/sqlline/SeparatedValuesOutputFormat.java b/src/main/java/sqlline/SeparatedValuesOutputFormat.java index 5db9d13f..a79c239e 100644 --- a/src/main/java/sqlline/SeparatedValuesOutputFormat.java +++ b/src/main/java/sqlline/SeparatedValuesOutputFormat.java @@ -14,15 +14,14 @@ /** * OutputFormat for values separated by a delimiter. */ -class SeparatedValuesOutputFormat implements OutputFormat { +class SeparatedValuesOutputFormat extends AbstractOutputFormat { private static final char DEFAULT_QUOTE_CHARACTER = '"'; - private final SqlLine sqlLine; final String separator; final char quoteCharacter; public SeparatedValuesOutputFormat(SqlLine sqlLine, String separator, char quoteCharacter) { - this.sqlLine = sqlLine; + super(sqlLine); this.separator = separator; this.quoteCharacter = quoteCharacter; } @@ -31,17 +30,18 @@ public SeparatedValuesOutputFormat(SqlLine sqlLine, String separator) { this(sqlLine, separator, DEFAULT_QUOTE_CHARACTER); } - public int print(Rows rows) { - int count = 0; - while (rows.hasNext()) { - printRow(rows, rows.next()); - count++; - } + @Override void printHeader(Rows.Row header) { + printRow(header); + } + + @Override void printFooter(Rows.Row header) { + } - return count - 1; // sans header row + @Override void printRow(Rows rows, Rows.Row header, Rows.Row row) { + printRow(row); } - public void printRow(Rows rows, Rows.Row row) { + private void printRow(Rows.Row row) { String[] vals = row.values; StringBuilder buf = new StringBuilder(); for (String val : vals) { diff --git a/src/main/java/sqlline/TableOutputFormat.java b/src/main/java/sqlline/TableOutputFormat.java index 0e5e4561..7edb142d 100644 --- a/src/main/java/sqlline/TableOutputFormat.java +++ b/src/main/java/sqlline/TableOutputFormat.java @@ -50,13 +50,14 @@ public int print(Rows rows) { headerCols.getVisibleLength()); } - if ((index == 0) - || (sqlLine.getOpts().getHeaderInterval() > 0 - && (index % sqlLine.getOpts().getHeaderInterval() == 0) - && sqlLine.getOpts().getShowHeader())) { - printRow(header, true); - printRow(headerCols, false); - printRow(header, true); + if (sqlLine.getOpts().getShowHeader()) { + if (index == 0 + || sqlLine.getOpts().getHeaderInterval() > 0 + && index % sqlLine.getOpts().getHeaderInterval() == 0) { + printRow(header, true); + printRow(headerCols, false); + printRow(header, true); + } } if (index != 0) { // don't output the header twice diff --git a/src/test/java/sqlline/SqlLineArgsTest.java b/src/test/java/sqlline/SqlLineArgsTest.java index eba82af1..a887adf3 100644 --- a/src/test/java/sqlline/SqlLineArgsTest.java +++ b/src/test/java/sqlline/SqlLineArgsTest.java @@ -263,6 +263,31 @@ public void testNull() throws Throwable { + "+-------------+-------------+-----+\n")); } + /** + * Table output without header. + */ + @Test + public void testTableOutputNullWithoutHeader() throws Throwable { + final String script = "!set showHeader false\n" + + "values (1, cast(null as integer), cast(null as varchar(3));\n"; + checkScriptFile(script, false, + equalTo(SqlLine.Status.OK), + containsString("| 1 | null | |\n")); + } + + /** + * Csv output without header. + */ + @Test + public void testCsvNullWithoutHeader() throws Throwable { + final String script = "!set showHeader false\n" + + "!set outputformat csv\n" + + "values (1, cast(null as integer), cast(null as varchar(3));\n"; + checkScriptFile(script, false, + equalTo(SqlLine.Status.OK), + containsString("'1','null',''\n")); + } + /** * Tests the "close" command, * [HIVE-5768] Beeline connection cannot be closed with '!close' command.