java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java Mon Mar 04 17:06:42 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,119 +0,0 @@
1.4 -/**
1.5 - * SQL-DK
1.6 - * Copyright © 2013 František Kučera (frantovo.cz)
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License, or
1.11 - * (at your option) any later version.
1.12 - *
1.13 - * This program is distributed in the hope that it will be useful,
1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 - * GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License
1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 - */
1.21 -package info.globalcode.sql.dk.formatting;
1.22 -
1.23 -import java.util.ArrayList;
1.24 -import java.util.List;
1.25 -
1.26 -/**
1.27 - * <p>
1.28 - * Prefetches whole result set and computes column widths. Whole table is flushed at once in
1.29 - * {@linkplain #writeEndResultSet()}.
1.30 - * </p>
1.31 - *
1.32 - * <p>
1.33 - * Long values will not overflow the cells, but whole result set must be loaded into the memory.
1.34 - * </p>
1.35 - *
1.36 - * @author Ing. František Kučera (frantovo.cz)
1.37 - */
1.38 -public class TabularPrefetchingFormatter extends TabularFormatter {
1.39 -
1.40 - public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
1.41 - private ColumnsHeader currentHeader;
1.42 - private List<Object[]> currentResultSet;
1.43 - private Object[] currentRow;
1.44 - private int currentColumnsCount;
1.45 - private boolean prefetchDone = false;
1.46 -
1.47 - public TabularPrefetchingFormatter(FormatterContext formatterContext) {
1.48 - super(formatterContext);
1.49 - }
1.50 -
1.51 - @Override
1.52 - protected int getCurrentColumnsCount() {
1.53 - if (prefetchDone) {
1.54 - return super.getCurrentColumnsCount();
1.55 - } else {
1.56 - return currentColumnsCount;
1.57 - }
1.58 - }
1.59 -
1.60 - @Override
1.61 - public void writeStartResultSet(ColumnsHeader header) {
1.62 - currentResultSet = new ArrayList<>();
1.63 - currentHeader = header;
1.64 - initColumnWidths(header.getColumnCount());
1.65 - }
1.66 -
1.67 - @Override
1.68 - public void writeStartRow() {
1.69 - currentRow = new Object[currentHeader.getColumnCount()];
1.70 - currentResultSet.add(currentRow);
1.71 - currentColumnsCount = 0;
1.72 - }
1.73 -
1.74 - @Override
1.75 - public void writeColumnValue(Object value) {
1.76 - currentRow[currentColumnsCount] = value;
1.77 - currentColumnsCount++;
1.78 - String textRepresentation = toString(value);
1.79 - /** TODO: count only printable characters (currently not an issue) */
1.80 - updateColumnWidth(currentColumnsCount, textRepresentation.length());
1.81 - }
1.82 -
1.83 - @Override
1.84 - public void writeEndRow() {
1.85 - // do nothing
1.86 - }
1.87 -
1.88 - @Override
1.89 - public void writeEndResultSet() {
1.90 - prefetchDone = true;
1.91 -
1.92 - postprocessPrefetchedResultSet(currentHeader, currentResultSet);
1.93 -
1.94 - super.writeStartResultSet(currentHeader);
1.95 -
1.96 - for (Object[] row : currentResultSet) {
1.97 - super.writeStartRow();
1.98 - for (Object cell : row) {
1.99 - super.writeColumnValue(cell);
1.100 - }
1.101 - super.writeEndRow();
1.102 - }
1.103 -
1.104 - currentColumnsCount = 0;
1.105 - currentHeader = null;
1.106 - currentRow = null;
1.107 - currentResultSet = null;
1.108 - super.writeEndResultSet();
1.109 - prefetchDone = false;
1.110 - }
1.111 -
1.112 - /**
1.113 - * Optional post-processing – override in sub-classes if needed.
1.114 - * Don't forget to {@linkplain #updateColumnWidth(int, int)}
1.115 - *
1.116 - * @param currentHeader
1.117 - * @param currentResultSet
1.118 - */
1.119 - protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
1.120 - }
1.121 -
1.122 -}