3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import static info.globalcode.sql.dk.Functions.isNotEmpty;
21 import static info.globalcode.sql.dk.Functions.equalz;
22 import info.globalcode.sql.dk.InfoLister.InfoType;
23 import info.globalcode.sql.dk.configuration.Properties;
24 import info.globalcode.sql.dk.configuration.Property;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.EnumSet;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import java.util.regex.Pattern;
34 import java.util.regex.PatternSyntaxException;
37 * Holds options from command line, validates them, combines with configuration and provides derived
40 * @author Ing. František Kučera (frantovo.cz)
42 public class CLIOptions {
44 public static final String DEFAULT_NAME_PREFIX = ":";
45 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
47 private String databaseName;
48 private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
49 private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
50 private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
51 private String namePrefix = DEFAULT_NAME_PREFIX;
52 private String nameSuffix = DEFAULT_NAME_SUFFIX;
53 private String formatterName;
54 private boolean batch;
55 private final Properties formatterProperties = new Properties();
56 private final Properties databaseProperties = new Properties();
65 private final List<NamedParameter> namedParameters = new ArrayList<>();
66 private final List<Parameter> numberedParameters = new ArrayList<>();
67 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
69 public void validate() throws InvalidOptionsException {
70 InvalidOptionsException e = new InvalidOptionsException();
72 MODE mode = getMode();
74 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
75 } else if (mode == MODE.JUST_SHOW_INFO) {
76 if (!namedParameters.isEmpty()) {
77 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
79 if (!numberedParameters.isEmpty()) {
80 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
82 if (isNotEmpty(sql, false)) {
83 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
85 if (isNotEmpty(databaseName, false)) {
86 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
89 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
91 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
92 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
94 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
95 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
97 if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
98 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
100 if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
101 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
105 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
106 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
110 Pattern.compile(namePrefix + "test" + nameSuffix);
111 } catch (PatternSyntaxException regexException) {
112 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
115 if (e.hasProblems()) {
120 private boolean hasSql() {
121 return isNotEmpty(getSql(), true);
124 private boolean hasDb() {
125 return isNotEmpty(getDatabaseName(), true);
129 * Depends on options: DB, BATCH, SQL
131 * @return mode | or null if options are not yet initialized or combination of options is
134 public MODE getMode() {
135 if (hasDb() && !batch && hasSql()) {
136 return MODE.QUERY_NOW;
137 } else if (!hasDb() && batch && hasSql()) {
138 return MODE.PREPARE_BATCH;
139 } else if (hasDb() && batch && !hasSql()) {
140 return MODE.EXECUTE_BATCH;
142 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
146 public String getSql() {
150 public void setSql(String sql) {
154 public String getDatabaseName() {
158 public void setDatabaseName(String databaseName) {
159 this.databaseName = databaseName;
162 public void setBatch(boolean batch) {
166 public Collection<NamedParameter> getNamedParameters() {
167 return namedParameters;
170 public List<Parameter> getNumberedParameters() {
171 return numberedParameters;
174 public void addNumberedParameter(Parameter p) {
175 numberedParameters.add(p);
178 public void addNamedParameter(NamedParameter p) {
179 namedParameters.add(p);
182 public Properties getDatabaseProperties() {
183 return databaseProperties;
186 public Properties getFormatterProperties() {
187 return formatterProperties;
190 public void addDatabaseProperty(Property p) {
191 databaseProperties.add(p);
194 public void addFormatterProperty(Property p) {
195 formatterProperties.add(p);
199 * @return regular expression describing the name prefix
201 public String getNamePrefix() {
207 * @see #getNamePrefix()
209 public void setNamePrefix(String namePrefix) {
210 this.namePrefix = namePrefix;
214 * @return regular expression describing the name prefix
216 public String getNameSuffix() {
222 * @see #getNameSuffix()
224 public void setNameSuffix(String nameSuffix) {
225 this.nameSuffix = nameSuffix;
228 public String getFormatterName() {
229 return formatterName;
232 public void setFormatterName(String formatterName) {
233 this.formatterName = formatterName;
236 public void addShowInfo(InfoType info) {
240 public EnumSet<InfoType> getShowInfo() {
244 public Set<String> getDatabaseNamesToTest() {
245 return databaseNamesToTest;
248 public void addDatabaseNameToTest(String name) {
249 databaseNamesToTest.add(name);
252 public Set<String> getDatabaseNamesToListProperties() {
253 return databaseNamesToListProperties;
256 public void addDatabaseNameToListProperties(String name) {
257 databaseNamesToListProperties.add(name);
260 public Set<String> getFormatterNamesToListProperties() {
261 return formatterNamesToListProperties;
264 public void addFormatterNameToListProperties(String name) {
265 formatterNamesToListProperties.add(name);
268 public SQLCommand getSQLCommand() {
269 if (namedParameters.isEmpty()) {
270 return new SQLCommandNumbered(sql, numberedParameters);
272 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
276 public OutputStream getOutputStream() {
280 public InputStream getInputStream() {