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