|
1 /** |
|
2 * SQL-DK |
|
3 * Copyright © 2015 František Kučera (frantovo.cz) |
|
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 |
|
20 import info.globalcode.sql.dk.ColorfulPrintWriter; |
|
21 |
|
22 /** |
|
23 * Formatter intended for printing one record (or few records) with many columns. |
|
24 * Prints each colum name and its value on separate line. |
|
25 * |
|
26 * @author Ing. František Kučera (frantovo.cz) |
|
27 */ |
|
28 public class SingleRecordFormatter extends AbstractFormatter { |
|
29 |
|
30 public static final String NAME = "record"; // bash-completion:formatter |
|
31 public static final String PROPERTY_COLORFUL = "color"; |
|
32 private final ColorfulPrintWriter out; |
|
33 private boolean firstResult = true; |
|
34 |
|
35 public SingleRecordFormatter(FormatterContext formatterContext) { |
|
36 super(formatterContext); |
|
37 out = new ColorfulPrintWriter(formatterContext.getOutputStream()); |
|
38 out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true)); |
|
39 } |
|
40 |
|
41 @Override |
|
42 public void writeStartResultSet(ColumnsHeader header) { |
|
43 super.writeStartResultSet(header); |
|
44 printResultSeparator(); |
|
45 } |
|
46 |
|
47 @Override |
|
48 public void writeStartRow() { |
|
49 super.writeStartRow(); |
|
50 printRecordSeparator(); |
|
51 out.print(ColorfulPrintWriter.TerminalColor.Red, "Record: "); |
|
52 out.print(getCurrentRowCount()); |
|
53 println(); |
|
54 } |
|
55 |
|
56 @Override |
|
57 public void writeColumnValue(Object value) { |
|
58 super.writeColumnValue(value); |
|
59 String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel(); |
|
60 out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": "); |
|
61 out.print(String.valueOf(value)); |
|
62 println(); |
|
63 } |
|
64 |
|
65 @Override |
|
66 public void writeUpdatesResult(int updatedRowsCount) { |
|
67 super.writeUpdatesResult(updatedRowsCount); |
|
68 printResultSeparator(); |
|
69 out.print(ColorfulPrintWriter.TerminalColor.Red, "Updated records: "); |
|
70 out.println(updatedRowsCount); |
|
71 printBellAndFlush(); |
|
72 } |
|
73 |
|
74 private void printBellAndFlush() { |
|
75 out.bell(); |
|
76 out.flush(); |
|
77 } |
|
78 |
|
79 private void println() { |
|
80 out.println(); |
|
81 printBellAndFlush(); |
|
82 } |
|
83 |
|
84 private void printRecordSeparator() { |
|
85 if (getCurrentRowCount() > 1) { |
|
86 println(); |
|
87 } |
|
88 } |
|
89 |
|
90 private void printResultSeparator() { |
|
91 if (firstResult) { |
|
92 firstResult = false; |
|
93 } else { |
|
94 println(); |
|
95 } |
|
96 } |
|
97 } |