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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.formatting;
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    21
import info.globalcode.sql.dk.Functions;
206
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    22
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    23
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    24
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
/**
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    27
 * Formatter intended for printing one record (or few records) with many columns.
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    28
 * Prints each colum name and its value on separate line.
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 * @author Ing. František Kučera (frantovo.cz)
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 */
207
2bba68ef47c1 property annotations: default values
František Kučera <franta-hg@frantovo.cz>
parents: 206
diff changeset
    32
@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    33
public class SingleRecordFormatter extends AbstractFormatter {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    34
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    35
	public static final String NAME = "record"; // bash-completion:formatter
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    36
	private final ColorfulPrintWriter out;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    37
	private boolean firstResult = true;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    38
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    39
	public SingleRecordFormatter(FormatterContext formatterContext) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    40
		super(formatterContext);
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    41
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
206
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    42
		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    43
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    45
	@Override
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    46
	public void writeStartResultSet(ColumnsHeader header) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    47
		super.writeStartResultSet(header);
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    48
		printResultSeparator();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    49
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    51
	@Override
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    52
	public void writeStartRow() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    53
		super.writeStartRow();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    54
		printRecordSeparator();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    55
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    56
		out.print(getCurrentRowCount());
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    57
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	@Override
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	public void writeColumnValue(Object value) {
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		super.writeColumnValue(value);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    63
		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    64
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    65
		Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    66
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    69
	private static String toString(Object value) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    70
		return String.valueOf(value);
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    71
	}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    72
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	@Override
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    74
	public void writeUpdatesResult(int updatedRowsCount) {
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    75
		super.writeUpdatesResult(updatedRowsCount);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    76
		printResultSeparator();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    77
		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    78
		out.println(updatedRowsCount);
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    79
		printBellAndFlush();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    80
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    81
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    82
	private void printBellAndFlush() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    83
		out.bell();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		out.flush();
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    86
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    87
	private void println() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    88
		out.println();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    89
		printBellAndFlush();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    90
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    91
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    92
	private void printRecordSeparator() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    93
		if (getCurrentRowCount() > 1) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    94
			println();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    95
		}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    96
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    97
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    98
	private void printResultSeparator() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    99
		if (firstResult) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   100
			firstResult = false;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   101
		} else {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   102
			println();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   103
		}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   104
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
}