java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 15 Jan 2014 21:06:12 +0100
branchv_0
changeset 159 9632b23df30c
parent 155 eb3676c6929b
child 162 7863d8a423d4
permissions -rw-r--r--
InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
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@159
    49
	private Set<String> databaseNamesToListProperties = new HashSet<>();
franta-hg@3
    50
	private String namePrefix = DEFAULT_NAME_PREFIX;
franta-hg@44
    51
	private String nameSuffix = DEFAULT_NAME_SUFFIX;
franta-hg@14
    52
	private String formatterName;
franta-hg@1
    53
	private boolean batch;
franta-hg@104
    54
	private Properties formatterProperties = new Properties();
franta-hg@104
    55
	private Properties databaseProperties = new Properties();
franta-hg@2
    56
franta-hg@2
    57
	public enum MODE {
franta-hg@2
    58
franta-hg@2
    59
		QUERY_NOW,
franta-hg@2
    60
		PREPARE_BATCH,
franta-hg@14
    61
		EXECUTE_BATCH,
franta-hg@14
    62
		JUST_SHOW_INFO
franta-hg@14
    63
	}
franta-hg@34
    64
	private final List<NamedParameter> namedParameters = new ArrayList<>();
franta-hg@1
    65
	private final List<Parameter> numberedParameters = new ArrayList<>();
franta-hg@69
    66
	private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
franta-hg@1
    67
franta-hg@1
    68
	public void validate() throws InvalidOptionsException {
franta-hg@1
    69
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@1
    70
franta-hg@14
    71
		MODE mode = getMode();
franta-hg@14
    72
		if (mode == null) {
franta-hg@1
    73
			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
franta-hg@14
    74
		} else if (mode == MODE.JUST_SHOW_INFO) {
franta-hg@14
    75
			if (!namedParameters.isEmpty()) {
franta-hg@14
    76
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
franta-hg@14
    77
			}
franta-hg@14
    78
			if (!numberedParameters.isEmpty()) {
franta-hg@14
    79
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
franta-hg@14
    80
			}
franta-hg@14
    81
			if (isNotEmpty(sql, false)) {
franta-hg@14
    82
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
franta-hg@14
    83
			}
franta-hg@14
    84
			if (isNotEmpty(databaseName, false)) {
franta-hg@14
    85
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
franta-hg@14
    86
			}
franta-hg@14
    87
			if (batch) {
franta-hg@14
    88
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
franta-hg@14
    89
			}
franta-hg@14
    90
			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
franta-hg@14
    91
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
franta-hg@14
    92
			}
franta-hg@44
    93
			if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
franta-hg@44
    94
				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
franta-hg@44
    95
			}
franta-hg@155
    96
			if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
franta-hg@15
    97
				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
franta-hg@15
    98
			}
franta-hg@159
    99
			if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
franta-hg@159
   100
				e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
franta-hg@159
   101
			}
franta-hg@1
   102
		}
franta-hg@1
   103
franta-hg@1
   104
		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
franta-hg@1
   105
			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
franta-hg@1
   106
		}
franta-hg@1
   107
franta-hg@63
   108
		try {
franta-hg@63
   109
			Pattern.compile(namePrefix + "test" + nameSuffix);
franta-hg@63
   110
		} catch (PatternSyntaxException regexException) {
franta-hg@63
   111
			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
franta-hg@63
   112
		}
franta-hg@1
   113
franta-hg@1
   114
		if (e.hasProblems()) {
franta-hg@1
   115
			throw e;
franta-hg@1
   116
		}
franta-hg@1
   117
	}
franta-hg@1
   118
franta-hg@2
   119
	private boolean hasSql() {
franta-hg@2
   120
		return isNotEmpty(getSql(), true);
franta-hg@1
   121
	}
franta-hg@1
   122
franta-hg@2
   123
	private boolean hasDb() {
franta-hg@2
   124
		return isNotEmpty(getDatabaseName(), true);
franta-hg@1
   125
	}
franta-hg@1
   126
franta-hg@2
   127
	/**
franta-hg@2
   128
	 * Depends on options: DB, BATCH, SQL
franta-hg@2
   129
	 *
franta-hg@2
   130
	 * @return mode | or null if options are not yet initialized or combination of options is
franta-hg@2
   131
	 * invalid
franta-hg@2
   132
	 */
franta-hg@2
   133
	public MODE getMode() {
franta-hg@2
   134
		if (hasDb() && !batch && hasSql()) {
franta-hg@2
   135
			return MODE.QUERY_NOW;
franta-hg@2
   136
		} else if (!hasDb() && batch && hasSql()) {
franta-hg@2
   137
			return MODE.PREPARE_BATCH;
franta-hg@2
   138
		} else if (hasDb() && batch && !hasSql()) {
franta-hg@2
   139
			return MODE.EXECUTE_BATCH;
franta-hg@2
   140
		} else {
franta-hg@14
   141
			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
franta-hg@2
   142
		}
franta-hg@2
   143
	}
franta-hg@2
   144
franta-hg@2
   145
	public String getSql() {
franta-hg@2
   146
		return sql;
franta-hg@2
   147
	}
franta-hg@2
   148
franta-hg@2
   149
	public void setSql(String sql) {
franta-hg@2
   150
		this.sql = sql;
franta-hg@2
   151
	}
franta-hg@2
   152
franta-hg@2
   153
	public String getDatabaseName() {
franta-hg@2
   154
		return databaseName;
franta-hg@2
   155
	}
franta-hg@2
   156
franta-hg@2
   157
	public void setDatabaseName(String databaseName) {
franta-hg@2
   158
		this.databaseName = databaseName;
franta-hg@2
   159
	}
franta-hg@2
   160
franta-hg@2
   161
	public void setBatch(boolean batch) {
franta-hg@2
   162
		this.batch = batch;
franta-hg@2
   163
	}
franta-hg@2
   164
franta-hg@2
   165
	public Collection<NamedParameter> getNamedParameters() {
franta-hg@2
   166
		return namedParameters;
franta-hg@2
   167
	}
franta-hg@2
   168
franta-hg@2
   169
	public List<Parameter> getNumberedParameters() {
franta-hg@2
   170
		return numberedParameters;
franta-hg@2
   171
	}
franta-hg@2
   172
franta-hg@2
   173
	public void addNumberedParameter(Parameter p) {
franta-hg@2
   174
		numberedParameters.add(p);
franta-hg@2
   175
	}
franta-hg@2
   176
franta-hg@2
   177
	public void addNamedParameter(NamedParameter p) {
franta-hg@2
   178
		namedParameters.add(p);
franta-hg@1
   179
	}
franta-hg@3
   180
franta-hg@104
   181
	public Properties getDatabaseProperties() {
franta-hg@104
   182
		return databaseProperties;
franta-hg@104
   183
	}
franta-hg@104
   184
franta-hg@104
   185
	public Properties getFormatterProperties() {
franta-hg@104
   186
		return formatterProperties;
franta-hg@104
   187
	}
franta-hg@104
   188
franta-hg@104
   189
	public void addDatabaseProperty(Property p) {
franta-hg@104
   190
		databaseProperties.add(p);
franta-hg@104
   191
	}
franta-hg@104
   192
franta-hg@104
   193
	public void addFormatterProperty(Property p) {
franta-hg@104
   194
		formatterProperties.add(p);
franta-hg@104
   195
	}
franta-hg@104
   196
franta-hg@54
   197
	/**
franta-hg@54
   198
	 * @return regular expression describing the name prefix
franta-hg@54
   199
	 */
franta-hg@3
   200
	public String getNamePrefix() {
franta-hg@3
   201
		return namePrefix;
franta-hg@3
   202
	}
franta-hg@3
   203
franta-hg@54
   204
	/**
franta-hg@54
   205
	 * @see #getNamePrefix()
franta-hg@54
   206
	 */
franta-hg@3
   207
	public void setNamePrefix(String namePrefix) {
franta-hg@3
   208
		this.namePrefix = namePrefix;
franta-hg@3
   209
	}
franta-hg@14
   210
franta-hg@54
   211
	/**
franta-hg@54
   212
	 * @return regular expression describing the name prefix
franta-hg@54
   213
	 */
franta-hg@44
   214
	public String getNameSuffix() {
franta-hg@44
   215
		return nameSuffix;
franta-hg@44
   216
	}
franta-hg@44
   217
franta-hg@54
   218
	/**
franta-hg@54
   219
	 * @see #getNameSuffix()
franta-hg@54
   220
	 */
franta-hg@44
   221
	public void setNameSuffix(String nameSuffix) {
franta-hg@44
   222
		this.nameSuffix = nameSuffix;
franta-hg@44
   223
	}
franta-hg@44
   224
franta-hg@14
   225
	public String getFormatterName() {
franta-hg@14
   226
		return formatterName;
franta-hg@14
   227
	}
franta-hg@14
   228
franta-hg@14
   229
	public void setFormatterName(String formatterName) {
franta-hg@14
   230
		this.formatterName = formatterName;
franta-hg@14
   231
	}
franta-hg@14
   232
franta-hg@69
   233
	public void addShowInfo(InfoType info) {
franta-hg@14
   234
		showInfo.add(info);
franta-hg@14
   235
	}
franta-hg@14
   236
franta-hg@69
   237
	public EnumSet<InfoType> getShowInfo() {
franta-hg@14
   238
		return showInfo;
franta-hg@14
   239
	}
franta-hg@15
   240
franta-hg@155
   241
	public Set<String> getDatabaseNamesToTest() {
franta-hg@155
   242
		return databaseNamesToTest;
franta-hg@15
   243
	}
franta-hg@15
   244
franta-hg@159
   245
	public void addDatabaseNameToTest(String name) {
franta-hg@159
   246
		databaseNamesToTest.add(name);
franta-hg@15
   247
	}
franta-hg@34
   248
franta-hg@159
   249
	public Set<String> getDatabaseNamesToListProperties() {
franta-hg@159
   250
		return databaseNamesToListProperties;
franta-hg@159
   251
	}
franta-hg@159
   252
franta-hg@159
   253
	public void addDatabaseNameToListProperties(String name) {
franta-hg@159
   254
		databaseNamesToListProperties.add(name);
franta-hg@159
   255
	}
franta-hg@159
   256
	
franta-hg@34
   257
	public SQLCommand getSQLCommand() {
franta-hg@34
   258
		if (namedParameters.isEmpty()) {
franta-hg@37
   259
			return new SQLCommandNumbered(sql, numberedParameters);
franta-hg@34
   260
		} else {
franta-hg@47
   261
			return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
franta-hg@34
   262
		}
franta-hg@34
   263
	}
franta-hg@34
   264
franta-hg@34
   265
	public OutputStream getOutputStream() {
franta-hg@34
   266
		return System.out;
franta-hg@34
   267
	}
franta-hg@146
   268
franta-hg@146
   269
	public InputStream getInputStream() {
franta-hg@146
   270
		return System.in;
franta-hg@146
   271
	}
franta-hg@1
   272
}