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.isEmpty;
22 import static info.globalcode.sql.dk.Functions.equalz;
23 import info.globalcode.sql.dk.InfoLister.InfoType;
24 import java.io.OutputStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.EnumSet;
28 import java.util.List;
29 import java.util.regex.Pattern;
30 import java.util.regex.PatternSyntaxException;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class CLIOptions {
38 public static final String DEFAULT_NAME_PREFIX = ":";
39 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
41 private String databaseName;
42 private String databaseNameToTest;
43 private String namePrefix = DEFAULT_NAME_PREFIX;
44 private String nameSuffix = DEFAULT_NAME_SUFFIX;
45 private String formatterName;
46 private boolean batch;
55 private final List<NamedParameter> namedParameters = new ArrayList<>();
56 private final List<Parameter> numberedParameters = new ArrayList<>();
57 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
59 public void validate() throws InvalidOptionsException {
60 InvalidOptionsException e = new InvalidOptionsException();
62 MODE mode = getMode();
64 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
65 } else if (mode == MODE.JUST_SHOW_INFO) {
66 if (!namedParameters.isEmpty()) {
67 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
69 if (!numberedParameters.isEmpty()) {
70 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
72 if (isNotEmpty(sql, false)) {
73 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
75 if (isNotEmpty(databaseName, false)) {
76 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
79 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
81 if (isNotEmpty(formatterName, false)) {
82 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
84 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
85 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
87 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
88 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
90 if (showInfo.contains(InfoType.CONNECTION) && isEmpty(databaseNameToTest, false)) {
91 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
95 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
96 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
100 Pattern.compile(namePrefix + "test" + nameSuffix);
101 } catch (PatternSyntaxException regexException) {
102 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
105 if (e.hasProblems()) {
110 private boolean hasSql() {
111 return isNotEmpty(getSql(), true);
114 private boolean hasDb() {
115 return isNotEmpty(getDatabaseName(), true);
119 * Depends on options: DB, BATCH, SQL
121 * @return mode | or null if options are not yet initialized or combination of options is
124 public MODE getMode() {
125 if (hasDb() && !batch && hasSql()) {
126 return MODE.QUERY_NOW;
127 } else if (!hasDb() && batch && hasSql()) {
128 return MODE.PREPARE_BATCH;
129 } else if (hasDb() && batch && !hasSql()) {
130 return MODE.EXECUTE_BATCH;
132 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
136 public String getSql() {
140 public void setSql(String sql) {
144 public String getDatabaseName() {
148 public void setDatabaseName(String databaseName) {
149 this.databaseName = databaseName;
152 public void setBatch(boolean batch) {
156 public Collection<NamedParameter> getNamedParameters() {
157 return namedParameters;
160 public List<Parameter> getNumberedParameters() {
161 return numberedParameters;
164 public void addNumberedParameter(Parameter p) {
165 numberedParameters.add(p);
168 public void addNamedParameter(NamedParameter p) {
169 namedParameters.add(p);
173 * @return regular expression describing the name prefix
175 public String getNamePrefix() {
180 * @see #getNamePrefix()
182 public void setNamePrefix(String namePrefix) {
183 this.namePrefix = namePrefix;
187 * @return regular expression describing the name prefix
189 public String getNameSuffix() {
194 * @see #getNameSuffix()
196 public void setNameSuffix(String nameSuffix) {
197 this.nameSuffix = nameSuffix;
200 public String getFormatterName() {
201 return formatterName;
204 public void setFormatterName(String formatterName) {
205 this.formatterName = formatterName;
208 public void addShowInfo(InfoType info) {
212 public EnumSet<InfoType> getShowInfo() {
216 public String getDatabaseNameToTest() {
217 return databaseNameToTest;
220 public void setDatabaseNameToTest(String databaseNameToTest) {
221 this.databaseNameToTest = databaseNameToTest;
224 public SQLCommand getSQLCommand() {
225 if (namedParameters.isEmpty()) {
226 return new SQLCommandNumbered(sql, numberedParameters);
228 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
232 public OutputStream getOutputStream() {