java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java
3 * Copyright © 2015 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.ColorfulPrintWriter;
21 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
22 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
23 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
28 * Prints results in the recfile format. This format is both human- and machine- readable. Can be
29 * processed by the <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>
30 * or <a href="https://relational-pipes.globalcode.info/">Relational pipes</a>.
32 * @author Ing. František Kučera (frantovo.cz)
34 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
35 public class RecfileFormatter extends AbstractFormatter {
37 private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName());
38 public static final String NAME = "recfile"; // bash-completion:formatter
39 private final ColorfulPrintWriter out;
40 private boolean firstResult = true;
42 public RecfileFormatter(FormatterContext formatterContext) {
43 super(formatterContext);
44 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
45 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
49 public void writeStartResultSet(ColumnsHeader header) {
50 super.writeStartResultSet(header);
51 printResultSeparator();
52 out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: ");
53 printRecfileValue(getCurrentRelationName());
54 // TODO: declare attribute data types where jdbc→recfile mapping is possible
58 public void writeStartRow() {
59 super.writeStartRow();
64 public void writeColumnValue(Object value) {
65 super.writeColumnValue(value);
66 String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
67 out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
68 printRecfileValue(value);
72 public void writeUpdatesResult(int updatedRowsCount) {
73 super.writeUpdatesResult(updatedRowsCount);
74 printResultSeparator();
75 log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
79 public void writeEndBatch() {
80 super.writeEndBatch();
84 private void println() {
89 private void printRecfileValue(Object value) {
91 // TODO: null values in recfiles?
93 for (char ch : value.toString().toCharArray()) {
96 out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
103 private void printResultSeparator() {