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.HashSet;
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 Set<String> databaseNamesToTest = new HashSet<>();
49 private String namePrefix = DEFAULT_NAME_PREFIX;
50 private String nameSuffix = DEFAULT_NAME_SUFFIX;
51 private String formatterName;
52 private boolean batch;
53 private Properties formatterProperties = new Properties();
54 private Properties databaseProperties = new Properties();
63 private final List<NamedParameter> namedParameters = new ArrayList<>();
64 private final List<Parameter> numberedParameters = new ArrayList<>();
65 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
67 public void validate() throws InvalidOptionsException {
68 InvalidOptionsException e = new InvalidOptionsException();
70 MODE mode = getMode();
72 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
73 } else if (mode == MODE.JUST_SHOW_INFO) {
74 if (!namedParameters.isEmpty()) {
75 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
77 if (!numberedParameters.isEmpty()) {
78 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
80 if (isNotEmpty(sql, false)) {
81 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
83 if (isNotEmpty(databaseName, false)) {
84 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
87 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
89 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
90 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
92 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
93 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
95 if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
96 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
100 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
101 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
105 Pattern.compile(namePrefix + "test" + nameSuffix);
106 } catch (PatternSyntaxException regexException) {
107 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
110 if (e.hasProblems()) {
115 private boolean hasSql() {
116 return isNotEmpty(getSql(), true);
119 private boolean hasDb() {
120 return isNotEmpty(getDatabaseName(), true);
124 * Depends on options: DB, BATCH, SQL
126 * @return mode | or null if options are not yet initialized or combination of options is
129 public MODE getMode() {
130 if (hasDb() && !batch && hasSql()) {
131 return MODE.QUERY_NOW;
132 } else if (!hasDb() && batch && hasSql()) {
133 return MODE.PREPARE_BATCH;
134 } else if (hasDb() && batch && !hasSql()) {
135 return MODE.EXECUTE_BATCH;
137 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
141 public String getSql() {
145 public void setSql(String sql) {
149 public String getDatabaseName() {
153 public void setDatabaseName(String databaseName) {
154 this.databaseName = databaseName;
157 public void setBatch(boolean batch) {
161 public Collection<NamedParameter> getNamedParameters() {
162 return namedParameters;
165 public List<Parameter> getNumberedParameters() {
166 return numberedParameters;
169 public void addNumberedParameter(Parameter p) {
170 numberedParameters.add(p);
173 public void addNamedParameter(NamedParameter p) {
174 namedParameters.add(p);
177 public Properties getDatabaseProperties() {
178 return databaseProperties;
181 public Properties getFormatterProperties() {
182 return formatterProperties;
185 public void addDatabaseProperty(Property p) {
186 databaseProperties.add(p);
189 public void addFormatterProperty(Property p) {
190 formatterProperties.add(p);
194 * @return regular expression describing the name prefix
196 public String getNamePrefix() {
201 * @see #getNamePrefix()
203 public void setNamePrefix(String namePrefix) {
204 this.namePrefix = namePrefix;
208 * @return regular expression describing the name prefix
210 public String getNameSuffix() {
215 * @see #getNameSuffix()
217 public void setNameSuffix(String nameSuffix) {
218 this.nameSuffix = nameSuffix;
221 public String getFormatterName() {
222 return formatterName;
225 public void setFormatterName(String formatterName) {
226 this.formatterName = formatterName;
229 public void addShowInfo(InfoType info) {
233 public EnumSet<InfoType> getShowInfo() {
237 public Set<String> getDatabaseNamesToTest() {
238 return databaseNamesToTest;
241 public void addDatabaseNamesToTest(String databaseNameToTest) {
242 this.databaseNamesToTest.add(databaseNameToTest);
245 public SQLCommand getSQLCommand() {
246 if (namedParameters.isEmpty()) {
247 return new SQLCommandNumbered(sql, numberedParameters);
249 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
253 public OutputStream getOutputStream() {
257 public InputStream getInputStream() {