java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
branchv_0
changeset 34 9335cf31c0f2
parent 33 04db6ccd6c48
child 36 025fbe816bbf
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 22 22:02:44 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 22 23:31:55 2013 +0100
     1.3 @@ -21,7 +21,13 @@
     1.4  import info.globalcode.sql.dk.CLIOptions.MODE;
     1.5  import info.globalcode.sql.dk.configuration.Configuration;
     1.6  import info.globalcode.sql.dk.configuration.ConfigurationException;
     1.7 +import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     1.8 +import info.globalcode.sql.dk.configuration.FormatterDefinition;
     1.9 +import info.globalcode.sql.dk.formatting.Formatter;
    1.10 +import info.globalcode.sql.dk.formatting.FormatterContext;
    1.11 +import info.globalcode.sql.dk.formatting.FormatterException;
    1.12  import java.io.IOException;
    1.13 +import java.sql.SQLException;
    1.14  import java.util.logging.Level;
    1.15  import java.util.logging.Logger;
    1.16  import javax.xml.bind.JAXBContext;
    1.17 @@ -54,6 +60,12 @@
    1.18  			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    1.19  		} catch (InvalidOptionsException e) {
    1.20  			log.log(Level.SEVERE, "Invalid CLI options", e);
    1.21 +		} catch (ConfigurationException e) {
    1.22 +			log.log(Level.SEVERE, "Configuration problem", e);
    1.23 +		} catch (SQLException e) {
    1.24 +			log.log(Level.SEVERE, "SQL problem", e);
    1.25 +		} catch (FormatterException e) {
    1.26 +			log.log(Level.SEVERE, "Formatting problem", e);
    1.27  		}
    1.28  	}
    1.29  
    1.30 @@ -61,7 +73,7 @@
    1.31  		this.options = options;
    1.32  	}
    1.33  
    1.34 -	private void process() {
    1.35 +	private void process() throws ConfigurationException, SQLException, FormatterException {
    1.36  		/** Show info */
    1.37  		if (!options.getShowInfo().isEmpty()) {
    1.38  			InfoLister infoLister = new InfoLister(System.err, this);
    1.39 @@ -71,10 +83,13 @@
    1.40  		MODE mode = options.getMode();
    1.41  		switch (mode) {
    1.42  			case QUERY_NOW:
    1.43 +				processQueryNow();
    1.44  				break;
    1.45  			case PREPARE_BATCH:
    1.46 +				processPrepareBatch();
    1.47  				break;
    1.48  			case EXECUTE_BATCH:
    1.49 +				processExecuteBatch();
    1.50  				break;
    1.51  			case JUST_SHOW_INFO:
    1.52  				// already done above
    1.53 @@ -85,6 +100,28 @@
    1.54  		}
    1.55  	}
    1.56  
    1.57 +	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
    1.58 +		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
    1.59 +		if (dd == null) {
    1.60 +			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
    1.61 +		} else {
    1.62 +			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
    1.63 +			if (fd == null) {
    1.64 +				throw new ConfigurationException("Formatter is not configured: " + options.getDatabaseName());
    1.65 +			} else {
    1.66 +				DatabaseConnection c = dd.connect();
    1.67 +				Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
    1.68 +				c.executeQuery(options.getSQLCommand(), f);
    1.69 +			}
    1.70 +		}
    1.71 +	}
    1.72 +
    1.73 +	private void processPrepareBatch() {
    1.74 +	}
    1.75 +
    1.76 +	private void processExecuteBatch() {
    1.77 +	}
    1.78 +
    1.79  	@Override
    1.80  	public Configuration getConfiguration() throws ConfigurationException {
    1.81  		if (configuration == null) {
    1.82 @@ -93,7 +130,7 @@
    1.83  		return configuration;
    1.84  	}
    1.85  
    1.86 -	private void installDefaultConfiguration() {
    1.87 +	private void installDefaultConfiguration() throws ConfigurationException {
    1.88  		Constants.DIR.mkdir();
    1.89  
    1.90  		if (Constants.CONFIG_FILE.exists()) {
    1.91 @@ -102,10 +139,9 @@
    1.92  			try {
    1.93  				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
    1.94  			} catch (IOException e) {
    1.95 -				log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
    1.96 +				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
    1.97  			}
    1.98  		}
    1.99 -
   1.100  	}
   1.101  
   1.102  	private Configuration loadConfiguration() throws ConfigurationException {