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