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