support custom relation names in the XML formatter (added --relation CLI option) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 05 Mar 2019 22:03:02 +0100
branchv_0
changeset 246277c18b48762
parent 245 b6ff5b7a8422
child 247 3380ae5275be
support custom relation names in the XML formatter (added --relation CLI option)
java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java
java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java
     1.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java	Tue Mar 05 21:22:33 2019 +0100
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java	Tue Mar 05 22:03:02 2019 +0100
     1.3 @@ -28,6 +28,7 @@
     1.4  import java.util.Collection;
     1.5  import java.util.EnumSet;
     1.6  import java.util.LinkedHashSet;
     1.7 +import java.util.LinkedList;
     1.8  import java.util.List;
     1.9  import java.util.Set;
    1.10  import java.util.regex.Pattern;
    1.11 @@ -45,6 +46,7 @@
    1.12  	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
    1.13  	private String sql;
    1.14  	private String databaseName;
    1.15 +	private final List<String> relationNames = new ArrayList<>();
    1.16  	private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
    1.17  	private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
    1.18  	private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
    1.19 @@ -159,6 +161,14 @@
    1.20  		this.databaseName = databaseName;
    1.21  	}
    1.22  
    1.23 +	public List<String> getRelationNames() {
    1.24 +		return relationNames;
    1.25 +	}
    1.26 +
    1.27 +	public void addRelationName(String name) {
    1.28 +		relationNames.add(name);
    1.29 +	}
    1.30 +
    1.31  	public void setBatch(boolean batch) {
    1.32  		this.batch = batch;
    1.33  	}
     2.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java	Tue Mar 05 21:22:33 2019 +0100
     2.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java	Tue Mar 05 22:03:02 2019 +0100
     2.3 @@ -28,9 +28,8 @@
     2.4  import java.util.Map;
     2.5  
     2.6  /**
     2.7 - * Converts command line arguments from String array to object.
     2.8 - * Checks basic constraints (if only supported options are used and if they have correct number of
     2.9 - * parameters)
    2.10 + * Converts command line arguments from String array to object. Checks basic constraints (if only
    2.11 + * supported options are used and if they have correct number of parameters)
    2.12   *
    2.13   * @author Ing. František Kučera (frantovo.cz)
    2.14   */
    2.15 @@ -111,6 +110,9 @@
    2.16  						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    2.17  					}
    2.18  					break;
    2.19 +				case Tokens.RELATION:
    2.20 +					options.addRelationName(fetchNext(args, ++i));
    2.21 +					break;
    2.22  				case Tokens.FORMATTER:
    2.23  					options.setFormatterName(fetchNext(args, ++i));
    2.24  					break;
    2.25 @@ -187,6 +189,7 @@
    2.26  		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
    2.27  		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
    2.28  		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
    2.29 +		public static final String RELATION = "--relation"; // bash-completion:option // help: name of the output relation (result set)
    2.30  		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
    2.31  		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
    2.32  		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
     3.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java	Tue Mar 05 21:22:33 2019 +0100
     3.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java	Tue Mar 05 22:03:02 2019 +0100
     3.3 @@ -158,7 +158,7 @@
     3.4  
     3.5  		try (DatabaseConnection c = dd.connect(options.getDatabaseProperties(), jmxBean)) {
     3.6  			log.log(Level.FINE, "Database connected");
     3.7 -			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) {
     3.8 +			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties(), options.getRelationNames()))) {
     3.9  				c.executeQuery(options.getSQLCommand(), f);
    3.10  			}
    3.11  		}
    3.12 @@ -180,7 +180,7 @@
    3.13  
    3.14  		try (DatabaseConnection c = dd.connect(options.getDatabaseProperties(), jmxBean)) {
    3.15  			log.log(Level.FINE, "Database connected");
    3.16 -			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) {
    3.17 +			try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties(), options.getRelationNames()))) {
    3.18  				c.executeBatch(b, f);
    3.19  			}
    3.20  		}
     4.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java	Tue Mar 05 21:22:33 2019 +0100
     4.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java	Tue Mar 05 22:03:02 2019 +0100
     4.3 @@ -172,7 +172,7 @@
     4.4  	private boolean isInstantiable(FormatterDefinition fd) {
     4.5  		try {
     4.6  			try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
     4.7 -				fd.getInstance(new FormatterContext(testStream, new Properties(0)));
     4.8 +				fd.getInstance(new FormatterContext(testStream, new Properties(0), null));
     4.9  				return true;
    4.10  			}
    4.11  		} catch (Exception e) {
    4.12 @@ -558,7 +558,7 @@
    4.13  		String formatterName = options.getFormatterName();
    4.14  		formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
    4.15  		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
    4.16 -		FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
    4.17 +		FormatterContext context = new FormatterContext(out, options.getFormatterProperties(), options.getRelationNames());
    4.18  		return fd.getInstance(context);
    4.19  	}
    4.20  
     5.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java	Tue Mar 05 21:22:33 2019 +0100
     5.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java	Tue Mar 05 22:03:02 2019 +0100
     5.3 @@ -19,6 +19,7 @@
     5.4  
     5.5  import info.globalcode.sql.dk.configuration.Properties;
     5.6  import java.io.OutputStream;
     5.7 +import java.util.List;
     5.8  
     5.9  /**
    5.10   * To be passed from the SQL-DK core to the formatter.
    5.11 @@ -29,10 +30,12 @@
    5.12  
    5.13  	private OutputStream outputStream;
    5.14  	private Properties properties;
    5.15 +	private List<String> relationNames;
    5.16  
    5.17 -	public FormatterContext(OutputStream outputStream, Properties properties) {
    5.18 +	public FormatterContext(OutputStream outputStream, Properties properties, List<String> relationNames) {
    5.19  		this.outputStream = outputStream;
    5.20  		this.properties = properties;
    5.21 +		this.relationNames = relationNames;
    5.22  	}
    5.23  
    5.24  	public OutputStream getOutputStream() {
    5.25 @@ -46,4 +49,13 @@
    5.26  	public void setProperties(Properties properties) {
    5.27  		this.properties = properties;
    5.28  	}
    5.29 +
    5.30 +	public List<String> getRelationNames() {
    5.31 +		return relationNames;
    5.32 +	}
    5.33 +
    5.34 +	public void setOutputStream(OutputStream outputStream) {
    5.35 +		this.outputStream = outputStream;
    5.36 +	}
    5.37 +
    5.38  }
     6.1 --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java	Tue Mar 05 21:22:33 2019 +0100
     6.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java	Tue Mar 05 22:03:02 2019 +0100
     6.3 @@ -184,8 +184,11 @@
     6.4  	}
     6.5  
     6.6  	private String getCurrentRelationName() {
     6.7 -		// TODO: support custom names (add CLI option)
     6.8 -		return "r" + resultSetCounter;
     6.9 +		if (getFormatterContext().getRelationNames() == null || getFormatterContext().getRelationNames().size() < resultSetCounter) {
    6.10 +			return "r" + resultSetCounter;
    6.11 +		} else {
    6.12 +			return getFormatterContext().getRelationNames().get(resultSetCounter - 1);
    6.13 +		}
    6.14  	}
    6.15  
    6.16  	@Override