SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line. v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 21 Jun 2015 16:21:51 +0200
branchv_0
changeset 20201078e09b85b
parent 201 d3db5a72a089
child 203 504c4ba56d1c
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Sun May 24 19:17:50 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Sun Jun 21 16:21:51 2015 +0200
     1.3 @@ -21,6 +21,7 @@
     1.4  import static info.globalcode.sql.dk.Functions.findByName;
     1.5  import info.globalcode.sql.dk.formatting.DsvFormatter;
     1.6  import info.globalcode.sql.dk.formatting.SilentFormatter;
     1.7 +import info.globalcode.sql.dk.formatting.SingleRecordFormatter;
     1.8  import info.globalcode.sql.dk.formatting.SingleValueFormatter;
     1.9  import info.globalcode.sql.dk.formatting.TabularFormatter;
    1.10  import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
    1.11 @@ -65,6 +66,7 @@
    1.12  		Collection<FormatterDefinition> l = new ArrayList<>();
    1.13  		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
    1.14  		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
    1.15 +		l.add(new FormatterDefinition(SingleRecordFormatter.NAME, SingleRecordFormatter.class.getName()));
    1.16  		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    1.17  		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
    1.18  		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    1.19 @@ -85,6 +87,8 @@
    1.20  	}
    1.21  
    1.22  	/**
    1.23 +	 * @param name
    1.24 +	 * @return 
    1.25  	 * @throws ConfigurationException if no database with this name is configured
    1.26  	 */
    1.27  	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sun Jun 21 16:21:51 2015 +0200
     2.3 @@ -0,0 +1,97 @@
     2.4 +/**
     2.5 + * SQL-DK
     2.6 + * Copyright © 2015 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package info.globalcode.sql.dk.formatting;
    2.22 +
    2.23 +import info.globalcode.sql.dk.ColorfulPrintWriter;
    2.24 +
    2.25 +/**
    2.26 + * Formatter intended for printing one record (or few records) with many columns.
    2.27 + * Prints each colum name and its value on separate line.
    2.28 + *
    2.29 + * @author Ing. František Kučera (frantovo.cz)
    2.30 + */
    2.31 +public class SingleRecordFormatter extends AbstractFormatter {
    2.32 +
    2.33 +	public static final String NAME = "record"; // bash-completion:formatter
    2.34 +	public static final String PROPERTY_COLORFUL = "color";
    2.35 +	private final ColorfulPrintWriter out;
    2.36 +	private boolean firstResult = true;
    2.37 +
    2.38 +	public SingleRecordFormatter(FormatterContext formatterContext) {
    2.39 +		super(formatterContext);
    2.40 +		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    2.41 +		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
    2.42 +	}
    2.43 +
    2.44 +	@Override
    2.45 +	public void writeStartResultSet(ColumnsHeader header) {
    2.46 +		super.writeStartResultSet(header);
    2.47 +		printResultSeparator();
    2.48 +	}
    2.49 +
    2.50 +	@Override
    2.51 +	public void writeStartRow() {
    2.52 +		super.writeStartRow();
    2.53 +		printRecordSeparator();
    2.54 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
    2.55 +		out.print(getCurrentRowCount());
    2.56 +		println();
    2.57 +	}
    2.58 +
    2.59 +	@Override
    2.60 +	public void writeColumnValue(Object value) {
    2.61 +		super.writeColumnValue(value);
    2.62 +		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
    2.63 +		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
    2.64 +		out.print(String.valueOf(value));
    2.65 +		println();
    2.66 +	}
    2.67 +
    2.68 +	@Override
    2.69 +	public void writeUpdatesResult(int updatedRowsCount) {
    2.70 +		super.writeUpdatesResult(updatedRowsCount);
    2.71 +		printResultSeparator();
    2.72 +		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
    2.73 +		out.println(updatedRowsCount);
    2.74 +		printBellAndFlush();
    2.75 +	}
    2.76 +
    2.77 +	private void printBellAndFlush() {
    2.78 +		out.bell();
    2.79 +		out.flush();
    2.80 +	}
    2.81 +
    2.82 +	private void println() {
    2.83 +		out.println();
    2.84 +		printBellAndFlush();
    2.85 +	}
    2.86 +
    2.87 +	private void printRecordSeparator() {
    2.88 +		if (getCurrentRowCount() > 1) {
    2.89 +			println();
    2.90 +		}
    2.91 +	}
    2.92 +
    2.93 +	private void printResultSeparator() {
    2.94 +		if (firstResult) {
    2.95 +			firstResult = false;
    2.96 +		} else {
    2.97 +			println();
    2.98 +		}
    2.99 +	}
   2.100 +}