SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
--- 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();
+ }
+ }
+}