java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/RecfileFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 23 Sep 2022 18:05:50 +0200
branchv_0
changeset 254 c4b901ff0703
parent 250 aae5009bd0af
permissions -rw-r--r--
recfile formatter: write record count (as a comment at the end of the relation)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
250
aae5009bd0af fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 248
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
package info.globalcode.sql.dk.formatting;
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    19
import info.globalcode.sql.dk.ColorfulPrintWriter;
206
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    20
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    21
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
e2f24eea8543 property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents: 202
diff changeset
    22
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    23
import java.util.logging.Level;
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    24
import java.util.logging.Logger;
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
/**
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    27
 * Prints results in the recfile format. This format is both human- and machine- readable. Can be
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    28
 * processed by the <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    29
 * or <a href="https://relational-pipes.globalcode.info/">Relational pipes</a>.
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 * @author Ing. František Kučera (frantovo.cz)
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 */
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    33
@PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    34
public class RecfileFormatter extends AbstractFormatter {
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    35
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    36
	private static final Logger log = Logger.getLogger(RecfileFormatter.class.getName());
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    37
	public static final String NAME = "recfile"; // bash-completion:formatter
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    38
	private final ColorfulPrintWriter out;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    39
	private boolean firstResult = true;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    40
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    41
	public RecfileFormatter(FormatterContext formatterContext) {
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    42
		super(formatterContext);
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    43
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    44
		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, false));
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    45
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    47
	@Override
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    48
	public void writeStartResultSet(ColumnsHeader header) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    49
		super.writeStartResultSet(header);
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    50
		printResultSeparator();
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    51
		out.print(ColorfulPrintWriter.TerminalColor.Red, "%rec: ");
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    52
		printRecfileValue(getCurrentRelationName());
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    53
		// TODO: declare attribute data types where jdbc→recfile mapping is possible
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    54
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    56
	@Override
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    57
	public void writeStartRow() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    58
		super.writeStartRow();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    59
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	@Override
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	public void writeColumnValue(Object value) {
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		super.writeColumnValue(value);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    65
		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    66
		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    67
		printRecfileValue(value);
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    68
	}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 207
diff changeset
    69
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	@Override
254
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    71
	public void writeEndResultSet() {
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    72
		super.writeEndResultSet();
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    73
		out.println();
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    74
		out.print("# Record count: ");
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    75
		out.println(getCurrentRowCount());
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    76
		out.flush();
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    77
	}
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    78
c4b901ff0703 recfile formatter: write record count (as a comment at the end of the relation)
František Kučera <franta-hg@frantovo.cz>
parents: 250
diff changeset
    79
	@Override
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    80
	public void writeUpdatesResult(int updatedRowsCount) {
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    81
		super.writeUpdatesResult(updatedRowsCount);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    82
		printResultSeparator();
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    83
		log.log(Level.INFO, "Updated records: {0}", updatedRowsCount);
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    84
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    85
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    86
	@Override
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    87
	public void writeEndBatch() {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    88
		super.writeEndBatch();
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    89
		println();
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    91
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    92
	private void println() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    93
		out.println();
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    94
		out.flush();
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    95
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    96
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    97
	private void printRecfileValue(Object value) {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    98
		if (value == null) {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    99
			// TODO: null values in recfiles?
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   100
		} else {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   101
			for (char ch : value.toString().toCharArray()) {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   102
				out.print(ch);
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   103
				if (ch == '\n') {
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   104
					out.print(ColorfulPrintWriter.TerminalColor.Magenta, "+ ");
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   105
				}
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   106
			}
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   107
		}
248
7f81cfa150d0 transform the record formatter into the recfile formatter
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   108
		println();
202
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   109
	}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   110
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   111
	private void printResultSeparator() {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   112
		if (firstResult) {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   113
			firstResult = false;
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   114
		} else {
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   115
			println();
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   116
		}
01078e09b85b SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   117
	}
60
d4e88172a363 SingleValueFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
}