java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:31:55 +0100
branchv_0
changeset 34 9335cf31c0f2
parent 29 d66858b4b563
child 37 9e6f8e5d5f98
permissions -rw-r--r--
first working version
     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.io.OutputStream;
    25 import java.util.ArrayList;
    26 import java.util.Collection;
    27 import java.util.EnumSet;
    28 import java.util.List;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class CLIOptions {
    35 
    36 	public static final String DEFAULT_NAME_PREFIX = ":";
    37 	private String sql;
    38 	private String databaseName;
    39 	private String databaseNameToTest;
    40 	private String namePrefix = DEFAULT_NAME_PREFIX;
    41 	private String formatterName;
    42 	private boolean batch;
    43 
    44 	public enum MODE {
    45 
    46 		QUERY_NOW,
    47 		PREPARE_BATCH,
    48 		EXECUTE_BATCH,
    49 		JUST_SHOW_INFO
    50 	}
    51 
    52 	public enum INFO_TYPE {
    53 
    54 		HELP,
    55 		VERSION,
    56 		LICENSE,
    57 		FORMATTERS,
    58 		TYPES,
    59 		DATABASES,
    60 		CONNECTION
    61 	}
    62 	private COMMAND_TYPE commandType;
    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 (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
    96 				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
    97 			}
    98 		}
    99 
   100 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
   101 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
   102 		}
   103 
   104 
   105 		if (e.hasProblems()) {
   106 			throw e;
   107 		}
   108 	}
   109 
   110 	private boolean hasSql() {
   111 		return isNotEmpty(getSql(), true);
   112 	}
   113 
   114 	private boolean hasDb() {
   115 		return isNotEmpty(getDatabaseName(), true);
   116 	}
   117 
   118 	/**
   119 	 * Depends on options: DB, BATCH, SQL
   120 	 *
   121 	 * @return mode | or null if options are not yet initialized or combination of options is
   122 	 * invalid
   123 	 */
   124 	public MODE getMode() {
   125 		if (hasDb() && !batch && hasSql()) {
   126 			return MODE.QUERY_NOW;
   127 		} else if (!hasDb() && batch && hasSql()) {
   128 			return MODE.PREPARE_BATCH;
   129 		} else if (hasDb() && batch && !hasSql()) {
   130 			return MODE.EXECUTE_BATCH;
   131 		} else {
   132 			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
   133 		}
   134 	}
   135 
   136 	public String getSql() {
   137 		return sql;
   138 	}
   139 
   140 	public void setSql(String sql) {
   141 		this.sql = sql;
   142 	}
   143 
   144 	public String getDatabaseName() {
   145 		return databaseName;
   146 	}
   147 
   148 	public void setDatabaseName(String databaseName) {
   149 		this.databaseName = databaseName;
   150 	}
   151 
   152 	public void setBatch(boolean batch) {
   153 		this.batch = batch;
   154 	}
   155 
   156 	public COMMAND_TYPE getCommandType() {
   157 		return commandType;
   158 	}
   159 
   160 	public void setCommandType(COMMAND_TYPE commandType) {
   161 		this.commandType = commandType;
   162 	}
   163 
   164 	public Collection<NamedParameter> getNamedParameters() {
   165 		return namedParameters;
   166 	}
   167 
   168 	public List<Parameter> getNumberedParameters() {
   169 		return numberedParameters;
   170 	}
   171 
   172 	public void addNumberedParameter(Parameter p) {
   173 		numberedParameters.add(p);
   174 	}
   175 
   176 	public void addNamedParameter(NamedParameter p) {
   177 		namedParameters.add(p);
   178 	}
   179 
   180 	public String getNamePrefix() {
   181 		return namePrefix;
   182 	}
   183 
   184 	public void setNamePrefix(String namePrefix) {
   185 		this.namePrefix = namePrefix;
   186 	}
   187 
   188 	public String getFormatterName() {
   189 		return formatterName;
   190 	}
   191 
   192 	public void setFormatterName(String formatterName) {
   193 		this.formatterName = formatterName;
   194 	}
   195 
   196 	public void addShowInfo(INFO_TYPE info) {
   197 		showInfo.add(info);
   198 	}
   199 
   200 	public EnumSet<INFO_TYPE> getShowInfo() {
   201 		return showInfo;
   202 	}
   203 
   204 	public String getDatabaseNameToTest() {
   205 		return databaseNameToTest;
   206 	}
   207 
   208 	public void setDatabaseNameToTest(String databaseNameToTest) {
   209 		this.databaseNameToTest = databaseNameToTest;
   210 	}
   211 
   212 	public SQLCommand getSQLCommand() {
   213 		if (namedParameters.isEmpty()) {
   214 			return new SQLCommandNumbered(commandType, sql, numberedParameters);
   215 		} else {
   216 			return new SQLCommandNamed(commandType, sql, namedParameters);
   217 		}
   218 	}
   219 
   220 	public OutputStream getOutputStream() {
   221 		return System.out;
   222 	}
   223 }