1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Sun Dec 15 19:20:50 2013 +0100
1.3 @@ -0,0 +1,63 @@
1.4 +package info.globalcode.sql.dk;
1.5 +
1.6 +import static info.globalcode.sql.dk.Functions.isNotEmpty;
1.7 +import java.util.ArrayList;
1.8 +import java.util.Collection;
1.9 +import java.util.List;
1.10 +
1.11 +/**
1.12 + *
1.13 + * @author Ing. František Kučera (frantovo.cz)
1.14 + */
1.15 +public class CLIOptions {
1.16 +
1.17 + private String sql;
1.18 + private String databaseName;
1.19 + private boolean batch;
1.20 +
1.21 +
1.22 + public enum COMMAND_TYPE {
1.23 +
1.24 + /** SELECT */
1.25 + QUERY,
1.26 + /** INSERT, UPDATE, DELETE */
1.27 + UPDATE
1.28 + };
1.29 + private COMMAND_TYPE commandType;
1.30 + private final Collection<NamedParameter> namedParameters = new ArrayList<>();
1.31 + private final List<Parameter> numberedParameters = new ArrayList<>();
1.32 +
1.33 + public void validate() throws InvalidOptionsException {
1.34 + InvalidOptionsException e = new InvalidOptionsException();
1.35 +
1.36 + if ( //
1.37 + (hasDb() ? 1 : 0) + //
1.38 + (hasSql() ? 1 : 0) + //
1.39 + (hasBatch() ? 1 : 0)
1.40 + != 2) //
1.41 + {
1.42 + e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
1.43 + }
1.44 +
1.45 + if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
1.46 + e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
1.47 + }
1.48 +
1.49 +
1.50 + if (e.hasProblems()) {
1.51 + throw e;
1.52 + }
1.53 + }
1.54 +
1.55 + public boolean hasSql() {
1.56 + return isNotEmpty(sql, true);
1.57 + }
1.58 +
1.59 + public boolean hasDb() {
1.60 + return isNotEmpty(databaseName, true);
1.61 + }
1.62 +
1.63 + public boolean hasBatch() {
1.64 + return batch;
1.65 + }
1.66 +}