support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
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 = ":";
37 private String databaseName;
38 private String databaseNameToTest;
39 private String namePrefix = DEFAULT_NAME_PREFIX;
40 private String formatterName;
41 private boolean batch;
51 public enum INFO_TYPE {
61 private final List<NamedParameter> namedParameters = new ArrayList<>();
62 private final List<Parameter> numberedParameters = new ArrayList<>();
63 private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.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 (isNotEmpty(formatterName, false)) {
88 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
90 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
91 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
93 if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) {
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 if (e.hasProblems()) {
108 private boolean hasSql() {
109 return isNotEmpty(getSql(), true);
112 private boolean hasDb() {
113 return isNotEmpty(getDatabaseName(), true);
117 * Depends on options: DB, BATCH, SQL
119 * @return mode | or null if options are not yet initialized or combination of options is
122 public MODE getMode() {
123 if (hasDb() && !batch && hasSql()) {
124 return MODE.QUERY_NOW;
125 } else if (!hasDb() && batch && hasSql()) {
126 return MODE.PREPARE_BATCH;
127 } else if (hasDb() && batch && !hasSql()) {
128 return MODE.EXECUTE_BATCH;
130 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
134 public String getSql() {
138 public void setSql(String sql) {
142 public String getDatabaseName() {
146 public void setDatabaseName(String databaseName) {
147 this.databaseName = databaseName;
150 public void setBatch(boolean batch) {
154 public Collection<NamedParameter> getNamedParameters() {
155 return namedParameters;
158 public List<Parameter> getNumberedParameters() {
159 return numberedParameters;
162 public void addNumberedParameter(Parameter p) {
163 numberedParameters.add(p);
166 public void addNamedParameter(NamedParameter p) {
167 namedParameters.add(p);
170 public String getNamePrefix() {
174 public void setNamePrefix(String namePrefix) {
175 this.namePrefix = namePrefix;
178 public String getFormatterName() {
179 return formatterName;
182 public void setFormatterName(String formatterName) {
183 this.formatterName = formatterName;
186 public void addShowInfo(INFO_TYPE info) {
190 public EnumSet<INFO_TYPE> getShowInfo() {
194 public String getDatabaseNameToTest() {
195 return databaseNameToTest;
198 public void setDatabaseNameToTest(String databaseNameToTest) {
199 this.databaseNameToTest = databaseNameToTest;
202 public SQLCommand getSQLCommand() {
203 if (namedParameters.isEmpty()) {
204 return new SQLCommandNumbered(sql, numberedParameters);
206 return new SQLCommandNamed(sql, namedParameters);
210 public OutputStream getOutputStream() {