franta-hg@1: package info.globalcode.sql.dk; franta-hg@1: franta-hg@1: import static info.globalcode.sql.dk.Functions.isNotEmpty; franta-hg@1: import java.util.ArrayList; franta-hg@1: import java.util.Collection; franta-hg@1: import java.util.List; franta-hg@1: franta-hg@1: /** franta-hg@1: * franta-hg@1: * @author Ing. František Kučera (frantovo.cz) franta-hg@1: */ franta-hg@1: public class CLIOptions { franta-hg@1: franta-hg@1: private String sql; franta-hg@1: private String databaseName; franta-hg@1: private boolean batch; franta-hg@1: franta-hg@1: franta-hg@1: public enum COMMAND_TYPE { franta-hg@1: franta-hg@1: /** SELECT */ franta-hg@1: QUERY, franta-hg@1: /** INSERT, UPDATE, DELETE */ franta-hg@1: UPDATE franta-hg@1: }; franta-hg@1: private COMMAND_TYPE commandType; franta-hg@1: private final Collection namedParameters = new ArrayList<>(); franta-hg@1: private final List numberedParameters = new ArrayList<>(); franta-hg@1: franta-hg@1: public void validate() throws InvalidOptionsException { franta-hg@1: InvalidOptionsException e = new InvalidOptionsException(); franta-hg@1: franta-hg@1: if ( // franta-hg@1: (hasDb() ? 1 : 0) + // franta-hg@1: (hasSql() ? 1 : 0) + // franta-hg@1: (hasBatch() ? 1 : 0) franta-hg@1: != 2) // franta-hg@1: { franta-hg@1: e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options")); franta-hg@1: } franta-hg@1: franta-hg@1: if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) { franta-hg@1: e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command.")); franta-hg@1: } franta-hg@1: franta-hg@1: franta-hg@1: if (e.hasProblems()) { franta-hg@1: throw e; franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: public boolean hasSql() { franta-hg@1: return isNotEmpty(sql, true); franta-hg@1: } franta-hg@1: franta-hg@1: public boolean hasDb() { franta-hg@1: return isNotEmpty(databaseName, true); franta-hg@1: } franta-hg@1: franta-hg@1: public boolean hasBatch() { franta-hg@1: return batch; franta-hg@1: } franta-hg@1: }