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) {
48 if (args.length == 0) {
49 args = new String[]{CLIParser.Tokens.INFO_HELP};
53 CLIParser parser = new CLIParser();
54 CLIOptions options = parser.parseOptions(args);
56 CLIStarter starter = new CLIStarter(options);
57 starter.installDefaultConfiguration();
59 } catch (CLIParserException e) {
60 log.log(Level.SEVERE, "Unable to parse CLI options", e);
61 } catch (InvalidOptionsException e) {
62 log.log(Level.SEVERE, "Invalid CLI options", e);
63 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
64 log.log(Level.SEVERE, "Option problem: {0}", p.getDescription());
66 } catch (ConfigurationException e) {
67 log.log(Level.SEVERE, "Configuration problem", e);
68 } catch (SQLException e) {
69 log.log(Level.SEVERE, "SQL problem", e);
70 } catch (FormatterException e) {
71 log.log(Level.SEVERE, "Formatting problem", e);
75 public CLIStarter(CLIOptions options) {
76 this.options = options;
79 private void process() throws ConfigurationException, SQLException, FormatterException {
81 if (!options.getShowInfo().isEmpty()) {
82 InfoLister infoLister = new InfoLister(System.err, this);
83 infoLister.showInfo(options);
86 MODE mode = options.getMode();
92 processPrepareBatch();
95 processExecuteBatch();
101 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
106 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
107 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
109 throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
111 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
113 throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
115 try (DatabaseConnection c = dd.connect()) {
116 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
117 c.executeQuery(options.getSQLCommand(), f);
123 private void processPrepareBatch() {
126 private void processExecuteBatch() {
130 public Configuration getConfiguration() throws ConfigurationException {
131 if (configuration == null) {
132 configuration = loadConfiguration();
134 return configuration;
137 private void installDefaultConfiguration() throws ConfigurationException {
138 Constants.DIR.mkdir();
140 if (Constants.CONFIG_FILE.exists()) {
141 log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
144 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
145 } catch (IOException e) {
146 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
151 private Configuration loadConfiguration() throws ConfigurationException {
153 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
154 Unmarshaller u = jaxb.createUnmarshaller();
155 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
156 } catch (Exception e) {
157 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);