formatter/database properties v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 31 Dec 2013 17:35:33 +0100
branchv_0
changeset 104245f1b88a3e6
parent 103 5410b6afc839
child 105 39d8b427e20f
formatter/database properties
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.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/FormatterDefinition.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Property.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Mon Dec 30 23:46:41 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Tue Dec 31 17:35:33 2013 +0100
     1.3 @@ -20,6 +20,8 @@
     1.4  import static info.globalcode.sql.dk.Functions.isNotEmpty;
     1.5  import static info.globalcode.sql.dk.Functions.equalz;
     1.6  import info.globalcode.sql.dk.InfoLister.InfoType;
     1.7 +import info.globalcode.sql.dk.configuration.Properties;
     1.8 +import info.globalcode.sql.dk.configuration.Property;
     1.9  import java.io.OutputStream;
    1.10  import java.util.ArrayList;
    1.11  import java.util.Collection;
    1.12 @@ -45,6 +47,8 @@
    1.13  	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    1.14  	private String formatterName;
    1.15  	private boolean batch;
    1.16 +	private Properties formatterProperties = new Properties();
    1.17 +	private Properties databaseProperties = new Properties();
    1.18  
    1.19  	public enum MODE {
    1.20  
    1.21 @@ -167,6 +171,22 @@
    1.22  		namedParameters.add(p);
    1.23  	}
    1.24  
    1.25 +	public Properties getDatabaseProperties() {
    1.26 +		return databaseProperties;
    1.27 +	}
    1.28 +
    1.29 +	public Properties getFormatterProperties() {
    1.30 +		return formatterProperties;
    1.31 +	}
    1.32 +
    1.33 +	public void addDatabaseProperty(Property p) {
    1.34 +		databaseProperties.add(p);
    1.35 +	}
    1.36 +
    1.37 +	public void addFormatterProperty(Property p) {
    1.38 +		formatterProperties.add(p);
    1.39 +	}
    1.40 +
    1.41  	/**
    1.42  	 * @return regular expression describing the name prefix
    1.43  	 */
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Mon Dec 30 23:46:41 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Tue Dec 31 17:35:33 2013 +0100
     2.3 @@ -141,7 +141,7 @@
     2.4  		FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
     2.5  		try (DatabaseConnection c = dd.connect()) {
     2.6  			log.log(Level.FINE, "Database connected");
     2.7 -			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()))) {
     2.8 +			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) {
     2.9  				c.executeQuery(options.getSQLCommand(), f);
    2.10  			}
    2.11  		}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Mon Dec 30 23:46:41 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Tue Dec 31 17:35:33 2013 +0100
     3.3 @@ -93,7 +93,7 @@
     3.4  	}
     3.5  
     3.6  	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
     3.7 -		for (T element : collection) {
     3.8 +		for (T element : notNull(collection)) {
     3.9  			if (element != null && equalz(element.getName(), name)) {
    3.10  				return element;
    3.11  			}
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Mon Dec 30 23:46:41 2013 +0100
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Tue Dec 31 17:35:33 2013 +0100
     4.3 @@ -214,7 +214,7 @@
     4.4  		String formatterName = options.getFormatterName();
     4.5  		formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
     4.6  		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
     4.7 -		FormatterContext context = new FormatterContext(out);
     4.8 +		FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
     4.9  		return fd.getInstance(context);
    4.10  	}
    4.11  
     5.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Mon Dec 30 23:46:41 2013 +0100
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Tue Dec 31 17:35:33 2013 +0100
     5.3 @@ -32,6 +32,7 @@
     5.4  	private String url;
     5.5  	private String userName;
     5.6  	private String password;
     5.7 +	private Properties properties = new Properties();
     5.8  
     5.9  	@XmlElement(name = "name", namespace = CONFIGURATION)
    5.10  	@Override
    5.11 @@ -70,6 +71,15 @@
    5.12  		this.password = password;
    5.13  	}
    5.14  
    5.15 +	@XmlElement(name = "property", namespace = CONFIGURATION)
    5.16 +	public Properties getProperties() {
    5.17 +		return properties;
    5.18 +	}
    5.19 +
    5.20 +	public void setProperties(Properties properties) {
    5.21 +		this.properties = properties;
    5.22 +	}
    5.23 +
    5.24  	public DatabaseConnection connect() throws SQLException {
    5.25  		return new DatabaseConnection(this);
    5.26  	}
     6.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Mon Dec 30 23:46:41 2013 +0100
     6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Tue Dec 31 17:35:33 2013 +0100
     6.3 @@ -34,6 +34,7 @@
     6.4  
     6.5  	private String name;
     6.6  	private String className;
     6.7 +	private Properties properties = new Properties();
     6.8  
     6.9  	public FormatterDefinition() {
    6.10  	}
    6.11 @@ -43,6 +44,11 @@
    6.12  		this.className = className;
    6.13  	}
    6.14  
    6.15 +	public FormatterDefinition(String name, String className, Properties properties) {
    6.16 +		this(name, className);
    6.17 +		this.properties = properties;
    6.18 +	}
    6.19 +
    6.20  	@XmlElement(name = "name", namespace = CONFIGURATION)
    6.21  	@Override
    6.22  	public String getName() {
    6.23 @@ -72,12 +78,22 @@
    6.24  		this.className = className;
    6.25  	}
    6.26  
    6.27 +	@XmlElement(name = "property", namespace = CONFIGURATION)
    6.28 +	public Properties getProperties() {
    6.29 +		return properties;
    6.30 +	}
    6.31 +
    6.32 +	public void setProperties(Properties properties) {
    6.33 +		this.properties = properties;
    6.34 +	}
    6.35 +
    6.36  	/**
    6.37  	 * @param context
    6.38  	 * @return
    6.39  	 * @throws DKException
    6.40  	 */
    6.41  	public Formatter getInstance(FormatterContext context) throws FormatterException {
    6.42 +		context.getProperties().setDefaults(properties);
    6.43  		try {
    6.44  			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
    6.45  
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java	Tue Dec 31 17:35:33 2013 +0100
     7.3 @@ -0,0 +1,76 @@
     7.4 +/**
     7.5 + * SQL-DK
     7.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, either version 3 of the License, or
    7.11 + * (at your option) any later version.
    7.12 + *
    7.13 + * This program is distributed in the hope that it will be useful,
    7.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    7.16 + * GNU General Public License for more details.
    7.17 + *
    7.18 + * You should have received a copy of the GNU General Public License
    7.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    7.20 + */
    7.21 +package info.globalcode.sql.dk.configuration;
    7.22 +
    7.23 +import java.util.ArrayList;
    7.24 +import javax.xml.bind.annotation.XmlTransient;
    7.25 +import static info.globalcode.sql.dk.Functions.findByName;
    7.26 +
    7.27 +/**
    7.28 + * <p>List of configurables.</p>
    7.29 + *
    7.30 + * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
    7.31 + * look
    7.32 + * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
    7.33 + * in default properties.</p>
    7.34 + *
    7.35 + * <p>Typical use: </p>
    7.36 + * <ul>
    7.37 + * <li>this instance – ad-hoc properties from CLI options</li>
    7.38 + * <li>default properties – from config file</li>
    7.39 + * <li>defaultValue – hardcoded default</li>
    7.40 + * </ul>
    7.41 + *
    7.42 + * @author Ing. František Kučera (frantovo.cz)
    7.43 + */
    7.44 +public class Properties extends ArrayList<Property> {
    7.45 +
    7.46 +	private Properties defaults;
    7.47 +
    7.48 +	@XmlTransient
    7.49 +	public Properties getDefaults() {
    7.50 +		return defaults;
    7.51 +	}
    7.52 +
    7.53 +	public void setDefaults(Properties defaults) {
    7.54 +		this.defaults = defaults;
    7.55 +	}
    7.56 +
    7.57 +	private Property findProperty(String name) {
    7.58 +		Property p = findByName(this, name);
    7.59 +		if (p == null) {
    7.60 +			p = findByName(defaults, name);
    7.61 +		}
    7.62 +		return p;
    7.63 +	}
    7.64 +
    7.65 +	public String getString(String name, String defaultValue) {
    7.66 +		Property p = findProperty(name);
    7.67 +		return p == null ? defaultValue : p.getValue();
    7.68 +	}
    7.69 +
    7.70 +	public boolean getBoolean(String name, boolean defaultValue) {
    7.71 +		Property p = findProperty(name);
    7.72 +		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    7.73 +	}
    7.74 +
    7.75 +	public int getInteger(String name, int defaultValue) {
    7.76 +		Property p = findProperty(name);
    7.77 +		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    7.78 +	}
    7.79 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Property.java	Tue Dec 31 17:35:33 2013 +0100
     8.3 @@ -0,0 +1,64 @@
     8.4 +/**
     8.5 + * SQL-DK
     8.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     8.7 + *
     8.8 + * This program is free software: you can redistribute it and/or modify
     8.9 + * it under the terms of the GNU General Public License as published by
    8.10 + * the Free Software Foundation, either version 3 of the License, or
    8.11 + * (at your option) any later version.
    8.12 + *
    8.13 + * This program is distributed in the hope that it will be useful,
    8.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    8.16 + * GNU General Public License for more details.
    8.17 + *
    8.18 + * You should have received a copy of the GNU General Public License
    8.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    8.20 + */
    8.21 +package info.globalcode.sql.dk.configuration;
    8.22 +
    8.23 +import javax.xml.bind.annotation.XmlAttribute;
    8.24 +import javax.xml.bind.annotation.XmlValue;
    8.25 +
    8.26 +/**
    8.27 + * One configurable
    8.28 + *
    8.29 + * @author Ing. František Kučera (frantovo.cz)
    8.30 + */
    8.31 +public class Property implements NameIdentified {
    8.32 +
    8.33 +	private String name;
    8.34 +	private String value;
    8.35 +
    8.36 +	public Property() {
    8.37 +	}
    8.38 +
    8.39 +	public Property(String name, String value) {
    8.40 +		this.name = name;
    8.41 +		this.value = value;
    8.42 +	}
    8.43 +
    8.44 +	@XmlAttribute(name = "name")
    8.45 +	@Override
    8.46 +	public String getName() {
    8.47 +		return name;
    8.48 +	}
    8.49 +
    8.50 +	public void setName(String name) {
    8.51 +		this.name = name;
    8.52 +	}
    8.53 +
    8.54 +	@XmlValue
    8.55 +	public String getValue() {
    8.56 +		return value;
    8.57 +	}
    8.58 +
    8.59 +	public void setValue(String value) {
    8.60 +		this.value = value;
    8.61 +	}
    8.62 +
    8.63 +	@Override
    8.64 +	public String toString() {
    8.65 +		return name + "='" + value + "'";
    8.66 +	}
    8.67 +}
     9.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java	Mon Dec 30 23:46:41 2013 +0100
     9.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java	Tue Dec 31 17:35:33 2013 +0100
     9.3 @@ -17,6 +17,7 @@
     9.4   */
     9.5  package info.globalcode.sql.dk.formatting;
     9.6  
     9.7 +import info.globalcode.sql.dk.configuration.Properties;
     9.8  import java.io.OutputStream;
     9.9  
    9.10  /**
    9.11 @@ -26,12 +27,22 @@
    9.12  public class FormatterContext {
    9.13  
    9.14  	private OutputStream outputStream;
    9.15 +	private Properties properties;
    9.16  
    9.17 -	public FormatterContext(OutputStream outputStream) {
    9.18 +	public FormatterContext(OutputStream outputStream, Properties properties) {
    9.19  		this.outputStream = outputStream;
    9.20 +		this.properties = properties;
    9.21  	}
    9.22  
    9.23  	public OutputStream getOutputStream() {
    9.24  		return outputStream;
    9.25  	}
    9.26 +
    9.27 +	public Properties getProperties() {
    9.28 +		return properties;
    9.29 +	}
    9.30 +
    9.31 +	public void setProperties(Properties properties) {
    9.32 +		this.properties = properties;
    9.33 +	}
    9.34  }
    10.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 30 23:46:41 2013 +0100
    10.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Tue Dec 31 17:35:33 2013 +0100
    10.3 @@ -33,21 +33,27 @@
    10.4  	public static final String NAME = "tabular"; // bash-completion:formatter
    10.5  	private static final String HEADER_TYPE_PREFIX = " (";
    10.6  	private static final String HEADER_TYPE_SUFFIX = ")";
    10.7 +	public static final String PROPERTY_ASCII = "ascii";
    10.8 +	public static final String PROPERTY_COLORFUL = "color";
    10.9 +	public static final String PROPERTY_TRIM = "trim";
   10.10  	private ColorfulPrintWriter out;
   10.11  	private boolean firstResult = true;
   10.12  	private int[] columnWidth;
   10.13  	/**
   10.14  	 * use ASCII borders instead of unicode ones
   10.15  	 */
   10.16 -	private final boolean asciiNostalgia = false;
   10.17 +	private final boolean asciiNostalgia;
   10.18  	/**
   10.19  	 * Trim values if they are longer than cell size
   10.20  	 */
   10.21 -	private final boolean trimValues = false;
   10.22 +	private final boolean trimValues;
   10.23  
   10.24  	public TabularFormatter(FormatterContext formatterContext) {
   10.25  		super(formatterContext);
   10.26  		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
   10.27 +		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
   10.28 +		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
   10.29 +		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
   10.30  	}
   10.31  
   10.32  	@Override