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