3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.configuration.Configuration;
21 import info.globalcode.sql.dk.configuration.ConfigurationException;
22 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
23 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
24 import info.globalcode.sql.dk.configuration.FormatterDefinition;
25 import info.globalcode.sql.dk.formatting.ColumnsHeader;
26 import info.globalcode.sql.dk.formatting.Formatter;
27 import info.globalcode.sql.dk.formatting.FormatterContext;
28 import info.globalcode.sql.dk.formatting.FormatterException;
29 import java.io.BufferedReader;
30 import java.io.InputStreamReader;
31 import java.io.PrintStream;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.EnumSet;
35 import java.util.List;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import javax.sql.rowset.RowSetMetaDataImpl;
41 * Displays info like help, version etc.
43 * @author Ing. František Kučera (frantovo.cz)
45 public class InfoLister {
47 private static final Logger log = Logger.getLogger(InfoLister.class.getName());
48 private PrintStream out;
49 private ConfigurationProvider configurationProvider;
50 private CLIOptions options;
51 private Formatter formatter;
53 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
55 this.configurationProvider = configurationProvider;
56 this.options = options;
59 public void showInfo() throws ConfigurationException, FormatterException {
60 EnumSet<InfoType> commands = options.getShowInfo();
62 for (InfoType infoType : commands) {
64 // only these need formatted output
69 try (Formatter f = getFormatter()) {
71 formatter.writeStartBatch();
72 formatter.writeStartDatabase(new DatabaseDefinition());
74 formatter.writeEndDatabase();
75 formatter.writeEndBatch();
85 private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
86 for (InfoType infoType : commands) {
87 infoType.showInfo(this);
91 private void listFormatters() throws ConfigurationException, FormatterException {
92 ColumnsHeader header = constructHeader(
93 new HeaderField("name", SQLType.VARCHAR),
94 new HeaderField("built_in", SQLType.BOOLEAN),
95 new HeaderField("default", SQLType.BOOLEAN),
96 new HeaderField("class_name", SQLType.VARCHAR));
97 List<Object[]> data = new ArrayList<>();
99 String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
100 defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
102 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
103 data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
106 for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
107 data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
110 printTable(formatter, header, data);
115 public void listTypes() throws FormatterException, ConfigurationException {
116 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
117 List<Object[]> data = new ArrayList<>();
118 for (SQLType sqlType : SQLType.values()) {
119 data.add(new Object[]{sqlType.name(), sqlType.getCode()});
121 printTable(formatter, header, data);
122 log.log(Level.INFO, "Type names in --types option are case insensitive");
125 public void listDatabases() throws ConfigurationException, FormatterException {
126 ColumnsHeader header = constructHeader(
127 new HeaderField("database_name", SQLType.VARCHAR),
128 new HeaderField("user_name", SQLType.VARCHAR),
129 new HeaderField("database_url", SQLType.VARCHAR));
130 List<Object[]> data = new ArrayList<>();
132 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
133 if (configuredDatabases.isEmpty()) {
134 log.log(Level.WARNING, "No databases are configured.");
136 for (DatabaseDefinition dd : configuredDatabases) {
137 data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
141 printTable(formatter, header, data);
144 public void testConnection() throws FormatterException, ConfigurationException {
145 ColumnsHeader header = constructHeader(
146 new HeaderField("database_name", SQLType.VARCHAR),
147 new HeaderField("configured", SQLType.BOOLEAN),
148 new HeaderField("connected", SQLType.BOOLEAN));
149 List<Object[]> data = new ArrayList<>();
151 for (String dbName : options.getDatabaseNameToTest()) {
152 data.add(testConnection(dbName));
155 printTable(formatter, header, data);
158 public Object[] testConnection(String dbName) {
159 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
161 boolean succesfullyConnected = false;
162 boolean succesfullyConfigured = false;
165 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
166 log.log(Level.FINE, "Database definition was loaded from configuration");
167 succesfullyConfigured = true;
168 try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
169 succesfullyConnected = dc.test();
171 log.log(Level.FINE, "Database connection test was successful");
172 } catch (ConfigurationException | SQLException e) {
173 log.log(Level.SEVERE, "Error during testing connection", e);
176 return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
179 public void printResource(String fileName) {
180 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
182 String line = reader.readLine();
189 } catch (Exception e) {
190 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
194 private void println(String line) {
198 private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
199 formatter.writeStartResultSet();
200 formatter.writeColumnsHeader(header);
202 for (Object[] row : data) {
203 formatter.writeStartRow();
204 for (Object cell : row) {
205 formatter.writeColumnValue(cell);
207 formatter.writeEndRow();
210 formatter.writeEndResultSet();
213 private Formatter getFormatter() throws ConfigurationException, FormatterException {
214 String formatterName = options.getFormatterName();
215 formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
216 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
217 FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
218 return fd.getInstance(context);
221 private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
223 RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
224 metaData.setColumnCount(fields.length);
226 for (int i = 0; i < fields.length; i++) {
227 HeaderField hf = fields[i];
228 int sqlIndex = i + 1;
229 metaData.setColumnName(sqlIndex, hf.name);
230 metaData.setColumnLabel(sqlIndex, hf.name);
231 metaData.setColumnType(sqlIndex, hf.type.getCode());
232 metaData.setColumnTypeName(sqlIndex, hf.type.name());
235 return new ColumnsHeader(metaData);
236 } catch (SQLException e) {
237 throw new FormatterException("Error while constructing table headers", e);
241 private static class HeaderField {
246 public HeaderField(String name, SQLType type) {
252 public enum InfoType {
256 public void showInfo(InfoLister infoLister) {
257 infoLister.printResource(Constants.HELP_FILE);
262 public void showInfo(InfoLister infoLister) {
263 infoLister.printResource(Constants.VERSION_FILE);
268 public void showInfo(InfoLister infoLister) {
269 infoLister.printResource(Constants.LICENSE_FILE);
274 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
275 infoLister.listFormatters();
280 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
281 infoLister.listTypes();
286 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
287 infoLister.listDatabases();
292 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
293 infoLister.testConnection();
297 public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;