tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk;
19 import static info.globalcode.sql.dk.Functions.isNotEmpty;
20 import static info.globalcode.sql.dk.Functions.equalz;
21 import info.globalcode.sql.dk.InfoLister.InfoType;
22 import info.globalcode.sql.dk.configuration.Properties;
23 import info.globalcode.sql.dk.configuration.Property;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.EnumSet;
29 import java.util.LinkedHashSet;
30 import java.util.LinkedList;
31 import java.util.List;
33 import java.util.regex.Pattern;
34 import java.util.regex.PatternSyntaxException;
37 * Holds options from command line, validates them, combines with configuration and provides derived
40 * @author Ing. František Kučera (frantovo.cz)
42 public class CLIOptions {
44 public static final String DEFAULT_NAME_PREFIX = ":";
45 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
47 private String databaseName;
48 private final List<String> relationNames = new ArrayList<>();
49 private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
50 private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
51 private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
52 private String namePrefix = DEFAULT_NAME_PREFIX;
53 private String nameSuffix = DEFAULT_NAME_SUFFIX;
54 private String formatterName;
55 private boolean batch;
56 private final Properties formatterProperties = new Properties();
57 private final Properties databaseProperties = new Properties();
66 private final List<NamedParameter> namedParameters = new ArrayList<>();
67 private final List<Parameter> numberedParameters = new ArrayList<>();
68 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
70 public void validate() throws InvalidOptionsException {
71 InvalidOptionsException e = new InvalidOptionsException();
73 MODE mode = getMode();
75 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
76 } else if (mode == MODE.JUST_SHOW_INFO) {
77 if (!namedParameters.isEmpty()) {
78 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
80 if (!numberedParameters.isEmpty()) {
81 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
83 if (isNotEmpty(sql, false)) {
84 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
86 if (isNotEmpty(databaseName, false)) {
87 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
90 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch 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(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
99 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
101 if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
102 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
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 Pattern.compile(namePrefix + "test" + nameSuffix);
112 } catch (PatternSyntaxException regexException) {
113 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
116 if (e.hasProblems()) {
121 private boolean hasSql() {
122 return isNotEmpty(getSql(), true);
125 private boolean hasDb() {
126 return isNotEmpty(getDatabaseName(), true);
130 * Depends on options: DB, BATCH, SQL
132 * @return mode | or null if options are not yet initialized or combination of options is
135 public MODE getMode() {
136 if (hasDb() && !batch && hasSql()) {
137 return MODE.QUERY_NOW;
138 } else if (!hasDb() && batch && hasSql()) {
139 return MODE.PREPARE_BATCH;
140 } else if (hasDb() && batch && !hasSql()) {
141 return MODE.EXECUTE_BATCH;
143 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
147 public String getSql() {
151 public void setSql(String sql) {
155 public String getDatabaseName() {
159 public void setDatabaseName(String databaseName) {
160 this.databaseName = databaseName;
163 public List<String> getRelationNames() {
164 return relationNames;
167 public void addRelationName(String name) {
168 relationNames.add(name);
171 public void setBatch(boolean batch) {
175 public Collection<NamedParameter> getNamedParameters() {
176 return namedParameters;
179 public List<Parameter> getNumberedParameters() {
180 return numberedParameters;
183 public void addNumberedParameter(Parameter p) {
184 numberedParameters.add(p);
187 public void addNamedParameter(NamedParameter p) {
188 namedParameters.add(p);
191 public Properties getDatabaseProperties() {
192 return databaseProperties;
195 public Properties getFormatterProperties() {
196 return formatterProperties;
199 public void addDatabaseProperty(Property p) {
200 databaseProperties.add(p);
203 public void addFormatterProperty(Property p) {
204 formatterProperties.add(p);
208 * @return regular expression describing the name prefix
210 public String getNamePrefix() {
216 * @see #getNamePrefix()
218 public void setNamePrefix(String namePrefix) {
219 this.namePrefix = namePrefix;
223 * @return regular expression describing the name prefix
225 public String getNameSuffix() {
231 * @see #getNameSuffix()
233 public void setNameSuffix(String nameSuffix) {
234 this.nameSuffix = nameSuffix;
237 public String getFormatterName() {
238 return formatterName;
241 public void setFormatterName(String formatterName) {
242 this.formatterName = formatterName;
245 public void addShowInfo(InfoType info) {
249 public EnumSet<InfoType> getShowInfo() {
253 public Set<String> getDatabaseNamesToTest() {
254 return databaseNamesToTest;
257 public void addDatabaseNameToTest(String name) {
258 databaseNamesToTest.add(name);
261 public Set<String> getDatabaseNamesToListProperties() {
262 return databaseNamesToListProperties;
265 public void addDatabaseNameToListProperties(String name) {
266 databaseNamesToListProperties.add(name);
269 public Set<String> getFormatterNamesToListProperties() {
270 return formatterNamesToListProperties;
273 public void addFormatterNameToListProperties(String name) {
274 formatterNamesToListProperties.add(name);
277 public SQLCommand getSQLCommand() {
278 if (namedParameters.isEmpty()) {
279 return new SQLCommandNumbered(sql, numberedParameters);
281 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
285 public OutputStream getOutputStream() {
289 public InputStream getInputStream() {