diff -r 7e08730da258 -r 4a1864c3e867 java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java Mon Mar 04 17:06:42 2019 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -/** - * 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, 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 . - */ -package info.globalcode.sql.dk.formatting; - -import java.util.ArrayList; -import java.util.List; - -/** - *

- * Prefetches whole result set and computes column widths. Whole table is flushed at once in - * {@linkplain #writeEndResultSet()}. - *

- * - *

- * Long values will not overflow the cells, but whole result set must be loaded into the memory. - *

- * - * @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 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 currentResultSet) { - } - -}