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
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;
206
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    21
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
    22
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
    23
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
/**
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
    26
 * 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
    27
 * Prints each colum name and its value on separate line.
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * @author Ing. František Kučera (frantovo.cz)
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 */
207
2bba68ef47c1 property annotations: default values
František Kučera <franta-hg@frantovo.cz>
parents: 206
diff changeset
    31
@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
    32
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
    33
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
	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
    35
	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
    36
	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
    37
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
	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
    39
		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
    40
		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
    41
		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
    42
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
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
    44
	@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
    45
	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
    46
		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
    47
		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
    48
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
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
    50
	@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
    51
	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
    52
		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
    53
		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
    54
		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
    55
		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
    56
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	}
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
	@Override
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	public void writeColumnValue(Object value) {
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		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
    62
		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
    63
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		out.print(String.valueOf(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
    65
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	}
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
	@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
    69
	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
    70
		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
    71
		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
    72
		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
    73
		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
    74
		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
    75
	}
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
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
	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
    78
		out.bell();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		out.flush();
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
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
    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 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
    83
		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
    84
		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
    85
	}
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 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
    88
		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
    89
			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
    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
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
	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
    94
		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
    95
			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
    96
		} 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
    97
			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
    98
		}
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
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
}