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
     1 /**
     2  * SQL-DK
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    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;
    25 
    26 /**
    27  * Formatter intended for printing one record (or few records) with many columns.
    28  * Prints each colum name and its value on separate line.
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
    33 public class SingleRecordFormatter extends AbstractFormatter {
    34 
    35 	public static final String NAME = "record"; // bash-completion:formatter
    36 	private final ColorfulPrintWriter out;
    37 	private boolean firstResult = true;
    38 
    39 	public SingleRecordFormatter(FormatterContext formatterContext) {
    40 		super(formatterContext);
    41 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    42 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    43 	}
    44 
    45 	@Override
    46 	public void writeStartResultSet(ColumnsHeader header) {
    47 		super.writeStartResultSet(header);
    48 		printResultSeparator();
    49 	}
    50 
    51 	@Override
    52 	public void writeStartRow() {
    53 		super.writeStartRow();
    54 		printRecordSeparator();
    55 		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
    56 		out.print(getCurrentRowCount());
    57 		println();
    58 	}
    59 
    60 	@Override
    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);
    66 		println();
    67 	}
    68 
    69 	private static String toString(Object value) {
    70 		return String.valueOf(value);
    71 	}
    72 
    73 	@Override
    74 	public void writeUpdatesResult(int updatedRowsCount) {
    75 		super.writeUpdatesResult(updatedRowsCount);
    76 		printResultSeparator();
    77 		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
    78 		out.println(updatedRowsCount);
    79 		printBellAndFlush();
    80 	}
    81 
    82 	private void printBellAndFlush() {
    83 		out.bell();
    84 		out.flush();
    85 	}
    86 
    87 	private void println() {
    88 		out.println();
    89 		printBellAndFlush();
    90 	}
    91 
    92 	private void printRecordSeparator() {
    93 		if (getCurrentRowCount() > 1) {
    94 			println();
    95 		}
    96 	}
    97 
    98 	private void printResultSeparator() {
    99 		if (firstResult) {
   100 			firstResult = false;
   101 		} else {
   102 			println();
   103 		}
   104 	}
   105 }