java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Jun 2015 16:21:51 +0200
branchv_0
changeset 202 01078e09b85b
parent 142 java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleValueFormatter.java@da1e38386d84
child 206 e2f24eea8543
permissions -rw-r--r--
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
franta-hg@60
     1
/**
franta-hg@60
     2
 * SQL-DK
franta-hg@202
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
franta-hg@60
     4
 *
franta-hg@60
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@60
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@60
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@60
     8
 * (at your option) any later version.
franta-hg@60
     9
 *
franta-hg@60
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@60
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@60
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@60
    13
 * GNU General Public License for more details.
franta-hg@60
    14
 *
franta-hg@60
    15
 * You should have received a copy of the GNU General Public License
franta-hg@60
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@60
    17
 */
franta-hg@60
    18
package info.globalcode.sql.dk.formatting;
franta-hg@60
    19
franta-hg@202
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
franta-hg@60
    21
franta-hg@60
    22
/**
franta-hg@202
    23
 * Formatter intended for printing one record (or few records) with many columns.
franta-hg@202
    24
 * Prints each colum name and its value on separate line.
franta-hg@60
    25
 *
franta-hg@60
    26
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@60
    27
 */
franta-hg@202
    28
public class SingleRecordFormatter extends AbstractFormatter {
franta-hg@60
    29
franta-hg@202
    30
	public static final String NAME = "record"; // bash-completion:formatter
franta-hg@202
    31
	public static final String PROPERTY_COLORFUL = "color";
franta-hg@202
    32
	private final ColorfulPrintWriter out;
franta-hg@202
    33
	private boolean firstResult = true;
franta-hg@60
    34
franta-hg@202
    35
	public SingleRecordFormatter(FormatterContext formatterContext) {
franta-hg@60
    36
		super(formatterContext);
franta-hg@202
    37
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
franta-hg@202
    38
		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
franta-hg@202
    39
	}
franta-hg@202
    40
franta-hg@202
    41
	@Override
franta-hg@202
    42
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@202
    43
		super.writeStartResultSet(header);
franta-hg@202
    44
		printResultSeparator();
franta-hg@202
    45
	}
franta-hg@202
    46
franta-hg@202
    47
	@Override
franta-hg@202
    48
	public void writeStartRow() {
franta-hg@202
    49
		super.writeStartRow();
franta-hg@202
    50
		printRecordSeparator();
franta-hg@202
    51
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
franta-hg@202
    52
		out.print(getCurrentRowCount());
franta-hg@202
    53
		println();
franta-hg@60
    54
	}
franta-hg@60
    55
franta-hg@60
    56
	@Override
franta-hg@60
    57
	public void writeColumnValue(Object value) {
franta-hg@60
    58
		super.writeColumnValue(value);
franta-hg@202
    59
		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
franta-hg@202
    60
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
franta-hg@60
    61
		out.print(String.valueOf(value));
franta-hg@202
    62
		println();
franta-hg@60
    63
	}
franta-hg@60
    64
franta-hg@60
    65
	@Override
franta-hg@142
    66
	public void writeUpdatesResult(int updatedRowsCount) {
franta-hg@142
    67
		super.writeUpdatesResult(updatedRowsCount);
franta-hg@202
    68
		printResultSeparator();
franta-hg@202
    69
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
franta-hg@202
    70
		out.println(updatedRowsCount);
franta-hg@202
    71
		printBellAndFlush();
franta-hg@202
    72
	}
franta-hg@202
    73
franta-hg@202
    74
	private void printBellAndFlush() {
franta-hg@202
    75
		out.bell();
franta-hg@60
    76
		out.flush();
franta-hg@60
    77
	}
franta-hg@202
    78
franta-hg@202
    79
	private void println() {
franta-hg@202
    80
		out.println();
franta-hg@202
    81
		printBellAndFlush();
franta-hg@202
    82
	}
franta-hg@202
    83
franta-hg@202
    84
	private void printRecordSeparator() {
franta-hg@202
    85
		if (getCurrentRowCount() > 1) {
franta-hg@202
    86
			println();
franta-hg@202
    87
		}
franta-hg@202
    88
	}
franta-hg@202
    89
franta-hg@202
    90
	private void printResultSeparator() {
franta-hg@202
    91
		if (firstResult) {
franta-hg@202
    92
			firstResult = false;
franta-hg@202
    93
		} else {
franta-hg@202
    94
			println();
franta-hg@202
    95
		}
franta-hg@202
    96
	}
franta-hg@60
    97
}