franta-hg@1
|
1 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
2 |
|
franta-hg@1
|
3 |
import static info.globalcode.sql.dk.Functions.isNotEmpty;
|
franta-hg@14
|
4 |
import static info.globalcode.sql.dk.Functions.equalz;
|
franta-hg@1
|
5 |
import java.util.ArrayList;
|
franta-hg@1
|
6 |
import java.util.Collection;
|
franta-hg@14
|
7 |
import java.util.EnumSet;
|
franta-hg@1
|
8 |
import java.util.List;
|
franta-hg@1
|
9 |
|
franta-hg@1
|
10 |
/**
|
franta-hg@1
|
11 |
*
|
franta-hg@1
|
12 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
13 |
*/
|
franta-hg@1
|
14 |
public class CLIOptions {
|
franta-hg@1
|
15 |
|
franta-hg@3
|
16 |
public static final String DEFAULT_NAME_PREFIX = ":";
|
franta-hg@1
|
17 |
private String sql;
|
franta-hg@1
|
18 |
private String databaseName;
|
franta-hg@3
|
19 |
private String namePrefix = DEFAULT_NAME_PREFIX;
|
franta-hg@14
|
20 |
private String formatterName;
|
franta-hg@1
|
21 |
private boolean batch;
|
franta-hg@2
|
22 |
|
franta-hg@2
|
23 |
public enum MODE {
|
franta-hg@2
|
24 |
|
franta-hg@2
|
25 |
QUERY_NOW,
|
franta-hg@2
|
26 |
PREPARE_BATCH,
|
franta-hg@14
|
27 |
EXECUTE_BATCH,
|
franta-hg@14
|
28 |
JUST_SHOW_INFO
|
franta-hg@14
|
29 |
}
|
franta-hg@14
|
30 |
|
franta-hg@14
|
31 |
public enum INFO_TYPE {
|
franta-hg@14
|
32 |
|
franta-hg@14
|
33 |
HELP,
|
franta-hg@14
|
34 |
VERSION,
|
franta-hg@14
|
35 |
LICENSE,
|
franta-hg@14
|
36 |
FORMATTERS,
|
franta-hg@14
|
37 |
TYPES
|
franta-hg@2
|
38 |
}
|
franta-hg@1
|
39 |
|
franta-hg@1
|
40 |
public enum COMMAND_TYPE {
|
franta-hg@1
|
41 |
|
franta-hg@1
|
42 |
/** SELECT */
|
franta-hg@1
|
43 |
QUERY,
|
franta-hg@1
|
44 |
/** INSERT, UPDATE, DELETE */
|
franta-hg@1
|
45 |
UPDATE
|
franta-hg@1
|
46 |
};
|
franta-hg@1
|
47 |
private COMMAND_TYPE commandType;
|
franta-hg@1
|
48 |
private final Collection<NamedParameter> namedParameters = new ArrayList<>();
|
franta-hg@1
|
49 |
private final List<Parameter> numberedParameters = new ArrayList<>();
|
franta-hg@14
|
50 |
private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
|
franta-hg@1
|
51 |
|
franta-hg@1
|
52 |
public void validate() throws InvalidOptionsException {
|
franta-hg@1
|
53 |
InvalidOptionsException e = new InvalidOptionsException();
|
franta-hg@1
|
54 |
|
franta-hg@14
|
55 |
MODE mode = getMode();
|
franta-hg@14
|
56 |
if (mode == null) {
|
franta-hg@1
|
57 |
e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
|
franta-hg@14
|
58 |
} else if (mode == MODE.JUST_SHOW_INFO) {
|
franta-hg@14
|
59 |
if (!namedParameters.isEmpty()) {
|
franta-hg@14
|
60 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
|
franta-hg@14
|
61 |
}
|
franta-hg@14
|
62 |
if (!numberedParameters.isEmpty()) {
|
franta-hg@14
|
63 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
|
franta-hg@14
|
64 |
}
|
franta-hg@14
|
65 |
if (isNotEmpty(sql, false)) {
|
franta-hg@14
|
66 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
|
franta-hg@14
|
67 |
}
|
franta-hg@14
|
68 |
if (isNotEmpty(databaseName, false)) {
|
franta-hg@14
|
69 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
|
franta-hg@14
|
70 |
}
|
franta-hg@14
|
71 |
if (batch) {
|
franta-hg@14
|
72 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
|
franta-hg@14
|
73 |
}
|
franta-hg@14
|
74 |
if (isNotEmpty(formatterName, false)) {
|
franta-hg@14
|
75 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
|
franta-hg@14
|
76 |
}
|
franta-hg@14
|
77 |
if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
|
franta-hg@14
|
78 |
e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
|
franta-hg@14
|
79 |
}
|
franta-hg@1
|
80 |
}
|
franta-hg@1
|
81 |
|
franta-hg@1
|
82 |
if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
|
franta-hg@1
|
83 |
e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
|
franta-hg@1
|
84 |
}
|
franta-hg@1
|
85 |
|
franta-hg@1
|
86 |
|
franta-hg@1
|
87 |
if (e.hasProblems()) {
|
franta-hg@1
|
88 |
throw e;
|
franta-hg@1
|
89 |
}
|
franta-hg@1
|
90 |
}
|
franta-hg@1
|
91 |
|
franta-hg@2
|
92 |
private boolean hasSql() {
|
franta-hg@2
|
93 |
return isNotEmpty(getSql(), true);
|
franta-hg@1
|
94 |
}
|
franta-hg@1
|
95 |
|
franta-hg@2
|
96 |
private boolean hasDb() {
|
franta-hg@2
|
97 |
return isNotEmpty(getDatabaseName(), true);
|
franta-hg@1
|
98 |
}
|
franta-hg@1
|
99 |
|
franta-hg@2
|
100 |
/**
|
franta-hg@2
|
101 |
* Depends on options: DB, BATCH, SQL
|
franta-hg@2
|
102 |
*
|
franta-hg@2
|
103 |
* @return mode | or null if options are not yet initialized or combination of options is
|
franta-hg@2
|
104 |
* invalid
|
franta-hg@2
|
105 |
*/
|
franta-hg@2
|
106 |
public MODE getMode() {
|
franta-hg@2
|
107 |
if (hasDb() && !batch && hasSql()) {
|
franta-hg@2
|
108 |
return MODE.QUERY_NOW;
|
franta-hg@2
|
109 |
} else if (!hasDb() && batch && hasSql()) {
|
franta-hg@2
|
110 |
return MODE.PREPARE_BATCH;
|
franta-hg@2
|
111 |
} else if (hasDb() && batch && !hasSql()) {
|
franta-hg@2
|
112 |
return MODE.EXECUTE_BATCH;
|
franta-hg@2
|
113 |
} else {
|
franta-hg@14
|
114 |
return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
|
franta-hg@2
|
115 |
}
|
franta-hg@2
|
116 |
}
|
franta-hg@2
|
117 |
|
franta-hg@2
|
118 |
public String getSql() {
|
franta-hg@2
|
119 |
return sql;
|
franta-hg@2
|
120 |
}
|
franta-hg@2
|
121 |
|
franta-hg@2
|
122 |
public void setSql(String sql) {
|
franta-hg@2
|
123 |
this.sql = sql;
|
franta-hg@2
|
124 |
}
|
franta-hg@2
|
125 |
|
franta-hg@2
|
126 |
public String getDatabaseName() {
|
franta-hg@2
|
127 |
return databaseName;
|
franta-hg@2
|
128 |
}
|
franta-hg@2
|
129 |
|
franta-hg@2
|
130 |
public void setDatabaseName(String databaseName) {
|
franta-hg@2
|
131 |
this.databaseName = databaseName;
|
franta-hg@2
|
132 |
}
|
franta-hg@2
|
133 |
|
franta-hg@2
|
134 |
public void setBatch(boolean batch) {
|
franta-hg@2
|
135 |
this.batch = batch;
|
franta-hg@2
|
136 |
}
|
franta-hg@2
|
137 |
|
franta-hg@2
|
138 |
public COMMAND_TYPE getCommandType() {
|
franta-hg@2
|
139 |
return commandType;
|
franta-hg@2
|
140 |
}
|
franta-hg@2
|
141 |
|
franta-hg@2
|
142 |
public void setCommandType(COMMAND_TYPE commandType) {
|
franta-hg@2
|
143 |
this.commandType = commandType;
|
franta-hg@2
|
144 |
}
|
franta-hg@2
|
145 |
|
franta-hg@2
|
146 |
public Collection<NamedParameter> getNamedParameters() {
|
franta-hg@2
|
147 |
return namedParameters;
|
franta-hg@2
|
148 |
}
|
franta-hg@2
|
149 |
|
franta-hg@2
|
150 |
public List<Parameter> getNumberedParameters() {
|
franta-hg@2
|
151 |
return numberedParameters;
|
franta-hg@2
|
152 |
}
|
franta-hg@2
|
153 |
|
franta-hg@2
|
154 |
public void addNumberedParameter(Parameter p) {
|
franta-hg@2
|
155 |
numberedParameters.add(p);
|
franta-hg@2
|
156 |
}
|
franta-hg@2
|
157 |
|
franta-hg@2
|
158 |
public void addNamedParameter(NamedParameter p) {
|
franta-hg@2
|
159 |
namedParameters.add(p);
|
franta-hg@1
|
160 |
}
|
franta-hg@3
|
161 |
|
franta-hg@3
|
162 |
public String getNamePrefix() {
|
franta-hg@3
|
163 |
return namePrefix;
|
franta-hg@3
|
164 |
}
|
franta-hg@3
|
165 |
|
franta-hg@3
|
166 |
public void setNamePrefix(String namePrefix) {
|
franta-hg@3
|
167 |
this.namePrefix = namePrefix;
|
franta-hg@3
|
168 |
}
|
franta-hg@14
|
169 |
|
franta-hg@14
|
170 |
public String getFormatterName() {
|
franta-hg@14
|
171 |
return formatterName;
|
franta-hg@14
|
172 |
}
|
franta-hg@14
|
173 |
|
franta-hg@14
|
174 |
public void setFormatterName(String formatterName) {
|
franta-hg@14
|
175 |
this.formatterName = formatterName;
|
franta-hg@14
|
176 |
}
|
franta-hg@14
|
177 |
|
franta-hg@14
|
178 |
public void addShowInfo(INFO_TYPE info) {
|
franta-hg@14
|
179 |
showInfo.add(info);
|
franta-hg@14
|
180 |
}
|
franta-hg@14
|
181 |
|
franta-hg@14
|
182 |
public EnumSet<INFO_TYPE> getShowInfo() {
|
franta-hg@14
|
183 |
return showInfo;
|
franta-hg@14
|
184 |
}
|
franta-hg@1
|
185 |
}
|