java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 19:20:50 +0100
branchv_0
changeset 1 f32dac78d13a
child 2 72da10f632b5
permissions -rw-r--r--
WOW some classes LOL; TODO: refactor
franta-hg@1
     1
package info.globalcode.sql.dk;
franta-hg@1
     2
franta-hg@1
     3
import static info.globalcode.sql.dk.Functions.isNotEmpty;
franta-hg@1
     4
import java.util.ArrayList;
franta-hg@1
     5
import java.util.Collection;
franta-hg@1
     6
import java.util.List;
franta-hg@1
     7
franta-hg@1
     8
/**
franta-hg@1
     9
 *
franta-hg@1
    10
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    11
 */
franta-hg@1
    12
public class CLIOptions {
franta-hg@1
    13
franta-hg@1
    14
	private String sql;
franta-hg@1
    15
	private String databaseName;
franta-hg@1
    16
	private boolean batch;
franta-hg@1
    17
	
franta-hg@1
    18
franta-hg@1
    19
	public enum COMMAND_TYPE {
franta-hg@1
    20
franta-hg@1
    21
		/** SELECT */
franta-hg@1
    22
		QUERY,
franta-hg@1
    23
		/** INSERT, UPDATE, DELETE */
franta-hg@1
    24
		UPDATE
franta-hg@1
    25
	};
franta-hg@1
    26
	private COMMAND_TYPE commandType;
franta-hg@1
    27
	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
franta-hg@1
    28
	private final List<Parameter> numberedParameters = new ArrayList<>();
franta-hg@1
    29
franta-hg@1
    30
	public void validate() throws InvalidOptionsException {
franta-hg@1
    31
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@1
    32
franta-hg@1
    33
		if ( //
franta-hg@1
    34
				(hasDb() ? 1 : 0) + //
franta-hg@1
    35
				(hasSql() ? 1 : 0) + //
franta-hg@1
    36
				(hasBatch() ? 1 : 0)
franta-hg@1
    37
				!= 2) //
franta-hg@1
    38
		{
franta-hg@1
    39
			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
franta-hg@1
    40
		}
franta-hg@1
    41
franta-hg@1
    42
		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
franta-hg@1
    43
			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
franta-hg@1
    44
		}
franta-hg@1
    45
franta-hg@1
    46
franta-hg@1
    47
		if (e.hasProblems()) {
franta-hg@1
    48
			throw e;
franta-hg@1
    49
		}
franta-hg@1
    50
	}
franta-hg@1
    51
franta-hg@1
    52
	public boolean hasSql() {
franta-hg@1
    53
		return isNotEmpty(sql, true);
franta-hg@1
    54
	}
franta-hg@1
    55
franta-hg@1
    56
	public boolean hasDb() {
franta-hg@1
    57
		return isNotEmpty(databaseName, true);
franta-hg@1
    58
	}
franta-hg@1
    59
franta-hg@1
    60
	public boolean hasBatch() {
franta-hg@1
    61
		return batch;
franta-hg@1
    62
	}
franta-hg@1
    63
}