WOW some classes LOL; TODO: refactor v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 19:20:50 +0100
branchv_0
changeset 1f32dac78d13a
parent 0 29df3b2e34df
child 2 72da10f632b5
WOW some classes LOL; TODO: refactor
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java
java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java
java/sql-dk/src/info/globalcode/sql/dk/Parameter.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommand.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Sun Dec 15 19:20:50 2013 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +package info.globalcode.sql.dk;
     1.5 +
     1.6 +import static info.globalcode.sql.dk.Functions.isNotEmpty;
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.Collection;
     1.9 +import java.util.List;
    1.10 +
    1.11 +/**
    1.12 + *
    1.13 + * @author Ing. František Kučera (frantovo.cz)
    1.14 + */
    1.15 +public class CLIOptions {
    1.16 +
    1.17 +	private String sql;
    1.18 +	private String databaseName;
    1.19 +	private boolean batch;
    1.20 +	
    1.21 +
    1.22 +	public enum COMMAND_TYPE {
    1.23 +
    1.24 +		/** SELECT */
    1.25 +		QUERY,
    1.26 +		/** INSERT, UPDATE, DELETE */
    1.27 +		UPDATE
    1.28 +	};
    1.29 +	private COMMAND_TYPE commandType;
    1.30 +	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
    1.31 +	private final List<Parameter> numberedParameters = new ArrayList<>();
    1.32 +
    1.33 +	public void validate() throws InvalidOptionsException {
    1.34 +		InvalidOptionsException e = new InvalidOptionsException();
    1.35 +
    1.36 +		if ( //
    1.37 +				(hasDb() ? 1 : 0) + //
    1.38 +				(hasSql() ? 1 : 0) + //
    1.39 +				(hasBatch() ? 1 : 0)
    1.40 +				!= 2) //
    1.41 +		{
    1.42 +			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    1.43 +		}
    1.44 +
    1.45 +		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
    1.46 +			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
    1.47 +		}
    1.48 +
    1.49 +
    1.50 +		if (e.hasProblems()) {
    1.51 +			throw e;
    1.52 +		}
    1.53 +	}
    1.54 +
    1.55 +	public boolean hasSql() {
    1.56 +		return isNotEmpty(sql, true);
    1.57 +	}
    1.58 +
    1.59 +	public boolean hasDb() {
    1.60 +		return isNotEmpty(databaseName, true);
    1.61 +	}
    1.62 +
    1.63 +	public boolean hasBatch() {
    1.64 +		return batch;
    1.65 +	}
    1.66 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 19:20:50 2013 +0100
     2.3 @@ -0,0 +1,58 @@
     2.4 +package info.globalcode.sql.dk;
     2.5 +
     2.6 +/**
     2.7 + *
     2.8 + * @author Ing. František Kučera (frantovo.cz)
     2.9 + */
    2.10 +public class CLIStarter {
    2.11 +
    2.12 +	public static void main(String[] args) {
    2.13 +		
    2.14 +		args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1"};
    2.15 +		
    2.16 +		CLIOptions options = parseOptions(args);
    2.17 +	}
    2.18 +
    2.19 +	private static CLIOptions parseOptions(String[] args) {
    2.20 +		CLIOptions options = new CLIOptions();
    2.21 +
    2.22 +		for (int i = 0; i < args.length; i++) {
    2.23 +			String arg = args[i];
    2.24 +
    2.25 +			switch (arg) {
    2.26 +				case Tokens.DB:
    2.27 +					String db = args[++i];
    2.28 +					System.out.println("DB: " + db);
    2.29 +					break;
    2.30 +				case Tokens.SQL:
    2.31 +					String sql = args[++i];
    2.32 +					System.out.println("SQL: " + sql);
    2.33 +					break;
    2.34 +				case Tokens.SQL_UPDATE:
    2.35 +					break;
    2.36 +				case Tokens.BATCH:
    2.37 +					break;
    2.38 +				case Tokens.DATA:
    2.39 +					break;
    2.40 +				case Tokens.TYPES:
    2.41 +					break;
    2.42 +			}
    2.43 +		}
    2.44 +
    2.45 +		return options;
    2.46 +
    2.47 +	}
    2.48 +
    2.49 +	public static class Tokens {
    2.50 +
    2.51 +		public static final String DB = "--db";
    2.52 +		public static final String SQL = "--sql";
    2.53 +		public static final String SQL_UPDATE = "--sql-update";
    2.54 +		public static final String BATCH = "--batch";
    2.55 +		public static final String DATA = "--data";
    2.56 +		public static final String TYPES = "--types";
    2.57 +
    2.58 +		private Tokens() {
    2.59 +		}
    2.60 +	}
    2.61 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sun Dec 15 19:20:50 2013 +0100
     3.3 @@ -0,0 +1,70 @@
     3.4 +package info.globalcode.sql.dk;
     3.5 +
     3.6 +import java.util.ArrayList;
     3.7 +import java.util.Collection;
     3.8 +import java.util.Map;
     3.9 +
    3.10 +/**
    3.11 + *
    3.12 + * @author Ing. František Kučera (frantovo.cz)
    3.13 + */
    3.14 +public class Functions {
    3.15 +
    3.16 +	private Functions() {
    3.17 +	}
    3.18 +
    3.19 +	public static boolean equalz(Object a, Object b) {
    3.20 +		return a == null ? b == null : a.equals(b);
    3.21 +	}
    3.22 +
    3.23 +	/**
    3.24 +	 *
    3.25 +	 * @param text String to be examinated
    3.26 +	 * @param trim whether text should be trimmed before examination
    3.27 +	 * @return whether text is not empty and one or more characters long (after prospective trim)
    3.28 +	 */
    3.29 +	public static boolean isEmpty(String text, boolean trim) {
    3.30 +		if (text == null) {
    3.31 +			return true;
    3.32 +		} else {
    3.33 +			if (trim) {
    3.34 +				text = text.trim();
    3.35 +			}
    3.36 +			return text.isEmpty();
    3.37 +		}
    3.38 +	}
    3.39 +
    3.40 +	/**
    3.41 +	 * @see #isEmpty(java.lang.String, boolean)
    3.42 +	 */
    3.43 +	public static boolean isNotEmpty(String text, boolean trim) {
    3.44 +		return !isEmpty(text, trim);
    3.45 +	}
    3.46 +
    3.47 +	public boolean isEmpty(Collection c) {
    3.48 +		return c == null || c.isEmpty();
    3.49 +	}
    3.50 +
    3.51 +	public boolean isNotEmpty(Collection c) {
    3.52 +		return !isEmpty(c);
    3.53 +	}
    3.54 +
    3.55 +	public boolean isEmpty(Map m) {
    3.56 +		return m == null || m.isEmpty();
    3.57 +	}
    3.58 +
    3.59 +	public boolean isNotEmpty(Map m) {
    3.60 +		return !isEmpty(m);
    3.61 +	}
    3.62 +
    3.63 +	/**
    3.64 +	 * @return empty collection if given one is null | or the original one
    3.65 +	 */
    3.66 +	public static <T> Collection<T> notNull(Collection<T> c) {
    3.67 +		if (c == null) {
    3.68 +			return new ArrayList<>();
    3.69 +		} else {
    3.70 +			return c;
    3.71 +		}
    3.72 +	}
    3.73 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java	Sun Dec 15 19:20:50 2013 +0100
     4.3 @@ -0,0 +1,39 @@
     4.4 +package info.globalcode.sql.dk;
     4.5 +
     4.6 +import java.util.ArrayList;
     4.7 +import java.util.Collection;
     4.8 +import java.util.Collections;
     4.9 +
    4.10 +/**
    4.11 + *
    4.12 + * @author Ing. František Kučera (frantovo.cz)
    4.13 + */
    4.14 +public class InvalidOptionsException extends Exception {
    4.15 +
    4.16 +	private final Collection<OptionProblem> problems = new ArrayList<>();
    4.17 +
    4.18 +	public Collection<OptionProblem> getProblems() {
    4.19 +		return Collections.unmodifiableCollection(problems);
    4.20 +	}
    4.21 +
    4.22 +	public void addProblem(OptionProblem p) {
    4.23 +		problems.add(p);
    4.24 +	}
    4.25 +
    4.26 +	public boolean hasProblems() {
    4.27 +		return !problems.isEmpty();
    4.28 +	}
    4.29 +
    4.30 +	public static class OptionProblem {
    4.31 +
    4.32 +		private String description;
    4.33 +
    4.34 +		public OptionProblem(String description) {
    4.35 +			this.description = description;
    4.36 +		}
    4.37 +
    4.38 +		public String getDescription() {
    4.39 +			return description;
    4.40 +		}
    4.41 +	}
    4.42 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Sun Dec 15 19:20:50 2013 +0100
     5.3 @@ -0,0 +1,18 @@
     5.4 +package info.globalcode.sql.dk;
     5.5 +
     5.6 +/**
     5.7 + *
     5.8 + * @author Ing. František Kučera (frantovo.cz)
     5.9 + */
    5.10 +public class NamedParameter extends Parameter {
    5.11 +
    5.12 +	private String name;
    5.13 +
    5.14 +	public String getName() {
    5.15 +		return name;
    5.16 +	}
    5.17 +
    5.18 +	public void setName(String name) {
    5.19 +		this.name = name;
    5.20 +	}
    5.21 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Sun Dec 15 19:20:50 2013 +0100
     6.3 @@ -0,0 +1,33 @@
     6.4 +package info.globalcode.sql.dk;
     6.5 +
     6.6 +/**
     6.7 + *
     6.8 + * @author Ing. František Kučera (frantovo.cz)
     6.9 + */
    6.10 +public class Parameter {
    6.11 +
    6.12 +	private Object value;
    6.13 +	private int type;
    6.14 +
    6.15 +	public Object getValue() {
    6.16 +		return value;
    6.17 +	}
    6.18 +
    6.19 +	public void setValue(Object value) {
    6.20 +		this.value = value;
    6.21 +	}
    6.22 +
    6.23 +	/**
    6.24 +	 * @see java.sql.Types
    6.25 +	 */
    6.26 +	public int getType() {
    6.27 +		return type;
    6.28 +	}
    6.29 +
    6.30 +	/**
    6.31 +	 * @see java.sql.Types
    6.32 +	 */
    6.33 +	public void setType(int type) {
    6.34 +		this.type = type;
    6.35 +	}
    6.36 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommand.java	Sun Dec 15 19:20:50 2013 +0100
     7.3 @@ -0,0 +1,17 @@
     7.4 +package info.globalcode.sql.dk;
     7.5 +
     7.6 +import java.sql.Connection;
     7.7 +import java.sql.PreparedStatement;
     7.8 +
     7.9 +/**
    7.10 + *
    7.11 + * @author Ing. František Kučera (frantovo.cz)
    7.12 + */
    7.13 +public abstract class SQLCommand {
    7.14 +
    7.15 +	private String query;
    7.16 +
    7.17 +	public abstract PreparedStatement prepareStatement(Connection c);
    7.18 +
    7.19 +	public abstract void parametrize(PreparedStatement ps);
    7.20 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Sun Dec 15 19:20:50 2013 +0100
     8.3 @@ -0,0 +1,21 @@
     8.4 +package info.globalcode.sql.dk;
     8.5 +
     8.6 +import java.sql.Connection;
     8.7 +import java.sql.PreparedStatement;
     8.8 +
     8.9 +/**
    8.10 + *
    8.11 + * @author Ing. František Kučera (frantovo.cz)
    8.12 + */
    8.13 +public class SQLCommandNamed extends SQLCommand {
    8.14 +
    8.15 +	@Override
    8.16 +	public PreparedStatement prepareStatement(Connection c) {
    8.17 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    8.18 +	}
    8.19 +
    8.20 +	@Override
    8.21 +	public void parametrize(PreparedStatement ps) {
    8.22 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    8.23 +	}
    8.24 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java	Sun Dec 15 19:20:50 2013 +0100
     9.3 @@ -0,0 +1,21 @@
     9.4 +package info.globalcode.sql.dk;
     9.5 +
     9.6 +import java.sql.Connection;
     9.7 +import java.sql.PreparedStatement;
     9.8 +
     9.9 +/**
    9.10 + *
    9.11 + * @author Ing. František Kučera (frantovo.cz)
    9.12 + */
    9.13 +public class SQLCommandNumbered extends SQLCommand {
    9.14 +
    9.15 +	@Override
    9.16 +	public PreparedStatement prepareStatement(Connection c) {
    9.17 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    9.18 +	}
    9.19 +
    9.20 +	@Override
    9.21 +	public void parametrize(PreparedStatement ps) {
    9.22 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    9.23 +	}
    9.24 +}