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