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