franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@250
|
7 |
* the Free Software Foundation, version 3 of the License.
|
franta-hg@16
|
8 |
*
|
franta-hg@16
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
12 |
* GNU General Public License for more details.
|
franta-hg@16
|
13 |
*
|
franta-hg@16
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
16 |
*/
|
franta-hg@1
|
17 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
18 |
|
franta-hg@1
|
19 |
import static info.globalcode.sql.dk.Functions.isNotEmpty;
|
franta-hg@14
|
20 |
import static info.globalcode.sql.dk.Functions.equalz;
|
franta-hg@69
|
21 |
import info.globalcode.sql.dk.InfoLister.InfoType;
|
franta-hg@104
|
22 |
import info.globalcode.sql.dk.configuration.Properties;
|
franta-hg@104
|
23 |
import info.globalcode.sql.dk.configuration.Property;
|
franta-hg@146
|
24 |
import java.io.InputStream;
|
franta-hg@34
|
25 |
import java.io.OutputStream;
|
franta-hg@1
|
26 |
import java.util.ArrayList;
|
franta-hg@1
|
27 |
import java.util.Collection;
|
franta-hg@14
|
28 |
import java.util.EnumSet;
|
franta-hg@162
|
29 |
import java.util.LinkedHashSet;
|
franta-hg@246
|
30 |
import java.util.LinkedList;
|
franta-hg@1
|
31 |
import java.util.List;
|
franta-hg@74
|
32 |
import java.util.Set;
|
franta-hg@63
|
33 |
import java.util.regex.Pattern;
|
franta-hg@63
|
34 |
import java.util.regex.PatternSyntaxException;
|
franta-hg@1
|
35 |
|
franta-hg@1
|
36 |
/**
|
franta-hg@155
|
37 |
* Holds options from command line, validates them, combines with configuration and provides derived
|
franta-hg@155
|
38 |
* objects.
|
franta-hg@1
|
39 |
*
|
franta-hg@1
|
40 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
41 |
*/
|
franta-hg@1
|
42 |
public class CLIOptions {
|
franta-hg@1
|
43 |
|
franta-hg@3
|
44 |
public static final String DEFAULT_NAME_PREFIX = ":";
|
franta-hg@54
|
45 |
public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
|
franta-hg@1
|
46 |
private String sql;
|
franta-hg@1
|
47 |
private String databaseName;
|
franta-hg@246
|
48 |
private final List<String> relationNames = new ArrayList<>();
|
franta-hg@200
|
49 |
private final Set<String> databaseNamesToTest = new LinkedHashSet<>();
|
franta-hg@200
|
50 |
private final Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
|
franta-hg@209
|
51 |
private final Set<String> formatterNamesToListProperties = new LinkedHashSet<>();
|
franta-hg@3
|
52 |
private String namePrefix = DEFAULT_NAME_PREFIX;
|
franta-hg@44
|
53 |
private String nameSuffix = DEFAULT_NAME_SUFFIX;
|
franta-hg@14
|
54 |
private String formatterName;
|
franta-hg@1
|
55 |
private boolean batch;
|
franta-hg@200
|
56 |
private final Properties formatterProperties = new Properties();
|
franta-hg@200
|
57 |
private final Properties databaseProperties = new Properties();
|
franta-hg@2
|
58 |
|
franta-hg@2
|
59 |
public enum MODE {
|
franta-hg@2
|
60 |
|
franta-hg@2
|
61 |
QUERY_NOW,
|
franta-hg@2
|
62 |
PREPARE_BATCH,
|
franta-hg@14
|
63 |
EXECUTE_BATCH,
|
franta-hg@14
|
64 |
JUST_SHOW_INFO
|
franta-hg@14
|
65 |
}
|
franta-hg@34
|
66 |
private final List<NamedParameter> namedParameters = new ArrayList<>();
|
franta-hg@1
|
67 |
private final List<Parameter> numberedParameters = new ArrayList<>();
|
franta-hg@69
|
68 |
private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
|
franta-hg@1
|
69 |
|
franta-hg@1
|
70 |
public void validate() throws InvalidOptionsException {
|
franta-hg@1
|
71 |
InvalidOptionsException e = new InvalidOptionsException();
|
franta-hg@1
|
72 |
|
franta-hg@14
|
73 |
MODE mode = getMode();
|
franta-hg@14
|
74 |
if (mode == null) {
|
franta-hg@1
|
75 |
e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
|
franta-hg@14
|
76 |
} else if (mode == MODE.JUST_SHOW_INFO) {
|
franta-hg@14
|
77 |
if (!namedParameters.isEmpty()) {
|
franta-hg@14
|
78 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
|
franta-hg@14
|
79 |
}
|
franta-hg@14
|
80 |
if (!numberedParameters.isEmpty()) {
|
franta-hg@14
|
81 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
|
franta-hg@14
|
82 |
}
|
franta-hg@14
|
83 |
if (isNotEmpty(sql, false)) {
|
franta-hg@14
|
84 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
|
franta-hg@14
|
85 |
}
|
franta-hg@14
|
86 |
if (isNotEmpty(databaseName, false)) {
|
franta-hg@14
|
87 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
|
franta-hg@14
|
88 |
}
|
franta-hg@14
|
89 |
if (batch) {
|
franta-hg@14
|
90 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
|
franta-hg@14
|
91 |
}
|
franta-hg@14
|
92 |
if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
|
franta-hg@14
|
93 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
|
franta-hg@14
|
94 |
}
|
franta-hg@44
|
95 |
if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
|
franta-hg@44
|
96 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
|
franta-hg@44
|
97 |
}
|
franta-hg@155
|
98 |
if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
|
franta-hg@15
|
99 |
e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
|
franta-hg@15
|
100 |
}
|
franta-hg@159
|
101 |
if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
|
franta-hg@159
|
102 |
e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
|
franta-hg@159
|
103 |
}
|
franta-hg@1
|
104 |
}
|
franta-hg@1
|
105 |
|
franta-hg@1
|
106 |
if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
|
franta-hg@1
|
107 |
e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
|
franta-hg@1
|
108 |
}
|
franta-hg@1
|
109 |
|
franta-hg@63
|
110 |
try {
|
franta-hg@63
|
111 |
Pattern.compile(namePrefix + "test" + nameSuffix);
|
franta-hg@63
|
112 |
} catch (PatternSyntaxException regexException) {
|
franta-hg@63
|
113 |
e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
|
franta-hg@63
|
114 |
}
|
franta-hg@1
|
115 |
|
franta-hg@1
|
116 |
if (e.hasProblems()) {
|
franta-hg@1
|
117 |
throw e;
|
franta-hg@1
|
118 |
}
|
franta-hg@1
|
119 |
}
|
franta-hg@1
|
120 |
|
franta-hg@2
|
121 |
private boolean hasSql() {
|
franta-hg@2
|
122 |
return isNotEmpty(getSql(), true);
|
franta-hg@1
|
123 |
}
|
franta-hg@1
|
124 |
|
franta-hg@2
|
125 |
private boolean hasDb() {
|
franta-hg@2
|
126 |
return isNotEmpty(getDatabaseName(), true);
|
franta-hg@1
|
127 |
}
|
franta-hg@1
|
128 |
|
franta-hg@2
|
129 |
/**
|
franta-hg@2
|
130 |
* Depends on options: DB, BATCH, SQL
|
franta-hg@2
|
131 |
*
|
franta-hg@2
|
132 |
* @return mode | or null if options are not yet initialized or combination of options is
|
franta-hg@2
|
133 |
* invalid
|
franta-hg@2
|
134 |
*/
|
franta-hg@2
|
135 |
public MODE getMode() {
|
franta-hg@2
|
136 |
if (hasDb() && !batch && hasSql()) {
|
franta-hg@2
|
137 |
return MODE.QUERY_NOW;
|
franta-hg@2
|
138 |
} else if (!hasDb() && batch && hasSql()) {
|
franta-hg@2
|
139 |
return MODE.PREPARE_BATCH;
|
franta-hg@2
|
140 |
} else if (hasDb() && batch && !hasSql()) {
|
franta-hg@2
|
141 |
return MODE.EXECUTE_BATCH;
|
franta-hg@2
|
142 |
} else {
|
franta-hg@14
|
143 |
return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
|
franta-hg@2
|
144 |
}
|
franta-hg@2
|
145 |
}
|
franta-hg@2
|
146 |
|
franta-hg@2
|
147 |
public String getSql() {
|
franta-hg@2
|
148 |
return sql;
|
franta-hg@2
|
149 |
}
|
franta-hg@2
|
150 |
|
franta-hg@2
|
151 |
public void setSql(String sql) {
|
franta-hg@2
|
152 |
this.sql = sql;
|
franta-hg@2
|
153 |
}
|
franta-hg@2
|
154 |
|
franta-hg@2
|
155 |
public String getDatabaseName() {
|
franta-hg@2
|
156 |
return databaseName;
|
franta-hg@2
|
157 |
}
|
franta-hg@2
|
158 |
|
franta-hg@2
|
159 |
public void setDatabaseName(String databaseName) {
|
franta-hg@2
|
160 |
this.databaseName = databaseName;
|
franta-hg@2
|
161 |
}
|
franta-hg@2
|
162 |
|
franta-hg@246
|
163 |
public List<String> getRelationNames() {
|
franta-hg@246
|
164 |
return relationNames;
|
franta-hg@246
|
165 |
}
|
franta-hg@246
|
166 |
|
franta-hg@246
|
167 |
public void addRelationName(String name) {
|
franta-hg@246
|
168 |
relationNames.add(name);
|
franta-hg@246
|
169 |
}
|
franta-hg@246
|
170 |
|
franta-hg@2
|
171 |
public void setBatch(boolean batch) {
|
franta-hg@2
|
172 |
this.batch = batch;
|
franta-hg@2
|
173 |
}
|
franta-hg@2
|
174 |
|
franta-hg@2
|
175 |
public Collection<NamedParameter> getNamedParameters() {
|
franta-hg@2
|
176 |
return namedParameters;
|
franta-hg@2
|
177 |
}
|
franta-hg@2
|
178 |
|
franta-hg@2
|
179 |
public List<Parameter> getNumberedParameters() {
|
franta-hg@2
|
180 |
return numberedParameters;
|
franta-hg@2
|
181 |
}
|
franta-hg@2
|
182 |
|
franta-hg@2
|
183 |
public void addNumberedParameter(Parameter p) {
|
franta-hg@2
|
184 |
numberedParameters.add(p);
|
franta-hg@2
|
185 |
}
|
franta-hg@2
|
186 |
|
franta-hg@2
|
187 |
public void addNamedParameter(NamedParameter p) {
|
franta-hg@2
|
188 |
namedParameters.add(p);
|
franta-hg@1
|
189 |
}
|
franta-hg@3
|
190 |
|
franta-hg@104
|
191 |
public Properties getDatabaseProperties() {
|
franta-hg@104
|
192 |
return databaseProperties;
|
franta-hg@104
|
193 |
}
|
franta-hg@104
|
194 |
|
franta-hg@104
|
195 |
public Properties getFormatterProperties() {
|
franta-hg@104
|
196 |
return formatterProperties;
|
franta-hg@104
|
197 |
}
|
franta-hg@104
|
198 |
|
franta-hg@104
|
199 |
public void addDatabaseProperty(Property p) {
|
franta-hg@104
|
200 |
databaseProperties.add(p);
|
franta-hg@104
|
201 |
}
|
franta-hg@104
|
202 |
|
franta-hg@104
|
203 |
public void addFormatterProperty(Property p) {
|
franta-hg@104
|
204 |
formatterProperties.add(p);
|
franta-hg@104
|
205 |
}
|
franta-hg@104
|
206 |
|
franta-hg@54
|
207 |
/**
|
franta-hg@54
|
208 |
* @return regular expression describing the name prefix
|
franta-hg@54
|
209 |
*/
|
franta-hg@3
|
210 |
public String getNamePrefix() {
|
franta-hg@3
|
211 |
return namePrefix;
|
franta-hg@3
|
212 |
}
|
franta-hg@3
|
213 |
|
franta-hg@54
|
214 |
/**
|
franta-hg@209
|
215 |
* @param namePrefix
|
franta-hg@54
|
216 |
* @see #getNamePrefix()
|
franta-hg@54
|
217 |
*/
|
franta-hg@3
|
218 |
public void setNamePrefix(String namePrefix) {
|
franta-hg@3
|
219 |
this.namePrefix = namePrefix;
|
franta-hg@3
|
220 |
}
|
franta-hg@14
|
221 |
|
franta-hg@54
|
222 |
/**
|
franta-hg@54
|
223 |
* @return regular expression describing the name prefix
|
franta-hg@54
|
224 |
*/
|
franta-hg@44
|
225 |
public String getNameSuffix() {
|
franta-hg@44
|
226 |
return nameSuffix;
|
franta-hg@44
|
227 |
}
|
franta-hg@44
|
228 |
|
franta-hg@54
|
229 |
/**
|
franta-hg@209
|
230 |
* @param nameSuffix
|
franta-hg@54
|
231 |
* @see #getNameSuffix()
|
franta-hg@54
|
232 |
*/
|
franta-hg@44
|
233 |
public void setNameSuffix(String nameSuffix) {
|
franta-hg@44
|
234 |
this.nameSuffix = nameSuffix;
|
franta-hg@44
|
235 |
}
|
franta-hg@44
|
236 |
|
franta-hg@14
|
237 |
public String getFormatterName() {
|
franta-hg@14
|
238 |
return formatterName;
|
franta-hg@14
|
239 |
}
|
franta-hg@14
|
240 |
|
franta-hg@14
|
241 |
public void setFormatterName(String formatterName) {
|
franta-hg@14
|
242 |
this.formatterName = formatterName;
|
franta-hg@14
|
243 |
}
|
franta-hg@14
|
244 |
|
franta-hg@69
|
245 |
public void addShowInfo(InfoType info) {
|
franta-hg@14
|
246 |
showInfo.add(info);
|
franta-hg@14
|
247 |
}
|
franta-hg@14
|
248 |
|
franta-hg@69
|
249 |
public EnumSet<InfoType> getShowInfo() {
|
franta-hg@14
|
250 |
return showInfo;
|
franta-hg@14
|
251 |
}
|
franta-hg@15
|
252 |
|
franta-hg@155
|
253 |
public Set<String> getDatabaseNamesToTest() {
|
franta-hg@155
|
254 |
return databaseNamesToTest;
|
franta-hg@15
|
255 |
}
|
franta-hg@15
|
256 |
|
franta-hg@159
|
257 |
public void addDatabaseNameToTest(String name) {
|
franta-hg@159
|
258 |
databaseNamesToTest.add(name);
|
franta-hg@15
|
259 |
}
|
franta-hg@34
|
260 |
|
franta-hg@159
|
261 |
public Set<String> getDatabaseNamesToListProperties() {
|
franta-hg@159
|
262 |
return databaseNamesToListProperties;
|
franta-hg@159
|
263 |
}
|
franta-hg@159
|
264 |
|
franta-hg@159
|
265 |
public void addDatabaseNameToListProperties(String name) {
|
franta-hg@159
|
266 |
databaseNamesToListProperties.add(name);
|
franta-hg@159
|
267 |
}
|
franta-hg@209
|
268 |
|
franta-hg@209
|
269 |
public Set<String> getFormatterNamesToListProperties() {
|
franta-hg@209
|
270 |
return formatterNamesToListProperties;
|
franta-hg@209
|
271 |
}
|
franta-hg@209
|
272 |
|
franta-hg@209
|
273 |
public void addFormatterNameToListProperties(String name) {
|
franta-hg@209
|
274 |
formatterNamesToListProperties.add(name);
|
franta-hg@209
|
275 |
}
|
franta-hg@209
|
276 |
|
franta-hg@34
|
277 |
public SQLCommand getSQLCommand() {
|
franta-hg@34
|
278 |
if (namedParameters.isEmpty()) {
|
franta-hg@37
|
279 |
return new SQLCommandNumbered(sql, numberedParameters);
|
franta-hg@34
|
280 |
} else {
|
franta-hg@47
|
281 |
return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
|
franta-hg@34
|
282 |
}
|
franta-hg@34
|
283 |
}
|
franta-hg@34
|
284 |
|
franta-hg@34
|
285 |
public OutputStream getOutputStream() {
|
franta-hg@34
|
286 |
return System.out;
|
franta-hg@34
|
287 |
}
|
franta-hg@146
|
288 |
|
franta-hg@146
|
289 |
public InputStream getInputStream() {
|
franta-hg@146
|
290 |
return System.in;
|
franta-hg@146
|
291 |
}
|
franta-hg@1
|
292 |
}
|