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

package info.globalcode.sql.dk;

import static info.globalcode.sql.dk.Functions.isNotEmpty;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class CLIOptions {

	private String sql;
	private String databaseName;
	private boolean batch;
	

	public enum COMMAND_TYPE {

		/** SELECT */
		QUERY,
		/** INSERT, UPDATE, DELETE */
		UPDATE
	};
	private COMMAND_TYPE commandType;
	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
	private final List<Parameter> numberedParameters = new ArrayList<>();

	public void validate() throws InvalidOptionsException {
		InvalidOptionsException e = new InvalidOptionsException();

		if ( //
				(hasDb() ? 1 : 0) + //
				(hasSql() ? 1 : 0) + //
				(hasBatch() ? 1 : 0)
				!= 2) //
		{
			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
		}

		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
		}


		if (e.hasProblems()) {
			throw e;
		}
	}

	public boolean hasSql() {
		return isNotEmpty(sql, true);
	}

	public boolean hasDb() {
		return isNotEmpty(databaseName, true);
	}

	public boolean hasBatch() {
		return batch;
	}
}