java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
branchv_0
changeset 34 9335cf31c0f2
parent 33 04db6ccd6c48
child 36 025fbe816bbf
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 22 22:02:44 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 22 23:31:55 2013 +0100
@@ -21,7 +21,13 @@
 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.sql.SQLException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.xml.bind.JAXBContext;
@@ -54,6 +60,12 @@
 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
 		} catch (InvalidOptionsException e) {
 			log.log(Level.SEVERE, "Invalid CLI options", e);
+		} catch (ConfigurationException e) {
+			log.log(Level.SEVERE, "Configuration problem", e);
+		} catch (SQLException e) {
+			log.log(Level.SEVERE, "SQL problem", e);
+		} catch (FormatterException e) {
+			log.log(Level.SEVERE, "Formatting problem", e);
 		}
 	}
 
@@ -61,7 +73,7 @@
 		this.options = options;
 	}
 
-	private void process() {
+	private void process() throws ConfigurationException, SQLException, FormatterException {
 		/** Show info */
 		if (!options.getShowInfo().isEmpty()) {
 			InfoLister infoLister = new InfoLister(System.err, this);
@@ -71,10 +83,13 @@
 		MODE mode = options.getMode();
 		switch (mode) {
 			case QUERY_NOW:
+				processQueryNow();
 				break;
 			case PREPARE_BATCH:
+				processPrepareBatch();
 				break;
 			case EXECUTE_BATCH:
+				processExecuteBatch();
 				break;
 			case JUST_SHOW_INFO:
 				// already done above
@@ -85,6 +100,28 @@
 		}
 	}
 
+	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.getDatabaseName());
+			} else {
+				DatabaseConnection c = dd.connect();
+				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) {
@@ -93,7 +130,7 @@
 		return configuration;
 	}
 
-	private void installDefaultConfiguration() {
+	private void installDefaultConfiguration() throws ConfigurationException {
 		Constants.DIR.mkdir();
 
 		if (Constants.CONFIG_FILE.exists()) {
@@ -102,10 +139,9 @@
 			try {
 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
 			} catch (IOException e) {
-				log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
+				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
 			}
 		}
-
 	}
 
 	private Configuration loadConfiguration() throws ConfigurationException {