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 java.io.OutputStream;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.EnumSet;
27 import java.util.List;
31 * @author Ing. František Kučera (frantovo.cz)
33 public class CLIOptions {
35 public static final String DEFAULT_NAME_PREFIX = ":";
36 public static final String DEFAULT_NAME_SUFFIX = ":";
38 private String databaseName;
39 private String databaseNameToTest;
40 private String namePrefix = DEFAULT_NAME_PREFIX;
41 private String nameSuffix = DEFAULT_NAME_SUFFIX;
42 private String formatterName;
43 private boolean batch;
53 public enum INFO_TYPE {
63 private final List<NamedParameter> namedParameters = new ArrayList<>();
64 private final List<Parameter> numberedParameters = new ArrayList<>();
65 private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.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 (isNotEmpty(formatterName, false)) {
90 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
92 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
93 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
95 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
96 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
98 if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
99 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
103 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
104 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
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 String getNamePrefix() {
179 public void setNamePrefix(String namePrefix) {
180 this.namePrefix = namePrefix;
183 public String getNameSuffix() {
187 public void setNameSuffix(String nameSuffix) {
188 this.nameSuffix = nameSuffix;
191 public String getFormatterName() {
192 return formatterName;
195 public void setFormatterName(String formatterName) {
196 this.formatterName = formatterName;
199 public void addShowInfo(INFO_TYPE info) {
203 public EnumSet<INFO_TYPE> getShowInfo() {
207 public String getDatabaseNameToTest() {
208 return databaseNameToTest;
211 public void setDatabaseNameToTest(String databaseNameToTest) {
212 this.databaseNameToTest = databaseNameToTest;
215 public SQLCommand getSQLCommand() {
216 if (namedParameters.isEmpty()) {
217 return new SQLCommandNumbered(sql, numberedParameters);
219 return new SQLCommandNamed(sql, namedParameters);
223 public OutputStream getOutputStream() {