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