java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 21:18:54 +0100
branchv_0
changeset 69 0befec5034c2
parent 63 3b9ec9c23a37
child 71 e5d04a68ce1e
permissions -rw-r--r--
InfoLister, InfoType: switch → enum
     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 info.globalcode.sql.dk.InfoLister.InfoType;
    24 import java.io.OutputStream;
    25 import java.util.ArrayList;
    26 import java.util.Collection;
    27 import java.util.EnumSet;
    28 import java.util.List;
    29 import java.util.regex.Pattern;
    30 import java.util.regex.PatternSyntaxException;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class CLIOptions {
    37 
    38 	public static final String DEFAULT_NAME_PREFIX = ":";
    39 	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
    40 	private String sql;
    41 	private String databaseName;
    42 	private String databaseNameToTest;
    43 	private String namePrefix = DEFAULT_NAME_PREFIX;
    44 	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    45 	private String formatterName;
    46 	private boolean batch;
    47 
    48 	public enum MODE {
    49 
    50 		QUERY_NOW,
    51 		PREPARE_BATCH,
    52 		EXECUTE_BATCH,
    53 		JUST_SHOW_INFO
    54 	}
    55 	private final List<NamedParameter> namedParameters = new ArrayList<>();
    56 	private final List<Parameter> numberedParameters = new ArrayList<>();
    57 	private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
    58 
    59 	public void validate() throws InvalidOptionsException {
    60 		InvalidOptionsException e = new InvalidOptionsException();
    61 
    62 		MODE mode = getMode();
    63 		if (mode == null) {
    64 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    65 		} else if (mode == MODE.JUST_SHOW_INFO) {
    66 			if (!namedParameters.isEmpty()) {
    67 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
    68 			}
    69 			if (!numberedParameters.isEmpty()) {
    70 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
    71 			}
    72 			if (isNotEmpty(sql, false)) {
    73 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
    74 			}
    75 			if (isNotEmpty(databaseName, false)) {
    76 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
    77 			}
    78 			if (batch) {
    79 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
    80 			}
    81 			if (isNotEmpty(formatterName, false)) {
    82 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
    83 			}
    84 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    85 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    86 			}
    87 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    88 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    89 			}
    90 			if (showInfo.contains(InfoType.CONNECTION) && isEmpty(databaseNameToTest, false)) {
    91 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
    92 			}
    93 		}
    94 
    95 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    96 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    97 		}
    98 
    99 		try {
   100 			Pattern.compile(namePrefix + "test" + nameSuffix);
   101 		} catch (PatternSyntaxException regexException) {
   102 			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
   103 		}
   104 
   105 		if (e.hasProblems()) {
   106 			throw e;
   107 		}
   108 	}
   109 
   110 	private boolean hasSql() {
   111 		return isNotEmpty(getSql(), true);
   112 	}
   113 
   114 	private boolean hasDb() {
   115 		return isNotEmpty(getDatabaseName(), true);
   116 	}
   117 
   118 	/**
   119 	 * Depends on options: DB, BATCH, SQL
   120 	 *
   121 	 * @return mode | or null if options are not yet initialized or combination of options is
   122 	 * invalid
   123 	 */
   124 	public MODE getMode() {
   125 		if (hasDb() && !batch && hasSql()) {
   126 			return MODE.QUERY_NOW;
   127 		} else if (!hasDb() && batch && hasSql()) {
   128 			return MODE.PREPARE_BATCH;
   129 		} else if (hasDb() && batch && !hasSql()) {
   130 			return MODE.EXECUTE_BATCH;
   131 		} else {
   132 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   133 		}
   134 	}
   135 
   136 	public String getSql() {
   137 		return sql;
   138 	}
   139 
   140 	public void setSql(String sql) {
   141 		this.sql = sql;
   142 	}
   143 
   144 	public String getDatabaseName() {
   145 		return databaseName;
   146 	}
   147 
   148 	public void setDatabaseName(String databaseName) {
   149 		this.databaseName = databaseName;
   150 	}
   151 
   152 	public void setBatch(boolean batch) {
   153 		this.batch = batch;
   154 	}
   155 
   156 	public Collection<NamedParameter> getNamedParameters() {
   157 		return namedParameters;
   158 	}
   159 
   160 	public List<Parameter> getNumberedParameters() {
   161 		return numberedParameters;
   162 	}
   163 
   164 	public void addNumberedParameter(Parameter p) {
   165 		numberedParameters.add(p);
   166 	}
   167 
   168 	public void addNamedParameter(NamedParameter p) {
   169 		namedParameters.add(p);
   170 	}
   171 
   172 	/**
   173 	 * @return regular expression describing the name prefix
   174 	 */
   175 	public String getNamePrefix() {
   176 		return namePrefix;
   177 	}
   178 
   179 	/**
   180 	 * @see #getNamePrefix()
   181 	 */
   182 	public void setNamePrefix(String namePrefix) {
   183 		this.namePrefix = namePrefix;
   184 	}
   185 
   186 	/**
   187 	 * @return regular expression describing the name prefix
   188 	 */
   189 	public String getNameSuffix() {
   190 		return nameSuffix;
   191 	}
   192 
   193 	/**
   194 	 * @see #getNameSuffix()
   195 	 */
   196 	public void setNameSuffix(String nameSuffix) {
   197 		this.nameSuffix = nameSuffix;
   198 	}
   199 
   200 	public String getFormatterName() {
   201 		return formatterName;
   202 	}
   203 
   204 	public void setFormatterName(String formatterName) {
   205 		this.formatterName = formatterName;
   206 	}
   207 
   208 	public void addShowInfo(InfoType info) {
   209 		showInfo.add(info);
   210 	}
   211 
   212 	public EnumSet<InfoType> getShowInfo() {
   213 		return showInfo;
   214 	}
   215 
   216 	public String getDatabaseNameToTest() {
   217 		return databaseNameToTest;
   218 	}
   219 
   220 	public void setDatabaseNameToTest(String databaseNameToTest) {
   221 		this.databaseNameToTest = databaseNameToTest;
   222 	}
   223 
   224 	public SQLCommand getSQLCommand() {
   225 		if (namedParameters.isEmpty()) {
   226 			return new SQLCommandNumbered(sql, numberedParameters);
   227 		} else {
   228 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   229 		}
   230 	}
   231 
   232 	public OutputStream getOutputStream() {
   233 		return System.out;
   234 	}
   235 }