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