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.configuration.PropertyDeclaration;
22 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
23 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
26 * Formatter intended for printing one record (or few records) with many columns.
27 * Prints each colum name and its value on separate line.
29 * @author Ing. František Kučera (frantovo.cz)
31 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
32 public class SingleRecordFormatter extends AbstractFormatter {
34 public static final String NAME = "record"; // bash-completion:formatter
35 private final ColorfulPrintWriter out;
36 private boolean firstResult = true;
38 public SingleRecordFormatter(FormatterContext formatterContext) {
39 super(formatterContext);
40 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
41 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
45 public void writeStartResultSet(ColumnsHeader header) {
46 super.writeStartResultSet(header);
47 printResultSeparator();
51 public void writeStartRow() {
52 super.writeStartRow();
53 printRecordSeparator();
54 out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
55 out.print(getCurrentRowCount());
60 public void writeColumnValue(Object value) {
61 super.writeColumnValue(value);
62 String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
63 out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
64 out.print(String.valueOf(value));
69 public void writeUpdatesResult(int updatedRowsCount) {
70 super.writeUpdatesResult(updatedRowsCount);
71 printResultSeparator();
72 out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
73 out.println(updatedRowsCount);
77 private void printBellAndFlush() {
82 private void println() {
87 private void printRecordSeparator() {
88 if (getCurrentRowCount() > 1) {
93 private void printResultSeparator() {