author | František Kučera <franta-hg@frantovo.cz> |
Sun, 15 Dec 2013 20:16:27 +0100 | |
branch | v_0 |
changeset 2 | 72da10f632b5 |
parent 1 | f32dac78d13a |
child 3 | efdf2b886feb |
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; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
import java.util.ArrayList; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
import java.util.Collection; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
import java.util.List; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
/** |
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 |
* @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
|
11 |
*/ |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
public class CLIOptions { |
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 |
private String sql; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
private String databaseName; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
private boolean batch; |
2 | 17 |
|
18 |
public enum MODE { |
|
19 |
||
20 |
QUERY_NOW, |
|
21 |
PREPARE_BATCH, |
|
22 |
EXECUTE_BATCH |
|
23 |
} |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
public enum COMMAND_TYPE { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
/** SELECT */ |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
QUERY, |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
/** INSERT, UPDATE, DELETE */ |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
UPDATE |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
}; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
private COMMAND_TYPE commandType; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
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
|
34 |
private final List<Parameter> numberedParameters = new ArrayList<>(); |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
public void validate() throws InvalidOptionsException { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
InvalidOptionsException e = new InvalidOptionsException(); |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
2 | 39 |
if (getMode() == null) { |
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); |
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 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
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
|
45 |
} |
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 |
|
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
if (e.hasProblems()) { |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
throw e; |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
} |
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 |
|
2 | 53 |
private boolean hasSql() { |
54 |
return isNotEmpty(getSql(), true); |
|
55 |
} |
|
56 |
||
57 |
private boolean hasDb() { |
|
58 |
return isNotEmpty(getDatabaseName(), true); |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Depends on options: DB, BATCH, SQL |
|
63 |
* |
|
64 |
* @return mode | or null if options are not yet initialized or combination of options is |
|
65 |
* invalid |
|
66 |
*/ |
|
67 |
public MODE getMode() { |
|
68 |
if (hasDb() && !batch && hasSql()) { |
|
69 |
return MODE.QUERY_NOW; |
|
70 |
} else if (!hasDb() && batch && hasSql()) { |
|
71 |
return MODE.PREPARE_BATCH; |
|
72 |
} else if (hasDb() && batch && !hasSql()) { |
|
73 |
return MODE.EXECUTE_BATCH; |
|
74 |
} else { |
|
75 |
return null; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
public String getSql() { |
|
80 |
return sql; |
|
81 |
} |
|
82 |
||
83 |
public void setSql(String sql) { |
|
84 |
this.sql = sql; |
|
1
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 |
|
2 | 87 |
public String getDatabaseName() { |
88 |
return databaseName; |
|
89 |
} |
|
90 |
||
91 |
public void setDatabaseName(String databaseName) { |
|
92 |
this.databaseName = databaseName; |
|
93 |
} |
|
94 |
||
95 |
public void setBatch(boolean batch) { |
|
96 |
this.batch = batch; |
|
97 |
} |
|
98 |
||
99 |
public COMMAND_TYPE getCommandType() { |
|
100 |
return commandType; |
|
1
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 |
|
2 | 103 |
public void setCommandType(COMMAND_TYPE commandType) { |
104 |
this.commandType = commandType; |
|
105 |
} |
|
106 |
||
107 |
public Collection<NamedParameter> getNamedParameters() { |
|
108 |
return namedParameters; |
|
109 |
} |
|
110 |
||
111 |
public List<Parameter> getNumberedParameters() { |
|
112 |
return numberedParameters; |
|
113 |
} |
|
114 |
||
115 |
public void addNumberedParameter(Parameter p) { |
|
116 |
numberedParameters.add(p); |
|
117 |
} |
|
118 |
||
119 |
public void addNamedParameter(NamedParameter p) { |
|
120 |
namedParameters.add(p); |
|
1
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
} |
f32dac78d13a
WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
} |