franta-hg@32
|
1 |
/**
|
franta-hg@32
|
2 |
* SQL-DK
|
franta-hg@32
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@32
|
4 |
*
|
franta-hg@32
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@32
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@32
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@32
|
8 |
* (at your option) any later version.
|
franta-hg@32
|
9 |
*
|
franta-hg@32
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@32
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@32
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@32
|
13 |
* GNU General Public License for more details.
|
franta-hg@32
|
14 |
*
|
franta-hg@32
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@32
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@32
|
17 |
*/
|
franta-hg@32
|
18 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@32
|
19 |
|
franta-hg@34
|
20 |
import info.globalcode.sql.dk.ColorfulPrintWriter;
|
franta-hg@37
|
21 |
import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
|
franta-hg@39
|
22 |
import static info.globalcode.sql.dk.Functions.lpad;
|
franta-hg@39
|
23 |
import static info.globalcode.sql.dk.Functions.rpad;
|
franta-hg@34
|
24 |
|
franta-hg@32
|
25 |
/**
|
franta-hg@32
|
26 |
*
|
franta-hg@32
|
27 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@32
|
28 |
*/
|
franta-hg@32
|
29 |
public class TabularFormatter extends AbstractFormatter {
|
franta-hg@32
|
30 |
|
franta-hg@32
|
31 |
public static final String NAME = "tabular";
|
franta-hg@39
|
32 |
private static final String HEADER_TYPE_PREFIX = " (";
|
franta-hg@39
|
33 |
private static final String HEADER_TYPE_SUFFIX = ")";
|
franta-hg@34
|
34 |
private ColorfulPrintWriter out;
|
franta-hg@37
|
35 |
private boolean firstResult = true;
|
franta-hg@39
|
36 |
private int[] columnWidth;
|
franta-hg@32
|
37 |
|
franta-hg@32
|
38 |
public TabularFormatter(FormatterContext formatterContext) {
|
franta-hg@32
|
39 |
super(formatterContext);
|
franta-hg@34
|
40 |
out = new ColorfulPrintWriter(formatterContext.getOutputStream());
|
franta-hg@34
|
41 |
}
|
franta-hg@34
|
42 |
|
franta-hg@34
|
43 |
@Override
|
franta-hg@37
|
44 |
public void writeStartResultSet() {
|
franta-hg@37
|
45 |
super.writeStartResultSet();
|
franta-hg@37
|
46 |
printResultSeparator();
|
franta-hg@37
|
47 |
}
|
franta-hg@37
|
48 |
|
franta-hg@37
|
49 |
@Override
|
franta-hg@37
|
50 |
public void writeColumnsHeader(ColumnsHeader header) {
|
franta-hg@37
|
51 |
super.writeColumnsHeader(header);
|
franta-hg@37
|
52 |
|
franta-hg@39
|
53 |
columnWidth = new int[header.getColumnCount()];
|
franta-hg@39
|
54 |
|
franta-hg@37
|
55 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@37
|
56 |
out.print(TerminalStyle.Bright, cd.getLabel());
|
franta-hg@39
|
57 |
out.print(HEADER_TYPE_PREFIX);
|
franta-hg@37
|
58 |
out.print(cd.getTypeName());
|
franta-hg@39
|
59 |
out.print(HEADER_TYPE_SUFFIX);
|
franta-hg@37
|
60 |
if (!cd.isLastColumn()) {
|
franta-hg@37
|
61 |
out.print(TerminalColor.Green, " | ");
|
franta-hg@37
|
62 |
}
|
franta-hg@39
|
63 |
|
franta-hg@39
|
64 |
setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
|
franta-hg@37
|
65 |
}
|
franta-hg@37
|
66 |
out.println();
|
franta-hg@37
|
67 |
out.flush();
|
franta-hg@37
|
68 |
}
|
franta-hg@37
|
69 |
|
franta-hg@37
|
70 |
@Override
|
franta-hg@34
|
71 |
public void writeColumnValue(Object value) {
|
franta-hg@34
|
72 |
super.writeColumnValue(value);
|
franta-hg@34
|
73 |
|
franta-hg@34
|
74 |
if (!isCurrentColumnFirst()) {
|
franta-hg@37
|
75 |
out.print(TerminalColor.Green, " | ");
|
franta-hg@34
|
76 |
}
|
franta-hg@37
|
77 |
|
franta-hg@39
|
78 |
out.print(TerminalColor.Cyan, toString(value));
|
franta-hg@39
|
79 |
}
|
franta-hg@39
|
80 |
|
franta-hg@39
|
81 |
private int getColumnWidth(int columnNumber) {
|
franta-hg@39
|
82 |
return columnWidth[columnNumber - 1];
|
franta-hg@39
|
83 |
}
|
franta-hg@39
|
84 |
|
franta-hg@39
|
85 |
private void setColumnWidth(int columnNumber, int width) {
|
franta-hg@39
|
86 |
columnWidth[columnNumber - 1] = width;
|
franta-hg@39
|
87 |
}
|
franta-hg@39
|
88 |
|
franta-hg@39
|
89 |
private void updateColumnWidth(int columnNumber, int width) {
|
franta-hg@39
|
90 |
int oldWidth = getColumnWidth(columnNumber);
|
franta-hg@39
|
91 |
setColumnWidth(columnNumber, Math.max(width, oldWidth));
|
franta-hg@39
|
92 |
|
franta-hg@39
|
93 |
}
|
franta-hg@39
|
94 |
|
franta-hg@39
|
95 |
@Override
|
franta-hg@39
|
96 |
protected String toString(Object value) {
|
franta-hg@39
|
97 |
final int width = getColumnWidth(getCurrentColumnsCount());
|
franta-hg@39
|
98 |
if (value instanceof Number) {
|
franta-hg@39
|
99 |
return lpad(super.toString(value), width);
|
franta-hg@39
|
100 |
} else {
|
franta-hg@39
|
101 |
return rpad(super.toString(value), width);
|
franta-hg@39
|
102 |
}
|
franta-hg@34
|
103 |
}
|
franta-hg@34
|
104 |
|
franta-hg@34
|
105 |
@Override
|
franta-hg@34
|
106 |
public void writeEndRow() {
|
franta-hg@34
|
107 |
super.writeEndRow();
|
franta-hg@34
|
108 |
out.println();
|
franta-hg@34
|
109 |
out.flush();
|
franta-hg@32
|
110 |
}
|
franta-hg@37
|
111 |
|
franta-hg@37
|
112 |
@Override
|
franta-hg@37
|
113 |
public void writeEndResultSet() {
|
franta-hg@37
|
114 |
super.writeEndResultSet();
|
franta-hg@37
|
115 |
out.print(TerminalColor.Yellow, "Record count: ");
|
franta-hg@37
|
116 |
out.println(getCurrentRowCount());
|
franta-hg@37
|
117 |
out.flush();
|
franta-hg@37
|
118 |
}
|
franta-hg@37
|
119 |
|
franta-hg@37
|
120 |
@Override
|
franta-hg@37
|
121 |
public void writeStartUpdatesResult() {
|
franta-hg@37
|
122 |
super.writeStartUpdatesResult();
|
franta-hg@37
|
123 |
printResultSeparator();
|
franta-hg@37
|
124 |
}
|
franta-hg@37
|
125 |
|
franta-hg@37
|
126 |
@Override
|
franta-hg@37
|
127 |
public void writeUpdatedRowsCount(int updatedRowsCount) {
|
franta-hg@37
|
128 |
super.writeUpdatedRowsCount(updatedRowsCount);
|
franta-hg@37
|
129 |
out.print(TerminalColor.Red, "Updated records: ");
|
franta-hg@37
|
130 |
out.println(updatedRowsCount);
|
franta-hg@37
|
131 |
out.flush();
|
franta-hg@37
|
132 |
}
|
franta-hg@37
|
133 |
|
franta-hg@37
|
134 |
@Override
|
franta-hg@37
|
135 |
public void writeEndDatabase() {
|
franta-hg@37
|
136 |
super.writeEndDatabase();
|
franta-hg@37
|
137 |
out.flush();
|
franta-hg@37
|
138 |
}
|
franta-hg@37
|
139 |
|
franta-hg@37
|
140 |
private void printResultSeparator() {
|
franta-hg@37
|
141 |
if (firstResult) {
|
franta-hg@37
|
142 |
firstResult = false;
|
franta-hg@37
|
143 |
} else {
|
franta-hg@37
|
144 |
out.println();
|
franta-hg@37
|
145 |
}
|
franta-hg@37
|
146 |
}
|
franta-hg@32
|
147 |
}
|