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