java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
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 info.globalcode.sql.dk.Parameter;
21 import java.util.ArrayList;
22 import java.util.List;
25 * Prefetches whole result set and computes column widths. Whole table is flushed at once in
26 * {@linkplain #writeEndResultSet()}.
28 * Long values will not overflow the cells, but whole result set must be loaded into memory.
30 * @author Ing. František Kučera (frantovo.cz)
32 public class TabularPrefetchingFormatter extends TabularFormatter {
34 public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
35 private String currentSql;
36 private List<? extends Parameter> currentParameters;
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() {
58 currentResultSet = new ArrayList<>();
62 public void writeQuery(String sql) {
67 public void writeParameters(List<? extends Parameter> parameters) {
68 currentParameters = parameters;
72 public void writeColumnsHeader(ColumnsHeader header) {
73 currentHeader = header;
74 initColumnWidths(header.getColumnCount());
78 public void writeStartRow() {
79 currentRow = new Object[currentHeader.getColumnCount()];
80 currentResultSet.add(currentRow);
81 currentColumnsCount = 0;
85 public void writeColumnValue(Object value) {
86 currentRow[currentColumnsCount] = value;
87 currentColumnsCount++;
88 String textRepresentation = toString(value);
89 /** TODO: count only printable characters (currently not an issue) */
90 updateColumnWidth(currentColumnsCount, textRepresentation.length());
94 public void writeEndRow() {
99 public void writeEndResultSet() {
102 super.writeStartResultSet();
103 super.writeQuery(currentSql);
104 super.writeParameters(currentParameters);
105 super.writeColumnsHeader(currentHeader);
107 for (Object[] row : currentResultSet) {
108 super.writeStartRow();
109 for (Object cell : row) {
110 super.writeColumnValue(cell);
115 currentColumnsCount = 0;
117 currentParameters = null;
118 currentHeader = null;
120 currentResultSet = null;
121 super.writeEndResultSet();
122 prefetchDone = false;