franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@1
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
19 |
|
franta-hg@26
|
20 |
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
|
franta-hg@20
|
21 |
import info.globalcode.sql.dk.CLIOptions.MODE;
|
franta-hg@26
|
22 |
import info.globalcode.sql.dk.configuration.Configuration;
|
franta-hg@33
|
23 |
import info.globalcode.sql.dk.configuration.ConfigurationException;
|
franta-hg@34
|
24 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@34
|
25 |
import info.globalcode.sql.dk.configuration.FormatterDefinition;
|
franta-hg@34
|
26 |
import info.globalcode.sql.dk.formatting.Formatter;
|
franta-hg@34
|
27 |
import info.globalcode.sql.dk.formatting.FormatterContext;
|
franta-hg@34
|
28 |
import info.globalcode.sql.dk.formatting.FormatterException;
|
franta-hg@33
|
29 |
import java.io.IOException;
|
franta-hg@34
|
30 |
import java.sql.SQLException;
|
franta-hg@13
|
31 |
import java.util.logging.Level;
|
franta-hg@13
|
32 |
import java.util.logging.Logger;
|
franta-hg@33
|
33 |
import javax.xml.bind.JAXBContext;
|
franta-hg@33
|
34 |
import javax.xml.bind.Unmarshaller;
|
franta-hg@13
|
35 |
|
franta-hg@1
|
36 |
/**
|
franta-hg@1
|
37 |
*
|
franta-hg@1
|
38 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
39 |
*/
|
franta-hg@20
|
40 |
public class CLIStarter implements ConfigurationProvider {
|
franta-hg@1
|
41 |
|
franta-hg@13
|
42 |
private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
|
franta-hg@20
|
43 |
private CLIOptions options;
|
franta-hg@20
|
44 |
private Configuration configuration;
|
franta-hg@13
|
45 |
|
franta-hg@1
|
46 |
public static void main(String[] args) {
|
franta-hg@21
|
47 |
|
franta-hg@21
|
48 |
if (args.length == 0) {
|
franta-hg@21
|
49 |
args = new String[]{CLIParser.Tokens.INFO_HELP};
|
franta-hg@21
|
50 |
}
|
franta-hg@21
|
51 |
|
franta-hg@13
|
52 |
try {
|
franta-hg@13
|
53 |
CLIParser parser = new CLIParser();
|
franta-hg@13
|
54 |
CLIOptions options = parser.parseOptions(args);
|
franta-hg@14
|
55 |
options.validate();
|
franta-hg@20
|
56 |
CLIStarter starter = new CLIStarter(options);
|
franta-hg@21
|
57 |
starter.installDefaultConfiguration();
|
franta-hg@20
|
58 |
starter.process();
|
franta-hg@13
|
59 |
} catch (CLIParserException e) {
|
franta-hg@14
|
60 |
log.log(Level.SEVERE, "Unable to parse CLI options", e);
|
franta-hg@14
|
61 |
} catch (InvalidOptionsException e) {
|
franta-hg@14
|
62 |
log.log(Level.SEVERE, "Invalid CLI options", e);
|
franta-hg@34
|
63 |
} catch (ConfigurationException e) {
|
franta-hg@34
|
64 |
log.log(Level.SEVERE, "Configuration problem", e);
|
franta-hg@34
|
65 |
} catch (SQLException e) {
|
franta-hg@34
|
66 |
log.log(Level.SEVERE, "SQL problem", e);
|
franta-hg@34
|
67 |
} catch (FormatterException e) {
|
franta-hg@34
|
68 |
log.log(Level.SEVERE, "Formatting problem", e);
|
franta-hg@13
|
69 |
}
|
franta-hg@4
|
70 |
}
|
franta-hg@20
|
71 |
|
franta-hg@20
|
72 |
public CLIStarter(CLIOptions options) {
|
franta-hg@20
|
73 |
this.options = options;
|
franta-hg@20
|
74 |
}
|
franta-hg@20
|
75 |
|
franta-hg@34
|
76 |
private void process() throws ConfigurationException, SQLException, FormatterException {
|
franta-hg@20
|
77 |
/** Show info */
|
franta-hg@20
|
78 |
if (!options.getShowInfo().isEmpty()) {
|
franta-hg@20
|
79 |
InfoLister infoLister = new InfoLister(System.err, this);
|
franta-hg@20
|
80 |
infoLister.showInfo(options);
|
franta-hg@20
|
81 |
}
|
franta-hg@20
|
82 |
|
franta-hg@20
|
83 |
MODE mode = options.getMode();
|
franta-hg@20
|
84 |
switch (mode) {
|
franta-hg@20
|
85 |
case QUERY_NOW:
|
franta-hg@34
|
86 |
processQueryNow();
|
franta-hg@20
|
87 |
break;
|
franta-hg@20
|
88 |
case PREPARE_BATCH:
|
franta-hg@34
|
89 |
processPrepareBatch();
|
franta-hg@20
|
90 |
break;
|
franta-hg@20
|
91 |
case EXECUTE_BATCH:
|
franta-hg@34
|
92 |
processExecuteBatch();
|
franta-hg@20
|
93 |
break;
|
franta-hg@20
|
94 |
case JUST_SHOW_INFO:
|
franta-hg@20
|
95 |
// already done above
|
franta-hg@20
|
96 |
break;
|
franta-hg@20
|
97 |
default:
|
franta-hg@20
|
98 |
log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
|
franta-hg@20
|
99 |
break;
|
franta-hg@20
|
100 |
}
|
franta-hg@20
|
101 |
}
|
franta-hg@20
|
102 |
|
franta-hg@34
|
103 |
private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
|
franta-hg@34
|
104 |
DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
|
franta-hg@34
|
105 |
if (dd == null) {
|
franta-hg@34
|
106 |
throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
|
franta-hg@34
|
107 |
} else {
|
franta-hg@34
|
108 |
FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
|
franta-hg@34
|
109 |
if (fd == null) {
|
franta-hg@36
|
110 |
throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
|
franta-hg@34
|
111 |
} else {
|
franta-hg@34
|
112 |
DatabaseConnection c = dd.connect();
|
franta-hg@34
|
113 |
Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
|
franta-hg@34
|
114 |
c.executeQuery(options.getSQLCommand(), f);
|
franta-hg@34
|
115 |
}
|
franta-hg@34
|
116 |
}
|
franta-hg@34
|
117 |
}
|
franta-hg@34
|
118 |
|
franta-hg@34
|
119 |
private void processPrepareBatch() {
|
franta-hg@34
|
120 |
}
|
franta-hg@34
|
121 |
|
franta-hg@34
|
122 |
private void processExecuteBatch() {
|
franta-hg@34
|
123 |
}
|
franta-hg@34
|
124 |
|
franta-hg@20
|
125 |
@Override
|
franta-hg@33
|
126 |
public Configuration getConfiguration() throws ConfigurationException {
|
franta-hg@20
|
127 |
if (configuration == null) {
|
franta-hg@20
|
128 |
configuration = loadConfiguration();
|
franta-hg@20
|
129 |
}
|
franta-hg@20
|
130 |
return configuration;
|
franta-hg@20
|
131 |
}
|
franta-hg@20
|
132 |
|
franta-hg@34
|
133 |
private void installDefaultConfiguration() throws ConfigurationException {
|
franta-hg@33
|
134 |
Constants.DIR.mkdir();
|
franta-hg@33
|
135 |
|
franta-hg@33
|
136 |
if (Constants.CONFIG_FILE.exists()) {
|
franta-hg@33
|
137 |
log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
|
franta-hg@33
|
138 |
} else {
|
franta-hg@33
|
139 |
try {
|
franta-hg@33
|
140 |
Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
|
franta-hg@33
|
141 |
} catch (IOException e) {
|
franta-hg@34
|
142 |
throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
143 |
}
|
franta-hg@33
|
144 |
}
|
franta-hg@20
|
145 |
}
|
franta-hg@20
|
146 |
|
franta-hg@33
|
147 |
private Configuration loadConfiguration() throws ConfigurationException {
|
franta-hg@33
|
148 |
try {
|
franta-hg@33
|
149 |
JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
|
franta-hg@33
|
150 |
Unmarshaller u = jaxb.createUnmarshaller();
|
franta-hg@33
|
151 |
return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
|
franta-hg@33
|
152 |
} catch (Exception e) {
|
franta-hg@33
|
153 |
throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
154 |
}
|
franta-hg@20
|
155 |
}
|
franta-hg@1
|
156 |
}
|