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@80
|
26 |
import info.globalcode.sql.dk.configuration.NameIdentified;
|
franta-hg@34
|
27 |
import info.globalcode.sql.dk.formatting.Formatter;
|
franta-hg@34
|
28 |
import info.globalcode.sql.dk.formatting.FormatterContext;
|
franta-hg@34
|
29 |
import info.globalcode.sql.dk.formatting.FormatterException;
|
franta-hg@80
|
30 |
import java.io.File;
|
franta-hg@80
|
31 |
import java.io.FileNotFoundException;
|
franta-hg@33
|
32 |
import java.io.IOException;
|
franta-hg@64
|
33 |
import java.io.PrintStream;
|
franta-hg@80
|
34 |
import java.io.PrintWriter;
|
franta-hg@34
|
35 |
import java.sql.SQLException;
|
franta-hg@80
|
36 |
import java.util.Collection;
|
franta-hg@13
|
37 |
import java.util.logging.Level;
|
franta-hg@63
|
38 |
import java.util.logging.LogRecord;
|
franta-hg@13
|
39 |
import java.util.logging.Logger;
|
franta-hg@33
|
40 |
import javax.xml.bind.JAXBContext;
|
franta-hg@33
|
41 |
import javax.xml.bind.Unmarshaller;
|
franta-hg@13
|
42 |
|
franta-hg@1
|
43 |
/**
|
franta-hg@1
|
44 |
*
|
franta-hg@1
|
45 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
46 |
*/
|
franta-hg@20
|
47 |
public class CLIStarter implements ConfigurationProvider {
|
franta-hg@1
|
48 |
|
franta-hg@13
|
49 |
private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
|
franta-hg@56
|
50 |
private static final int EXIT_SUCCESS = 0;
|
franta-hg@56
|
51 |
private static final int EXIT_EXPECTED_ERROR = 3;
|
franta-hg@56
|
52 |
private static final int EXIT_SQL_ERROR = 4;
|
franta-hg@20
|
53 |
private CLIOptions options;
|
franta-hg@20
|
54 |
private Configuration configuration;
|
franta-hg@13
|
55 |
|
franta-hg@1
|
56 |
public static void main(String[] args) {
|
franta-hg@55
|
57 |
log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
|
franta-hg@56
|
58 |
int exitCode = EXIT_EXPECTED_ERROR;
|
franta-hg@63
|
59 |
|
franta-hg@21
|
60 |
if (args.length == 0) {
|
franta-hg@21
|
61 |
args = new String[]{CLIParser.Tokens.INFO_HELP};
|
franta-hg@21
|
62 |
}
|
franta-hg@21
|
63 |
|
franta-hg@13
|
64 |
try {
|
franta-hg@13
|
65 |
CLIParser parser = new CLIParser();
|
franta-hg@13
|
66 |
CLIOptions options = parser.parseOptions(args);
|
franta-hg@14
|
67 |
options.validate();
|
franta-hg@20
|
68 |
CLIStarter starter = new CLIStarter(options);
|
franta-hg@21
|
69 |
starter.installDefaultConfiguration();
|
franta-hg@20
|
70 |
starter.process();
|
franta-hg@55
|
71 |
log.log(Level.FINE, "All done");
|
franta-hg@56
|
72 |
exitCode = EXIT_SUCCESS;
|
franta-hg@13
|
73 |
} catch (CLIParserException e) {
|
franta-hg@14
|
74 |
log.log(Level.SEVERE, "Unable to parse CLI options", e);
|
franta-hg@14
|
75 |
} catch (InvalidOptionsException e) {
|
franta-hg@14
|
76 |
log.log(Level.SEVERE, "Invalid CLI options", e);
|
franta-hg@48
|
77 |
for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
|
franta-hg@63
|
78 |
LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
|
franta-hg@63
|
79 |
r.setThrown(p.getException());
|
franta-hg@63
|
80 |
r.setParameters(new Object[]{p.getDescription()});
|
franta-hg@63
|
81 |
log.log(r);
|
franta-hg@48
|
82 |
}
|
franta-hg@34
|
83 |
} catch (ConfigurationException e) {
|
franta-hg@34
|
84 |
log.log(Level.SEVERE, "Configuration problem", e);
|
franta-hg@34
|
85 |
} catch (SQLException e) {
|
franta-hg@34
|
86 |
log.log(Level.SEVERE, "SQL problem", e);
|
franta-hg@56
|
87 |
exitCode = EXIT_SQL_ERROR;
|
franta-hg@34
|
88 |
} catch (FormatterException e) {
|
franta-hg@34
|
89 |
log.log(Level.SEVERE, "Formatting problem", e);
|
franta-hg@13
|
90 |
}
|
franta-hg@56
|
91 |
|
franta-hg@56
|
92 |
System.exit(exitCode);
|
franta-hg@4
|
93 |
}
|
franta-hg@20
|
94 |
|
franta-hg@20
|
95 |
public CLIStarter(CLIOptions options) {
|
franta-hg@20
|
96 |
this.options = options;
|
franta-hg@20
|
97 |
}
|
franta-hg@20
|
98 |
|
franta-hg@34
|
99 |
private void process() throws ConfigurationException, SQLException, FormatterException {
|
franta-hg@64
|
100 |
MODE mode = options.getMode();
|
franta-hg@64
|
101 |
|
franta-hg@20
|
102 |
/** Show info */
|
franta-hg@20
|
103 |
if (!options.getShowInfo().isEmpty()) {
|
franta-hg@64
|
104 |
PrintStream infoOut = mode == MODE.JUST_SHOW_INFO ? System.out : System.err;
|
franta-hg@69
|
105 |
InfoLister infoLister = new InfoLister(infoOut, this, options);
|
franta-hg@70
|
106 |
infoLister.showInfo();
|
franta-hg@20
|
107 |
}
|
franta-hg@20
|
108 |
|
franta-hg@20
|
109 |
switch (mode) {
|
franta-hg@20
|
110 |
case QUERY_NOW:
|
franta-hg@34
|
111 |
processQueryNow();
|
franta-hg@20
|
112 |
break;
|
franta-hg@20
|
113 |
case PREPARE_BATCH:
|
franta-hg@34
|
114 |
processPrepareBatch();
|
franta-hg@20
|
115 |
break;
|
franta-hg@20
|
116 |
case EXECUTE_BATCH:
|
franta-hg@34
|
117 |
processExecuteBatch();
|
franta-hg@20
|
118 |
break;
|
franta-hg@20
|
119 |
case JUST_SHOW_INFO:
|
franta-hg@20
|
120 |
// already done above
|
franta-hg@20
|
121 |
break;
|
franta-hg@20
|
122 |
default:
|
franta-hg@20
|
123 |
log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
|
franta-hg@20
|
124 |
break;
|
franta-hg@20
|
125 |
}
|
franta-hg@80
|
126 |
|
franta-hg@80
|
127 |
generateBashCompletion();
|
franta-hg@20
|
128 |
}
|
franta-hg@20
|
129 |
|
franta-hg@34
|
130 |
private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
|
franta-hg@34
|
131 |
DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
|
franta-hg@75
|
132 |
FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
|
franta-hg@75
|
133 |
try (DatabaseConnection c = dd.connect()) {
|
franta-hg@75
|
134 |
log.log(Level.FINE, "Database connected");
|
franta-hg@75
|
135 |
Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
|
franta-hg@75
|
136 |
c.executeQuery(options.getSQLCommand(), f);
|
franta-hg@34
|
137 |
}
|
franta-hg@34
|
138 |
}
|
franta-hg@34
|
139 |
|
franta-hg@34
|
140 |
private void processPrepareBatch() {
|
franta-hg@34
|
141 |
}
|
franta-hg@34
|
142 |
|
franta-hg@34
|
143 |
private void processExecuteBatch() {
|
franta-hg@34
|
144 |
}
|
franta-hg@34
|
145 |
|
franta-hg@20
|
146 |
@Override
|
franta-hg@33
|
147 |
public Configuration getConfiguration() throws ConfigurationException {
|
franta-hg@20
|
148 |
if (configuration == null) {
|
franta-hg@20
|
149 |
configuration = loadConfiguration();
|
franta-hg@20
|
150 |
}
|
franta-hg@20
|
151 |
return configuration;
|
franta-hg@20
|
152 |
}
|
franta-hg@20
|
153 |
|
franta-hg@34
|
154 |
private void installDefaultConfiguration() throws ConfigurationException {
|
franta-hg@33
|
155 |
Constants.DIR.mkdir();
|
franta-hg@33
|
156 |
|
franta-hg@33
|
157 |
if (Constants.CONFIG_FILE.exists()) {
|
franta-hg@55
|
158 |
log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
|
franta-hg@33
|
159 |
} else {
|
franta-hg@33
|
160 |
try {
|
franta-hg@33
|
161 |
Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
|
franta-hg@55
|
162 |
log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
|
franta-hg@33
|
163 |
} catch (IOException e) {
|
franta-hg@34
|
164 |
throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
165 |
}
|
franta-hg@33
|
166 |
}
|
franta-hg@20
|
167 |
}
|
franta-hg@20
|
168 |
|
franta-hg@33
|
169 |
private Configuration loadConfiguration() throws ConfigurationException {
|
franta-hg@33
|
170 |
try {
|
franta-hg@33
|
171 |
JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
|
franta-hg@33
|
172 |
Unmarshaller u = jaxb.createUnmarshaller();
|
franta-hg@33
|
173 |
return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
|
franta-hg@33
|
174 |
} catch (Exception e) {
|
franta-hg@33
|
175 |
throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
176 |
}
|
franta-hg@20
|
177 |
}
|
franta-hg@80
|
178 |
|
franta-hg@80
|
179 |
private void generateBashCompletion() {
|
franta-hg@80
|
180 |
if (configuration == null) {
|
franta-hg@80
|
181 |
log.log(Level.FINER, "Not writing Bash completion helper files. In order to generate these files please run some command which requires configuration.");
|
franta-hg@80
|
182 |
} else {
|
franta-hg@80
|
183 |
try {
|
franta-hg@80
|
184 |
File dir = new File(Constants.DIR, "bash-completion");
|
franta-hg@80
|
185 |
dir.mkdir();
|
franta-hg@80
|
186 |
writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases"));
|
franta-hg@80
|
187 |
writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters"));
|
franta-hg@80
|
188 |
} catch (Exception e) {
|
franta-hg@80
|
189 |
log.log(Level.WARNING, "Unable to generate Bash completion helper files", e);
|
franta-hg@80
|
190 |
}
|
franta-hg@80
|
191 |
}
|
franta-hg@80
|
192 |
}
|
franta-hg@80
|
193 |
|
franta-hg@80
|
194 |
private void writeBashCompletionHelperFile(Collection<? extends NameIdentified> items, File target) throws FileNotFoundException {
|
franta-hg@80
|
195 |
if (Constants.CONFIG_FILE.lastModified() > target.lastModified()) {
|
franta-hg@80
|
196 |
try (PrintWriter fw = new PrintWriter(target)) {
|
franta-hg@80
|
197 |
for (NameIdentified dd : items) {
|
franta-hg@80
|
198 |
fw.println(dd.getName());
|
franta-hg@80
|
199 |
}
|
franta-hg@80
|
200 |
fw.close();
|
franta-hg@80
|
201 |
log.log(Level.FINE, "Bash completion helper file was written: {0}", target);
|
franta-hg@80
|
202 |
}
|
franta-hg@80
|
203 |
} else {
|
franta-hg@80
|
204 |
log.log(Level.FINER, "Not writing Bash completion helper file: {0} because configuration {1} has not been changed", new Object[]{target, Constants.CONFIG_FILE});
|
franta-hg@80
|
205 |
}
|
franta-hg@80
|
206 |
}
|
franta-hg@1
|
207 |
}
|