# HG changeset patch # User František Kučera # Date 1387131650 -3600 # Node ID f32dac78d13a7c15ea0e6bfe0421b0c975e1d10a # Parent 29df3b2e34dfbd0aa7e6c8eda973b58ea76ce5ce WOW some classes LOL; TODO: refactor diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,63 @@ +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 namedParameters = new ArrayList<>(); + private final List 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; + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,58 @@ +package info.globalcode.sql.dk; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIStarter { + + public static void main(String[] args) { + + args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1"}; + + CLIOptions options = parseOptions(args); + } + + private static CLIOptions parseOptions(String[] args) { + CLIOptions options = new CLIOptions(); + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + + switch (arg) { + case Tokens.DB: + String db = args[++i]; + System.out.println("DB: " + db); + break; + case Tokens.SQL: + String sql = args[++i]; + System.out.println("SQL: " + sql); + break; + case Tokens.SQL_UPDATE: + break; + case Tokens.BATCH: + break; + case Tokens.DATA: + break; + case Tokens.TYPES: + break; + } + } + + return options; + + } + + public static class Tokens { + + public static final String DB = "--db"; + public static final String SQL = "--sql"; + public static final String SQL_UPDATE = "--sql-update"; + public static final String BATCH = "--batch"; + public static final String DATA = "--data"; + public static final String TYPES = "--types"; + + private Tokens() { + } + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/Functions.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,70 @@ +package info.globalcode.sql.dk; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Functions { + + private Functions() { + } + + public static boolean equalz(Object a, Object b) { + return a == null ? b == null : a.equals(b); + } + + /** + * + * @param text String to be examinated + * @param trim whether text should be trimmed before examination + * @return whether text is not empty and one or more characters long (after prospective trim) + */ + public static boolean isEmpty(String text, boolean trim) { + if (text == null) { + return true; + } else { + if (trim) { + text = text.trim(); + } + return text.isEmpty(); + } + } + + /** + * @see #isEmpty(java.lang.String, boolean) + */ + public static boolean isNotEmpty(String text, boolean trim) { + return !isEmpty(text, trim); + } + + public boolean isEmpty(Collection c) { + return c == null || c.isEmpty(); + } + + public boolean isNotEmpty(Collection c) { + return !isEmpty(c); + } + + public boolean isEmpty(Map m) { + return m == null || m.isEmpty(); + } + + public boolean isNotEmpty(Map m) { + return !isEmpty(m); + } + + /** + * @return empty collection if given one is null | or the original one + */ + public static Collection notNull(Collection c) { + if (c == null) { + return new ArrayList<>(); + } else { + return c; + } + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,39 @@ +package info.globalcode.sql.dk; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class InvalidOptionsException extends Exception { + + private final Collection problems = new ArrayList<>(); + + public Collection getProblems() { + return Collections.unmodifiableCollection(problems); + } + + public void addProblem(OptionProblem p) { + problems.add(p); + } + + public boolean hasProblems() { + return !problems.isEmpty(); + } + + public static class OptionProblem { + + private String description; + + public OptionProblem(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,18 @@ +package info.globalcode.sql.dk; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class NamedParameter extends Parameter { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/Parameter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,33 @@ +package info.globalcode.sql.dk; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Parameter { + + private Object value; + private int type; + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + /** + * @see java.sql.Types + */ + public int getType() { + return type; + } + + /** + * @see java.sql.Types + */ + public void setType(int type) { + this.type = type; + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/SQLCommand.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommand.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,17 @@ +package info.globalcode.sql.dk; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public abstract class SQLCommand { + + private String query; + + public abstract PreparedStatement prepareStatement(Connection c); + + public abstract void parametrize(PreparedStatement ps); +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,21 @@ +package info.globalcode.sql.dk; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class SQLCommandNamed extends SQLCommand { + + @Override + public PreparedStatement prepareStatement(Connection c) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void parametrize(PreparedStatement ps) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } +} diff -r 29df3b2e34df -r f32dac78d13a java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java Sun Dec 15 19:20:50 2013 +0100 @@ -0,0 +1,21 @@ +package info.globalcode.sql.dk; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class SQLCommandNumbered extends SQLCommand { + + @Override + public PreparedStatement prepareStatement(Connection c) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void parametrize(PreparedStatement ps) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } +}