java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2013 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, version 3 of the License.
 *
 * 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 java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Prefetches whole result set and computes column widths. Whole table is flushed at once in
 * {@linkplain #writeEndResultSet()}.
 * </p>
 *
 * <p>
 * Long values will not overflow the cells, but whole result set must be loaded into the memory.
 * </p>
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class TabularPrefetchingFormatter extends TabularFormatter {

	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
	private ColumnsHeader currentHeader;
	private List<Object[]> currentResultSet;
	private Object[] currentRow;
	private int currentColumnsCount;
	private boolean prefetchDone = false;

	public TabularPrefetchingFormatter(FormatterContext formatterContext) {
		super(formatterContext);
	}

	@Override
	protected int getCurrentColumnsCount() {
		if (prefetchDone) {
			return super.getCurrentColumnsCount();
		} else {
			return currentColumnsCount;
		}
	}

	@Override
	public void writeStartResultSet(ColumnsHeader header) {
		currentResultSet = new ArrayList<>();
		currentHeader = header;
		initColumnWidths(header.getColumnCount());
	}

	@Override
	public void writeStartRow() {
		currentRow = new Object[currentHeader.getColumnCount()];
		currentResultSet.add(currentRow);
		currentColumnsCount = 0;
	}

	@Override
	public void writeColumnValue(Object value) {
		currentRow[currentColumnsCount] = value;
		currentColumnsCount++;
		String textRepresentation = toString(value);
		/** TODO: count only printable characters (currently not an issue) */
		updateColumnWidth(currentColumnsCount, textRepresentation.length());
	}

	@Override
	public void writeEndRow() {
		// do nothing
	}

	@Override
	public void writeEndResultSet() {
		prefetchDone = true;

		postprocessPrefetchedResultSet(currentHeader, currentResultSet);

		super.writeStartResultSet(currentHeader);

		for (Object[] row : currentResultSet) {
			super.writeStartRow();
			for (Object cell : row) {
				super.writeColumnValue(cell);
			}
			super.writeEndRow();
		}

		currentColumnsCount = 0;
		currentHeader = null;
		currentRow = null;
		currentResultSet = null;
		super.writeEndResultSet();
		prefetchDone = false;
	}

	/**
	 * Optional post-processing – override in sub-classes if needed.
	 * Don't forget to {@linkplain #updateColumnWidth(int, int)}
	 *
	 * @param currentHeader
	 * @param currentResultSet
	 */
	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
	}

}