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