java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import java.util.ArrayList;
21 import java.util.List;
24 * Prefetches whole result set and computes column widths. Whole table is flushed at once in
25 * {@linkplain #writeEndResultSet()}.
27 * Long values will not overflow the cells, but whole result set must be loaded into memory.
29 * @author Ing. František Kučera (frantovo.cz)
31 public class TabularPrefetchingFormatter extends TabularFormatter {
33 public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
34 private ColumnsHeader currentHeader;
35 private List<Object[]> currentResultSet;
36 private Object[] currentRow;
37 private int currentColumnsCount;
38 private boolean prefetchDone = false;
40 public TabularPrefetchingFormatter(FormatterContext formatterContext) {
41 super(formatterContext);
45 protected int getCurrentColumnsCount() {
47 return super.getCurrentColumnsCount();
49 return currentColumnsCount;
54 public void writeStartResultSet(ColumnsHeader header) {
55 currentResultSet = new ArrayList<>();
56 currentHeader = header;
57 initColumnWidths(header.getColumnCount());
61 public void writeStartRow() {
62 currentRow = new Object[currentHeader.getColumnCount()];
63 currentResultSet.add(currentRow);
64 currentColumnsCount = 0;
68 public void writeColumnValue(Object value) {
69 currentRow[currentColumnsCount] = value;
70 currentColumnsCount++;
71 String textRepresentation = toString(value);
72 /** TODO: count only printable characters (currently not an issue) */
73 updateColumnWidth(currentColumnsCount, textRepresentation.length());
77 public void writeEndRow() {
82 public void writeEndResultSet() {
85 super.writeStartResultSet(currentHeader);
87 for (Object[] row : currentResultSet) {
88 super.writeStartRow();
89 for (Object cell : row) {
90 super.writeColumnValue(cell);
95 currentColumnsCount = 0;
98 currentResultSet = null;
99 super.writeEndResultSet();
100 prefetchDone = false;