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