java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 146 4f4f515df807
child 159 9632b23df30c
permissions -rw-r--r--
more JavaDoc
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@14
    21
import static info.globalcode.sql.dk.Functions.equalz;
franta-hg@69
    22
import info.globalcode.sql.dk.InfoLister.InfoType;
franta-hg@104
    23
import info.globalcode.sql.dk.configuration.Properties;
franta-hg@104
    24
import info.globalcode.sql.dk.configuration.Property;
franta-hg@146
    25
import java.io.InputStream;
franta-hg@34
    26
import java.io.OutputStream;
franta-hg@1
    27
import java.util.ArrayList;
franta-hg@1
    28
import java.util.Collection;
franta-hg@14
    29
import java.util.EnumSet;
franta-hg@74
    30
import java.util.HashSet;
franta-hg@1
    31
import java.util.List;
franta-hg@74
    32
import java.util.Set;
franta-hg@63
    33
import java.util.regex.Pattern;
franta-hg@63
    34
import java.util.regex.PatternSyntaxException;
franta-hg@1
    35
franta-hg@1
    36
/**
franta-hg@155
    37
 * Holds options from command line, validates them, combines with configuration and provides derived
franta-hg@155
    38
 * objects.
franta-hg@1
    39
 *
franta-hg@1
    40
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    41
 */
franta-hg@1
    42
public class CLIOptions {
franta-hg@1
    43
franta-hg@3
    44
	public static final String DEFAULT_NAME_PREFIX = ":";
franta-hg@54
    45
	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
franta-hg@1
    46
	private String sql;
franta-hg@1
    47
	private String databaseName;
franta-hg@155
    48
	private Set<String> databaseNamesToTest = new HashSet<>();
franta-hg@3
    49
	private String namePrefix = DEFAULT_NAME_PREFIX;
franta-hg@44
    50
	private String nameSuffix = DEFAULT_NAME_SUFFIX;
franta-hg@14
    51
	private String formatterName;
franta-hg@1
    52
	private boolean batch;
franta-hg@104
    53
	private Properties formatterProperties = new Properties();
franta-hg@104
    54
	private Properties databaseProperties = new Properties();
franta-hg@2
    55
franta-hg@2
    56
	public enum MODE {
franta-hg@2
    57
franta-hg@2
    58
		QUERY_NOW,
franta-hg@2
    59
		PREPARE_BATCH,
franta-hg@14
    60
		EXECUTE_BATCH,
franta-hg@14
    61
		JUST_SHOW_INFO
franta-hg@14
    62
	}
franta-hg@34
    63
	private final List<NamedParameter> namedParameters = new ArrayList<>();
franta-hg@1
    64
	private final List<Parameter> numberedParameters = new ArrayList<>();
franta-hg@69
    65
	private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.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 (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
franta-hg@14
    90
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
franta-hg@14
    91
			}
franta-hg@44
    92
			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
franta-hg@44
    93
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
franta-hg@44
    94
			}
franta-hg@155
    95
			if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
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@63
   104
		try {
franta-hg@63
   105
			Pattern.compile(namePrefix + "test" + nameSuffix);
franta-hg@63
   106
		} catch (PatternSyntaxException regexException) {
franta-hg@63
   107
			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
franta-hg@63
   108
		}
franta-hg@1
   109
franta-hg@1
   110
		if (e.hasProblems()) {
franta-hg@1
   111
			throw e;
franta-hg@1
   112
		}
franta-hg@1
   113
	}
franta-hg@1
   114
franta-hg@2
   115
	private boolean hasSql() {
franta-hg@2
   116
		return isNotEmpty(getSql(), true);
franta-hg@1
   117
	}
franta-hg@1
   118
franta-hg@2
   119
	private boolean hasDb() {
franta-hg@2
   120
		return isNotEmpty(getDatabaseName(), true);
franta-hg@1
   121
	}
franta-hg@1
   122
franta-hg@2
   123
	/**
franta-hg@2
   124
	 * Depends on options: DB, BATCH, SQL
franta-hg@2
   125
	 *
franta-hg@2
   126
	 * @return mode | or null if options are not yet initialized or combination of options is
franta-hg@2
   127
	 * invalid
franta-hg@2
   128
	 */
franta-hg@2
   129
	public MODE getMode() {
franta-hg@2
   130
		if (hasDb() && !batch && hasSql()) {
franta-hg@2
   131
			return MODE.QUERY_NOW;
franta-hg@2
   132
		} else if (!hasDb() && batch && hasSql()) {
franta-hg@2
   133
			return MODE.PREPARE_BATCH;
franta-hg@2
   134
		} else if (hasDb() && batch && !hasSql()) {
franta-hg@2
   135
			return MODE.EXECUTE_BATCH;
franta-hg@2
   136
		} else {
franta-hg@14
   137
			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
franta-hg@2
   138
		}
franta-hg@2
   139
	}
franta-hg@2
   140
franta-hg@2
   141
	public String getSql() {
franta-hg@2
   142
		return sql;
franta-hg@2
   143
	}
franta-hg@2
   144
franta-hg@2
   145
	public void setSql(String sql) {
franta-hg@2
   146
		this.sql = sql;
franta-hg@2
   147
	}
franta-hg@2
   148
franta-hg@2
   149
	public String getDatabaseName() {
franta-hg@2
   150
		return databaseName;
franta-hg@2
   151
	}
franta-hg@2
   152
franta-hg@2
   153
	public void setDatabaseName(String databaseName) {
franta-hg@2
   154
		this.databaseName = databaseName;
franta-hg@2
   155
	}
franta-hg@2
   156
franta-hg@2
   157
	public void setBatch(boolean batch) {
franta-hg@2
   158
		this.batch = batch;
franta-hg@2
   159
	}
franta-hg@2
   160
franta-hg@2
   161
	public Collection<NamedParameter> getNamedParameters() {
franta-hg@2
   162
		return namedParameters;
franta-hg@2
   163
	}
franta-hg@2
   164
franta-hg@2
   165
	public List<Parameter> getNumberedParameters() {
franta-hg@2
   166
		return numberedParameters;
franta-hg@2
   167
	}
franta-hg@2
   168
franta-hg@2
   169
	public void addNumberedParameter(Parameter p) {
franta-hg@2
   170
		numberedParameters.add(p);
franta-hg@2
   171
	}
franta-hg@2
   172
franta-hg@2
   173
	public void addNamedParameter(NamedParameter p) {
franta-hg@2
   174
		namedParameters.add(p);
franta-hg@1
   175
	}
franta-hg@3
   176
franta-hg@104
   177
	public Properties getDatabaseProperties() {
franta-hg@104
   178
		return databaseProperties;
franta-hg@104
   179
	}
franta-hg@104
   180
franta-hg@104
   181
	public Properties getFormatterProperties() {
franta-hg@104
   182
		return formatterProperties;
franta-hg@104
   183
	}
franta-hg@104
   184
franta-hg@104
   185
	public void addDatabaseProperty(Property p) {
franta-hg@104
   186
		databaseProperties.add(p);
franta-hg@104
   187
	}
franta-hg@104
   188
franta-hg@104
   189
	public void addFormatterProperty(Property p) {
franta-hg@104
   190
		formatterProperties.add(p);
franta-hg@104
   191
	}
franta-hg@104
   192
franta-hg@54
   193
	/**
franta-hg@54
   194
	 * @return regular expression describing the name prefix
franta-hg@54
   195
	 */
franta-hg@3
   196
	public String getNamePrefix() {
franta-hg@3
   197
		return namePrefix;
franta-hg@3
   198
	}
franta-hg@3
   199
franta-hg@54
   200
	/**
franta-hg@54
   201
	 * @see #getNamePrefix()
franta-hg@54
   202
	 */
franta-hg@3
   203
	public void setNamePrefix(String namePrefix) {
franta-hg@3
   204
		this.namePrefix = namePrefix;
franta-hg@3
   205
	}
franta-hg@14
   206
franta-hg@54
   207
	/**
franta-hg@54
   208
	 * @return regular expression describing the name prefix
franta-hg@54
   209
	 */
franta-hg@44
   210
	public String getNameSuffix() {
franta-hg@44
   211
		return nameSuffix;
franta-hg@44
   212
	}
franta-hg@44
   213
franta-hg@54
   214
	/**
franta-hg@54
   215
	 * @see #getNameSuffix()
franta-hg@54
   216
	 */
franta-hg@44
   217
	public void setNameSuffix(String nameSuffix) {
franta-hg@44
   218
		this.nameSuffix = nameSuffix;
franta-hg@44
   219
	}
franta-hg@44
   220
franta-hg@14
   221
	public String getFormatterName() {
franta-hg@14
   222
		return formatterName;
franta-hg@14
   223
	}
franta-hg@14
   224
franta-hg@14
   225
	public void setFormatterName(String formatterName) {
franta-hg@14
   226
		this.formatterName = formatterName;
franta-hg@14
   227
	}
franta-hg@14
   228
franta-hg@69
   229
	public void addShowInfo(InfoType info) {
franta-hg@14
   230
		showInfo.add(info);
franta-hg@14
   231
	}
franta-hg@14
   232
franta-hg@69
   233
	public EnumSet<InfoType> getShowInfo() {
franta-hg@14
   234
		return showInfo;
franta-hg@14
   235
	}
franta-hg@15
   236
franta-hg@155
   237
	public Set<String> getDatabaseNamesToTest() {
franta-hg@155
   238
		return databaseNamesToTest;
franta-hg@15
   239
	}
franta-hg@15
   240
franta-hg@155
   241
	public void addDatabaseNamesToTest(String databaseNameToTest) {
franta-hg@155
   242
		this.databaseNamesToTest.add(databaseNameToTest);
franta-hg@15
   243
	}
franta-hg@34
   244
franta-hg@34
   245
	public SQLCommand getSQLCommand() {
franta-hg@34
   246
		if (namedParameters.isEmpty()) {
franta-hg@37
   247
			return new SQLCommandNumbered(sql, numberedParameters);
franta-hg@34
   248
		} else {
franta-hg@47
   249
			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
franta-hg@34
   250
		}
franta-hg@34
   251
	}
franta-hg@34
   252
franta-hg@34
   253
	public OutputStream getOutputStream() {
franta-hg@34
   254
		return System.out;
franta-hg@34
   255
	}
franta-hg@146
   256
franta-hg@146
   257
	public InputStream getInputStream() {
franta-hg@146
   258
		return System.in;
franta-hg@146
   259
	}
franta-hg@1
   260
}