java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 30 Apr 2019 19:49:17 +0200
branchv_0
changeset 248 7f81cfa150d0
parent 238 java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java@4a1864c3e867
child 250 aae5009bd0af
permissions -rw-r--r--
transform the record formatter into the recfile formatter
still a human-readable format and very similar
but also machine-readable – can be processed in GNU Recutils and Relational pipes
     1 /**
     2  * SQL-DK
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    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;
    26 
    27 /**
    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>.
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
    35 public class RecfileFormatter extends AbstractFormatter {
    36 
    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;
    41 
    42 	public RecfileFormatter(FormatterContext formatterContext) {
    43 		super(formatterContext);
    44 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    45 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
    46 	}
    47 
    48 	@Override
    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
    55 	}
    56 
    57 	@Override
    58 	public void writeStartRow() {
    59 		super.writeStartRow();
    60 		println();
    61 	}
    62 
    63 	@Override
    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);
    69 	}
    70 
    71 	@Override
    72 	public void writeUpdatesResult(int updatedRowsCount) {
    73 		super.writeUpdatesResult(updatedRowsCount);
    74 		printResultSeparator();
    75 		log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
    76 	}
    77 
    78 	@Override
    79 	public void writeEndBatch() {
    80 		super.writeEndBatch();
    81 		println();
    82 	}
    83 
    84 	private void println() {
    85 		out.println();
    86 		out.flush();
    87 	}
    88 
    89 	private void printRecfileValue(Object value) {
    90 		if (value == null) {
    91 			// TODO: null values in recfiles?
    92 		} else {
    93 			for (char ch : value.toString().toCharArray()) {
    94 				out.print(ch);
    95 				if (ch == '\n') {
    96 					out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
    97 				}
    98 			}
    99 		}
   100 		println();
   101 	}
   102 
   103 	private void printResultSeparator() {
   104 		if (firstResult) {
   105 			firstResult = false;
   106 		} else {
   107 			println();
   108 		}
   109 	}
   110 }