java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
branchv_0
changeset 88 102ba0fcb07f
child 142 da1e38386d84
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java	Sat Dec 28 12:19:39 2013 +0100
     1.3 @@ -0,0 +1,124 @@
     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 info.globalcode.sql.dk.Parameter;
    1.24 +import java.util.ArrayList;
    1.25 +import java.util.List;
    1.26 +
    1.27 +/**
    1.28 + * Prefetches whole result set and computes column widths. Whole table is flushed at once in
    1.29 + * {@linkplain #writeEndResultSet()}.
    1.30 + *
    1.31 + * Long values will not overflow the cells, but whole result set must be loaded into memory.
    1.32 + *
    1.33 + * @author Ing. František Kučera (frantovo.cz)
    1.34 + */
    1.35 +public class TabularPrefetchingFormatter extends TabularFormatter {
    1.36 +
    1.37 +	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
    1.38 +	private String currentSql;
    1.39 +	private List<? extends Parameter> currentParameters;
    1.40 +	private ColumnsHeader currentHeader;
    1.41 +	private List<Object[]> currentResultSet;
    1.42 +	private Object[] currentRow;
    1.43 +	private int currentColumnsCount;
    1.44 +	private boolean prefetchDone = false;
    1.45 +
    1.46 +	public TabularPrefetchingFormatter(FormatterContext formatterContext) {
    1.47 +		super(formatterContext);
    1.48 +	}
    1.49 +
    1.50 +	@Override
    1.51 +	protected int getCurrentColumnsCount() {
    1.52 +		if (prefetchDone) {
    1.53 +			return super.getCurrentColumnsCount();
    1.54 +		} else {
    1.55 +			return currentColumnsCount;
    1.56 +		}
    1.57 +	}
    1.58 +
    1.59 +	@Override
    1.60 +	public void writeStartResultSet() {
    1.61 +		currentResultSet = new ArrayList<>();
    1.62 +	}
    1.63 +
    1.64 +	@Override
    1.65 +	public void writeQuery(String sql) {
    1.66 +		currentSql = sql;
    1.67 +	}
    1.68 +
    1.69 +	@Override
    1.70 +	public void writeParameters(List<? extends Parameter> parameters) {
    1.71 +		currentParameters = parameters;
    1.72 +	}
    1.73 +
    1.74 +	@Override
    1.75 +	public void writeColumnsHeader(ColumnsHeader header) {
    1.76 +		currentHeader = header;
    1.77 +		initColumnWidths(header.getColumnCount());
    1.78 +	}
    1.79 +
    1.80 +	@Override
    1.81 +	public void writeStartRow() {
    1.82 +		currentRow = new Object[currentHeader.getColumnCount()];
    1.83 +		currentResultSet.add(currentRow);
    1.84 +		currentColumnsCount = 0;
    1.85 +	}
    1.86 +
    1.87 +	@Override
    1.88 +	public void writeColumnValue(Object value) {
    1.89 +		currentRow[currentColumnsCount] = value;
    1.90 +		currentColumnsCount++;
    1.91 +		String textRepresentation = toString(value);
    1.92 +		/** TODO: count only printable characters (currently not an issue) */
    1.93 +		updateColumnWidth(currentColumnsCount, textRepresentation.length());
    1.94 +	}
    1.95 +
    1.96 +	@Override
    1.97 +	public void writeEndRow() {
    1.98 +		// do nothing
    1.99 +	}
   1.100 +
   1.101 +	@Override
   1.102 +	public void writeEndResultSet() {
   1.103 +		prefetchDone = true;
   1.104 +
   1.105 +		super.writeStartResultSet();
   1.106 +		super.writeQuery(currentSql);
   1.107 +		super.writeParameters(currentParameters);
   1.108 +		super.writeColumnsHeader(currentHeader);
   1.109 +
   1.110 +		for (Object[] row : currentResultSet) {
   1.111 +			super.writeStartRow();
   1.112 +			for (Object cell : row) {
   1.113 +				super.writeColumnValue(cell);
   1.114 +			}
   1.115 +			super.writeEndRow();
   1.116 +		}
   1.117 +
   1.118 +		currentColumnsCount = 0;
   1.119 +		currentSql = null;
   1.120 +		currentParameters = null;
   1.121 +		currentHeader = null;
   1.122 +		currentRow = null;
   1.123 +		currentResultSet = null;
   1.124 +		super.writeEndResultSet();
   1.125 +		prefetchDone = false;
   1.126 +	}
   1.127 +}