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;
38 * @author Ing. František Kučera (frantovo.cz)
40 public class CLIOptions {
42 public static final String DEFAULT_NAME_PREFIX = ":";
43 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
45 private String databaseName;
46 private Set<String> databaseNameToTest = new HashSet<>();
47 private String namePrefix = DEFAULT_NAME_PREFIX;
48 private String nameSuffix = DEFAULT_NAME_SUFFIX;
49 private String formatterName;
50 private boolean batch;
51 private Properties formatterProperties = new Properties();
52 private Properties databaseProperties = new Properties();
61 private final List<NamedParameter> namedParameters = new ArrayList<>();
62 private final List<Parameter> numberedParameters = new ArrayList<>();
63 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
65 public void validate() throws InvalidOptionsException {
66 InvalidOptionsException e = new InvalidOptionsException();
68 MODE mode = getMode();
70 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
71 } else if (mode == MODE.JUST_SHOW_INFO) {
72 if (!namedParameters.isEmpty()) {
73 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
75 if (!numberedParameters.isEmpty()) {
76 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
78 if (isNotEmpty(sql, false)) {
79 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
81 if (isNotEmpty(databaseName, false)) {
82 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
85 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
87 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
88 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
90 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
91 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
93 if (showInfo.contains(InfoType.CONNECTION) && databaseNameToTest.isEmpty()) {
94 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
98 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
99 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
103 Pattern.compile(namePrefix + "test" + nameSuffix);
104 } catch (PatternSyntaxException regexException) {
105 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
108 if (e.hasProblems()) {
113 private boolean hasSql() {
114 return isNotEmpty(getSql(), true);
117 private boolean hasDb() {
118 return isNotEmpty(getDatabaseName(), true);
122 * Depends on options: DB, BATCH, SQL
124 * @return mode | or null if options are not yet initialized or combination of options is
127 public MODE getMode() {
128 if (hasDb() && !batch && hasSql()) {
129 return MODE.QUERY_NOW;
130 } else if (!hasDb() && batch && hasSql()) {
131 return MODE.PREPARE_BATCH;
132 } else if (hasDb() && batch && !hasSql()) {
133 return MODE.EXECUTE_BATCH;
135 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
139 public String getSql() {
143 public void setSql(String sql) {
147 public String getDatabaseName() {
151 public void setDatabaseName(String databaseName) {
152 this.databaseName = databaseName;
155 public void setBatch(boolean batch) {
159 public Collection<NamedParameter> getNamedParameters() {
160 return namedParameters;
163 public List<Parameter> getNumberedParameters() {
164 return numberedParameters;
167 public void addNumberedParameter(Parameter p) {
168 numberedParameters.add(p);
171 public void addNamedParameter(NamedParameter p) {
172 namedParameters.add(p);
175 public Properties getDatabaseProperties() {
176 return databaseProperties;
179 public Properties getFormatterProperties() {
180 return formatterProperties;
183 public void addDatabaseProperty(Property p) {
184 databaseProperties.add(p);
187 public void addFormatterProperty(Property p) {
188 formatterProperties.add(p);
192 * @return regular expression describing the name prefix
194 public String getNamePrefix() {
199 * @see #getNamePrefix()
201 public void setNamePrefix(String namePrefix) {
202 this.namePrefix = namePrefix;
206 * @return regular expression describing the name prefix
208 public String getNameSuffix() {
213 * @see #getNameSuffix()
215 public void setNameSuffix(String nameSuffix) {
216 this.nameSuffix = nameSuffix;
219 public String getFormatterName() {
220 return formatterName;
223 public void setFormatterName(String formatterName) {
224 this.formatterName = formatterName;
227 public void addShowInfo(InfoType info) {
231 public EnumSet<InfoType> getShowInfo() {
235 public Set<String> getDatabaseNameToTest() {
236 return databaseNameToTest;
239 public void addDatabaseNameToTest(String databaseNameToTest) {
240 this.databaseNameToTest.add(databaseNameToTest);
243 public SQLCommand getSQLCommand() {
244 if (namedParameters.isEmpty()) {
245 return new SQLCommandNumbered(sql, numberedParameters);
247 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
251 public OutputStream getOutputStream() {
255 public InputStream getInputStream() {