|
1 package info.globalcode.sql.dk; |
|
2 |
|
3 import static info.globalcode.sql.dk.Functions.isNotEmpty; |
|
4 import java.util.ArrayList; |
|
5 import java.util.Collection; |
|
6 import java.util.List; |
|
7 |
|
8 /** |
|
9 * |
|
10 * @author Ing. František Kučera (frantovo.cz) |
|
11 */ |
|
12 public class CLIOptions { |
|
13 |
|
14 private String sql; |
|
15 private String databaseName; |
|
16 private boolean batch; |
|
17 |
|
18 |
|
19 public enum COMMAND_TYPE { |
|
20 |
|
21 /** SELECT */ |
|
22 QUERY, |
|
23 /** INSERT, UPDATE, DELETE */ |
|
24 UPDATE |
|
25 }; |
|
26 private COMMAND_TYPE commandType; |
|
27 private final Collection<NamedParameter> namedParameters = new ArrayList<>(); |
|
28 private final List<Parameter> numberedParameters = new ArrayList<>(); |
|
29 |
|
30 public void validate() throws InvalidOptionsException { |
|
31 InvalidOptionsException e = new InvalidOptionsException(); |
|
32 |
|
33 if ( // |
|
34 (hasDb() ? 1 : 0) + // |
|
35 (hasSql() ? 1 : 0) + // |
|
36 (hasBatch() ? 1 : 0) |
|
37 != 2) // |
|
38 { |
|
39 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); |
|
40 } |
|
41 |
|
42 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { |
|
43 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command.")); |
|
44 } |
|
45 |
|
46 |
|
47 if (e.hasProblems()) { |
|
48 throw e; |
|
49 } |
|
50 } |
|
51 |
|
52 public boolean hasSql() { |
|
53 return isNotEmpty(sql, true); |
|
54 } |
|
55 |
|
56 public boolean hasDb() { |
|
57 return isNotEmpty(databaseName, true); |
|
58 } |
|
59 |
|
60 public boolean hasBatch() { |
|
61 return batch; |
|
62 } |
|
63 } |