29 import info.globalcode.sql.dk.formatting.FormatterException; |
29 import info.globalcode.sql.dk.formatting.FormatterException; |
30 import java.io.BufferedReader; |
30 import java.io.BufferedReader; |
31 import java.io.InputStreamReader; |
31 import java.io.InputStreamReader; |
32 import java.io.PrintStream; |
32 import java.io.PrintStream; |
33 import java.sql.SQLException; |
33 import java.sql.SQLException; |
|
34 import java.util.ArrayList; |
|
35 import java.util.EnumSet; |
34 import java.util.List; |
36 import java.util.List; |
35 import java.util.logging.Level; |
37 import java.util.logging.Level; |
36 import java.util.logging.Logger; |
38 import java.util.logging.Logger; |
37 import javax.sql.rowset.RowSetMetaDataImpl; |
39 import javax.sql.rowset.RowSetMetaDataImpl; |
38 |
40 |
45 |
47 |
46 private static final Logger log = Logger.getLogger(InfoLister.class.getName()); |
48 private static final Logger log = Logger.getLogger(InfoLister.class.getName()); |
47 private PrintStream out; |
49 private PrintStream out; |
48 private ConfigurationProvider configurationProvider; |
50 private ConfigurationProvider configurationProvider; |
49 private CLIOptions options; |
51 private CLIOptions options; |
|
52 private Formatter formatter; |
50 |
53 |
51 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) { |
54 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) { |
52 this.out = out; |
55 this.out = out; |
53 this.configurationProvider = configurationProvider; |
56 this.configurationProvider = configurationProvider; |
54 this.options = options; |
57 this.options = options; |
55 } |
58 } |
56 |
59 |
57 public void listFormatters() throws ConfigurationException { |
60 public void showInfo() throws ConfigurationException, FormatterException { |
|
61 EnumSet<InfoType> commands = options.getShowInfo(); |
|
62 |
|
63 for (InfoType infoType : commands) { |
|
64 switch (infoType) { |
|
65 // only these needs formatted output |
|
66 case CONNECTION: |
|
67 case DATABASES: |
|
68 case FORMATTERS: |
|
69 case TYPES: |
|
70 formatter = getFormatter(); |
|
71 formatter.writeStartDatabase(new DatabaseDefinition()); |
|
72 } |
|
73 } |
|
74 |
|
75 for (InfoType infoType : commands) { |
|
76 infoType.showInfo(this); |
|
77 } |
|
78 |
|
79 if (formatter != null) { |
|
80 formatter.writeEndDatabase(); |
|
81 } |
|
82 } |
|
83 |
|
84 private void listFormatters() throws ConfigurationException { |
58 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) { |
85 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) { |
59 log.log(Level.INFO, "Built-in formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); |
86 log.log(Level.INFO, "Built-in formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); |
60 } |
87 } |
61 List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters(); |
88 List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters(); |
62 for (FormatterDefinition fd : configuredFormatters) { |
89 for (FormatterDefinition fd : configuredFormatters) { |
71 } else { |
98 } else { |
72 log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter); |
99 log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter); |
73 } |
100 } |
74 } |
101 } |
75 |
102 |
76 public void listTypes() throws FormatterException { |
103 public void listTypes() throws FormatterException, ConfigurationException { |
|
104 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER)); |
|
105 List<Object[]> data = new ArrayList<>(); |
|
106 for (SQLType sqlType : SQLType.values()) { |
|
107 data.add(new Object[]{sqlType.name(), sqlType.getCode()}); |
|
108 } |
|
109 printTable(formatter, header, data); |
77 } |
110 } |
78 |
111 |
79 public void listDatabases() throws ConfigurationException { |
112 public void listDatabases() throws ConfigurationException { |
80 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases(); |
113 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases(); |
81 if (configuredDatabases.isEmpty()) { |
114 if (configuredDatabases.isEmpty()) { |
197 infoLister.printResource(Constants.LICENSE_FILE); |
230 infoLister.printResource(Constants.LICENSE_FILE); |
198 } |
231 } |
199 }, |
232 }, |
200 FORMATTERS { |
233 FORMATTERS { |
201 @Override |
234 @Override |
202 public void showInfo(InfoLister infoLister) throws ConfigurationException { |
235 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException { |
203 infoLister.listFormatters(); |
236 infoLister.listFormatters(); |
204 } |
237 } |
205 }, |
238 }, |
206 TYPES { |
239 TYPES { |
207 @Override |
240 @Override |
208 public void showInfo(InfoLister infoLister) throws FormatterException { |
241 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException { |
209 infoLister.listTypes(); |
242 infoLister.listTypes(); |
210 } |
243 } |
211 }, |
244 }, |
212 DATABASES { |
245 DATABASES { |
213 @Override |
246 @Override |
214 public void showInfo(InfoLister infoLister) throws ConfigurationException { |
247 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException { |
215 infoLister.listDatabases(); |
248 infoLister.listDatabases(); |
216 } |
249 } |
217 }, |
250 }, |
218 CONNECTION { |
251 CONNECTION { |
219 @Override |
252 @Override |