diff -r 3380ae5275be -r 7f81cfa150d0 java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java Tue Apr 30 19:49:17 2019 +0200 @@ -0,0 +1,110 @@ +/** + * 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 . + */ +package info.globalcode.sql.dk.formatting; + +import info.globalcode.sql.dk.ColorfulPrintWriter; +import info.globalcode.sql.dk.configuration.PropertyDeclaration; +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL; +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Prints results in the recfile format. This format is both human- and machine- readable. Can be + * processed by the GNU Recutils + * or Relational pipes. + * + * @author Ing. František Kučera (frantovo.cz) + */ +@PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION) +public class RecfileFormatter extends AbstractFormatter { + + private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName()); + public static final String NAME = "recfile"; // bash-completion:formatter + private final ColorfulPrintWriter out; + private boolean firstResult = true; + + public RecfileFormatter(FormatterContext formatterContext) { + super(formatterContext); + out = new ColorfulPrintWriter(formatterContext.getOutputStream()); + out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false)); + } + + @Override + public void writeStartResultSet(ColumnsHeader header) { + super.writeStartResultSet(header); + printResultSeparator(); + out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: "); + printRecfileValue(getCurrentRelationName()); + // TODO: declare attribute data types where jdbc→recfile mapping is possible + } + + @Override + public void writeStartRow() { + super.writeStartRow(); + println(); + } + + @Override + public void writeColumnValue(Object value) { + super.writeColumnValue(value); + String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel(); + out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": "); + printRecfileValue(value); + } + + @Override + public void writeUpdatesResult(int updatedRowsCount) { + super.writeUpdatesResult(updatedRowsCount); + printResultSeparator(); + log.log(Level.INFO, "Updated records: {0}", updatedRowsCount); + } + + @Override + public void writeEndBatch() { + super.writeEndBatch(); + println(); + } + + private void println() { + out.println(); + out.flush(); + } + + private void printRecfileValue(Object value) { + if (value == null) { + // TODO: null values in recfiles? + } else { + for (char ch : value.toString().toCharArray()) { + out.print(ch); + if (ch == '\n') { + out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ "); + } + } + } + println(); + } + + private void printResultSeparator() { + if (firstResult) { + firstResult = false; + } else { + println(); + } + } +}