java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 11:50:24 +0100
branchv_0
changeset 37 9e6f8e5d5f98
parent 34 9335cf31c0f2
child 39 be8db46a38c3
permissions -rw-r--r--
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
franta-hg@32
     1
/**
franta-hg@32
     2
 * SQL-DK
franta-hg@32
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@32
     4
 *
franta-hg@32
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@32
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@32
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@32
     8
 * (at your option) any later version.
franta-hg@32
     9
 *
franta-hg@32
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@32
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@32
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@32
    13
 * GNU General Public License for more details.
franta-hg@32
    14
 *
franta-hg@32
    15
 * You should have received a copy of the GNU General Public License
franta-hg@32
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@32
    17
 */
franta-hg@32
    18
package info.globalcode.sql.dk.formatting;
franta-hg@32
    19
franta-hg@34
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
franta-hg@37
    21
import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
franta-hg@34
    22
franta-hg@32
    23
/**
franta-hg@32
    24
 *
franta-hg@32
    25
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@32
    26
 */
franta-hg@32
    27
public class TabularFormatter extends AbstractFormatter {
franta-hg@32
    28
franta-hg@32
    29
	public static final String NAME = "tabular";
franta-hg@34
    30
	private ColorfulPrintWriter out;
franta-hg@37
    31
	private boolean firstResult = true;
franta-hg@32
    32
franta-hg@32
    33
	public TabularFormatter(FormatterContext formatterContext) {
franta-hg@32
    34
		super(formatterContext);
franta-hg@34
    35
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
franta-hg@34
    36
	}
franta-hg@34
    37
franta-hg@34
    38
	@Override
franta-hg@37
    39
	public void writeStartResultSet() {
franta-hg@37
    40
		super.writeStartResultSet();
franta-hg@37
    41
		printResultSeparator();
franta-hg@37
    42
	}
franta-hg@37
    43
franta-hg@37
    44
	@Override
franta-hg@37
    45
	public void writeColumnsHeader(ColumnsHeader header) {
franta-hg@37
    46
		super.writeColumnsHeader(header);
franta-hg@37
    47
franta-hg@37
    48
		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
franta-hg@37
    49
			out.print(TerminalStyle.Bright, cd.getLabel());
franta-hg@37
    50
			out.print(" (");
franta-hg@37
    51
			out.print(cd.getTypeName());
franta-hg@37
    52
			out.print(")");
franta-hg@37
    53
			if (!cd.isLastColumn()) {
franta-hg@37
    54
				out.print(TerminalColor.Green, " | ");
franta-hg@37
    55
			}
franta-hg@37
    56
		}
franta-hg@37
    57
		out.println();
franta-hg@37
    58
		out.flush();
franta-hg@37
    59
	}
franta-hg@37
    60
franta-hg@37
    61
	@Override
franta-hg@34
    62
	public void writeColumnValue(Object value) {
franta-hg@34
    63
		super.writeColumnValue(value);
franta-hg@34
    64
franta-hg@34
    65
		if (!isCurrentColumnFirst()) {
franta-hg@37
    66
			out.print(TerminalColor.Green, " | ");
franta-hg@34
    67
		}
franta-hg@37
    68
franta-hg@37
    69
		out.print(TerminalColor.Cyan, String.valueOf(value));
franta-hg@34
    70
	}
franta-hg@34
    71
franta-hg@34
    72
	@Override
franta-hg@34
    73
	public void writeEndRow() {
franta-hg@34
    74
		super.writeEndRow();
franta-hg@34
    75
		out.println();
franta-hg@34
    76
		out.flush();
franta-hg@32
    77
	}
franta-hg@37
    78
franta-hg@37
    79
	@Override
franta-hg@37
    80
	public void writeEndResultSet() {
franta-hg@37
    81
		super.writeEndResultSet();
franta-hg@37
    82
		out.print(TerminalColor.Yellow, "Record count: ");
franta-hg@37
    83
		out.println(getCurrentRowCount());
franta-hg@37
    84
		out.flush();
franta-hg@37
    85
	}
franta-hg@37
    86
franta-hg@37
    87
	@Override
franta-hg@37
    88
	public void writeStartUpdatesResult() {
franta-hg@37
    89
		super.writeStartUpdatesResult();
franta-hg@37
    90
		printResultSeparator();
franta-hg@37
    91
	}
franta-hg@37
    92
franta-hg@37
    93
	@Override
franta-hg@37
    94
	public void writeUpdatedRowsCount(int updatedRowsCount) {
franta-hg@37
    95
		super.writeUpdatedRowsCount(updatedRowsCount);
franta-hg@37
    96
		out.print(TerminalColor.Red, "Updated records: ");
franta-hg@37
    97
		out.println(updatedRowsCount);
franta-hg@37
    98
		out.flush();
franta-hg@37
    99
	}
franta-hg@37
   100
franta-hg@37
   101
	@Override
franta-hg@37
   102
	public void writeEndDatabase() {
franta-hg@37
   103
		super.writeEndDatabase();
franta-hg@37
   104
		out.flush();
franta-hg@37
   105
	}
franta-hg@37
   106
franta-hg@37
   107
	private void printResultSeparator() {
franta-hg@37
   108
		if (firstResult) {
franta-hg@37
   109
			firstResult = false;
franta-hg@37
   110
		} else {
franta-hg@37
   111
			out.println();
franta-hg@37
   112
		}
franta-hg@37
   113
	}
franta-hg@32
   114
}