java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
branchv_0
changeset 2 72da10f632b5
parent 1 f32dac78d13a
child 3 efdf2b886feb
equal deleted inserted replaced
1:f32dac78d13a 2:72da10f632b5
    12 public class CLIOptions {
    12 public class CLIOptions {
    13 
    13 
    14 	private String sql;
    14 	private String sql;
    15 	private String databaseName;
    15 	private String databaseName;
    16 	private boolean batch;
    16 	private boolean batch;
    17 	
    17 
       
    18 	public enum MODE {
       
    19 
       
    20 		QUERY_NOW,
       
    21 		PREPARE_BATCH,
       
    22 		EXECUTE_BATCH
       
    23 	}
    18 
    24 
    19 	public enum COMMAND_TYPE {
    25 	public enum COMMAND_TYPE {
    20 
    26 
    21 		/** SELECT */
    27 		/** SELECT */
    22 		QUERY,
    28 		QUERY,
    28 	private final List<Parameter> numberedParameters = new ArrayList<>();
    34 	private final List<Parameter> numberedParameters = new ArrayList<>();
    29 
    35 
    30 	public void validate() throws InvalidOptionsException {
    36 	public void validate() throws InvalidOptionsException {
    31 		InvalidOptionsException e = new InvalidOptionsException();
    37 		InvalidOptionsException e = new InvalidOptionsException();
    32 
    38 
    33 		if ( //
    39 		if (getMode() == null) {
    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 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    40 		}
    41 		}
    41 
    42 
    42 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    43 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    43 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    44 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    47 		if (e.hasProblems()) {
    48 		if (e.hasProblems()) {
    48 			throw e;
    49 			throw e;
    49 		}
    50 		}
    50 	}
    51 	}
    51 
    52 
    52 	public boolean hasSql() {
    53 	private boolean hasSql() {
    53 		return isNotEmpty(sql, true);
    54 		return isNotEmpty(getSql(), true);
    54 	}
    55 	}
    55 
    56 
    56 	public boolean hasDb() {
    57 	private boolean hasDb() {
    57 		return isNotEmpty(databaseName, true);
    58 		return isNotEmpty(getDatabaseName(), true);
    58 	}
    59 	}
    59 
    60 
    60 	public boolean hasBatch() {
    61 	/**
    61 		return batch;
    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);
    62 	}
   121 	}
    63 }
   122 }