java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 00:15:35 +0100
branchv_0
changeset 47 92b83789330a
parent 44 67581ec4396e
child 54 53020d0bd2e4
permissions -rw-r--r--
fix default suffix
     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 java.io.OutputStream;
    24 import java.util.ArrayList;
    25 import java.util.Collection;
    26 import java.util.EnumSet;
    27 import java.util.List;
    28 
    29 /**
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class CLIOptions {
    34 
    35 	public static final String DEFAULT_NAME_PREFIX = ":";
    36 	public static final String DEFAULT_NAME_SUFFIX = "";
    37 	private String sql;
    38 	private String databaseName;
    39 	private String databaseNameToTest;
    40 	private String namePrefix = DEFAULT_NAME_PREFIX;
    41 	private String nameSuffix = DEFAULT_NAME_SUFFIX;
    42 	private String formatterName;
    43 	private boolean batch;
    44 
    45 	public enum MODE {
    46 
    47 		QUERY_NOW,
    48 		PREPARE_BATCH,
    49 		EXECUTE_BATCH,
    50 		JUST_SHOW_INFO
    51 	}
    52 
    53 	public enum INFO_TYPE {
    54 
    55 		HELP,
    56 		VERSION,
    57 		LICENSE,
    58 		FORMATTERS,
    59 		TYPES,
    60 		DATABASES,
    61 		CONNECTION
    62 	}
    63 	private final List<NamedParameter> namedParameters = new ArrayList<>();
    64 	private final List<Parameter> numberedParameters = new ArrayList<>();
    65 	private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
    66 
    67 	public void validate() throws InvalidOptionsException {
    68 		InvalidOptionsException e = new InvalidOptionsException();
    69 
    70 		MODE mode = getMode();
    71 		if (mode == null) {
    72 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    73 		} else if (mode == MODE.JUST_SHOW_INFO) {
    74 			if (!namedParameters.isEmpty()) {
    75 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
    76 			}
    77 			if (!numberedParameters.isEmpty()) {
    78 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
    79 			}
    80 			if (isNotEmpty(sql, false)) {
    81 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
    82 			}
    83 			if (isNotEmpty(databaseName, false)) {
    84 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
    85 			}
    86 			if (batch) {
    87 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
    88 			}
    89 			if (isNotEmpty(formatterName, false)) {
    90 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
    91 			}
    92 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    93 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    94 			}
    95 			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
    96 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
    97 			}
    98 			if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
    99 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
   100 			}
   101 		}
   102 
   103 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
   104 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
   105 		}
   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 String getNamePrefix() {
   176 		return namePrefix;
   177 	}
   178 
   179 	public void setNamePrefix(String namePrefix) {
   180 		this.namePrefix = namePrefix;
   181 	}
   182 
   183 	public String getNameSuffix() {
   184 		return nameSuffix;
   185 	}
   186 
   187 	public void setNameSuffix(String nameSuffix) {
   188 		this.nameSuffix = nameSuffix;
   189 	}
   190 
   191 	public String getFormatterName() {
   192 		return formatterName;
   193 	}
   194 
   195 	public void setFormatterName(String formatterName) {
   196 		this.formatterName = formatterName;
   197 	}
   198 
   199 	public void addShowInfo(INFO_TYPE info) {
   200 		showInfo.add(info);
   201 	}
   202 
   203 	public EnumSet<INFO_TYPE> getShowInfo() {
   204 		return showInfo;
   205 	}
   206 
   207 	public String getDatabaseNameToTest() {
   208 		return databaseNameToTest;
   209 	}
   210 
   211 	public void setDatabaseNameToTest(String databaseNameToTest) {
   212 		this.databaseNameToTest = databaseNameToTest;
   213 	}
   214 
   215 	public SQLCommand getSQLCommand() {
   216 		if (namedParameters.isEmpty()) {
   217 			return new SQLCommandNumbered(sql, numberedParameters);
   218 		} else {
   219 			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
   220 		}
   221 	}
   222 
   223 	public OutputStream getOutputStream() {
   224 		return System.out;
   225 	}
   226 }