java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 21:18:54 +0100
branchv_0
changeset 69 0befec5034c2
parent 67 10c9b9e54622
child 70 02c8eaa425e8
permissions -rw-r--r--
InfoLister, InfoType: switch → enum
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     2
 * SQL-DK
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     8
 * (at your option) any later version.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     9
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    13
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    14
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    17
 */
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
67
10c9b9e54622 option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    20
import info.globalcode.sql.dk.configuration.Configuration;
65
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    21
import info.globalcode.sql.dk.configuration.ConfigurationException;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    22
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
65
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    23
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
67
10c9b9e54622 option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    24
import info.globalcode.sql.dk.configuration.FormatterDefinition;
10c9b9e54622 option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    25
import static info.globalcode.sql.dk.Functions.rpad;
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    26
import info.globalcode.sql.dk.formatting.ColumnsHeader;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    27
import info.globalcode.sql.dk.formatting.Formatter;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    28
import info.globalcode.sql.dk.formatting.FormatterContext;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    29
import info.globalcode.sql.dk.formatting.FormatterException;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    30
import java.io.BufferedReader;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    31
import java.io.InputStreamReader;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
import java.io.PrintStream;
65
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    33
import java.sql.SQLException;
66
6e28893eaada option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
    34
import java.util.List;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    35
import java.util.logging.Level;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    36
import java.util.logging.Logger;
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    37
import javax.sql.rowset.RowSetMetaDataImpl;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
/**
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
 * Displays info like help, version etc.
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
 *
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
 * @author Ing. František Kučera (frantovo.cz)
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
 */
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
public class InfoLister {
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    46
	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    47
	private PrintStream out;
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    48
	private ConfigurationProvider configurationProvider;
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    49
	private CLIOptions options;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    50
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    51
	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    52
		this.out = out;
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    53
		this.configurationProvider = configurationProvider;
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    54
		this.options = options;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    55
	}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    56
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    57
	public void listFormatters() throws ConfigurationException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    58
		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    59
			log.log(Level.INFO, "Built-in formatter:   {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    60
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    61
		List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    62
		for (FormatterDefinition fd : configuredFormatters) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    63
			log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    64
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    65
		if (configuredFormatters.isEmpty()) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    66
			log.log(Level.INFO, "No other formatters are configured");
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    67
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    68
		String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    69
		if (configuredDefaultFormatter == null) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    70
			log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    71
		} else {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    72
			log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    73
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    74
	}
67
10c9b9e54622 option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    75
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    76
	public void listTypes() throws FormatterException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    77
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    78
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    79
	public void listDatabases() throws ConfigurationException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    80
		final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    81
		if (configuredDatabases.isEmpty()) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    82
			log.log(Level.WARNING, "No databases are configured.");
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    83
		} else {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    84
			for (DatabaseDefinition dd : configuredDatabases) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    85
				log.log(Level.INFO, "Configured database: {0}", dd.getName());
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			}
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		}
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    89
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    90
	public void testConnection() {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    91
		boolean connectionTestResult = false;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    92
		String dbName = options.getDatabaseNameToTest();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    93
		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    94
		try {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    95
			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    96
			if (dd == null) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    97
				log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    98
			} else {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
    99
				log.log(Level.FINE, "Database definition was loaded from configuration");
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   100
				DatabaseConnection dc = dd.connect();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   101
				connectionTestResult = dc.test();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   102
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   103
		} catch (ConfigurationException | SQLException e) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   104
			log.log(Level.SEVERE, "Error during testing connection", e);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   105
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   106
		log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   107
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   108
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   109
	public void printResource(String fileName) {
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   110
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   111
			while (true) {
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   112
				String line = reader.readLine();
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
				if (line == null) {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   114
					break;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   115
				} else {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   116
					println(line);
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   117
				}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   118
			}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   119
		} catch (Exception e) {
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   120
			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   121
		}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   122
	}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   123
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   124
	private void println(String line) {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   125
		out.println(line);
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   126
	}
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   127
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   128
	private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   129
		formatter.writeStartResultSet();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   130
		formatter.writeColumnsHeader(header);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   131
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   132
		for (Object[] row : data) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   133
			formatter.writeStartRow();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   134
			for (Object cell : row) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   135
				formatter.writeColumnValue(cell);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   136
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   137
			formatter.writeEndRow();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   138
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   139
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   140
		formatter.writeEndResultSet();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   141
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   142
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   143
	private Formatter getFormatter() throws ConfigurationException, FormatterException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   144
		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(options.getFormatterName());
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   145
		FormatterContext context = new FormatterContext(out);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   146
		return fd.getInstance(context);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   147
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   148
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   149
	private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   150
		try {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   151
			RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   152
			metaData.setColumnCount(fields.length);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   153
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   154
			for (int i = 0; i < fields.length; i++) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   155
				HeaderField hf = fields[i];
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   156
				int sqlIndex = i + 1;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   157
				metaData.setColumnName(sqlIndex, hf.name);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   158
				metaData.setColumnLabel(sqlIndex, hf.name);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   159
				metaData.setColumnType(sqlIndex, hf.type.getCode());
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   160
				metaData.setColumnTypeName(sqlIndex, hf.type.name());
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   161
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   162
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   163
			return new ColumnsHeader(metaData);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   164
		} catch (SQLException e) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   165
			throw new FormatterException("Error while constructing table headers", e);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   166
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   167
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   168
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   169
	private static class HeaderField {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   170
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   171
		String name;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   172
		SQLType type;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   173
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   174
		public HeaderField(String name, SQLType type) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   175
			this.name = name;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   176
			this.type = type;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   177
		}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   178
	}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   179
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   180
	public enum InfoType {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   181
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   182
		HELP {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   183
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   184
			public void showInfo(InfoLister infoLister) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   185
				infoLister.printResource(Constants.HELP_FILE);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   186
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   187
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   188
		VERSION {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   189
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   190
			public void showInfo(InfoLister infoLister) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   191
				infoLister.printResource(Constants.VERSION_FILE);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   192
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   193
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   194
		LICENSE {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   195
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   196
			public void showInfo(InfoLister infoLister) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   197
				infoLister.printResource(Constants.LICENSE_FILE);
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   198
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   199
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   200
		FORMATTERS {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   201
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   202
			public void showInfo(InfoLister infoLister) throws ConfigurationException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   203
				infoLister.listFormatters();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   204
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   205
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   206
		TYPES {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   207
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   208
			public void showInfo(InfoLister infoLister) throws FormatterException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   209
				infoLister.listTypes();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   210
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   211
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   212
		DATABASES {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   213
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   214
			public void showInfo(InfoLister infoLister) throws ConfigurationException {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   215
				infoLister.listDatabases();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   216
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   217
		},
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   218
		CONNECTION {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   219
			@Override
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   220
			public void showInfo(InfoLister infoLister) {
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   221
				infoLister.testConnection();
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   222
			}
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   223
		};
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   224
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   225
		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 67
diff changeset
   226
	}
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
}