1 package info.globalcode.sql.dk; |
1 package info.globalcode.sql.dk; |
2 |
2 |
3 import static info.globalcode.sql.dk.Functions.isNotEmpty; |
3 import static info.globalcode.sql.dk.Functions.isNotEmpty; |
|
4 import static info.globalcode.sql.dk.Functions.equalz; |
4 import java.util.ArrayList; |
5 import java.util.ArrayList; |
5 import java.util.Collection; |
6 import java.util.Collection; |
|
7 import java.util.EnumSet; |
6 import java.util.List; |
8 import java.util.List; |
7 |
9 |
8 /** |
10 /** |
9 * |
11 * |
10 * @author Ing. František Kučera (frantovo.cz) |
12 * @author Ing. František Kučera (frantovo.cz) |
13 |
15 |
14 public static final String DEFAULT_NAME_PREFIX = ":"; |
16 public static final String DEFAULT_NAME_PREFIX = ":"; |
15 private String sql; |
17 private String sql; |
16 private String databaseName; |
18 private String databaseName; |
17 private String namePrefix = DEFAULT_NAME_PREFIX; |
19 private String namePrefix = DEFAULT_NAME_PREFIX; |
|
20 private String formatterName; |
18 private boolean batch; |
21 private boolean batch; |
19 |
22 |
20 public enum MODE { |
23 public enum MODE { |
21 |
24 |
22 QUERY_NOW, |
25 QUERY_NOW, |
23 PREPARE_BATCH, |
26 PREPARE_BATCH, |
24 EXECUTE_BATCH |
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 |
25 } |
38 } |
26 |
39 |
27 public enum COMMAND_TYPE { |
40 public enum COMMAND_TYPE { |
28 |
41 |
29 /** SELECT */ |
42 /** SELECT */ |
32 UPDATE |
45 UPDATE |
33 }; |
46 }; |
34 private COMMAND_TYPE commandType; |
47 private COMMAND_TYPE commandType; |
35 private final Collection<NamedParameter> namedParameters = new ArrayList<>(); |
48 private final Collection<NamedParameter> namedParameters = new ArrayList<>(); |
36 private final List<Parameter> numberedParameters = new ArrayList<>(); |
49 private final List<Parameter> numberedParameters = new ArrayList<>(); |
|
50 private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class); |
37 |
51 |
38 public void validate() throws InvalidOptionsException { |
52 public void validate() throws InvalidOptionsException { |
39 InvalidOptionsException e = new InvalidOptionsException(); |
53 InvalidOptionsException e = new InvalidOptionsException(); |
40 |
54 |
41 if (getMode() == null) { |
55 MODE mode = getMode(); |
|
56 if (mode == null) { |
42 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); |
57 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); |
|
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 } |
43 } |
80 } |
44 |
81 |
45 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { |
82 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { |
46 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command.")); |
83 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command.")); |
47 } |
84 } |
72 } else if (!hasDb() && batch && hasSql()) { |
109 } else if (!hasDb() && batch && hasSql()) { |
73 return MODE.PREPARE_BATCH; |
110 return MODE.PREPARE_BATCH; |
74 } else if (hasDb() && batch && !hasSql()) { |
111 } else if (hasDb() && batch && !hasSql()) { |
75 return MODE.EXECUTE_BATCH; |
112 return MODE.EXECUTE_BATCH; |
76 } else { |
113 } else { |
77 return null; |
114 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO; |
78 } |
115 } |
79 } |
116 } |
80 |
117 |
81 public String getSql() { |
118 public String getSql() { |
82 return sql; |
119 return sql; |
127 } |
164 } |
128 |
165 |
129 public void setNamePrefix(String namePrefix) { |
166 public void setNamePrefix(String namePrefix) { |
130 this.namePrefix = namePrefix; |
167 this.namePrefix = namePrefix; |
131 } |
168 } |
|
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 } |
132 } |
185 } |