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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.formatting;
19 import info.globalcode.sql.dk.ColorfulPrintWriter;
20 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
21 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
22 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
27 * Prints results in the recfile format. This format is both human- and machine- readable. Can be
28 * processed by the <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>
29 * or <a href="https://relational-pipes.globalcode.info/">Relational pipes</a>.
31 * @author Ing. František Kučera (frantovo.cz)
33 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
34 public class RecfileFormatter extends AbstractFormatter {
36 private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName());
37 public static final String NAME = "recfile"; // bash-completion:formatter
38 private final ColorfulPrintWriter out;
39 private boolean firstResult = true;
41 public RecfileFormatter(FormatterContext formatterContext) {
42 super(formatterContext);
43 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
44 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
48 public void writeStartResultSet(ColumnsHeader header) {
49 super.writeStartResultSet(header);
50 printResultSeparator();
51 out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: ");
52 printRecfileValue(getCurrentRelationName());
53 // TODO: declare attribute data types where jdbc→recfile mapping is possible
57 public void writeStartRow() {
58 super.writeStartRow();
63 public void writeColumnValue(Object value) {
64 super.writeColumnValue(value);
65 String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
66 out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
67 printRecfileValue(value);
71 public void writeUpdatesResult(int updatedRowsCount) {
72 super.writeUpdatesResult(updatedRowsCount);
73 printResultSeparator();
74 log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
78 public void writeEndBatch() {
79 super.writeEndBatch();
83 private void println() {
88 private void printRecfileValue(Object value) {
90 // TODO: null values in recfiles?
92 for (char ch : value.toString().toCharArray()) {
95 out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
102 private void printResultSeparator() {