java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 21:48:59 +0100
branchv_0
changeset 71 e5d04a68ce1e
parent 69 0befec5034c2
child 74 a8444f6a54f3
permissions -rw-r--r--
allow custom formatters also for JUST_SHOW_INFO commands
     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 (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    82 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    83 			}
    84 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    85 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    86 			}
    87 			if (showInfo.contains(InfoType.CONNECTION) && isEmpty(databaseNameToTest, false)) {
    88 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
    89 			}
    90 		}
    91 
    92 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    93 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    94 		}
    95 
    96 		try {
    97 			Pattern.compile(namePrefix + "test" + nameSuffix);
    98 		} catch (PatternSyntaxException regexException) {
    99 			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
   100 		}
   101 
   102 		if (e.hasProblems()) {
   103 			throw e;
   104 		}
   105 	}
   106 
   107 	private boolean hasSql() {
   108 		return isNotEmpty(getSql(), true);
   109 	}
   110 
   111 	private boolean hasDb() {
   112 		return isNotEmpty(getDatabaseName(), true);
   113 	}
   114 
   115 	/**
   116 	 * Depends on options: DB, BATCH, SQL
   117 	 *
   118 	 * @return mode | or null if options are not yet initialized or combination of options is
   119 	 * invalid
   120 	 */
   121 	public MODE getMode() {
   122 		if (hasDb() && !batch && hasSql()) {
   123 			return MODE.QUERY_NOW;
   124 		} else if (!hasDb() && batch && hasSql()) {
   125 			return MODE.PREPARE_BATCH;
   126 		} else if (hasDb() && batch && !hasSql()) {
   127 			return MODE.EXECUTE_BATCH;
   128 		} else {
   129 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   130 		}
   131 	}
   132 
   133 	public String getSql() {
   134 		return sql;
   135 	}
   136 
   137 	public void setSql(String sql) {
   138 		this.sql = sql;
   139 	}
   140 
   141 	public String getDatabaseName() {
   142 		return databaseName;
   143 	}
   144 
   145 	public void setDatabaseName(String databaseName) {
   146 		this.databaseName = databaseName;
   147 	}
   148 
   149 	public void setBatch(boolean batch) {
   150 		this.batch = batch;
   151 	}
   152 
   153 	public Collection<NamedParameter> getNamedParameters() {
   154 		return namedParameters;
   155 	}
   156 
   157 	public List<Parameter> getNumberedParameters() {
   158 		return numberedParameters;
   159 	}
   160 
   161 	public void addNumberedParameter(Parameter p) {
   162 		numberedParameters.add(p);
   163 	}
   164 
   165 	public void addNamedParameter(NamedParameter p) {
   166 		namedParameters.add(p);
   167 	}
   168 
   169 	/**
   170 	 * @return regular expression describing the name prefix
   171 	 */
   172 	public String getNamePrefix() {
   173 		return namePrefix;
   174 	}
   175 
   176 	/**
   177 	 * @see #getNamePrefix()
   178 	 */
   179 	public void setNamePrefix(String namePrefix) {
   180 		this.namePrefix = namePrefix;
   181 	}
   182 
   183 	/**
   184 	 * @return regular expression describing the name prefix
   185 	 */
   186 	public String getNameSuffix() {
   187 		return nameSuffix;
   188 	}
   189 
   190 	/**
   191 	 * @see #getNameSuffix()
   192 	 */
   193 	public void setNameSuffix(String nameSuffix) {
   194 		this.nameSuffix = nameSuffix;
   195 	}
   196 
   197 	public String getFormatterName() {
   198 		return formatterName;
   199 	}
   200 
   201 	public void setFormatterName(String formatterName) {
   202 		this.formatterName = formatterName;
   203 	}
   204 
   205 	public void addShowInfo(InfoType info) {
   206 		showInfo.add(info);
   207 	}
   208 
   209 	public EnumSet<InfoType> getShowInfo() {
   210 		return showInfo;
   211 	}
   212 
   213 	public String getDatabaseNameToTest() {
   214 		return databaseNameToTest;
   215 	}
   216 
   217 	public void setDatabaseNameToTest(String databaseNameToTest) {
   218 		this.databaseNameToTest = databaseNameToTest;
   219 	}
   220 
   221 	public SQLCommand getSQLCommand() {
   222 		if (namedParameters.isEmpty()) {
   223 			return new SQLCommandNumbered(sql, numberedParameters);
   224 		} else {
   225 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   226 		}
   227 	}
   228 
   229 	public OutputStream getOutputStream() {
   230 		return System.out;
   231 	}
   232 }