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 } catch (ConfigurationException e) {
64 log.log(Level.SEVERE, "Configuration problem", e);
65 } catch (SQLException e) {
66 log.log(Level.SEVERE, "SQL problem", e);
67 } catch (FormatterException e) {
68 log.log(Level.SEVERE, "Formatting problem", e);
72 public CLIStarter(CLIOptions options) {
73 this.options = options;
76 private void process() throws ConfigurationException, SQLException, FormatterException {
78 if (!options.getShowInfo().isEmpty()) {
79 InfoLister infoLister = new InfoLister(System.err, this);
80 infoLister.showInfo(options);
83 MODE mode = options.getMode();
89 processPrepareBatch();
92 processExecuteBatch();
98 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
103 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
104 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
106 throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
108 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
110 throw new ConfigurationException("Formatter is not configured: " + options.getDatabaseName());
112 DatabaseConnection c = dd.connect();
113 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
114 c.executeQuery(options.getSQLCommand(), f);
119 private void processPrepareBatch() {
122 private void processExecuteBatch() {
126 public Configuration getConfiguration() throws ConfigurationException {
127 if (configuration == null) {
128 configuration = loadConfiguration();
130 return configuration;
133 private void installDefaultConfiguration() throws ConfigurationException {
134 Constants.DIR.mkdir();
136 if (Constants.CONFIG_FILE.exists()) {
137 log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
140 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
141 } catch (IOException e) {
142 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
147 private Configuration loadConfiguration() throws ConfigurationException {
149 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
150 Unmarshaller u = jaxb.createUnmarshaller();
151 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
152 } catch (Exception e) {
153 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);