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