franta-hg@60: /** franta-hg@60: * SQL-DK franta-hg@202: * Copyright © 2015 František Kučera (frantovo.cz) franta-hg@60: * franta-hg@60: * This program is free software: you can redistribute it and/or modify franta-hg@60: * it under the terms of the GNU General Public License as published by franta-hg@250: * the Free Software Foundation, version 3 of the License. franta-hg@60: * franta-hg@60: * This program is distributed in the hope that it will be useful, franta-hg@60: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@60: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@60: * GNU General Public License for more details. franta-hg@60: * franta-hg@60: * You should have received a copy of the GNU General Public License franta-hg@60: * along with this program. If not, see . franta-hg@60: */ franta-hg@60: package info.globalcode.sql.dk.formatting; franta-hg@60: franta-hg@202: import info.globalcode.sql.dk.ColorfulPrintWriter; franta-hg@206: import info.globalcode.sql.dk.configuration.PropertyDeclaration; franta-hg@206: import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL; franta-hg@206: import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION; franta-hg@248: import java.util.logging.Level; franta-hg@248: import java.util.logging.Logger; franta-hg@60: franta-hg@60: /** franta-hg@248: * Prints results in the recfile format. This format is both human- and machine- readable. Can be franta-hg@248: * processed by the GNU Recutils franta-hg@248: * or Relational pipes. franta-hg@60: * franta-hg@60: * @author Ing. František Kučera (frantovo.cz) franta-hg@60: */ franta-hg@248: @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION) franta-hg@248: public class RecfileFormatter extends AbstractFormatter { franta-hg@60: franta-hg@248: private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName()); franta-hg@248: public static final String NAME = "recfile"; // bash-completion:formatter franta-hg@202: private final ColorfulPrintWriter out; franta-hg@202: private boolean firstResult = true; franta-hg@60: franta-hg@248: public RecfileFormatter(FormatterContext formatterContext) { franta-hg@60: super(formatterContext); franta-hg@202: out = new ColorfulPrintWriter(formatterContext.getOutputStream()); franta-hg@248: out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false)); franta-hg@202: } franta-hg@202: franta-hg@202: @Override franta-hg@202: public void writeStartResultSet(ColumnsHeader header) { franta-hg@202: super.writeStartResultSet(header); franta-hg@202: printResultSeparator(); franta-hg@248: out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: "); franta-hg@248: printRecfileValue(getCurrentRelationName()); franta-hg@248: // TODO: declare attribute data types where jdbc→recfile mapping is possible franta-hg@202: } franta-hg@202: franta-hg@202: @Override franta-hg@202: public void writeStartRow() { franta-hg@202: super.writeStartRow(); franta-hg@202: println(); franta-hg@60: } franta-hg@60: franta-hg@60: @Override franta-hg@60: public void writeColumnValue(Object value) { franta-hg@60: super.writeColumnValue(value); franta-hg@202: String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel(); franta-hg@202: out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": "); franta-hg@248: printRecfileValue(value); franta-hg@218: } franta-hg@218: franta-hg@60: @Override franta-hg@142: public void writeUpdatesResult(int updatedRowsCount) { franta-hg@142: super.writeUpdatesResult(updatedRowsCount); franta-hg@202: printResultSeparator(); franta-hg@248: log.log(Level.INFO, "Updated records: {0}", updatedRowsCount); franta-hg@202: } franta-hg@202: franta-hg@248: @Override franta-hg@248: public void writeEndBatch() { franta-hg@248: super.writeEndBatch(); franta-hg@248: println(); franta-hg@60: } franta-hg@202: franta-hg@202: private void println() { franta-hg@202: out.println(); franta-hg@248: out.flush(); franta-hg@202: } franta-hg@202: franta-hg@248: private void printRecfileValue(Object value) { franta-hg@248: if (value == null) { franta-hg@248: // TODO: null values in recfiles? franta-hg@248: } else { franta-hg@248: for (char ch : value.toString().toCharArray()) { franta-hg@248: out.print(ch); franta-hg@248: if (ch == '\n') { franta-hg@248: out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ "); franta-hg@248: } franta-hg@248: } franta-hg@202: } franta-hg@248: println(); franta-hg@202: } franta-hg@202: franta-hg@202: private void printResultSeparator() { franta-hg@202: if (firstResult) { franta-hg@202: firstResult = false; franta-hg@202: } else { franta-hg@202: println(); franta-hg@202: } franta-hg@202: } franta-hg@60: }