java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java Tue Apr 30 19:49:17 2019 +0200
1.3 @@ -0,0 +1,110 @@
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 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
1.25 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
1.26 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
1.27 +import java.util.logging.Level;
1.28 +import java.util.logging.Logger;
1.29 +
1.30 +/**
1.31 + * Prints results in the recfile format. This format is both human- and machine- readable. Can be
1.32 + * processed by the <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>
1.33 + * or <a href="https://relational-pipes.globalcode.info/">Relational pipes</a>.
1.34 + *
1.35 + * @author Ing. František Kučera (frantovo.cz)
1.36 + */
1.37 +@PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
1.38 +public class RecfileFormatter extends AbstractFormatter {
1.39 +
1.40 + private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName());
1.41 + public static final String NAME = "recfile"; // bash-completion:formatter
1.42 + private final ColorfulPrintWriter out;
1.43 + private boolean firstResult = true;
1.44 +
1.45 + public RecfileFormatter(FormatterContext formatterContext) {
1.46 + super(formatterContext);
1.47 + out = new ColorfulPrintWriter(formatterContext.getOutputStream());
1.48 + out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
1.49 + }
1.50 +
1.51 + @Override
1.52 + public void writeStartResultSet(ColumnsHeader header) {
1.53 + super.writeStartResultSet(header);
1.54 + printResultSeparator();
1.55 + out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: ");
1.56 + printRecfileValue(getCurrentRelationName());
1.57 + // TODO: declare attribute data types where jdbc→recfile mapping is possible
1.58 + }
1.59 +
1.60 + @Override
1.61 + public void writeStartRow() {
1.62 + super.writeStartRow();
1.63 + println();
1.64 + }
1.65 +
1.66 + @Override
1.67 + public void writeColumnValue(Object value) {
1.68 + super.writeColumnValue(value);
1.69 + String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
1.70 + out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
1.71 + printRecfileValue(value);
1.72 + }
1.73 +
1.74 + @Override
1.75 + public void writeUpdatesResult(int updatedRowsCount) {
1.76 + super.writeUpdatesResult(updatedRowsCount);
1.77 + printResultSeparator();
1.78 + log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
1.79 + }
1.80 +
1.81 + @Override
1.82 + public void writeEndBatch() {
1.83 + super.writeEndBatch();
1.84 + println();
1.85 + }
1.86 +
1.87 + private void println() {
1.88 + out.println();
1.89 + out.flush();
1.90 + }
1.91 +
1.92 + private void printRecfileValue(Object value) {
1.93 + if (value == null) {
1.94 + // TODO: null values in recfiles?
1.95 + } else {
1.96 + for (char ch : value.toString().toCharArray()) {
1.97 + out.print(ch);
1.98 + if (ch == '\n') {
1.99 + out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
1.100 + }
1.101 + }
1.102 + }
1.103 + println();
1.104 + }
1.105 +
1.106 + private void printResultSeparator() {
1.107 + if (firstResult) {
1.108 + firstResult = false;
1.109 + } else {
1.110 + println();
1.111 + }
1.112 + }
1.113 +}