3 * Copyright © 2015 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.ColorfulPrintWriter;
21 import info.globalcode.sql.dk.Functions;
22 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
23 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
24 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
27 * Formatter intended for printing one record (or few records) with many columns.
28 * Prints each colum name and its value on separate line.
30 * @author Ing. František Kučera (frantovo.cz)
32 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
33 public class SingleRecordFormatter extends AbstractFormatter {
35 public static final String NAME = "record"; // bash-completion:formatter
36 private final ColorfulPrintWriter out;
37 private boolean firstResult = true;
39 public SingleRecordFormatter(FormatterContext formatterContext) {
40 super(formatterContext);
41 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
42 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
46 public void writeStartResultSet(ColumnsHeader header) {
47 super.writeStartResultSet(header);
48 printResultSeparator();
52 public void writeStartRow() {
53 super.writeStartRow();
54 printRecordSeparator();
55 out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
56 out.print(getCurrentRowCount());
61 public void writeColumnValue(Object value) {
62 super.writeColumnValue(value);
63 String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
64 out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
65 Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red);
69 private static String toString(Object value) {
70 return String.valueOf(value);
74 public void writeUpdatesResult(int updatedRowsCount) {
75 super.writeUpdatesResult(updatedRowsCount);
76 printResultSeparator();
77 out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
78 out.println(updatedRowsCount);
82 private void printBellAndFlush() {
87 private void println() {
92 private void printRecordSeparator() {
93 if (getCurrentRowCount() > 1) {
98 private void printResultSeparator() {