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