java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 30 Apr 2019 20:04:06 +0200
branchv_0
changeset 249 7655df0622ee
parent 246 277c18b48762
child 250 aae5009bd0af
permissions -rw-r--r--
change XMLNS for the configuration
Use The 'tag' URI Scheme <http://www.faqs.org/rfcs/rfc4151.html>
Upgrade local configuration:
sed 's@https://sql-dk.globalcode.info/xmlns/configuration@tag:globalcode.info,2018:sqldk/configuration@g' -i ~/.sql-dk/config.xml
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import static info.globalcode.sql.dk.Functions.isNotEmpty;
    21 import static info.globalcode.sql.dk.Functions.equalz;
    22 import info.globalcode.sql.dk.InfoLister.InfoType;
    23 import info.globalcode.sql.dk.configuration.Properties;
    24 import info.globalcode.sql.dk.configuration.Property;
    25 import java.io.InputStream;
    26 import java.io.OutputStream;
    27 import java.util.ArrayList;
    28 import java.util.Collection;
    29 import java.util.EnumSet;
    30 import java.util.LinkedHashSet;
    31 import java.util.LinkedList;
    32 import java.util.List;
    33 import java.util.Set;
    34 import java.util.regex.Pattern;
    35 import java.util.regex.PatternSyntaxException;
    36 
    37 /**
    38  * Holds options from command line, validates them, combines with configuration and provides derived
    39  * objects.
    40  *
    41  * @author Ing. František Kučera (frantovo.cz)
    42  */
    43 public class CLIOptions {
    44 
    45 	public static final String DEFAULT_NAME_PREFIX = ":";
    46 	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
    47 	private String sql;
    48 	private String databaseName;
    49 	private final List<String> relationNames = new ArrayList<>();
    50 	private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
    51 	private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
    52 	private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
    53 	private String namePrefix = DEFAULT_NAME_PREFIX;
    54 	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    55 	private String formatterName;
    56 	private boolean batch;
    57 	private final Properties formatterProperties = new Properties();
    58 	private final Properties databaseProperties = new Properties();
    59 
    60 	public enum MODE {
    61 
    62 		QUERY_NOW,
    63 		PREPARE_BATCH,
    64 		EXECUTE_BATCH,
    65 		JUST_SHOW_INFO
    66 	}
    67 	private final List<NamedParameter> namedParameters = new ArrayList<>();
    68 	private final List<Parameter> numberedParameters = new ArrayList<>();
    69 	private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
    70 
    71 	public void validate() throws InvalidOptionsException {
    72 		InvalidOptionsException e = new InvalidOptionsException();
    73 
    74 		MODE mode = getMode();
    75 		if (mode == null) {
    76 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    77 		} else if (mode == MODE.JUST_SHOW_INFO) {
    78 			if (!namedParameters.isEmpty()) {
    79 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
    80 			}
    81 			if (!numberedParameters.isEmpty()) {
    82 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
    83 			}
    84 			if (isNotEmpty(sql, false)) {
    85 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
    86 			}
    87 			if (isNotEmpty(databaseName, false)) {
    88 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
    89 			}
    90 			if (batch) {
    91 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
    92 			}
    93 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    94 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    95 			}
    96 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    97 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    98 			}
    99 			if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
   100 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
   101 			}
   102 			if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
   103 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
   104 			}
   105 		}
   106 
   107 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
   108 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
   109 		}
   110 
   111 		try {
   112 			Pattern.compile(namePrefix + "test" + nameSuffix);
   113 		} catch (PatternSyntaxException regexException) {
   114 			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
   115 		}
   116 
   117 		if (e.hasProblems()) {
   118 			throw e;
   119 		}
   120 	}
   121 
   122 	private boolean hasSql() {
   123 		return isNotEmpty(getSql(), true);
   124 	}
   125 
   126 	private boolean hasDb() {
   127 		return isNotEmpty(getDatabaseName(), true);
   128 	}
   129 
   130 	/**
   131 	 * Depends on options: DB, BATCH, SQL
   132 	 *
   133 	 * @return mode | or null if options are not yet initialized or combination of options is
   134 	 * invalid
   135 	 */
   136 	public MODE getMode() {
   137 		if (hasDb() && !batch && hasSql()) {
   138 			return MODE.QUERY_NOW;
   139 		} else if (!hasDb() && batch && hasSql()) {
   140 			return MODE.PREPARE_BATCH;
   141 		} else if (hasDb() && batch && !hasSql()) {
   142 			return MODE.EXECUTE_BATCH;
   143 		} else {
   144 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   145 		}
   146 	}
   147 
   148 	public String getSql() {
   149 		return sql;
   150 	}
   151 
   152 	public void setSql(String sql) {
   153 		this.sql = sql;
   154 	}
   155 
   156 	public String getDatabaseName() {
   157 		return databaseName;
   158 	}
   159 
   160 	public void setDatabaseName(String databaseName) {
   161 		this.databaseName = databaseName;
   162 	}
   163 
   164 	public List<String> getRelationNames() {
   165 		return relationNames;
   166 	}
   167 
   168 	public void addRelationName(String name) {
   169 		relationNames.add(name);
   170 	}
   171 
   172 	public void setBatch(boolean batch) {
   173 		this.batch = batch;
   174 	}
   175 
   176 	public Collection<NamedParameter> getNamedParameters() {
   177 		return namedParameters;
   178 	}
   179 
   180 	public List<Parameter> getNumberedParameters() {
   181 		return numberedParameters;
   182 	}
   183 
   184 	public void addNumberedParameter(Parameter p) {
   185 		numberedParameters.add(p);
   186 	}
   187 
   188 	public void addNamedParameter(NamedParameter p) {
   189 		namedParameters.add(p);
   190 	}
   191 
   192 	public Properties getDatabaseProperties() {
   193 		return databaseProperties;
   194 	}
   195 
   196 	public Properties getFormatterProperties() {
   197 		return formatterProperties;
   198 	}
   199 
   200 	public void addDatabaseProperty(Property p) {
   201 		databaseProperties.add(p);
   202 	}
   203 
   204 	public void addFormatterProperty(Property p) {
   205 		formatterProperties.add(p);
   206 	}
   207 
   208 	/**
   209 	 * @return regular expression describing the name prefix
   210 	 */
   211 	public String getNamePrefix() {
   212 		return namePrefix;
   213 	}
   214 
   215 	/**
   216 	 * @param namePrefix
   217 	 * @see #getNamePrefix()
   218 	 */
   219 	public void setNamePrefix(String namePrefix) {
   220 		this.namePrefix = namePrefix;
   221 	}
   222 
   223 	/**
   224 	 * @return regular expression describing the name prefix
   225 	 */
   226 	public String getNameSuffix() {
   227 		return nameSuffix;
   228 	}
   229 
   230 	/**
   231 	 * @param nameSuffix
   232 	 * @see #getNameSuffix()
   233 	 */
   234 	public void setNameSuffix(String nameSuffix) {
   235 		this.nameSuffix = nameSuffix;
   236 	}
   237 
   238 	public String getFormatterName() {
   239 		return formatterName;
   240 	}
   241 
   242 	public void setFormatterName(String formatterName) {
   243 		this.formatterName = formatterName;
   244 	}
   245 
   246 	public void addShowInfo(InfoType info) {
   247 		showInfo.add(info);
   248 	}
   249 
   250 	public EnumSet<InfoType> getShowInfo() {
   251 		return showInfo;
   252 	}
   253 
   254 	public Set<String> getDatabaseNamesToTest() {
   255 		return databaseNamesToTest;
   256 	}
   257 
   258 	public void addDatabaseNameToTest(String name) {
   259 		databaseNamesToTest.add(name);
   260 	}
   261 
   262 	public Set<String> getDatabaseNamesToListProperties() {
   263 		return databaseNamesToListProperties;
   264 	}
   265 
   266 	public void addDatabaseNameToListProperties(String name) {
   267 		databaseNamesToListProperties.add(name);
   268 	}
   269 
   270 	public Set<String> getFormatterNamesToListProperties() {
   271 		return formatterNamesToListProperties;
   272 	}
   273 
   274 	public void addFormatterNameToListProperties(String name) {
   275 		formatterNamesToListProperties.add(name);
   276 	}
   277 
   278 	public SQLCommand getSQLCommand() {
   279 		if (namedParameters.isEmpty()) {
   280 			return new SQLCommandNumbered(sql, numberedParameters);
   281 		} else {
   282 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   283 		}
   284 	}
   285 
   286 	public OutputStream getOutputStream() {
   287 		return System.out;
   288 	}
   289 
   290 	public InputStream getInputStream() {
   291 		return System.in;
   292 	}
   293 }