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