Skip to content

Commit

Permalink
[SQLLINE-57] Respect 'showheader=false' in file output (Renny Koshy a…
Browse files Browse the repository at this point in the history
…nd Sergey Nuyanzin)

File output also respects showHeader. (Renny Koshy)

Handle csv/tsv/table output, rollback non-related to showheader
changes, add tests. (Sergey Nuyanzin)
  • Loading branch information
rkoshy authored and julianhyde committed Aug 18, 2018
1 parent 9b0f7b8 commit 14d3014
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/main/java/sqlline/AbstractOutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/sqlline/SeparatedValuesOutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/sqlline/TableOutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 14d3014

Please sign in to comment.