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

/**
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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();
		}
	}
}