author | František Kučera <franta-hg@frantovo.cz> |
Sun, 13 Sep 2015 19:25:36 +0200 | |
branch | v_0 |
changeset 227 | 0094319a274a |
parent 218 | 8e38caf43ca8 |
permissions | -rw-r--r-- |
60 | 1 |
/** |
2 |
* SQL-DK |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
3 |
* Copyright © 2015 František Kučera (frantovo.cz) |
60 | 4 |
* |
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. |
|
9 |
* |
|
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. |
|
14 |
* |
|
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/>. |
|
17 |
*/ |
|
18 |
package info.globalcode.sql.dk.formatting; |
|
19 |
||
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
20 |
import info.globalcode.sql.dk.ColorfulPrintWriter; |
218
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
21 |
import info.globalcode.sql.dk.Functions; |
206
e2f24eea8543
property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents:
202
diff
changeset
|
22 |
import info.globalcode.sql.dk.configuration.PropertyDeclaration; |
e2f24eea8543
property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents:
202
diff
changeset
|
23 |
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL; |
e2f24eea8543
property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents:
202
diff
changeset
|
24 |
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION; |
60 | 25 |
|
26 |
/** |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
27 |
* Formatter intended for printing one record (or few records) with many columns. |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
28 |
* Prints each colum name and its value on separate line. |
60 | 29 |
* |
30 |
* @author Ing. František Kučera (frantovo.cz) |
|
31 |
*/ |
|
207
2bba68ef47c1
property annotations: default values
František Kučera <franta-hg@frantovo.cz>
parents:
206
diff
changeset
|
32 |
@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION) |
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
33 |
public class SingleRecordFormatter extends AbstractFormatter { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
34 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
35 |
public static final String NAME = "record"; // bash-completion:formatter |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
36 |
private final ColorfulPrintWriter out; |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
37 |
private boolean firstResult = true; |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
38 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
39 |
public SingleRecordFormatter(FormatterContext formatterContext) { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
40 |
super(formatterContext); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
41 |
out = new ColorfulPrintWriter(formatterContext.getOutputStream()); |
206
e2f24eea8543
property annotations (documentation) for particular formatters
František Kučera <franta-hg@frantovo.cz>
parents:
202
diff
changeset
|
42 |
out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); |
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
43 |
} |
60 | 44 |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
45 |
@Override |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
46 |
public void writeStartResultSet(ColumnsHeader header) { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
47 |
super.writeStartResultSet(header); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
48 |
printResultSeparator(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
49 |
} |
60 | 50 |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
51 |
@Override |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
52 |
public void writeStartRow() { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
53 |
super.writeStartRow(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
54 |
printRecordSeparator(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
55 |
out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: "); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
56 |
out.print(getCurrentRowCount()); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
57 |
println(); |
60 | 58 |
} |
59 |
||
60 |
@Override |
|
61 |
public void writeColumnValue(Object value) { |
|
62 |
super.writeColumnValue(value); |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
63 |
String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
64 |
out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": "); |
218
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
65 |
Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red); |
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
66 |
println(); |
60 | 67 |
} |
68 |
||
218
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
69 |
private static String toString(Object value) { |
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
70 |
return String.valueOf(value); |
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
71 |
} |
8e38caf43ca8
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
72 |
|
60 | 73 |
@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:
79
diff
changeset
|
74 |
public void writeUpdatesResult(int updatedRowsCount) { |
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:
79
diff
changeset
|
75 |
super.writeUpdatesResult(updatedRowsCount); |
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
76 |
printResultSeparator(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
77 |
out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: "); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
78 |
out.println(updatedRowsCount); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
79 |
printBellAndFlush(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
80 |
} |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
81 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
82 |
private void printBellAndFlush() { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
83 |
out.bell(); |
60 | 84 |
out.flush(); |
85 |
} |
|
202
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
86 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
87 |
private void println() { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
88 |
out.println(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
89 |
printBellAndFlush(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
90 |
} |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
91 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
92 |
private void printRecordSeparator() { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
93 |
if (getCurrentRowCount() > 1) { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
94 |
println(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
95 |
} |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
96 |
} |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
97 |
|
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
98 |
private void printResultSeparator() { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
99 |
if (firstResult) { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
100 |
firstResult = false; |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
101 |
} else { |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
102 |
println(); |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
103 |
} |
01078e09b85b
SingleRecordFormatter: Formatter intended for printing one record (or few records) with many columns.Prints each colum name and its value on separate line.
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
104 |
} |
60 | 105 |
} |