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.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.EnumSet;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.regex.Pattern;
33 import java.util.regex.PatternSyntaxException;
37 * @author Ing. František Kučera (frantovo.cz)
39 public class CLIOptions {
41 public static final String DEFAULT_NAME_PREFIX = ":";
42 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
44 private String databaseName;
45 private Set<String> databaseNameToTest = new HashSet<>();
46 private String namePrefix = DEFAULT_NAME_PREFIX;
47 private String nameSuffix = DEFAULT_NAME_SUFFIX;
48 private String formatterName;
49 private boolean batch;
50 private Properties formatterProperties = new Properties();
51 private Properties databaseProperties = new Properties();
60 private final List<NamedParameter> namedParameters = new ArrayList<>();
61 private final List<Parameter> numberedParameters = new ArrayList<>();
62 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
64 public void validate() throws InvalidOptionsException {
65 InvalidOptionsException e = new InvalidOptionsException();
67 MODE mode = getMode();
69 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
70 } else if (mode == MODE.JUST_SHOW_INFO) {
71 if (!namedParameters.isEmpty()) {
72 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
74 if (!numberedParameters.isEmpty()) {
75 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
77 if (isNotEmpty(sql, false)) {
78 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
80 if (isNotEmpty(databaseName, false)) {
81 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
84 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
86 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
87 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
89 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
90 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
92 if (showInfo.contains(InfoType.CONNECTION) && databaseNameToTest.isEmpty()) {
93 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
97 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
98 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
102 Pattern.compile(namePrefix + "test" + nameSuffix);
103 } catch (PatternSyntaxException regexException) {
104 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
107 if (e.hasProblems()) {
112 private boolean hasSql() {
113 return isNotEmpty(getSql(), true);
116 private boolean hasDb() {
117 return isNotEmpty(getDatabaseName(), true);
121 * Depends on options: DB, BATCH, SQL
123 * @return mode | or null if options are not yet initialized or combination of options is
126 public MODE getMode() {
127 if (hasDb() && !batch && hasSql()) {
128 return MODE.QUERY_NOW;
129 } else if (!hasDb() && batch && hasSql()) {
130 return MODE.PREPARE_BATCH;
131 } else if (hasDb() && batch && !hasSql()) {
132 return MODE.EXECUTE_BATCH;
134 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
138 public String getSql() {
142 public void setSql(String sql) {
146 public String getDatabaseName() {
150 public void setDatabaseName(String databaseName) {
151 this.databaseName = databaseName;
154 public void setBatch(boolean batch) {
158 public Collection<NamedParameter> getNamedParameters() {
159 return namedParameters;
162 public List<Parameter> getNumberedParameters() {
163 return numberedParameters;
166 public void addNumberedParameter(Parameter p) {
167 numberedParameters.add(p);
170 public void addNamedParameter(NamedParameter p) {
171 namedParameters.add(p);
174 public Properties getDatabaseProperties() {
175 return databaseProperties;
178 public Properties getFormatterProperties() {
179 return formatterProperties;
182 public void addDatabaseProperty(Property p) {
183 databaseProperties.add(p);
186 public void addFormatterProperty(Property p) {
187 formatterProperties.add(p);
191 * @return regular expression describing the name prefix
193 public String getNamePrefix() {
198 * @see #getNamePrefix()
200 public void setNamePrefix(String namePrefix) {
201 this.namePrefix = namePrefix;
205 * @return regular expression describing the name prefix
207 public String getNameSuffix() {
212 * @see #getNameSuffix()
214 public void setNameSuffix(String nameSuffix) {
215 this.nameSuffix = nameSuffix;
218 public String getFormatterName() {
219 return formatterName;
222 public void setFormatterName(String formatterName) {
223 this.formatterName = formatterName;
226 public void addShowInfo(InfoType info) {
230 public EnumSet<InfoType> getShowInfo() {
234 public Set<String> getDatabaseNameToTest() {
235 return databaseNameToTest;
238 public void addDatabaseNameToTest(String databaseNameToTest) {
239 this.databaseNameToTest.add(databaseNameToTest);
242 public SQLCommand getSQLCommand() {
243 if (namedParameters.isEmpty()) {
244 return new SQLCommandNumbered(sql, numberedParameters);
246 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
250 public OutputStream getOutputStream() {