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