# HG changeset patch # User František Kučera # Date 1388507733 -3600 # Node ID 245f1b88a3e680218d1a907e2a6c860df9cc355d # Parent 5410b6afc839daca8b5852d2aabc9c05293454b7 formatter/database properties diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Tue Dec 31 17:35:33 2013 +0100 @@ -20,6 +20,8 @@ import static info.globalcode.sql.dk.Functions.isNotEmpty; import static info.globalcode.sql.dk.Functions.equalz; import info.globalcode.sql.dk.InfoLister.InfoType; +import info.globalcode.sql.dk.configuration.Properties; +import info.globalcode.sql.dk.configuration.Property; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; @@ -45,6 +47,8 @@ private String nameSuffix = DEFAULT_NAME_SUFFIX; private String formatterName; private boolean batch; + private Properties formatterProperties = new Properties(); + private Properties databaseProperties = new Properties(); public enum MODE { @@ -167,6 +171,22 @@ namedParameters.add(p); } + public Properties getDatabaseProperties() { + return databaseProperties; + } + + public Properties getFormatterProperties() { + return formatterProperties; + } + + public void addDatabaseProperty(Property p) { + databaseProperties.add(p); + } + + public void addFormatterProperty(Property p) { + formatterProperties.add(p); + } + /** * @return regular expression describing the name prefix */ diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Tue Dec 31 17:35:33 2013 +0100 @@ -141,7 +141,7 @@ FormatterDefinition fd = configuration.getFormatter(options.getFormatterName()); try (DatabaseConnection c = dd.connect()) { log.log(Level.FINE, "Database connected"); - try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()))) { + try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) { c.executeQuery(options.getSQLCommand(), f); } } diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/Functions.java --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Tue Dec 31 17:35:33 2013 +0100 @@ -93,7 +93,7 @@ } public static T findByName(Collection collection, String name) { - for (T element : collection) { + for (T element : notNull(collection)) { if (element != null && equalz(element.getName(), name)) { return element; } diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Tue Dec 31 17:35:33 2013 +0100 @@ -214,7 +214,7 @@ String formatterName = options.getFormatterName(); formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName; FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName); - FormatterContext context = new FormatterContext(out); + FormatterContext context = new FormatterContext(out, options.getFormatterProperties()); return fd.getInstance(context); } diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java Tue Dec 31 17:35:33 2013 +0100 @@ -32,6 +32,7 @@ private String url; private String userName; private String password; + private Properties properties = new Properties(); @XmlElement(name = "name", namespace = CONFIGURATION) @Override @@ -70,6 +71,15 @@ this.password = password; } + @XmlElement(name = "property", namespace = CONFIGURATION) + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } + public DatabaseConnection connect() throws SQLException { return new DatabaseConnection(this); } diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java Tue Dec 31 17:35:33 2013 +0100 @@ -34,6 +34,7 @@ private String name; private String className; + private Properties properties = new Properties(); public FormatterDefinition() { } @@ -43,6 +44,11 @@ this.className = className; } + public FormatterDefinition(String name, String className, Properties properties) { + this(name, className); + this.properties = properties; + } + @XmlElement(name = "name", namespace = CONFIGURATION) @Override public String getName() { @@ -72,12 +78,22 @@ this.className = className; } + @XmlElement(name = "property", namespace = CONFIGURATION) + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } + /** * @param context * @return * @throws DKException */ public Formatter getInstance(FormatterContext context) throws FormatterException { + context.getProperties().setDefaults(properties); try { Constructor constructor = Class.forName(className).getConstructor(context.getClass()); diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java Tue Dec 31 17:35:33 2013 +0100 @@ -0,0 +1,76 @@ +/** + * SQL-DK + * Copyright © 2013 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk.configuration; + +import java.util.ArrayList; +import javax.xml.bind.annotation.XmlTransient; +import static info.globalcode.sql.dk.Functions.findByName; + +/** + *

List of configurables.

+ * + *

Can be backed by defaults – if value for given name is nof found in this instance, we will + * look + * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even + * in default properties.

+ * + *

Typical use:

+ *
    + *
  • this instance – ad-hoc properties from CLI options
  • + *
  • default properties – from config file
  • + *
  • defaultValue – hardcoded default
  • + *
+ * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Properties extends ArrayList { + + private Properties defaults; + + @XmlTransient + public Properties getDefaults() { + return defaults; + } + + public void setDefaults(Properties defaults) { + this.defaults = defaults; + } + + private Property findProperty(String name) { + Property p = findByName(this, name); + if (p == null) { + p = findByName(defaults, name); + } + return p; + } + + public String getString(String name, String defaultValue) { + Property p = findProperty(name); + return p == null ? defaultValue : p.getValue(); + } + + public boolean getBoolean(String name, boolean defaultValue) { + Property p = findProperty(name); + return p == null ? defaultValue : Boolean.valueOf(p.getValue()); + } + + public int getInteger(String name, int defaultValue) { + Property p = findProperty(name); + return p == null ? defaultValue : Integer.valueOf(p.getValue()); + } +} diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/configuration/Property.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Property.java Tue Dec 31 17:35:33 2013 +0100 @@ -0,0 +1,64 @@ +/** + * SQL-DK + * Copyright © 2013 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk.configuration; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + +/** + * One configurable + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Property implements NameIdentified { + + private String name; + private String value; + + public Property() { + } + + public Property(String name, String value) { + this.name = name; + this.value = value; + } + + @XmlAttribute(name = "name") + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return name + "='" + value + "'"; + } +} diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/FormatterContext.java Tue Dec 31 17:35:33 2013 +0100 @@ -17,6 +17,7 @@ */ package info.globalcode.sql.dk.formatting; +import info.globalcode.sql.dk.configuration.Properties; import java.io.OutputStream; /** @@ -26,12 +27,22 @@ public class FormatterContext { private OutputStream outputStream; + private Properties properties; - public FormatterContext(OutputStream outputStream) { + public FormatterContext(OutputStream outputStream, Properties properties) { this.outputStream = outputStream; + this.properties = properties; } public OutputStream getOutputStream() { return outputStream; } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff -r 5410b6afc839 -r 245f1b88a3e6 java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Mon Dec 30 23:46:41 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Tue Dec 31 17:35:33 2013 +0100 @@ -33,21 +33,27 @@ public static final String NAME = "tabular"; // bash-completion:formatter private static final String HEADER_TYPE_PREFIX = " ("; private static final String HEADER_TYPE_SUFFIX = ")"; + public static final String PROPERTY_ASCII = "ascii"; + public static final String PROPERTY_COLORFUL = "color"; + public static final String PROPERTY_TRIM = "trim"; private ColorfulPrintWriter out; private boolean firstResult = true; private int[] columnWidth; /** * use ASCII borders instead of unicode ones */ - private final boolean asciiNostalgia = false; + private final boolean asciiNostalgia; /** * Trim values if they are longer than cell size */ - private final boolean trimValues = false; + private final boolean trimValues; public TabularFormatter(FormatterContext formatterContext) { super(formatterContext); out = new ColorfulPrintWriter(formatterContext.getOutputStream()); + asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false); + trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false); + out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true)); } @Override