java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 18:19:38 +0100
branchv_0
changeset 29 d66858b4b563
parent 16 5b8fcd35d4d6
child 34 9335cf31c0f2
permissions -rw-r--r--
more configuration, more JAXB, more formatters
     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.SQLCommand.COMMAND_TYPE;
    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 	private String sql;
    37 	private String databaseName;
    38 	private String databaseNameToTest;
    39 	private String namePrefix = DEFAULT_NAME_PREFIX;
    40 	private String formatterName;
    41 	private boolean batch;
    42 
    43 	public enum MODE {
    44 
    45 		QUERY_NOW,
    46 		PREPARE_BATCH,
    47 		EXECUTE_BATCH,
    48 		JUST_SHOW_INFO
    49 	}
    50 
    51 	public enum INFO_TYPE {
    52 
    53 		HELP,
    54 		VERSION,
    55 		LICENSE,
    56 		FORMATTERS,
    57 		TYPES,
    58 		DATABASES,
    59 		CONNECTION
    60 	}
    61 	private COMMAND_TYPE commandType;
    62 	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
    63 	private final List<Parameter> numberedParameters = new ArrayList<>();
    64 	private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
    65 
    66 	public void validate() throws InvalidOptionsException {
    67 		InvalidOptionsException e = new InvalidOptionsException();
    68 
    69 		MODE mode = getMode();
    70 		if (mode == null) {
    71 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    72 		} else if (mode == MODE.JUST_SHOW_INFO) {
    73 			if (!namedParameters.isEmpty()) {
    74 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
    75 			}
    76 			if (!numberedParameters.isEmpty()) {
    77 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
    78 			}
    79 			if (isNotEmpty(sql, false)) {
    80 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
    81 			}
    82 			if (isNotEmpty(databaseName, false)) {
    83 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
    84 			}
    85 			if (batch) {
    86 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
    87 			}
    88 			if (isNotEmpty(formatterName, false)) {
    89 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
    90 			}
    91 			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
    92 				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
    93 			}
    94 			if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
    95 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
    96 			}
    97 		}
    98 
    99 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
   100 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
   101 		}
   102 
   103 
   104 		if (e.hasProblems()) {
   105 			throw e;
   106 		}
   107 	}
   108 
   109 	private boolean hasSql() {
   110 		return isNotEmpty(getSql(), true);
   111 	}
   112 
   113 	private boolean hasDb() {
   114 		return isNotEmpty(getDatabaseName(), true);
   115 	}
   116 
   117 	/**
   118 	 * Depends on options: DB, BATCH, SQL
   119 	 *
   120 	 * @return mode | or null if options are not yet initialized or combination of options is
   121 	 * invalid
   122 	 */
   123 	public MODE getMode() {
   124 		if (hasDb() && !batch && hasSql()) {
   125 			return MODE.QUERY_NOW;
   126 		} else if (!hasDb() && batch && hasSql()) {
   127 			return MODE.PREPARE_BATCH;
   128 		} else if (hasDb() && batch && !hasSql()) {
   129 			return MODE.EXECUTE_BATCH;
   130 		} else {
   131 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   132 		}
   133 	}
   134 
   135 	public String getSql() {
   136 		return sql;
   137 	}
   138 
   139 	public void setSql(String sql) {
   140 		this.sql = sql;
   141 	}
   142 
   143 	public String getDatabaseName() {
   144 		return databaseName;
   145 	}
   146 
   147 	public void setDatabaseName(String databaseName) {
   148 		this.databaseName = databaseName;
   149 	}
   150 
   151 	public void setBatch(boolean batch) {
   152 		this.batch = batch;
   153 	}
   154 
   155 	public COMMAND_TYPE getCommandType() {
   156 		return commandType;
   157 	}
   158 
   159 	public void setCommandType(COMMAND_TYPE commandType) {
   160 		this.commandType = commandType;
   161 	}
   162 
   163 	public Collection<NamedParameter> getNamedParameters() {
   164 		return namedParameters;
   165 	}
   166 
   167 	public List<Parameter> getNumberedParameters() {
   168 		return numberedParameters;
   169 	}
   170 
   171 	public void addNumberedParameter(Parameter p) {
   172 		numberedParameters.add(p);
   173 	}
   174 
   175 	public void addNamedParameter(NamedParameter p) {
   176 		namedParameters.add(p);
   177 	}
   178 
   179 	public String getNamePrefix() {
   180 		return namePrefix;
   181 	}
   182 
   183 	public void setNamePrefix(String namePrefix) {
   184 		this.namePrefix = namePrefix;
   185 	}
   186 
   187 	public String getFormatterName() {
   188 		return formatterName;
   189 	}
   190 
   191 	public void setFormatterName(String formatterName) {
   192 		this.formatterName = formatterName;
   193 	}
   194 
   195 	public void addShowInfo(INFO_TYPE info) {
   196 		showInfo.add(info);
   197 	}
   198 
   199 	public EnumSet<INFO_TYPE> getShowInfo() {
   200 		return showInfo;
   201 	}
   202 
   203 	public String getDatabaseNameToTest() {
   204 		return databaseNameToTest;
   205 	}
   206 
   207 	public void setDatabaseNameToTest(String databaseNameToTest) {
   208 		this.databaseNameToTest = databaseNameToTest;
   209 	}
   210 }