java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.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, version 3 of the License.
*
* 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.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 <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>
* or <a href="https://relational-pipes.globalcode.info/">Relational pipes</a>.
*
* @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();
}
}
}