java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 69 0befec5034c2
parent 67 10c9b9e54622
child 70 02c8eaa425e8
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 11:58:14 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 21:18:54 2013 +0100
     1.3 @@ -23,14 +23,18 @@
     1.4  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     1.5  import info.globalcode.sql.dk.configuration.FormatterDefinition;
     1.6  import static info.globalcode.sql.dk.Functions.rpad;
     1.7 +import info.globalcode.sql.dk.formatting.ColumnsHeader;
     1.8 +import info.globalcode.sql.dk.formatting.Formatter;
     1.9 +import info.globalcode.sql.dk.formatting.FormatterContext;
    1.10 +import info.globalcode.sql.dk.formatting.FormatterException;
    1.11  import java.io.BufferedReader;
    1.12  import java.io.InputStreamReader;
    1.13  import java.io.PrintStream;
    1.14  import java.sql.SQLException;
    1.15 -import java.util.EnumSet;
    1.16  import java.util.List;
    1.17  import java.util.logging.Level;
    1.18  import java.util.logging.Logger;
    1.19 +import javax.sql.rowset.RowSetMetaDataImpl;
    1.20  
    1.21  /**
    1.22   * Displays info like help, version etc.
    1.23 @@ -42,86 +46,67 @@
    1.24  	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
    1.25  	private PrintStream out;
    1.26  	private ConfigurationProvider configurationProvider;
    1.27 +	private CLIOptions options;
    1.28  
    1.29 -	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
    1.30 +	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
    1.31  		this.out = out;
    1.32  		this.configurationProvider = configurationProvider;
    1.33 +		this.options = options;
    1.34  	}
    1.35  
    1.36 -	public void showInfo(CLIOptions options) throws ConfigurationException {
    1.37 -		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
    1.38 -		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
    1.39 -			switch (infoType) {
    1.40 -				/**
    1.41 -				 * TODO: implement show info
    1.42 -				 */
    1.43 -				case FORMATTERS:
    1.44 -					for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
    1.45 -						log.log(Level.INFO, "Built-in formatter:   {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    1.46 -					}
    1.47 +	public void listFormatters() throws ConfigurationException {
    1.48 +		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
    1.49 +			log.log(Level.INFO, "Built-in formatter:   {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    1.50 +		}
    1.51 +		List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters();
    1.52 +		for (FormatterDefinition fd : configuredFormatters) {
    1.53 +			log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    1.54 +		}
    1.55 +		if (configuredFormatters.isEmpty()) {
    1.56 +			log.log(Level.INFO, "No other formatters are configured");
    1.57 +		}
    1.58 +		String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
    1.59 +		if (configuredDefaultFormatter == null) {
    1.60 +			log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER);
    1.61 +		} else {
    1.62 +			log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter);
    1.63 +		}
    1.64 +	}
    1.65  
    1.66 -					List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters();
    1.67 -					for (FormatterDefinition fd : configuredFormatters) {
    1.68 -						log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    1.69 -					}
    1.70 -					if (configuredFormatters.isEmpty()) {
    1.71 -						log.log(Level.INFO, "No other formatters are configured");
    1.72 -					}
    1.73 +	public void listTypes() throws FormatterException {
    1.74 +	}
    1.75  
    1.76 -					String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
    1.77 -					if (configuredDefaultFormatter == null) {
    1.78 -						log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER);
    1.79 -					} else {
    1.80 -						log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter);
    1.81 -					}
    1.82 -					break;
    1.83 -				case HELP:
    1.84 -					printResource(Constants.HELP_FILE);
    1.85 -					break;
    1.86 -				case LICENSE:
    1.87 -					printResource(Constants.LICENSE_FILE);
    1.88 -					break;
    1.89 -				case TYPES:
    1.90 -					println("TODO: list supported types");
    1.91 -					break;
    1.92 -				case VERSION:
    1.93 -					printResource(Constants.VERSION_FILE);
    1.94 -					break;
    1.95 -				case DATABASES:
    1.96 -					final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
    1.97 -					if (configuredDatabases.isEmpty()) {
    1.98 -						log.log(Level.WARNING, "No databases are configured.");
    1.99 -					} else {
   1.100 -						for (DatabaseDefinition dd : configuredDatabases) {
   1.101 -							log.log(Level.INFO, "Configured database: {0}", dd.getName());
   1.102 -						}
   1.103 -					}
   1.104 -					break;
   1.105 -				case CONNECTION:
   1.106 -					boolean connectionTestResult = false;
   1.107 -					String dbName = options.getDatabaseNameToTest();
   1.108 -					log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   1.109 -					try {
   1.110 -						DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
   1.111 -						if (dd == null) {
   1.112 -							log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
   1.113 -						} else {
   1.114 -							log.log(Level.FINE, "Database definition was loaded from configuration");
   1.115 -							DatabaseConnection dc = dd.connect();
   1.116 -							connectionTestResult = dc.test();
   1.117 -						}
   1.118 -					} catch (ConfigurationException | SQLException e) {
   1.119 -						log.log(Level.SEVERE, "Error during testing connection", e);
   1.120 -					}
   1.121 -					log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
   1.122 -					break;
   1.123 -				default:
   1.124 -					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
   1.125 +	public void listDatabases() throws ConfigurationException {
   1.126 +		final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
   1.127 +		if (configuredDatabases.isEmpty()) {
   1.128 +			log.log(Level.WARNING, "No databases are configured.");
   1.129 +		} else {
   1.130 +			for (DatabaseDefinition dd : configuredDatabases) {
   1.131 +				log.log(Level.INFO, "Configured database: {0}", dd.getName());
   1.132  			}
   1.133  		}
   1.134  	}
   1.135  
   1.136 -	private void printResource(String fileName) {
   1.137 +	public void testConnection() {
   1.138 +		boolean connectionTestResult = false;
   1.139 +		String dbName = options.getDatabaseNameToTest();
   1.140 +		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   1.141 +		try {
   1.142 +			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
   1.143 +			if (dd == null) {
   1.144 +				log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
   1.145 +			} else {
   1.146 +				log.log(Level.FINE, "Database definition was loaded from configuration");
   1.147 +				DatabaseConnection dc = dd.connect();
   1.148 +				connectionTestResult = dc.test();
   1.149 +			}
   1.150 +		} catch (ConfigurationException | SQLException e) {
   1.151 +			log.log(Level.SEVERE, "Error during testing connection", e);
   1.152 +		}
   1.153 +		log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
   1.154 +	}
   1.155 +
   1.156 +	public void printResource(String fileName) {
   1.157  		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
   1.158  			while (true) {
   1.159  				String line = reader.readLine();
   1.160 @@ -139,4 +124,104 @@
   1.161  	private void println(String line) {
   1.162  		out.println(line);
   1.163  	}
   1.164 +
   1.165 +	private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
   1.166 +		formatter.writeStartResultSet();
   1.167 +		formatter.writeColumnsHeader(header);
   1.168 +
   1.169 +		for (Object[] row : data) {
   1.170 +			formatter.writeStartRow();
   1.171 +			for (Object cell : row) {
   1.172 +				formatter.writeColumnValue(cell);
   1.173 +			}
   1.174 +			formatter.writeEndRow();
   1.175 +		}
   1.176 +
   1.177 +		formatter.writeEndResultSet();
   1.178 +	}
   1.179 +
   1.180 +	private Formatter getFormatter() throws ConfigurationException, FormatterException {
   1.181 +		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(options.getFormatterName());
   1.182 +		FormatterContext context = new FormatterContext(out);
   1.183 +		return fd.getInstance(context);
   1.184 +	}
   1.185 +
   1.186 +	private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
   1.187 +		try {
   1.188 +			RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
   1.189 +			metaData.setColumnCount(fields.length);
   1.190 +
   1.191 +			for (int i = 0; i < fields.length; i++) {
   1.192 +				HeaderField hf = fields[i];
   1.193 +				int sqlIndex = i + 1;
   1.194 +				metaData.setColumnName(sqlIndex, hf.name);
   1.195 +				metaData.setColumnLabel(sqlIndex, hf.name);
   1.196 +				metaData.setColumnType(sqlIndex, hf.type.getCode());
   1.197 +				metaData.setColumnTypeName(sqlIndex, hf.type.name());
   1.198 +			}
   1.199 +
   1.200 +			return new ColumnsHeader(metaData);
   1.201 +		} catch (SQLException e) {
   1.202 +			throw new FormatterException("Error while constructing table headers", e);
   1.203 +		}
   1.204 +	}
   1.205 +
   1.206 +	private static class HeaderField {
   1.207 +
   1.208 +		String name;
   1.209 +		SQLType type;
   1.210 +
   1.211 +		public HeaderField(String name, SQLType type) {
   1.212 +			this.name = name;
   1.213 +			this.type = type;
   1.214 +		}
   1.215 +	}
   1.216 +
   1.217 +	public enum InfoType {
   1.218 +
   1.219 +		HELP {
   1.220 +			@Override
   1.221 +			public void showInfo(InfoLister infoLister) {
   1.222 +				infoLister.printResource(Constants.HELP_FILE);
   1.223 +			}
   1.224 +		},
   1.225 +		VERSION {
   1.226 +			@Override
   1.227 +			public void showInfo(InfoLister infoLister) {
   1.228 +				infoLister.printResource(Constants.VERSION_FILE);
   1.229 +			}
   1.230 +		},
   1.231 +		LICENSE {
   1.232 +			@Override
   1.233 +			public void showInfo(InfoLister infoLister) {
   1.234 +				infoLister.printResource(Constants.LICENSE_FILE);
   1.235 +			}
   1.236 +		},
   1.237 +		FORMATTERS {
   1.238 +			@Override
   1.239 +			public void showInfo(InfoLister infoLister) throws ConfigurationException {
   1.240 +				infoLister.listFormatters();
   1.241 +			}
   1.242 +		},
   1.243 +		TYPES {
   1.244 +			@Override
   1.245 +			public void showInfo(InfoLister infoLister) throws FormatterException {
   1.246 +				infoLister.listTypes();
   1.247 +			}
   1.248 +		},
   1.249 +		DATABASES {
   1.250 +			@Override
   1.251 +			public void showInfo(InfoLister infoLister) throws ConfigurationException {
   1.252 +				infoLister.listDatabases();
   1.253 +			}
   1.254 +		},
   1.255 +		CONNECTION {
   1.256 +			@Override
   1.257 +			public void showInfo(InfoLister infoLister) {
   1.258 +				infoLister.testConnection();
   1.259 +			}
   1.260 +		};
   1.261 +
   1.262 +		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
   1.263 +	}
   1.264  }