java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.formatting;
19 import java.util.ArrayList;
20 import java.util.List;
24 * Prefetches whole result set and computes column widths. Whole table is flushed at once in
25 * {@linkplain #writeEndResultSet()}.
29 * Long values will not overflow the cells, but whole result set must be loaded into the memory.
32 * @author Ing. František Kučera (frantovo.cz)
34 public class TabularPrefetchingFormatter extends TabularFormatter {
36 public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
37 private ColumnsHeader currentHeader;
38 private List<Object[]> currentResultSet;
39 private Object[] currentRow;
40 private int currentColumnsCount;
41 private boolean prefetchDone = false;
43 public TabularPrefetchingFormatter(FormatterContext formatterContext) {
44 super(formatterContext);
48 protected int getCurrentColumnsCount() {
50 return super.getCurrentColumnsCount();
52 return currentColumnsCount;
57 public void writeStartResultSet(ColumnsHeader header) {
58 currentResultSet = new ArrayList<>();
59 currentHeader = header;
60 initColumnWidths(header.getColumnCount());
64 public void writeStartRow() {
65 currentRow = new Object[currentHeader.getColumnCount()];
66 currentResultSet.add(currentRow);
67 currentColumnsCount = 0;
71 public void writeColumnValue(Object value) {
72 currentRow[currentColumnsCount] = value;
73 currentColumnsCount++;
74 String textRepresentation = toString(value);
75 /** TODO: count only printable characters (currently not an issue) */
76 updateColumnWidth(currentColumnsCount, textRepresentation.length());
80 public void writeEndRow() {
85 public void writeEndResultSet() {
88 postprocessPrefetchedResultSet(currentHeader, currentResultSet);
90 super.writeStartResultSet(currentHeader);
92 for (Object[] row : currentResultSet) {
93 super.writeStartRow();
94 for (Object cell : row) {
95 super.writeColumnValue(cell);
100 currentColumnsCount = 0;
101 currentHeader = null;
103 currentResultSet = null;
104 super.writeEndResultSet();
105 prefetchDone = false;
109 * Optional post-processing – override in sub-classes if needed.
110 * Don't forget to {@linkplain #updateColumnWidth(int, int)}
112 * @param currentHeader
113 * @param currentResultSet
115 protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {