java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 20:16:27 +0100
branchv_0
changeset 2 72da10f632b5
parent 1 f32dac78d13a
child 3 efdf2b886feb
permissions -rw-r--r--
more code
     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 	public enum MODE {
    19 
    20 		QUERY_NOW,
    21 		PREPARE_BATCH,
    22 		EXECUTE_BATCH
    23 	}
    24 
    25 	public enum COMMAND_TYPE {
    26 
    27 		/** SELECT */
    28 		QUERY,
    29 		/** INSERT, UPDATE, DELETE */
    30 		UPDATE
    31 	};
    32 	private COMMAND_TYPE commandType;
    33 	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
    34 	private final List<Parameter> numberedParameters = new ArrayList<>();
    35 
    36 	public void validate() throws InvalidOptionsException {
    37 		InvalidOptionsException e = new InvalidOptionsException();
    38 
    39 		if (getMode() == null) {
    40 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    41 		}
    42 
    43 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    44 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    45 		}
    46 
    47 
    48 		if (e.hasProblems()) {
    49 			throw e;
    50 		}
    51 	}
    52 
    53 	private boolean hasSql() {
    54 		return isNotEmpty(getSql(), true);
    55 	}
    56 
    57 	private boolean hasDb() {
    58 		return isNotEmpty(getDatabaseName(), true);
    59 	}
    60 
    61 	/**
    62 	 * Depends on options: DB, BATCH, SQL
    63 	 *
    64 	 * @return mode | or null if options are not yet initialized or combination of options is
    65 	 * invalid
    66 	 */
    67 	public MODE getMode() {
    68 		if (hasDb() && !batch && hasSql()) {
    69 			return MODE.QUERY_NOW;
    70 		} else if (!hasDb() && batch && hasSql()) {
    71 			return MODE.PREPARE_BATCH;
    72 		} else if (hasDb() && batch && !hasSql()) {
    73 			return MODE.EXECUTE_BATCH;
    74 		} else {
    75 			return null;
    76 		}
    77 	}
    78 
    79 	public String getSql() {
    80 		return sql;
    81 	}
    82 
    83 	public void setSql(String sql) {
    84 		this.sql = sql;
    85 	}
    86 
    87 	public String getDatabaseName() {
    88 		return databaseName;
    89 	}
    90 
    91 	public void setDatabaseName(String databaseName) {
    92 		this.databaseName = databaseName;
    93 	}
    94 
    95 	public void setBatch(boolean batch) {
    96 		this.batch = batch;
    97 	}
    98 
    99 	public COMMAND_TYPE getCommandType() {
   100 		return commandType;
   101 	}
   102 
   103 	public void setCommandType(COMMAND_TYPE commandType) {
   104 		this.commandType = commandType;
   105 	}
   106 
   107 	public Collection<NamedParameter> getNamedParameters() {
   108 		return namedParameters;
   109 	}
   110 
   111 	public List<Parameter> getNumberedParameters() {
   112 		return numberedParameters;
   113 	}
   114 
   115 	public void addNumberedParameter(Parameter p) {
   116 		numberedParameters.add(p);
   117 	}
   118 
   119 	public void addNamedParameter(NamedParameter p) {
   120 		namedParameters.add(p);
   121 	}
   122 }