java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 248 7f81cfa150d0
child 254 c4b901ff0703
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package info.globalcode.sql.dk.formatting;
    18 
    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;
    25 
    26 /**
    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>.
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
    34 public class RecfileFormatter extends AbstractFormatter {
    35 
    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;
    40 
    41 	public RecfileFormatter(FormatterContext formatterContext) {
    42 		super(formatterContext);
    43 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    44 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
    45 	}
    46 
    47 	@Override
    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
    54 	}
    55 
    56 	@Override
    57 	public void writeStartRow() {
    58 		super.writeStartRow();
    59 		println();
    60 	}
    61 
    62 	@Override
    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);
    68 	}
    69 
    70 	@Override
    71 	public void writeUpdatesResult(int updatedRowsCount) {
    72 		super.writeUpdatesResult(updatedRowsCount);
    73 		printResultSeparator();
    74 		log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
    75 	}
    76 
    77 	@Override
    78 	public void writeEndBatch() {
    79 		super.writeEndBatch();
    80 		println();
    81 	}
    82 
    83 	private void println() {
    84 		out.println();
    85 		out.flush();
    86 	}
    87 
    88 	private void printRecfileValue(Object value) {
    89 		if (value == null) {
    90 			// TODO: null values in recfiles?
    91 		} else {
    92 			for (char ch : value.toString().toCharArray()) {
    93 				out.print(ch);
    94 				if (ch == '\n') {
    95 					out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
    96 				}
    97 			}
    98 		}
    99 		println();
   100 	}
   101 
   102 	private void printResultSeparator() {
   103 		if (firstResult) {
   104 			firstResult = false;
   105 		} else {
   106 			println();
   107 		}
   108 	}
   109 }