java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 209 java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java@8dfe037b3274
child 246 277c18b48762
permissions -rw-r--r--
mavenized: sql-dk
     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.List;
    32 import java.util.Set;
    33 import java.util.regex.Pattern;
    34 import java.util.regex.PatternSyntaxException;
    35 
    36 /**
    37  * Holds options from command line, validates them, combines with configuration and provides derived
    38  * objects.
    39  *
    40  * @author Ing. František Kučera (frantovo.cz)
    41  */
    42 public class CLIOptions {
    43 
    44 	public static final String DEFAULT_NAME_PREFIX = ":";
    45 	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
    46 	private String sql;
    47 	private String databaseName;
    48 	private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
    49 	private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
    50 	private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
    51 	private String namePrefix = DEFAULT_NAME_PREFIX;
    52 	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    53 	private String formatterName;
    54 	private boolean batch;
    55 	private final Properties formatterProperties = new Properties();
    56 	private final Properties databaseProperties = new Properties();
    57 
    58 	public enum MODE {
    59 
    60 		QUERY_NOW,
    61 		PREPARE_BATCH,
    62 		EXECUTE_BATCH,
    63 		JUST_SHOW_INFO
    64 	}
    65 	private final List<NamedParameter> namedParameters = new ArrayList<>();
    66 	private final List<Parameter> numberedParameters = new ArrayList<>();
    67 	private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
    68 
    69 	public void validate() throws InvalidOptionsException {
    70 		InvalidOptionsException e = new InvalidOptionsException();
    71 
    72 		MODE mode = getMode();
    73 		if (mode == null) {
    74 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    75 		} else if (mode == MODE.JUST_SHOW_INFO) {
    76 			if (!namedParameters.isEmpty()) {
    77 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
    78 			}
    79 			if (!numberedParameters.isEmpty()) {
    80 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
    81 			}
    82 			if (isNotEmpty(sql, false)) {
    83 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
    84 			}
    85 			if (isNotEmpty(databaseName, false)) {
    86 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
    87 			}
    88 			if (batch) {
    89 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
    90 			}
    91 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    92 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    93 			}
    94 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    95 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    96 			}
    97 			if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
    98 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
    99 			}
   100 			if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
   101 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
   102 			}
   103 		}
   104 
   105 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
   106 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
   107 		}
   108 
   109 		try {
   110 			Pattern.compile(namePrefix + "test" + nameSuffix);
   111 		} catch (PatternSyntaxException regexException) {
   112 			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
   113 		}
   114 
   115 		if (e.hasProblems()) {
   116 			throw e;
   117 		}
   118 	}
   119 
   120 	private boolean hasSql() {
   121 		return isNotEmpty(getSql(), true);
   122 	}
   123 
   124 	private boolean hasDb() {
   125 		return isNotEmpty(getDatabaseName(), true);
   126 	}
   127 
   128 	/**
   129 	 * Depends on options: DB, BATCH, SQL
   130 	 *
   131 	 * @return mode | or null if options are not yet initialized or combination of options is
   132 	 * invalid
   133 	 */
   134 	public MODE getMode() {
   135 		if (hasDb() && !batch && hasSql()) {
   136 			return MODE.QUERY_NOW;
   137 		} else if (!hasDb() && batch && hasSql()) {
   138 			return MODE.PREPARE_BATCH;
   139 		} else if (hasDb() && batch && !hasSql()) {
   140 			return MODE.EXECUTE_BATCH;
   141 		} else {
   142 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   143 		}
   144 	}
   145 
   146 	public String getSql() {
   147 		return sql;
   148 	}
   149 
   150 	public void setSql(String sql) {
   151 		this.sql = sql;
   152 	}
   153 
   154 	public String getDatabaseName() {
   155 		return databaseName;
   156 	}
   157 
   158 	public void setDatabaseName(String databaseName) {
   159 		this.databaseName = databaseName;
   160 	}
   161 
   162 	public void setBatch(boolean batch) {
   163 		this.batch = batch;
   164 	}
   165 
   166 	public Collection<NamedParameter> getNamedParameters() {
   167 		return namedParameters;
   168 	}
   169 
   170 	public List<Parameter> getNumberedParameters() {
   171 		return numberedParameters;
   172 	}
   173 
   174 	public void addNumberedParameter(Parameter p) {
   175 		numberedParameters.add(p);
   176 	}
   177 
   178 	public void addNamedParameter(NamedParameter p) {
   179 		namedParameters.add(p);
   180 	}
   181 
   182 	public Properties getDatabaseProperties() {
   183 		return databaseProperties;
   184 	}
   185 
   186 	public Properties getFormatterProperties() {
   187 		return formatterProperties;
   188 	}
   189 
   190 	public void addDatabaseProperty(Property p) {
   191 		databaseProperties.add(p);
   192 	}
   193 
   194 	public void addFormatterProperty(Property p) {
   195 		formatterProperties.add(p);
   196 	}
   197 
   198 	/**
   199 	 * @return regular expression describing the name prefix
   200 	 */
   201 	public String getNamePrefix() {
   202 		return namePrefix;
   203 	}
   204 
   205 	/**
   206 	 * @param namePrefix
   207 	 * @see #getNamePrefix()
   208 	 */
   209 	public void setNamePrefix(String namePrefix) {
   210 		this.namePrefix = namePrefix;
   211 	}
   212 
   213 	/**
   214 	 * @return regular expression describing the name prefix
   215 	 */
   216 	public String getNameSuffix() {
   217 		return nameSuffix;
   218 	}
   219 
   220 	/**
   221 	 * @param nameSuffix
   222 	 * @see #getNameSuffix()
   223 	 */
   224 	public void setNameSuffix(String nameSuffix) {
   225 		this.nameSuffix = nameSuffix;
   226 	}
   227 
   228 	public String getFormatterName() {
   229 		return formatterName;
   230 	}
   231 
   232 	public void setFormatterName(String formatterName) {
   233 		this.formatterName = formatterName;
   234 	}
   235 
   236 	public void addShowInfo(InfoType info) {
   237 		showInfo.add(info);
   238 	}
   239 
   240 	public EnumSet<InfoType> getShowInfo() {
   241 		return showInfo;
   242 	}
   243 
   244 	public Set<String> getDatabaseNamesToTest() {
   245 		return databaseNamesToTest;
   246 	}
   247 
   248 	public void addDatabaseNameToTest(String name) {
   249 		databaseNamesToTest.add(name);
   250 	}
   251 
   252 	public Set<String> getDatabaseNamesToListProperties() {
   253 		return databaseNamesToListProperties;
   254 	}
   255 
   256 	public void addDatabaseNameToListProperties(String name) {
   257 		databaseNamesToListProperties.add(name);
   258 	}
   259 
   260 	public Set<String> getFormatterNamesToListProperties() {
   261 		return formatterNamesToListProperties;
   262 	}
   263 
   264 	public void addFormatterNameToListProperties(String name) {
   265 		formatterNamesToListProperties.add(name);
   266 	}
   267 
   268 	public SQLCommand getSQLCommand() {
   269 		if (namedParameters.isEmpty()) {
   270 			return new SQLCommandNumbered(sql, numberedParameters);
   271 		} else {
   272 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   273 		}
   274 	}
   275 
   276 	public OutputStream getOutputStream() {
   277 		return System.out;
   278 	}
   279 
   280 	public InputStream getInputStream() {
   281 		return System.in;
   282 	}
   283 }