java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 07 Jan 2014 21:54:59 +0100
branchv_0
changeset 142 da1e38386d84
parent 88 102ba0fcb07f
child 155 eb3676c6929b
permissions -rw-r--r--
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
franta-hg@88
     1
/**
franta-hg@88
     2
 * SQL-DK
franta-hg@88
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@88
     4
 *
franta-hg@88
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@88
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@88
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@88
     8
 * (at your option) any later version.
franta-hg@88
     9
 *
franta-hg@88
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@88
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@88
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@88
    13
 * GNU General Public License for more details.
franta-hg@88
    14
 *
franta-hg@88
    15
 * You should have received a copy of the GNU General Public License
franta-hg@88
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@88
    17
 */
franta-hg@88
    18
package info.globalcode.sql.dk.formatting;
franta-hg@88
    19
franta-hg@88
    20
import java.util.ArrayList;
franta-hg@88
    21
import java.util.List;
franta-hg@88
    22
franta-hg@88
    23
/**
franta-hg@88
    24
 * Prefetches whole result set and computes column widths. Whole table is flushed at once in
franta-hg@88
    25
 * {@linkplain #writeEndResultSet()}.
franta-hg@88
    26
 *
franta-hg@88
    27
 * Long values will not overflow the cells, but whole result set must be loaded into memory.
franta-hg@88
    28
 *
franta-hg@88
    29
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@88
    30
 */
franta-hg@88
    31
public class TabularPrefetchingFormatter extends TabularFormatter {
franta-hg@88
    32
franta-hg@88
    33
	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
franta-hg@88
    34
	private ColumnsHeader currentHeader;
franta-hg@88
    35
	private List<Object[]> currentResultSet;
franta-hg@88
    36
	private Object[] currentRow;
franta-hg@88
    37
	private int currentColumnsCount;
franta-hg@88
    38
	private boolean prefetchDone = false;
franta-hg@88
    39
franta-hg@88
    40
	public TabularPrefetchingFormatter(FormatterContext formatterContext) {
franta-hg@88
    41
		super(formatterContext);
franta-hg@88
    42
	}
franta-hg@88
    43
franta-hg@88
    44
	@Override
franta-hg@88
    45
	protected int getCurrentColumnsCount() {
franta-hg@88
    46
		if (prefetchDone) {
franta-hg@88
    47
			return super.getCurrentColumnsCount();
franta-hg@88
    48
		} else {
franta-hg@88
    49
			return currentColumnsCount;
franta-hg@88
    50
		}
franta-hg@88
    51
	}
franta-hg@88
    52
franta-hg@88
    53
	@Override
franta-hg@142
    54
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@88
    55
		currentResultSet = new ArrayList<>();
franta-hg@88
    56
		currentHeader = header;
franta-hg@88
    57
		initColumnWidths(header.getColumnCount());
franta-hg@88
    58
	}
franta-hg@88
    59
franta-hg@88
    60
	@Override
franta-hg@88
    61
	public void writeStartRow() {
franta-hg@88
    62
		currentRow = new Object[currentHeader.getColumnCount()];
franta-hg@88
    63
		currentResultSet.add(currentRow);
franta-hg@88
    64
		currentColumnsCount = 0;
franta-hg@88
    65
	}
franta-hg@88
    66
franta-hg@88
    67
	@Override
franta-hg@88
    68
	public void writeColumnValue(Object value) {
franta-hg@88
    69
		currentRow[currentColumnsCount] = value;
franta-hg@88
    70
		currentColumnsCount++;
franta-hg@88
    71
		String textRepresentation = toString(value);
franta-hg@88
    72
		/** TODO: count only printable characters (currently not an issue) */
franta-hg@88
    73
		updateColumnWidth(currentColumnsCount, textRepresentation.length());
franta-hg@88
    74
	}
franta-hg@88
    75
franta-hg@88
    76
	@Override
franta-hg@88
    77
	public void writeEndRow() {
franta-hg@88
    78
		// do nothing
franta-hg@88
    79
	}
franta-hg@88
    80
franta-hg@88
    81
	@Override
franta-hg@88
    82
	public void writeEndResultSet() {
franta-hg@88
    83
		prefetchDone = true;
franta-hg@88
    84
franta-hg@142
    85
		super.writeStartResultSet(currentHeader);
franta-hg@88
    86
franta-hg@88
    87
		for (Object[] row : currentResultSet) {
franta-hg@88
    88
			super.writeStartRow();
franta-hg@88
    89
			for (Object cell : row) {
franta-hg@88
    90
				super.writeColumnValue(cell);
franta-hg@88
    91
			}
franta-hg@88
    92
			super.writeEndRow();
franta-hg@88
    93
		}
franta-hg@88
    94
franta-hg@88
    95
		currentColumnsCount = 0;
franta-hg@88
    96
		currentHeader = null;
franta-hg@88
    97
		currentRow = null;
franta-hg@88
    98
		currentResultSet = null;
franta-hg@88
    99
		super.writeEndResultSet();
franta-hg@88
   100
		prefetchDone = false;
franta-hg@88
   101
	}
franta-hg@88
   102
}