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 202 01078e09b85b
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
--- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Sun May 24 19:17:50 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Sun Jun 21 16:21:51 2015 +0200
@@ -21,6 +21,7 @@
 import static info.globalcode.sql.dk.Functions.findByName;
 import info.globalcode.sql.dk.formatting.DsvFormatter;
 import info.globalcode.sql.dk.formatting.SilentFormatter;
+import info.globalcode.sql.dk.formatting.SingleRecordFormatter;
 import info.globalcode.sql.dk.formatting.SingleValueFormatter;
 import info.globalcode.sql.dk.formatting.TabularFormatter;
 import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
@@ -65,6 +66,7 @@
 		Collection<FormatterDefinition> l = new ArrayList<>();
 		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
 		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
+		l.add(new FormatterDefinition(SingleRecordFormatter.NAME, SingleRecordFormatter.class.getName()));
 		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
 		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
 		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
@@ -85,6 +87,8 @@
 	}
 
 	/**
+	 * @param name
+	 * @return 
 	 * @throws ConfigurationException if no database with this name is configured
 	 */
 	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sun Jun 21 16:21:51 2015 +0200
@@ -0,0 +1,97 @@
+/**
+ * SQL-DK
+ * Copyright © 2015 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.formatting;
+
+import info.globalcode.sql.dk.ColorfulPrintWriter;
+
+/**
+ * Formatter intended for printing one record (or few records) with many columns.
+ * Prints each colum name and its value on separate line.
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class SingleRecordFormatter extends AbstractFormatter {
+
+	public static final String NAME = "record"; // bash-completion:formatter
+	public static final String PROPERTY_COLORFUL = "color";
+	private final ColorfulPrintWriter out;
+	private boolean firstResult = true;
+
+	public SingleRecordFormatter(FormatterContext formatterContext) {
+		super(formatterContext);
+		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
+		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
+	}
+
+	@Override
+	public void writeStartResultSet(ColumnsHeader header) {
+		super.writeStartResultSet(header);
+		printResultSeparator();
+	}
+
+	@Override
+	public void writeStartRow() {
+		super.writeStartRow();
+		printRecordSeparator();
+		out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: ");
+		out.print(getCurrentRowCount());
+		println();
+	}
+
+	@Override
+	public void writeColumnValue(Object value) {
+		super.writeColumnValue(value);
+		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
+		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
+		out.print(String.valueOf(value));
+		println();
+	}
+
+	@Override
+	public void writeUpdatesResult(int updatedRowsCount) {
+		super.writeUpdatesResult(updatedRowsCount);
+		printResultSeparator();
+		out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: ");
+		out.println(updatedRowsCount);
+		printBellAndFlush();
+	}
+
+	private void printBellAndFlush() {
+		out.bell();
+		out.flush();
+	}
+
+	private void println() {
+		out.println();
+		printBellAndFlush();
+	}
+
+	private void printRecordSeparator() {
+		if (getCurrentRowCount() > 1) {
+			println();
+		}
+	}
+
+	private void printResultSeparator() {
+		if (firstResult) {
+			firstResult = false;
+		} else {
+			println();
+		}
+	}
+}