java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
branchv_0
changeset 238 4a1864c3e867
parent 218 8e38caf43ca8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Mon Mar 04 20:15:24 2019 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2015 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.formatting;
    1.22 +
    1.23 +import info.globalcode.sql.dk.ColorfulPrintWriter;
    1.24 +import info.globalcode.sql.dk.Functions;
    1.25 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
    1.26 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
    1.27 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    1.28 +
    1.29 +/**
    1.30 + * Formatter intended for printing one record (or few records) with many columns.
    1.31 + * Prints each colum name and its value on separate line.
    1.32 + *
    1.33 + * @author Ing. František Kučera (frantovo.cz)
    1.34 + */
    1.35 +@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
    1.36 +public class SingleRecordFormatter extends AbstractFormatter {
    1.37 +
    1.38 +	public static final String NAME = "record"; // bash-completion:formatter
    1.39 +	private final ColorfulPrintWriter out;
    1.40 +	private boolean firstResult = true;
    1.41 +
    1.42 +	public SingleRecordFormatter(FormatterContext formatterContext) {
    1.43 +		super(formatterContext);
    1.44 +		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    1.45 +		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    1.46 +	}
    1.47 +
    1.48 +	@Override
    1.49 +	public void writeStartResultSet(ColumnsHeader header) {
    1.50 +		super.writeStartResultSet(header);
    1.51 +		printResultSeparator();
    1.52 +	}
    1.53 +
    1.54 +	@Override
    1.55 +	public void writeStartRow() {
    1.56 +		super.writeStartRow();
    1.57 +		printRecordSeparator();
    1.58 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
    1.59 +		out.print(getCurrentRowCount());
    1.60 +		println();
    1.61 +	}
    1.62 +
    1.63 +	@Override
    1.64 +	public void writeColumnValue(Object value) {
    1.65 +		super.writeColumnValue(value);
    1.66 +		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
    1.67 +		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
    1.68 +		Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red);
    1.69 +		println();
    1.70 +	}
    1.71 +
    1.72 +	private static String toString(Object value) {
    1.73 +		return String.valueOf(value);
    1.74 +	}
    1.75 +
    1.76 +	@Override
    1.77 +	public void writeUpdatesResult(int updatedRowsCount) {
    1.78 +		super.writeUpdatesResult(updatedRowsCount);
    1.79 +		printResultSeparator();
    1.80 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
    1.81 +		out.println(updatedRowsCount);
    1.82 +		printBellAndFlush();
    1.83 +	}
    1.84 +
    1.85 +	private void printBellAndFlush() {
    1.86 +		out.bell();
    1.87 +		out.flush();
    1.88 +	}
    1.89 +
    1.90 +	private void println() {
    1.91 +		out.println();
    1.92 +		printBellAndFlush();
    1.93 +	}
    1.94 +
    1.95 +	private void printRecordSeparator() {
    1.96 +		if (getCurrentRowCount() > 1) {
    1.97 +			println();
    1.98 +		}
    1.99 +	}
   1.100 +
   1.101 +	private void printResultSeparator() {
   1.102 +		if (firstResult) {
   1.103 +			firstResult = false;
   1.104 +		} else {
   1.105 +			println();
   1.106 +		}
   1.107 +	}
   1.108 +}