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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.EnumSet;
26 import java.util.List;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class CLIOptions {
34 public static final String DEFAULT_NAME_PREFIX = ":";
36 private String databaseName;
37 private String databaseNameToTest;
38 private String namePrefix = DEFAULT_NAME_PREFIX;
39 private String formatterName;
40 private boolean batch;
50 public enum INFO_TYPE {
61 public enum COMMAND_TYPE {
65 /** INSERT, UPDATE, DELETE */
68 private COMMAND_TYPE commandType;
69 private final Collection<NamedParameter> namedParameters = new ArrayList<>();
70 private final List<Parameter> numberedParameters = new ArrayList<>();
71 private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
73 public void validate() throws InvalidOptionsException {
74 InvalidOptionsException e = new InvalidOptionsException();
76 MODE mode = getMode();
78 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
79 } else if (mode == MODE.JUST_SHOW_INFO) {
80 if (!namedParameters.isEmpty()) {
81 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
83 if (!numberedParameters.isEmpty()) {
84 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
86 if (isNotEmpty(sql, false)) {
87 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
89 if (isNotEmpty(databaseName, false)) {
90 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
93 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
95 if (isNotEmpty(formatterName, false)) {
96 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
98 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
99 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
101 if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
102 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
106 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
107 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
111 if (e.hasProblems()) {
116 private boolean hasSql() {
117 return isNotEmpty(getSql(), true);
120 private boolean hasDb() {
121 return isNotEmpty(getDatabaseName(), true);
125 * Depends on options: DB, BATCH, SQL
127 * @return mode | or null if options are not yet initialized or combination of options is
130 public MODE getMode() {
131 if (hasDb() && !batch && hasSql()) {
132 return MODE.QUERY_NOW;
133 } else if (!hasDb() && batch && hasSql()) {
134 return MODE.PREPARE_BATCH;
135 } else if (hasDb() && batch && !hasSql()) {
136 return MODE.EXECUTE_BATCH;
138 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
142 public String getSql() {
146 public void setSql(String sql) {
150 public String getDatabaseName() {
154 public void setDatabaseName(String databaseName) {
155 this.databaseName = databaseName;
158 public void setBatch(boolean batch) {
162 public COMMAND_TYPE getCommandType() {
166 public void setCommandType(COMMAND_TYPE commandType) {
167 this.commandType = commandType;
170 public Collection<NamedParameter> getNamedParameters() {
171 return namedParameters;
174 public List<Parameter> getNumberedParameters() {
175 return numberedParameters;
178 public void addNumberedParameter(Parameter p) {
179 numberedParameters.add(p);
182 public void addNamedParameter(NamedParameter p) {
183 namedParameters.add(p);
186 public String getNamePrefix() {
190 public void setNamePrefix(String namePrefix) {
191 this.namePrefix = namePrefix;
194 public String getFormatterName() {
195 return formatterName;
198 public void setFormatterName(String formatterName) {
199 this.formatterName = formatterName;
202 public void addShowInfo(INFO_TYPE info) {
206 public EnumSet<INFO_TYPE> getShowInfo() {
210 public String getDatabaseNameToTest() {
211 return databaseNameToTest;
214 public void setDatabaseNameToTest(String databaseNameToTest) {
215 this.databaseNameToTest = databaseNameToTest;