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