java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
branchv_0
changeset 202 01078e09b85b
parent 142 da1e38386d84
child 206 e2f24eea8543
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sun Jun 21 16:21:51 2015 +0200
     1.3 @@ -0,0 +1,97 @@
     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 +
    1.25 +/**
    1.26 + * Formatter intended for printing one record (or few records) with many columns.
    1.27 + * Prints each colum name and its value on separate line.
    1.28 + *
    1.29 + * @author Ing. František Kučera (frantovo.cz)
    1.30 + */
    1.31 +public class SingleRecordFormatter extends AbstractFormatter {
    1.32 +
    1.33 +	public static final String NAME = "record"; // bash-completion:formatter
    1.34 +	public static final String PROPERTY_COLORFUL = "color";
    1.35 +	private final ColorfulPrintWriter out;
    1.36 +	private boolean firstResult = true;
    1.37 +
    1.38 +	public SingleRecordFormatter(FormatterContext formatterContext) {
    1.39 +		super(formatterContext);
    1.40 +		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    1.41 +		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
    1.42 +	}
    1.43 +
    1.44 +	@Override
    1.45 +	public void writeStartResultSet(ColumnsHeader header) {
    1.46 +		super.writeStartResultSet(header);
    1.47 +		printResultSeparator();
    1.48 +	}
    1.49 +
    1.50 +	@Override
    1.51 +	public void writeStartRow() {
    1.52 +		super.writeStartRow();
    1.53 +		printRecordSeparator();
    1.54 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
    1.55 +		out.print(getCurrentRowCount());
    1.56 +		println();
    1.57 +	}
    1.58 +
    1.59 +	@Override
    1.60 +	public void writeColumnValue(Object value) {
    1.61 +		super.writeColumnValue(value);
    1.62 +		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
    1.63 +		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
    1.64 +		out.print(String.valueOf(value));
    1.65 +		println();
    1.66 +	}
    1.67 +
    1.68 +	@Override
    1.69 +	public void writeUpdatesResult(int updatedRowsCount) {
    1.70 +		super.writeUpdatesResult(updatedRowsCount);
    1.71 +		printResultSeparator();
    1.72 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
    1.73 +		out.println(updatedRowsCount);
    1.74 +		printBellAndFlush();
    1.75 +	}
    1.76 +
    1.77 +	private void printBellAndFlush() {
    1.78 +		out.bell();
    1.79 +		out.flush();
    1.80 +	}
    1.81 +
    1.82 +	private void println() {
    1.83 +		out.println();
    1.84 +		printBellAndFlush();
    1.85 +	}
    1.86 +
    1.87 +	private void printRecordSeparator() {
    1.88 +		if (getCurrentRowCount() > 1) {
    1.89 +			println();
    1.90 +		}
    1.91 +	}
    1.92 +
    1.93 +	private void printResultSeparator() {
    1.94 +		if (firstResult) {
    1.95 +			firstResult = false;
    1.96 +		} else {
    1.97 +			println();
    1.98 +		}
    1.99 +	}
   1.100 +}