use database properties v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 01 Jan 2014 01:06:38 +0100
branchv_0
changeset 106e9c3583580c8
parent 105 39d8b427e20f
child 107 8189a4a28cd8
use database properties
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Tue Dec 31 18:11:50 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Wed Jan 01 01:06:38 2014 +0100
     1.3 @@ -139,7 +139,7 @@
     1.4  	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
     1.5  		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
     1.6  		FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
     1.7 -		try (DatabaseConnection c = dd.connect()) {
     1.8 +		try (DatabaseConnection c = dd.connect(options.getDatabaseProperties())) {
     1.9  			log.log(Level.FINE, "Database connected");
    1.10  			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) {
    1.11  				c.executeQuery(options.getSQLCommand(), f);
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Tue Dec 31 18:11:50 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Wed Jan 01 01:06:38 2014 +0100
     2.3 @@ -19,6 +19,7 @@
     2.4  
     2.5  import info.globalcode.sql.dk.batch.Batch;
     2.6  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     2.7 +import info.globalcode.sql.dk.configuration.Properties;
     2.8  import info.globalcode.sql.dk.formatting.ColumnsHeader;
     2.9  import info.globalcode.sql.dk.formatting.Formatter;
    2.10  import java.sql.Connection;
    2.11 @@ -39,11 +40,18 @@
    2.12  	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
    2.13  	private DatabaseDefinition databaseDefinition;
    2.14  	private Connection connection;
    2.15 +	private Properties properties;
    2.16  
    2.17 -	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
    2.18 +	public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
    2.19  		this.databaseDefinition = databaseDefinition;
    2.20 +		this.properties = properties;
    2.21  
    2.22 -		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
    2.23 +		properties.setDefaults(databaseDefinition.getProperties());
    2.24 +		java.util.Properties javaProperties = properties.getJavaProperties();
    2.25 +		javaProperties.setProperty("user", databaseDefinition.getUserName());
    2.26 +		javaProperties.setProperty("password", databaseDefinition.getPassword());
    2.27 +
    2.28 +		connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
    2.29  	}
    2.30  
    2.31  	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Tue Dec 31 18:11:50 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Wed Jan 01 01:06:38 2014 +0100
     3.3 @@ -165,7 +165,7 @@
     3.4  			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
     3.5  			log.log(Level.FINE, "Database definition was loaded from configuration");
     3.6  			succesfullyConfigured = true;
     3.7 -			try (DatabaseConnection dc = dd.connect()) {
     3.8 +			try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
     3.9  				succesfullyConnected = dc.test();
    3.10  			}
    3.11  			log.log(Level.FINE, "Database connection test was successful");
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Tue Dec 31 18:11:50 2013 +0100
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Wed Jan 01 01:06:38 2014 +0100
     4.3 @@ -80,7 +80,10 @@
     4.4  		this.properties = properties;
     4.5  	}
     4.6  
     4.7 -	public DatabaseConnection connect() throws SQLException {
     4.8 -		return new DatabaseConnection(this);
     4.9 +	/**
    4.10 +	 * @param properties ad-hoc properties from CLI options
    4.11 +	 */
    4.12 +	public DatabaseConnection connect(Properties properties) throws SQLException {
    4.13 +		return new DatabaseConnection(this, properties);
    4.14  	}
    4.15  }
     5.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java	Tue Dec 31 18:11:50 2013 +0100
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java	Wed Jan 01 01:06:38 2014 +0100
     5.3 @@ -88,4 +88,22 @@
     5.4  		Collections.copy(clone, this);
     5.5  		return clone;
     5.6  	}
     5.7 +
     5.8 +	/**
     5.9 +	 * @return merged this and backing defaults as Java Properties
    5.10 +	 */
    5.11 +	public java.util.Properties getJavaProperties() {
    5.12 +		java.util.Properties javaProperties = new java.util.Properties();
    5.13 +		if (defaults != null) {
    5.14 +			duplicateTo(javaProperties);
    5.15 +		}
    5.16 +		duplicateTo(javaProperties);
    5.17 +		return javaProperties;
    5.18 +	}
    5.19 +
    5.20 +	private void duplicateTo(java.util.Properties javaProperties) {
    5.21 +		for (Property p : this) {
    5.22 +			javaProperties.setProperty(p.getName(), p.getValue());
    5.23 +		}
    5.24 +	}
    5.25  }