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@60: * the Free Software Foundation, either version 3 of the License, or franta-hg@60: * (at your option) any later version. 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@60: franta-hg@60: /** franta-hg@202: * Formatter intended for printing one record (or few records) with many columns. franta-hg@202: * Prints each colum name and its value on separate line. franta-hg@60: * franta-hg@60: * @author Ing. František Kučera (frantovo.cz) franta-hg@60: */ franta-hg@207: @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION) franta-hg@202: public class SingleRecordFormatter extends AbstractFormatter { franta-hg@60: franta-hg@202: public static final String NAME = "record"; // bash-completion:formatter franta-hg@202: private final ColorfulPrintWriter out; franta-hg@202: private boolean firstResult = true; franta-hg@60: franta-hg@202: public SingleRecordFormatter(FormatterContext formatterContext) { franta-hg@60: super(formatterContext); franta-hg@202: out = new ColorfulPrintWriter(formatterContext.getOutputStream()); franta-hg@206: out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); 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@202: } franta-hg@202: franta-hg@202: @Override franta-hg@202: public void writeStartRow() { franta-hg@202: super.writeStartRow(); franta-hg@202: printRecordSeparator(); franta-hg@202: out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: "); franta-hg@202: out.print(getCurrentRowCount()); 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@60: out.print(String.valueOf(value)); franta-hg@202: println(); franta-hg@60: } franta-hg@60: franta-hg@60: @Override franta-hg@142: public void writeUpdatesResult(int updatedRowsCount) { franta-hg@142: super.writeUpdatesResult(updatedRowsCount); franta-hg@202: printResultSeparator(); franta-hg@202: out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: "); franta-hg@202: out.println(updatedRowsCount); franta-hg@202: printBellAndFlush(); franta-hg@202: } franta-hg@202: franta-hg@202: private void printBellAndFlush() { franta-hg@202: out.bell(); franta-hg@60: out.flush(); franta-hg@60: } franta-hg@202: franta-hg@202: private void println() { franta-hg@202: out.println(); franta-hg@202: printBellAndFlush(); franta-hg@202: } franta-hg@202: franta-hg@202: private void printRecordSeparator() { franta-hg@202: if (getCurrentRowCount() > 1) { franta-hg@202: println(); franta-hg@202: } 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: }