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.
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;
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
/**
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
    23
 * 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
    24
 * Prints each colum name and its value on separate line.
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
 * @author Ing. František Kučera (frantovo.cz)
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 */
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
    28
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
    29
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
    30
	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
    31
	public static final String PROPERTY_COLORFUL = "color";
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
	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
    33
	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
    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 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
    36
		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
    37
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
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
		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, 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
    39
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
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
    41
	@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
    42
	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
    43
		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
    44
		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
    45
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
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
    47
	@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
    48
	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
    49
		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
    50
		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
    51
		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
    52
		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
    53
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	@Override
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	public void writeColumnValue(Object value) {
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		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
    59
		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
    60
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		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
    62
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	@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
    66
	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
    67
		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
    68
		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
    69
		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
    70
		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
    71
		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
    72
	}
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
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
	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
    75
		out.bell();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		out.flush();
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
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
    78
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
	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
    80
		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
    81
		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
    82
	}
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
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
	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
    85
		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
    86
			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
    87
		}
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
	}
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
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
	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
    91
		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
    92
			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
    93
		} 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
    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
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}