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:20:39 +0200
branchv_0
changeset 206 e2f24eea8543
parent 202 01078e09b85b
child 207 2bba68ef47c1
permissions -rw-r--r--
property annotations (documentation) for particular formatters
     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.configuration.PropertyDeclaration;
    22 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
    23 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    24 
    25 /**
    26  * Formatter intended for printing one record (or few records) with many columns.
    27  * Prints each colum name and its value on separate line.
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 @PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
    32 public class SingleRecordFormatter extends AbstractFormatter {
    33 
    34 	public static final String NAME = "record"; // bash-completion:formatter
    35 	private final ColorfulPrintWriter out;
    36 	private boolean firstResult = true;
    37 
    38 	public SingleRecordFormatter(FormatterContext formatterContext) {
    39 		super(formatterContext);
    40 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    41 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    42 	}
    43 
    44 	@Override
    45 	public void writeStartResultSet(ColumnsHeader header) {
    46 		super.writeStartResultSet(header);
    47 		printResultSeparator();
    48 	}
    49 
    50 	@Override
    51 	public void writeStartRow() {
    52 		super.writeStartRow();
    53 		printRecordSeparator();
    54 		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
    55 		out.print(getCurrentRowCount());
    56 		println();
    57 	}
    58 
    59 	@Override
    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));
    65 		println();
    66 	}
    67 
    68 	@Override
    69 	public void writeUpdatesResult(int updatedRowsCount) {
    70 		super.writeUpdatesResult(updatedRowsCount);
    71 		printResultSeparator();
    72 		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
    73 		out.println(updatedRowsCount);
    74 		printBellAndFlush();
    75 	}
    76 
    77 	private void printBellAndFlush() {
    78 		out.bell();
    79 		out.flush();
    80 	}
    81 
    82 	private void println() {
    83 		out.println();
    84 		printBellAndFlush();
    85 	}
    86 
    87 	private void printRecordSeparator() {
    88 		if (getCurrentRowCount() > 1) {
    89 			println();
    90 		}
    91 	}
    92 
    93 	private void printResultSeparator() {
    94 		if (firstResult) {
    95 			firstResult = false;
    96 		} else {
    97 			println();
    98 		}
    99 	}
   100 }