java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 10:34:18 +0200
branchv_0
changeset 207 2bba68ef47c1
parent 206 e2f24eea8543
child 218 8e38caf43ca8
permissions -rw-r--r--
property annotations: default values
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@206
    21
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
franta-hg@206
    22
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
franta-hg@206
    23
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
franta-hg@60
    24
franta-hg@60
    25
/**
franta-hg@202
    26
 * Formatter intended for printing one record (or few records) with many columns.
franta-hg@202
    27
 * Prints each colum name and its value on separate line.
franta-hg@60
    28
 *
franta-hg@60
    29
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@60
    30
 */
franta-hg@207
    31
@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
franta-hg@202
    32
public class SingleRecordFormatter extends AbstractFormatter {
franta-hg@60
    33
franta-hg@202
    34
	public static final String NAME = "record"; // bash-completion:formatter
franta-hg@202
    35
	private final ColorfulPrintWriter out;
franta-hg@202
    36
	private boolean firstResult = true;
franta-hg@60
    37
franta-hg@202
    38
	public SingleRecordFormatter(FormatterContext formatterContext) {
franta-hg@60
    39
		super(formatterContext);
franta-hg@202
    40
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
franta-hg@206
    41
		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
franta-hg@202
    42
	}
franta-hg@202
    43
franta-hg@202
    44
	@Override
franta-hg@202
    45
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@202
    46
		super.writeStartResultSet(header);
franta-hg@202
    47
		printResultSeparator();
franta-hg@202
    48
	}
franta-hg@202
    49
franta-hg@202
    50
	@Override
franta-hg@202
    51
	public void writeStartRow() {
franta-hg@202
    52
		super.writeStartRow();
franta-hg@202
    53
		printRecordSeparator();
franta-hg@202
    54
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
franta-hg@202
    55
		out.print(getCurrentRowCount());
franta-hg@202
    56
		println();
franta-hg@60
    57
	}
franta-hg@60
    58
franta-hg@60
    59
	@Override
franta-hg@60
    60
	public void writeColumnValue(Object value) {
franta-hg@60
    61
		super.writeColumnValue(value);
franta-hg@202
    62
		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
franta-hg@202
    63
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
franta-hg@60
    64
		out.print(String.valueOf(value));
franta-hg@202
    65
		println();
franta-hg@60
    66
	}
franta-hg@60
    67
franta-hg@60
    68
	@Override
franta-hg@142
    69
	public void writeUpdatesResult(int updatedRowsCount) {
franta-hg@142
    70
		super.writeUpdatesResult(updatedRowsCount);
franta-hg@202
    71
		printResultSeparator();
franta-hg@202
    72
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
franta-hg@202
    73
		out.println(updatedRowsCount);
franta-hg@202
    74
		printBellAndFlush();
franta-hg@202
    75
	}
franta-hg@202
    76
franta-hg@202
    77
	private void printBellAndFlush() {
franta-hg@202
    78
		out.bell();
franta-hg@60
    79
		out.flush();
franta-hg@60
    80
	}
franta-hg@202
    81
franta-hg@202
    82
	private void println() {
franta-hg@202
    83
		out.println();
franta-hg@202
    84
		printBellAndFlush();
franta-hg@202
    85
	}
franta-hg@202
    86
franta-hg@202
    87
	private void printRecordSeparator() {
franta-hg@202
    88
		if (getCurrentRowCount() > 1) {
franta-hg@202
    89
			println();
franta-hg@202
    90
		}
franta-hg@202
    91
	}
franta-hg@202
    92
franta-hg@202
    93
	private void printResultSeparator() {
franta-hg@202
    94
		if (firstResult) {
franta-hg@202
    95
			firstResult = false;
franta-hg@202
    96
		} else {
franta-hg@202
    97
			println();
franta-hg@202
    98
		}
franta-hg@202
    99
	}
franta-hg@60
   100
}