# HG changeset patch # User František Kučera # Date 1551819782 -3600 # Node ID 277c18b48762cda839e04bc207247fa648bacc85 # Parent b6ff5b7a842255c17ec33d238fc9e4fcc3daf8df support custom relation names in the XML formatter (added --relation CLI option) diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java Tue Mar 05 22:03:02 2019 +0100 @@ -28,6 +28,7 @@ import java.util.Collection; import java.util.EnumSet; import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.regex.Pattern; @@ -45,6 +46,7 @@ public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))"; private String sql; private String databaseName; + private final List relationNames = new ArrayList<>(); private final Set databaseNamesToTest = new LinkedHashSet<>(); private final Set databaseNamesToListProperties = new LinkedHashSet<>(); private final Set formatterNamesToListProperties = new LinkedHashSet<>(); @@ -159,6 +161,14 @@ this.databaseName = databaseName; } + public List getRelationNames() { + return relationNames; + } + + public void addRelationName(String name) { + relationNames.add(name); + } + public void setBatch(boolean batch) { this.batch = batch; } diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java Tue Mar 05 22:03:02 2019 +0100 @@ -28,9 +28,8 @@ import java.util.Map; /** - * Converts command line arguments from String array to object. - * Checks basic constraints (if only supported options are used and if they have correct number of - * parameters) + * Converts command line arguments from String array to object. Checks basic constraints (if only + * supported options are used and if they have correct number of parameters) * * @author Ing. František Kučera (frantovo.cz) */ @@ -111,6 +110,9 @@ options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); } break; + case Tokens.RELATION: + options.addRelationName(fetchNext(args, ++i)); + break; case Tokens.FORMATTER: options.setFormatterName(fetchNext(args, ++i)); break; @@ -187,6 +189,7 @@ public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types + public static final String RELATION = "--relation"; // bash-completion:option // help: name of the output relation (result set) public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIStarter.java Tue Mar 05 22:03:02 2019 +0100 @@ -158,7 +158,7 @@ try (DatabaseConnection c = dd.connect(options.getDatabaseProperties(), jmxBean)) { log.log(Level.FINE, "Database connected"); - try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) { + try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties(), options.getRelationNames()))) { c.executeQuery(options.getSQLCommand(), f); } } @@ -180,7 +180,7 @@ try (DatabaseConnection c = dd.connect(options.getDatabaseProperties(), jmxBean)) { log.log(Level.FINE, "Database connected"); - try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) { + try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties(), options.getRelationNames()))) { c.executeBatch(b, f); } } diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/InfoLister.java Tue Mar 05 22:03:02 2019 +0100 @@ -172,7 +172,7 @@ private boolean isInstantiable(FormatterDefinition fd) { try { try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) { - fd.getInstance(new FormatterContext(testStream, new Properties(0))); + fd.getInstance(new FormatterContext(testStream, new Properties(0), null)); return true; } } catch (Exception e) { @@ -558,7 +558,7 @@ String formatterName = options.getFormatterName(); formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName; FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName); - FormatterContext context = new FormatterContext(out, options.getFormatterProperties()); + FormatterContext context = new FormatterContext(out, options.getFormatterProperties(), options.getRelationNames()); return fd.getInstance(context); } diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/FormatterContext.java Tue Mar 05 22:03:02 2019 +0100 @@ -19,6 +19,7 @@ import info.globalcode.sql.dk.configuration.Properties; import java.io.OutputStream; +import java.util.List; /** * To be passed from the SQL-DK core to the formatter. @@ -29,10 +30,12 @@ private OutputStream outputStream; private Properties properties; + private List relationNames; - public FormatterContext(OutputStream outputStream, Properties properties) { + public FormatterContext(OutputStream outputStream, Properties properties, List relationNames) { this.outputStream = outputStream; this.properties = properties; + this.relationNames = relationNames; } public OutputStream getOutputStream() { @@ -46,4 +49,13 @@ public void setProperties(Properties properties) { this.properties = properties; } + + public List getRelationNames() { + return relationNames; + } + + public void setOutputStream(OutputStream outputStream) { + this.outputStream = outputStream; + } + } diff -r b6ff5b7a8422 -r 277c18b48762 java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java --- a/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java Tue Mar 05 21:22:33 2019 +0100 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java Tue Mar 05 22:03:02 2019 +0100 @@ -184,8 +184,11 @@ } private String getCurrentRelationName() { - // TODO: support custom names (add CLI option) - return "r" + resultSetCounter; + if (getFormatterContext().getRelationNames() == null || getFormatterContext().getRelationNames().size() < resultSetCounter) { + return "r" + resultSetCounter; + } else { + return getFormatterContext().getRelationNames().get(resultSetCounter - 1); + } } @Override