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.LinkedList;
32 import java.util.List;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
38 * Holds options from command line, validates them, combines with configuration and provides derived
41 * @author Ing. František Kučera (frantovo.cz)
43 public class CLIOptions {
45 public static final String DEFAULT_NAME_PREFIX = ":";
46 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
48 private String databaseName;
49 private final List<String> relationNames = new ArrayList<>();
50 private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
51 private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
52 private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
53 private String namePrefix = DEFAULT_NAME_PREFIX;
54 private String nameSuffix = DEFAULT_NAME_SUFFIX;
55 private String formatterName;
56 private boolean batch;
57 private final Properties formatterProperties = new Properties();
58 private final Properties databaseProperties = new Properties();
67 private final List<NamedParameter> namedParameters = new ArrayList<>();
68 private final List<Parameter> numberedParameters = new ArrayList<>();
69 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
71 public void validate() throws InvalidOptionsException {
72 InvalidOptionsException e = new InvalidOptionsException();
74 MODE mode = getMode();
76 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
77 } else if (mode == MODE.JUST_SHOW_INFO) {
78 if (!namedParameters.isEmpty()) {
79 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
81 if (!numberedParameters.isEmpty()) {
82 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
84 if (isNotEmpty(sql, false)) {
85 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
87 if (isNotEmpty(databaseName, false)) {
88 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
91 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
93 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
94 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
96 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
97 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
99 if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
100 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
102 if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
103 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
107 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
108 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
112 Pattern.compile(namePrefix + "test" + nameSuffix);
113 } catch (PatternSyntaxException regexException) {
114 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
117 if (e.hasProblems()) {
122 private boolean hasSql() {
123 return isNotEmpty(getSql(), true);
126 private boolean hasDb() {
127 return isNotEmpty(getDatabaseName(), true);
131 * Depends on options: DB, BATCH, SQL
133 * @return mode | or null if options are not yet initialized or combination of options is
136 public MODE getMode() {
137 if (hasDb() && !batch && hasSql()) {
138 return MODE.QUERY_NOW;
139 } else if (!hasDb() && batch && hasSql()) {
140 return MODE.PREPARE_BATCH;
141 } else if (hasDb() && batch && !hasSql()) {
142 return MODE.EXECUTE_BATCH;
144 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
148 public String getSql() {
152 public void setSql(String sql) {
156 public String getDatabaseName() {
160 public void setDatabaseName(String databaseName) {
161 this.databaseName = databaseName;
164 public List<String> getRelationNames() {
165 return relationNames;
168 public void addRelationName(String name) {
169 relationNames.add(name);
172 public void setBatch(boolean batch) {
176 public Collection<NamedParameter> getNamedParameters() {
177 return namedParameters;
180 public List<Parameter> getNumberedParameters() {
181 return numberedParameters;
184 public void addNumberedParameter(Parameter p) {
185 numberedParameters.add(p);
188 public void addNamedParameter(NamedParameter p) {
189 namedParameters.add(p);
192 public Properties getDatabaseProperties() {
193 return databaseProperties;
196 public Properties getFormatterProperties() {
197 return formatterProperties;
200 public void addDatabaseProperty(Property p) {
201 databaseProperties.add(p);
204 public void addFormatterProperty(Property p) {
205 formatterProperties.add(p);
209 * @return regular expression describing the name prefix
211 public String getNamePrefix() {
217 * @see #getNamePrefix()
219 public void setNamePrefix(String namePrefix) {
220 this.namePrefix = namePrefix;
224 * @return regular expression describing the name prefix
226 public String getNameSuffix() {
232 * @see #getNameSuffix()
234 public void setNameSuffix(String nameSuffix) {
235 this.nameSuffix = nameSuffix;
238 public String getFormatterName() {
239 return formatterName;
242 public void setFormatterName(String formatterName) {
243 this.formatterName = formatterName;
246 public void addShowInfo(InfoType info) {
250 public EnumSet<InfoType> getShowInfo() {
254 public Set<String> getDatabaseNamesToTest() {
255 return databaseNamesToTest;
258 public void addDatabaseNameToTest(String name) {
259 databaseNamesToTest.add(name);
262 public Set<String> getDatabaseNamesToListProperties() {
263 return databaseNamesToListProperties;
266 public void addDatabaseNameToListProperties(String name) {
267 databaseNamesToListProperties.add(name);
270 public Set<String> getFormatterNamesToListProperties() {
271 return formatterNamesToListProperties;
274 public void addFormatterNameToListProperties(String name) {
275 formatterNamesToListProperties.add(name);
278 public SQLCommand getSQLCommand() {
279 if (namedParameters.isEmpty()) {
280 return new SQLCommandNumbered(sql, numberedParameters);
282 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
286 public OutputStream getOutputStream() {
290 public InputStream getInputStream() {