author | František Kučera <franta-hg@frantovo.cz> |
Mon, 18 May 2015 00:36:23 +0200 | |
branch | v_0 |
changeset 197 | 7a2f535017e4 |
parent 155 | eb3676c6929b |
child 224 | 36db9fd27436 |
permissions | -rw-r--r-- |
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@155 | 24 |
* <p> |
franta-hg@88 | 25 |
* Prefetches whole result set and computes column widths. Whole table is flushed at once in |
franta-hg@88 | 26 |
* {@linkplain #writeEndResultSet()}. |
franta-hg@155 | 27 |
* </p> |
franta-hg@88 | 28 |
* |
franta-hg@155 | 29 |
* <p> |
franta-hg@155 | 30 |
* Long values will not overflow the cells, but whole result set must be loaded into the memory. |
franta-hg@155 | 31 |
* </p> |
franta-hg@88 | 32 |
* |
franta-hg@88 | 33 |
* @author Ing. František Kučera (frantovo.cz) |
franta-hg@88 | 34 |
*/ |
franta-hg@88 | 35 |
public class TabularPrefetchingFormatter extends TabularFormatter { |
franta-hg@88 | 36 |
|
franta-hg@88 | 37 |
public static final String NAME = "tabular-prefetching"; // bash-completion:formatter |
franta-hg@88 | 38 |
private ColumnsHeader currentHeader; |
franta-hg@88 | 39 |
private List<Object[]> currentResultSet; |
franta-hg@88 | 40 |
private Object[] currentRow; |
franta-hg@88 | 41 |
private int currentColumnsCount; |
franta-hg@88 | 42 |
private boolean prefetchDone = false; |
franta-hg@88 | 43 |
|
franta-hg@88 | 44 |
public TabularPrefetchingFormatter(FormatterContext formatterContext) { |
franta-hg@88 | 45 |
super(formatterContext); |
franta-hg@88 | 46 |
} |
franta-hg@88 | 47 |
|
franta-hg@88 | 48 |
@Override |
franta-hg@88 | 49 |
protected int getCurrentColumnsCount() { |
franta-hg@88 | 50 |
if (prefetchDone) { |
franta-hg@88 | 51 |
return super.getCurrentColumnsCount(); |
franta-hg@88 | 52 |
} else { |
franta-hg@88 | 53 |
return currentColumnsCount; |
franta-hg@88 | 54 |
} |
franta-hg@88 | 55 |
} |
franta-hg@88 | 56 |
|
franta-hg@88 | 57 |
@Override |
franta-hg@142 | 58 |
public void writeStartResultSet(ColumnsHeader header) { |
franta-hg@88 | 59 |
currentResultSet = new ArrayList<>(); |
franta-hg@88 | 60 |
currentHeader = header; |
franta-hg@88 | 61 |
initColumnWidths(header.getColumnCount()); |
franta-hg@88 | 62 |
} |
franta-hg@88 | 63 |
|
franta-hg@88 | 64 |
@Override |
franta-hg@88 | 65 |
public void writeStartRow() { |
franta-hg@88 | 66 |
currentRow = new Object[currentHeader.getColumnCount()]; |
franta-hg@88 | 67 |
currentResultSet.add(currentRow); |
franta-hg@88 | 68 |
currentColumnsCount = 0; |
franta-hg@88 | 69 |
} |
franta-hg@88 | 70 |
|
franta-hg@88 | 71 |
@Override |
franta-hg@88 | 72 |
public void writeColumnValue(Object value) { |
franta-hg@88 | 73 |
currentRow[currentColumnsCount] = value; |
franta-hg@88 | 74 |
currentColumnsCount++; |
franta-hg@88 | 75 |
String textRepresentation = toString(value); |
franta-hg@88 | 76 |
/** TODO: count only printable characters (currently not an issue) */ |
franta-hg@88 | 77 |
updateColumnWidth(currentColumnsCount, textRepresentation.length()); |
franta-hg@88 | 78 |
} |
franta-hg@88 | 79 |
|
franta-hg@88 | 80 |
@Override |
franta-hg@88 | 81 |
public void writeEndRow() { |
franta-hg@88 | 82 |
// do nothing |
franta-hg@88 | 83 |
} |
franta-hg@88 | 84 |
|
franta-hg@88 | 85 |
@Override |
franta-hg@88 | 86 |
public void writeEndResultSet() { |
franta-hg@88 | 87 |
prefetchDone = true; |
franta-hg@88 | 88 |
|
franta-hg@142 | 89 |
super.writeStartResultSet(currentHeader); |
franta-hg@88 | 90 |
|
franta-hg@88 | 91 |
for (Object[] row : currentResultSet) { |
franta-hg@88 | 92 |
super.writeStartRow(); |
franta-hg@88 | 93 |
for (Object cell : row) { |
franta-hg@88 | 94 |
super.writeColumnValue(cell); |
franta-hg@88 | 95 |
} |
franta-hg@88 | 96 |
super.writeEndRow(); |
franta-hg@88 | 97 |
} |
franta-hg@88 | 98 |
|
franta-hg@88 | 99 |
currentColumnsCount = 0; |
franta-hg@88 | 100 |
currentHeader = null; |
franta-hg@88 | 101 |
currentRow = null; |
franta-hg@88 | 102 |
currentResultSet = null; |
franta-hg@88 | 103 |
super.writeEndResultSet(); |
franta-hg@88 | 104 |
prefetchDone = false; |
franta-hg@88 | 105 |
} |
franta-hg@88 | 106 |
} |