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.ConfigurationProvider;
21 import info.globalcode.sql.dk.CLIOptions.MODE;
22 import info.globalcode.sql.dk.configuration.Configuration;
23 import info.globalcode.sql.dk.configuration.ConfigurationException;
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
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.IOException;
30 import java.sql.SQLException;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.Unmarshaller;
38 * @author Ing. František Kučera (frantovo.cz)
40 public class CLIStarter implements ConfigurationProvider {
42 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
43 private CLIOptions options;
44 private Configuration configuration;
46 public static void main(String[] args) {
47 log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
49 if (args.length == 0) {
50 args = new String[]{CLIParser.Tokens.INFO_HELP};
54 CLIParser parser = new CLIParser();
55 CLIOptions options = parser.parseOptions(args);
57 CLIStarter starter = new CLIStarter(options);
58 starter.installDefaultConfiguration();
60 log.log(Level.FINE, "All done");
61 } catch (CLIParserException e) {
62 log.log(Level.SEVERE, "Unable to parse CLI options", e);
63 } catch (InvalidOptionsException e) {
64 log.log(Level.SEVERE, "Invalid CLI options", e);
65 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
66 log.log(Level.SEVERE, "Option problem: {0}", p.getDescription());
68 } catch (ConfigurationException e) {
69 log.log(Level.SEVERE, "Configuration problem", e);
70 } catch (SQLException e) {
71 log.log(Level.SEVERE, "SQL problem", e);
72 } catch (FormatterException e) {
73 log.log(Level.SEVERE, "Formatting problem", e);
77 public CLIStarter(CLIOptions options) {
78 this.options = options;
81 private void process() throws ConfigurationException, SQLException, FormatterException {
83 if (!options.getShowInfo().isEmpty()) {
84 InfoLister infoLister = new InfoLister(System.err, this);
85 infoLister.showInfo(options);
88 MODE mode = options.getMode();
94 processPrepareBatch();
97 processExecuteBatch();
100 // already done above
103 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
108 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
109 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
111 throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
113 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
115 throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
117 try (DatabaseConnection c = dd.connect()) {
118 log.log(Level.FINE, "Database connected");
119 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
120 c.executeQuery(options.getSQLCommand(), f);
126 private void processPrepareBatch() {
129 private void processExecuteBatch() {
133 public Configuration getConfiguration() throws ConfigurationException {
134 if (configuration == null) {
135 configuration = loadConfiguration();
137 return configuration;
140 private void installDefaultConfiguration() throws ConfigurationException {
141 Constants.DIR.mkdir();
143 if (Constants.CONFIG_FILE.exists()) {
144 log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
147 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
148 log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
149 } catch (IOException e) {
150 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
155 private Configuration loadConfiguration() throws ConfigurationException {
157 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
158 Unmarshaller u = jaxb.createUnmarshaller();
159 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
160 } catch (Exception e) {
161 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);