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