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
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@1
    18
package info.globalcode.sql.dk;
franta-hg@1
    19
franta-hg@1
    20
import static info.globalcode.sql.dk.Functions.isNotEmpty;
franta-hg@15
    21
import static info.globalcode.sql.dk.Functions.isEmpty;
franta-hg@14
    22
import static info.globalcode.sql.dk.Functions.equalz;
franta-hg@29
    23
import info.globalcode.sql.dk.SQLCommand.COMMAND_TYPE;
franta-hg@1
    24
import java.util.ArrayList;
franta-hg@1
    25
import java.util.Collection;
franta-hg@14
    26
import java.util.EnumSet;
franta-hg@1
    27
import java.util.List;
franta-hg@1
    28
franta-hg@1
    29
/**
franta-hg@1
    30
 *
franta-hg@1
    31
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    32
 */
franta-hg@1
    33
public class CLIOptions {
franta-hg@1
    34
franta-hg@3
    35
	public static final String DEFAULT_NAME_PREFIX = ":";
franta-hg@1
    36
	private String sql;
franta-hg@1
    37
	private String databaseName;
franta-hg@15
    38
	private String databaseNameToTest;
franta-hg@3
    39
	private String namePrefix = DEFAULT_NAME_PREFIX;
franta-hg@14
    40
	private String formatterName;
franta-hg@1
    41
	private boolean batch;
franta-hg@2
    42
franta-hg@2
    43
	public enum MODE {
franta-hg@2
    44
franta-hg@2
    45
		QUERY_NOW,
franta-hg@2
    46
		PREPARE_BATCH,
franta-hg@14
    47
		EXECUTE_BATCH,
franta-hg@14
    48
		JUST_SHOW_INFO
franta-hg@14
    49
	}
franta-hg@14
    50
franta-hg@14
    51
	public enum INFO_TYPE {
franta-hg@14
    52
franta-hg@14
    53
		HELP,
franta-hg@14
    54
		VERSION,
franta-hg@14
    55
		LICENSE,
franta-hg@14
    56
		FORMATTERS,
franta-hg@15
    57
		TYPES,
franta-hg@15
    58
		DATABASES,
franta-hg@15
    59
		CONNECTION
franta-hg@2
    60
	}
franta-hg@1
    61
	private COMMAND_TYPE commandType;
franta-hg@1
    62
	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
franta-hg@1
    63
	private final List<Parameter> numberedParameters = new ArrayList<>();
franta-hg@14
    64
	private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
franta-hg@1
    65
franta-hg@1
    66
	public void validate() throws InvalidOptionsException {
franta-hg@1
    67
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@1
    68
franta-hg@14
    69
		MODE mode = getMode();
franta-hg@14
    70
		if (mode == null) {
franta-hg@1
    71
			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
franta-hg@14
    72
		} else if (mode == MODE.JUST_SHOW_INFO) {
franta-hg@14
    73
			if (!namedParameters.isEmpty()) {
franta-hg@14
    74
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
franta-hg@14
    75
			}
franta-hg@14
    76
			if (!numberedParameters.isEmpty()) {
franta-hg@14
    77
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
franta-hg@14
    78
			}
franta-hg@14
    79
			if (isNotEmpty(sql, false)) {
franta-hg@14
    80
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
franta-hg@14
    81
			}
franta-hg@14
    82
			if (isNotEmpty(databaseName, false)) {
franta-hg@14
    83
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
franta-hg@14
    84
			}
franta-hg@14
    85
			if (batch) {
franta-hg@14
    86
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
franta-hg@14
    87
			}
franta-hg@14
    88
			if (isNotEmpty(formatterName, false)) {
franta-hg@14
    89
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
franta-hg@14
    90
			}
franta-hg@14
    91
			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
franta-hg@14
    92
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
franta-hg@14
    93
			}
franta-hg@15
    94
			if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
franta-hg@15
    95
				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
franta-hg@15
    96
			}
franta-hg@1
    97
		}
franta-hg@1
    98
franta-hg@1
    99
		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
franta-hg@1
   100
			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
franta-hg@1
   101
		}
franta-hg@1
   102
franta-hg@1
   103
franta-hg@1
   104
		if (e.hasProblems()) {
franta-hg@1
   105
			throw e;
franta-hg@1
   106
		}
franta-hg@1
   107
	}
franta-hg@1
   108
franta-hg@2
   109
	private boolean hasSql() {
franta-hg@2
   110
		return isNotEmpty(getSql(), true);
franta-hg@1
   111
	}
franta-hg@1
   112
franta-hg@2
   113
	private boolean hasDb() {
franta-hg@2
   114
		return isNotEmpty(getDatabaseName(), true);
franta-hg@1
   115
	}
franta-hg@1
   116
franta-hg@2
   117
	/**
franta-hg@2
   118
	 * Depends on options: DB, BATCH, SQL
franta-hg@2
   119
	 *
franta-hg@2
   120
	 * @return mode | or null if options are not yet initialized or combination of options is
franta-hg@2
   121
	 * invalid
franta-hg@2
   122
	 */
franta-hg@2
   123
	public MODE getMode() {
franta-hg@2
   124
		if (hasDb() && !batch && hasSql()) {
franta-hg@2
   125
			return MODE.QUERY_NOW;
franta-hg@2
   126
		} else if (!hasDb() && batch && hasSql()) {
franta-hg@2
   127
			return MODE.PREPARE_BATCH;
franta-hg@2
   128
		} else if (hasDb() && batch && !hasSql()) {
franta-hg@2
   129
			return MODE.EXECUTE_BATCH;
franta-hg@2
   130
		} else {
franta-hg@14
   131
			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
franta-hg@2
   132
		}
franta-hg@2
   133
	}
franta-hg@2
   134
franta-hg@2
   135
	public String getSql() {
franta-hg@2
   136
		return sql;
franta-hg@2
   137
	}
franta-hg@2
   138
franta-hg@2
   139
	public void setSql(String sql) {
franta-hg@2
   140
		this.sql = sql;
franta-hg@2
   141
	}
franta-hg@2
   142
franta-hg@2
   143
	public String getDatabaseName() {
franta-hg@2
   144
		return databaseName;
franta-hg@2
   145
	}
franta-hg@2
   146
franta-hg@2
   147
	public void setDatabaseName(String databaseName) {
franta-hg@2
   148
		this.databaseName = databaseName;
franta-hg@2
   149
	}
franta-hg@2
   150
franta-hg@2
   151
	public void setBatch(boolean batch) {
franta-hg@2
   152
		this.batch = batch;
franta-hg@2
   153
	}
franta-hg@2
   154
franta-hg@2
   155
	public COMMAND_TYPE getCommandType() {
franta-hg@2
   156
		return commandType;
franta-hg@2
   157
	}
franta-hg@2
   158
franta-hg@2
   159
	public void setCommandType(COMMAND_TYPE commandType) {
franta-hg@2
   160
		this.commandType = commandType;
franta-hg@2
   161
	}
franta-hg@2
   162
franta-hg@2
   163
	public Collection<NamedParameter> getNamedParameters() {
franta-hg@2
   164
		return namedParameters;
franta-hg@2
   165
	}
franta-hg@2
   166
franta-hg@2
   167
	public List<Parameter> getNumberedParameters() {
franta-hg@2
   168
		return numberedParameters;
franta-hg@2
   169
	}
franta-hg@2
   170
franta-hg@2
   171
	public void addNumberedParameter(Parameter p) {
franta-hg@2
   172
		numberedParameters.add(p);
franta-hg@2
   173
	}
franta-hg@2
   174
franta-hg@2
   175
	public void addNamedParameter(NamedParameter p) {
franta-hg@2
   176
		namedParameters.add(p);
franta-hg@1
   177
	}
franta-hg@3
   178
franta-hg@3
   179
	public String getNamePrefix() {
franta-hg@3
   180
		return namePrefix;
franta-hg@3
   181
	}
franta-hg@3
   182
franta-hg@3
   183
	public void setNamePrefix(String namePrefix) {
franta-hg@3
   184
		this.namePrefix = namePrefix;
franta-hg@3
   185
	}
franta-hg@14
   186
franta-hg@14
   187
	public String getFormatterName() {
franta-hg@14
   188
		return formatterName;
franta-hg@14
   189
	}
franta-hg@14
   190
franta-hg@14
   191
	public void setFormatterName(String formatterName) {
franta-hg@14
   192
		this.formatterName = formatterName;
franta-hg@14
   193
	}
franta-hg@14
   194
franta-hg@14
   195
	public void addShowInfo(INFO_TYPE info) {
franta-hg@14
   196
		showInfo.add(info);
franta-hg@14
   197
	}
franta-hg@14
   198
franta-hg@14
   199
	public EnumSet<INFO_TYPE> getShowInfo() {
franta-hg@14
   200
		return showInfo;
franta-hg@14
   201
	}
franta-hg@15
   202
franta-hg@15
   203
	public String getDatabaseNameToTest() {
franta-hg@15
   204
		return databaseNameToTest;
franta-hg@15
   205
	}
franta-hg@15
   206
franta-hg@15
   207
	public void setDatabaseNameToTest(String databaseNameToTest) {
franta-hg@15
   208
		this.databaseNameToTest = databaseNameToTest;
franta-hg@15
   209
	}
franta-hg@1
   210
}