author | František Kučera <franta-hg@frantovo.cz> |
Sun, 22 Dec 2013 18:19:38 +0100 | |
branch | v_0 |
changeset 29 | d66858b4b563 |
parent 16 | 5b8fcd35d4d6 |
child 34 | 9335cf31c0f2 |
permissions | -rw-r--r-- |
16
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
1 |
/** |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
2 |
* SQL-DK |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz) |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
4 |
* |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
8 |
* (at your option) any later version. |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
9 |
* |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
13 |
* GNU General Public License for more details. |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
14 |
* |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
5b8fcd35d4d6
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
17 |
*/ |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
package info.globalcode.sql.dk; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
import static info.globalcode.sql.dk.Functions.isNotEmpty; |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
21 |
import static info.globalcode.sql.dk.Functions.isEmpty; |
14 | 22 |
import static info.globalcode.sql.dk.Functions.equalz; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
23 |
import info.globalcode.sql.dk.SQLCommand.COMMAND_TYPE; |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
import java.util.ArrayList; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
import java.util.Collection; |
14 | 26 |
import java.util.EnumSet; |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
import java.util.List; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
/** |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
* |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
* @author Ing. František Kučera (frantovo.cz) |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
*/ |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
public class CLIOptions { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
|
3 | 35 |
public static final String DEFAULT_NAME_PREFIX = ":"; |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
private String sql; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
private String databaseName; |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
38 |
private String databaseNameToTest; |
3 | 39 |
private String namePrefix = DEFAULT_NAME_PREFIX; |
14 | 40 |
private String formatterName; |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
private boolean batch; |
2 | 42 |
|
43 |
public enum MODE { |
|
44 |
||
45 |
QUERY_NOW, |
|
46 |
PREPARE_BATCH, |
|
14 | 47 |
EXECUTE_BATCH, |
48 |
JUST_SHOW_INFO |
|
49 |
} |
|
50 |
||
51 |
public enum INFO_TYPE { |
|
52 |
||
53 |
HELP, |
|
54 |
VERSION, |
|
55 |
LICENSE, |
|
56 |
FORMATTERS, |
|
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
57 |
TYPES, |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
58 |
DATABASES, |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
59 |
CONNECTION |
2 | 60 |
} |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
private COMMAND_TYPE commandType; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
private final Collection<NamedParameter> namedParameters = new ArrayList<>(); |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
private final List<Parameter> numberedParameters = new ArrayList<>(); |
14 | 64 |
private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class); |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
public void validate() throws InvalidOptionsException { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
InvalidOptionsException e = new InvalidOptionsException(); |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
14 | 69 |
MODE mode = getMode(); |
70 |
if (mode == null) { |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); |
14 | 72 |
} else if (mode == MODE.JUST_SHOW_INFO) { |
73 |
if (!namedParameters.isEmpty()) { |
|
74 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info.")); |
|
75 |
} |
|
76 |
if (!numberedParameters.isEmpty()) { |
|
77 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info.")); |
|
78 |
} |
|
79 |
if (isNotEmpty(sql, false)) { |
|
80 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info.")); |
|
81 |
} |
|
82 |
if (isNotEmpty(databaseName, false)) { |
|
83 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info.")); |
|
84 |
} |
|
85 |
if (batch) { |
|
86 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info.")); |
|
87 |
} |
|
88 |
if (isNotEmpty(formatterName, false)) { |
|
89 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info.")); |
|
90 |
} |
|
91 |
if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) { |
|
92 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info.")); |
|
93 |
} |
|
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
94 |
if (showInfo.contains(INFO_TYPE.CONNECTION) && isEmpty(databaseNameToTest, false)) { |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
95 |
e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested.")); |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
96 |
} |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command.")); |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
if (e.hasProblems()) { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
throw e; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
|
2 | 109 |
private boolean hasSql() { |
110 |
return isNotEmpty(getSql(), true); |
|
111 |
} |
|
112 |
||
113 |
private boolean hasDb() { |
|
114 |
return isNotEmpty(getDatabaseName(), true); |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Depends on options: DB, BATCH, SQL |
|
119 |
* |
|
120 |
* @return mode | or null if options are not yet initialized or combination of options is |
|
121 |
* invalid |
|
122 |
*/ |
|
123 |
public MODE getMode() { |
|
124 |
if (hasDb() && !batch && hasSql()) { |
|
125 |
return MODE.QUERY_NOW; |
|
126 |
} else if (!hasDb() && batch && hasSql()) { |
|
127 |
return MODE.PREPARE_BATCH; |
|
128 |
} else if (hasDb() && batch && !hasSql()) { |
|
129 |
return MODE.EXECUTE_BATCH; |
|
130 |
} else { |
|
14 | 131 |
return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO; |
2 | 132 |
} |
133 |
} |
|
134 |
||
135 |
public String getSql() { |
|
136 |
return sql; |
|
137 |
} |
|
138 |
||
139 |
public void setSql(String sql) { |
|
140 |
this.sql = sql; |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
141 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
142 |
|
2 | 143 |
public String getDatabaseName() { |
144 |
return databaseName; |
|
145 |
} |
|
146 |
||
147 |
public void setDatabaseName(String databaseName) { |
|
148 |
this.databaseName = databaseName; |
|
149 |
} |
|
150 |
||
151 |
public void setBatch(boolean batch) { |
|
152 |
this.batch = batch; |
|
153 |
} |
|
154 |
||
155 |
public COMMAND_TYPE getCommandType() { |
|
156 |
return commandType; |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
157 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
|
2 | 159 |
public void setCommandType(COMMAND_TYPE commandType) { |
160 |
this.commandType = commandType; |
|
161 |
} |
|
162 |
||
163 |
public Collection<NamedParameter> getNamedParameters() { |
|
164 |
return namedParameters; |
|
165 |
} |
|
166 |
||
167 |
public List<Parameter> getNumberedParameters() { |
|
168 |
return numberedParameters; |
|
169 |
} |
|
170 |
||
171 |
public void addNumberedParameter(Parameter p) { |
|
172 |
numberedParameters.add(p); |
|
173 |
} |
|
174 |
||
175 |
public void addNamedParameter(NamedParameter p) { |
|
176 |
namedParameters.add(p); |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
177 |
} |
3 | 178 |
|
179 |
public String getNamePrefix() { |
|
180 |
return namePrefix; |
|
181 |
} |
|
182 |
||
183 |
public void setNamePrefix(String namePrefix) { |
|
184 |
this.namePrefix = namePrefix; |
|
185 |
} |
|
14 | 186 |
|
187 |
public String getFormatterName() { |
|
188 |
return formatterName; |
|
189 |
} |
|
190 |
||
191 |
public void setFormatterName(String formatterName) { |
|
192 |
this.formatterName = formatterName; |
|
193 |
} |
|
194 |
||
195 |
public void addShowInfo(INFO_TYPE info) { |
|
196 |
showInfo.add(info); |
|
197 |
} |
|
198 |
||
199 |
public EnumSet<INFO_TYPE> getShowInfo() { |
|
200 |
return showInfo; |
|
201 |
} |
|
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
202 |
|
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
203 |
public String getDatabaseNameToTest() { |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
204 |
return databaseNameToTest; |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
205 |
} |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
206 |
|
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
207 |
public void setDatabaseNameToTest(String databaseNameToTest) { |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
208 |
this.databaseNameToTest = databaseNameToTest; |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
209 |
} |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
210 |
} |