/**
* SQL-DK
* Copyright © 2013 František Kučera (frantovo.cz)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package info.globalcode.sql.dk;
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
import info.globalcode.sql.dk.CLIOptions.MODE;
import info.globalcode.sql.dk.configuration.Configuration;
import info.globalcode.sql.dk.configuration.ConfigurationException;
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
import info.globalcode.sql.dk.configuration.FormatterDefinition;
import info.globalcode.sql.dk.formatting.Formatter;
import info.globalcode.sql.dk.formatting.FormatterContext;
import info.globalcode.sql.dk.formatting.FormatterException;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
/**
*
* @author Ing. František Kučera (frantovo.cz)
*/
public class CLIStarter implements ConfigurationProvider {
private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
private static final int EXIT_SUCCESS = 0;
private static final int EXIT_EXPECTED_ERROR = 3;
private static final int EXIT_SQL_ERROR = 4;
private CLIOptions options;
private Configuration configuration;
public static void main(String[] args) {
log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
int exitCode = EXIT_EXPECTED_ERROR;
if (args.length == 0) {
args = new String[]{CLIParser.Tokens.INFO_HELP};
}
try {
CLIParser parser = new CLIParser();
CLIOptions options = parser.parseOptions(args);
options.validate();
CLIStarter starter = new CLIStarter(options);
starter.installDefaultConfiguration();
starter.process();
log.log(Level.FINE, "All done");
exitCode = EXIT_SUCCESS;
} catch (CLIParserException e) {
log.log(Level.SEVERE, "Unable to parse CLI options", e);
} catch (InvalidOptionsException e) {
log.log(Level.SEVERE, "Invalid CLI options", e);
for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
r.setThrown(p.getException());
r.setParameters(new Object[]{p.getDescription()});
log.log(r);
}
} catch (ConfigurationException e) {
log.log(Level.SEVERE, "Configuration problem", e);
} catch (SQLException e) {
log.log(Level.SEVERE, "SQL problem", e);
exitCode = EXIT_SQL_ERROR;
} catch (FormatterException e) {
log.log(Level.SEVERE, "Formatting problem", e);
}
System.exit(exitCode);
}
public CLIStarter(CLIOptions options) {
this.options = options;
}
private void process() throws ConfigurationException, SQLException, FormatterException {
MODE mode = options.getMode();
/** Show info */
if (!options.getShowInfo().isEmpty()) {
PrintStream infoOut = mode == MODE.JUST_SHOW_INFO ? System.out : System.err;
InfoLister infoLister = new InfoLister(infoOut, this, options);
infoLister.showInfo();
}
switch (mode) {
case QUERY_NOW:
processQueryNow();
break;
case PREPARE_BATCH:
processPrepareBatch();
break;
case EXECUTE_BATCH:
processExecuteBatch();
break;
case JUST_SHOW_INFO:
// already done above
break;
default:
log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
break;
}
}
private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
if (dd == null) {
throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
} else {
FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
if (fd == null) {
throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
} else {
try (DatabaseConnection c = dd.connect()) {
log.log(Level.FINE, "Database connected");
Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
c.executeQuery(options.getSQLCommand(), f);
}
}
}
}
private void processPrepareBatch() {
}
private void processExecuteBatch() {
}
@Override
public Configuration getConfiguration() throws ConfigurationException {
if (configuration == null) {
configuration = loadConfiguration();
}
return configuration;
}
private void installDefaultConfiguration() throws ConfigurationException {
Constants.DIR.mkdir();
if (Constants.CONFIG_FILE.exists()) {
log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
} else {
try {
Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
} catch (IOException e) {
throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
}
}
}
private Configuration loadConfiguration() throws ConfigurationException {
try {
JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
Unmarshaller u = jaxb.createUnmarshaller();
return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
} catch (Exception e) {
throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
}
}
}