java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 00:58:00 +0100
branchv_0
changeset 63 3b9ec9c23a37
parent 54 53020d0bd2e4
child 69 0befec5034c2
permissions -rw-r--r--
validation: test if prefix/suffix are valid regular expressions
     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.isEmpty;
    22 import static info.globalcode.sql.dk.Functions.equalz;
    23 import java.io.OutputStream;
    24 import java.util.ArrayList;
    25 import java.util.Collection;
    26 import java.util.EnumSet;
    27 import java.util.List;
    28 import java.util.regex.Pattern;
    29 import java.util.regex.PatternSyntaxException;
    30 
    31 /**
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class CLIOptions {
    36 
    37 	public static final String DEFAULT_NAME_PREFIX = ":";
    38 	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
    39 	private String sql;
    40 	private String databaseName;
    41 	private String databaseNameToTest;
    42 	private String namePrefix = DEFAULT_NAME_PREFIX;
    43 	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    44 	private String formatterName;
    45 	private boolean batch;
    46 
    47 	public enum MODE {
    48 
    49 		QUERY_NOW,
    50 		PREPARE_BATCH,
    51 		EXECUTE_BATCH,
    52 		JUST_SHOW_INFO
    53 	}
    54 
    55 	public enum INFO_TYPE {
    56 
    57 		HELP,
    58 		VERSION,
    59 		LICENSE,
    60 		FORMATTERS,
    61 		TYPES,
    62 		DATABASES,
    63 		CONNECTION
    64 	}
    65 	private final List<NamedParameter> namedParameters = new ArrayList<>();
    66 	private final List<Parameter> numberedParameters = new ArrayList<>();
    67 	private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.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 (isNotEmpty(formatterName, false)) {
    92 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
    93 			}
    94 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    95 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    96 			}
    97 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    98 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    99 			}
   100 			if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
   101 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
   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 	/**
   183 	 * @return regular expression describing the name prefix
   184 	 */
   185 	public String getNamePrefix() {
   186 		return namePrefix;
   187 	}
   188 
   189 	/**
   190 	 * @see #getNamePrefix()
   191 	 */
   192 	public void setNamePrefix(String namePrefix) {
   193 		this.namePrefix = namePrefix;
   194 	}
   195 
   196 	/**
   197 	 * @return regular expression describing the name prefix
   198 	 */
   199 	public String getNameSuffix() {
   200 		return nameSuffix;
   201 	}
   202 
   203 	/**
   204 	 * @see #getNameSuffix()
   205 	 */
   206 	public void setNameSuffix(String nameSuffix) {
   207 		this.nameSuffix = nameSuffix;
   208 	}
   209 
   210 	public String getFormatterName() {
   211 		return formatterName;
   212 	}
   213 
   214 	public void setFormatterName(String formatterName) {
   215 		this.formatterName = formatterName;
   216 	}
   217 
   218 	public void addShowInfo(INFO_TYPE info) {
   219 		showInfo.add(info);
   220 	}
   221 
   222 	public EnumSet<INFO_TYPE> getShowInfo() {
   223 		return showInfo;
   224 	}
   225 
   226 	public String getDatabaseNameToTest() {
   227 		return databaseNameToTest;
   228 	}
   229 
   230 	public void setDatabaseNameToTest(String databaseNameToTest) {
   231 		this.databaseNameToTest = databaseNameToTest;
   232 	}
   233 
   234 	public SQLCommand getSQLCommand() {
   235 		if (namedParameters.isEmpty()) {
   236 			return new SQLCommandNumbered(sql, numberedParameters);
   237 		} else {
   238 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   239 		}
   240 	}
   241 
   242 	public OutputStream getOutputStream() {
   243 		return System.out;
   244 	}
   245 }