java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
/**
* 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;
import info.globalcode.sql.dk.Functions;
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;
/**
* 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)
*/
@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
public class SingleRecordFormatter extends AbstractFormatter {
public static final String NAME = "record"; // bash-completion:formatter
private final ColorfulPrintWriter out;
private boolean firstResult = true;
public SingleRecordFormatter(FormatterContext formatterContext) {
super(formatterContext);
out = new ColorfulPrintWriter(formatterContext.getOutputStream());
out.setColorful(formatterContext.getProperties().getBoolean(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 + ": ");
Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red);
println();
}
private static String toString(Object value) {
return String.valueOf(value);
}
@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();
}
}
}