formatted output for: --test-connection v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 22:32:06 +0100
branchv_0
changeset 73d32fd50d3c2c
parent 72 fc9fc1f26b88
child 74 a8444f6a54f3
formatted output for: --test-connection
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 22:18:24 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 22:32:06 2013 +0100
     1.3 @@ -132,23 +132,44 @@
     1.4  		printTable(formatter, header, data);
     1.5  	}
     1.6  
     1.7 -	public void testConnection() {
     1.8 -		boolean connectionTestResult = false;
     1.9 +	public void testConnection() throws FormatterException, ConfigurationException {
    1.10 +		ColumnsHeader header = constructHeader(
    1.11 +				new HeaderField("database_name", SQLType.VARCHAR),
    1.12 +				new HeaderField("configured", SQLType.VARCHAR),
    1.13 +				new HeaderField("connected", SQLType.VARCHAR));
    1.14 +		List<Object[]> data = new ArrayList<>();
    1.15 +
    1.16 +		/** TODO: support multiple DB to test */
    1.17  		String dbName = options.getDatabaseNameToTest();
    1.18 +
    1.19 +		data.add(testConnection(dbName));
    1.20 +
    1.21 +		printTable(formatter, header, data);
    1.22 +	}
    1.23 +
    1.24 +	public Object[] testConnection(String dbName) {
    1.25  		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
    1.26 +
    1.27 +		boolean succesfullyConnected = false;
    1.28 +		boolean succesfullyConfigured = false;
    1.29 +
    1.30  		try {
    1.31  			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
    1.32  			if (dd == null) {
    1.33 -				log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
    1.34 +				log.log(Level.FINE, "No database with this name is configured: {0}", dbName);
    1.35  			} else {
    1.36  				log.log(Level.FINE, "Database definition was loaded from configuration");
    1.37 -				DatabaseConnection dc = dd.connect();
    1.38 -				connectionTestResult = dc.test();
    1.39 +				succesfullyConfigured = true;
    1.40 +				try (DatabaseConnection dc = dd.connect()) {
    1.41 +					succesfullyConnected = dc.test();
    1.42 +				}
    1.43 +				log.log(Level.FINE, "Database connection test was successful");
    1.44  			}
    1.45  		} catch (ConfigurationException | SQLException e) {
    1.46  			log.log(Level.SEVERE, "Error during testing connection", e);
    1.47  		}
    1.48 -		log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
    1.49 +
    1.50 +		return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
    1.51  	}
    1.52  
    1.53  	public void printResource(String fileName) {
    1.54 @@ -262,7 +283,7 @@
    1.55  		},
    1.56  		CONNECTION {
    1.57  			@Override
    1.58 -			public void showInfo(InfoLister infoLister) {
    1.59 +			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
    1.60  				infoLister.testConnection();
    1.61  			}
    1.62  		};