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 boolean formattinNeeded = false;
64 for (InfoType infoType : commands) {
70 formattinNeeded = true;
75 if (formattinNeeded) {
76 try (Formatter f = getFormatter()) {
78 formatter.writeStartBatch();
79 formatter.writeStartDatabase(new DatabaseDefinition());
80 formatter.writeStartStatement();
82 formatter.writeEndStatement();
83 formatter.writeEndDatabase();
84 formatter.writeEndBatch();
92 private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
93 for (InfoType infoType : commands) {
94 infoType.showInfo(this);
98 private void listFormatters() throws ConfigurationException, FormatterException {
99 ColumnsHeader header = constructHeader(
100 new HeaderField("name", SQLType.VARCHAR),
101 new HeaderField("built_in", SQLType.BOOLEAN),
102 new HeaderField("default", SQLType.BOOLEAN),
103 new HeaderField("class_name", SQLType.VARCHAR));
104 List<Object[]> data = new ArrayList<>();
106 String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
107 defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
109 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
110 data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
113 for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
114 data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
117 printTable(formatter, header, data);
122 public void listTypes() throws FormatterException, ConfigurationException {
123 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
124 List<Object[]> data = new ArrayList<>();
125 for (SQLType sqlType : SQLType.values()) {
126 data.add(new Object[]{sqlType.name(), sqlType.getCode()});
128 printTable(formatter, header, data);
129 log.log(Level.INFO, "Type names in --types option are case insensitive");
132 public void listDatabases() throws ConfigurationException, FormatterException {
133 ColumnsHeader header = constructHeader(
134 new HeaderField("database_name", SQLType.VARCHAR),
135 new HeaderField("user_name", SQLType.VARCHAR),
136 new HeaderField("database_url", SQLType.VARCHAR));
137 List<Object[]> data = new ArrayList<>();
139 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
140 if (configuredDatabases.isEmpty()) {
141 log.log(Level.WARNING, "No databases are configured.");
143 for (DatabaseDefinition dd : configuredDatabases) {
144 data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
148 printTable(formatter, header, data);
151 public void testConnection() throws FormatterException, ConfigurationException {
152 ColumnsHeader header = constructHeader(
153 new HeaderField("database_name", SQLType.VARCHAR),
154 new HeaderField("configured", SQLType.BOOLEAN),
155 new HeaderField("connected", SQLType.BOOLEAN));
156 List<Object[]> data = new ArrayList<>();
158 for (String dbName : options.getDatabaseNameToTest()) {
159 data.add(testConnection(dbName));
162 printTable(formatter, header, data);
165 public Object[] testConnection(String dbName) {
166 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
168 boolean succesfullyConnected = false;
169 boolean succesfullyConfigured = false;
172 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
173 log.log(Level.FINE, "Database definition was loaded from configuration");
174 succesfullyConfigured = true;
175 try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
176 succesfullyConnected = dc.test();
178 log.log(Level.FINE, "Database connection test was successful");
179 } catch (ConfigurationException | SQLException e) {
180 log.log(Level.SEVERE, "Error during testing connection", e);
183 return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
186 public void printResource(String fileName) {
187 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
189 String line = reader.readLine();
196 } catch (Exception e) {
197 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
201 private void println(String line) {
205 private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
206 formatter.writeStartResultSet(header);
208 for (Object[] row : data) {
209 formatter.writeStartRow();
210 for (Object cell : row) {
211 formatter.writeColumnValue(cell);
213 formatter.writeEndRow();
216 formatter.writeEndResultSet();
219 private Formatter getFormatter() throws ConfigurationException, FormatterException {
220 String formatterName = options.getFormatterName();
221 formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
222 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
223 FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
224 return fd.getInstance(context);
227 private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
229 RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
230 metaData.setColumnCount(fields.length);
232 for (int i = 0; i < fields.length; i++) {
233 HeaderField hf = fields[i];
234 int sqlIndex = i + 1;
235 metaData.setColumnName(sqlIndex, hf.name);
236 metaData.setColumnLabel(sqlIndex, hf.name);
237 metaData.setColumnType(sqlIndex, hf.type.getCode());
238 metaData.setColumnTypeName(sqlIndex, hf.type.name());
241 return new ColumnsHeader(metaData);
242 } catch (SQLException e) {
243 throw new FormatterException("Error while constructing table headers", e);
247 private static class HeaderField {
252 public HeaderField(String name, SQLType type) {
258 public enum InfoType {
262 public void showInfo(InfoLister infoLister) {
263 infoLister.printResource(Constants.HELP_FILE);
268 public void showInfo(InfoLister infoLister) {
269 infoLister.printResource(Constants.VERSION_FILE);
274 public void showInfo(InfoLister infoLister) {
275 infoLister.printResource(Constants.LICENSE_FILE);
280 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
281 infoLister.listFormatters();
286 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
287 infoLister.listTypes();
292 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
293 infoLister.listDatabases();
298 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
299 infoLister.testConnection();
303 public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;