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