more OOP v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 22:44:11 +0100
branchv_0
changeset 526223eb63851
parent 4 f5c3350f3d78
child 6 c79a5b030e99
more OOP
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Sun Dec 15 22:44:11 2013 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +package info.globalcode.sql.dk;
     1.5 +
     1.6 +import java.sql.Types;
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.Collections;
     1.9 +import java.util.HashMap;
    1.10 +import java.util.List;
    1.11 +import java.util.Map;
    1.12 +
    1.13 +/**
    1.14 + *
    1.15 + * @author Ing. František Kučera (frantovo.cz)
    1.16 + */
    1.17 +public class CLIParser {
    1.18 +
    1.19 +	public static final String TYPE_NAME_SEPARATOR = ":";
    1.20 +	private final Map<String, Integer> types;
    1.21 +
    1.22 +	public CLIParser() {
    1.23 +		Map<String, Integer> m = new HashMap<>();
    1.24 +		m.put("int", Types.INTEGER);
    1.25 +		m.put("string", Types.VARCHAR);
    1.26 +		m.put("boolean", Types.BOOLEAN);
    1.27 +		/**
    1.28 +		 * TODO: more types
    1.29 +		 */
    1.30 +		types = Collections.unmodifiableMap(m);
    1.31 +	}
    1.32 +
    1.33 +	public CLIOptions parseOptions(String[] args) {
    1.34 +		CLIOptions options = new CLIOptions();
    1.35 +
    1.36 +		List<Integer> numberedTypes = new ArrayList<>();
    1.37 +		Map<String, Integer> namedTypes = new HashMap<>();
    1.38 +
    1.39 +		for (int i = 0; i < args.length; i++) {
    1.40 +			String arg = args[i];
    1.41 +			switch (arg) {
    1.42 +				case Tokens.TYPES:
    1.43 +					String typesString = args[++i];
    1.44 +
    1.45 +					for (String oneType : typesString.split("\\s*,\\s*")) {
    1.46 +						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    1.47 +						if (sepatratorIndex == -1) {
    1.48 +							numberedTypes.add(getType(oneType));
    1.49 +						} else {
    1.50 +							String namePart = oneType.substring(0, sepatratorIndex);
    1.51 +							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    1.52 +							namedTypes.put(namePart, getType(typePart));
    1.53 +						}
    1.54 +					}
    1.55 +					break;
    1.56 +				case Tokens.NAME_PREFIX:
    1.57 +					options.setNamePrefix(args[++i]);
    1.58 +					break;
    1.59 +				case Tokens.DB:
    1.60 +					options.setDatabaseName(args[++i]);
    1.61 +					break;
    1.62 +				case Tokens.SQL:
    1.63 +					options.setSql(args[++i]);
    1.64 +					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
    1.65 +					break;
    1.66 +				case Tokens.SQL_UPDATE:
    1.67 +				case Tokens.SQL_INSERT:
    1.68 +					options.setSql(args[++i]);
    1.69 +					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
    1.70 +					break;
    1.71 +				case Tokens.BATCH:
    1.72 +					options.setBatch(true);
    1.73 +					break;
    1.74 +				case Tokens.DATA: // --data is the last option
    1.75 +					for (i++; i < args.length; i++) {
    1.76 +						arg = args[i];
    1.77 +
    1.78 +						if (arg.startsWith(options.getNamePrefix())) {
    1.79 +							String paramName = arg.substring(options.getNamePrefix().length());
    1.80 +							String paramValue = args[++i];
    1.81 +							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    1.82 +						} else {
    1.83 +							int paramIndex = options.getNumberedParameters().size();
    1.84 +							int paramType;
    1.85 +							try {
    1.86 +								paramType = numberedTypes.get(paramIndex);
    1.87 +							} catch (IndexOutOfBoundsException e) {
    1.88 +								throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    1.89 +							} catch (NullPointerException e) {
    1.90 +								throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    1.91 +							}
    1.92 +							options.addNumberedParameter(new Parameter(arg, paramType));
    1.93 +						}
    1.94 +					}
    1.95 +					break;
    1.96 +				default:
    1.97 +					throw new IllegalArgumentException("Unknown option: " + arg);
    1.98 +			}
    1.99 +		}
   1.100 +
   1.101 +
   1.102 +
   1.103 +		return options;
   1.104 +
   1.105 +	}
   1.106 +
   1.107 +	public static class Tokens {
   1.108 +
   1.109 +		public static final String DB = "--db";
   1.110 +		public static final String SQL = "--sql";
   1.111 +		public static final String SQL_UPDATE = "--sql-update";
   1.112 +		public static final String SQL_INSERT = "--sql-insert";
   1.113 +		public static final String BATCH = "--batch";
   1.114 +		public static final String DATA = "--data";
   1.115 +		public static final String NAME_PREFIX = "--name-prefix";
   1.116 +		public static final String TYPES = "--types";
   1.117 +
   1.118 +		private Tokens() {
   1.119 +		}
   1.120 +	}
   1.121 +
   1.122 +	private Integer getType(String typeString) {
   1.123 +		return types.get(typeString);
   1.124 +	}
   1.125 +}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 22:07:51 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 22:44:11 2013 +0100
     2.3 @@ -1,129 +1,13 @@
     2.4  package info.globalcode.sql.dk;
     2.5  
     2.6 -import java.sql.Types;
     2.7 -import java.util.ArrayList;
     2.8 -import java.util.Collections;
     2.9 -import java.util.HashMap;
    2.10 -import java.util.List;
    2.11 -import java.util.Map;
    2.12 -
    2.13  /**
    2.14   *
    2.15   * @author Ing. František Kučera (frantovo.cz)
    2.16   */
    2.17  public class CLIStarter {
    2.18  
    2.19 -	public static final String TYPE_NAME_SEPARATOR = ":";
    2.20 -	private static final Map<String, Integer> types;
    2.21 -
    2.22 -	static {
    2.23 -		Map<String, Integer> m = new HashMap<>();
    2.24 -		m.put("int", Types.INTEGER);
    2.25 -		m.put("string", Types.VARCHAR);
    2.26 -		m.put("boolean", Types.BOOLEAN);
    2.27 -		/**
    2.28 -		 * TODO: more types
    2.29 -		 */
    2.30 -		types = Collections.unmodifiableMap(m);
    2.31 -	}
    2.32 -
    2.33  	public static void main(String[] args) {
    2.34 -
    2.35 -		args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1", "--types", "int,bbb,omfg:int,omg:boolean", "--data", "xxx", ":omfg", "hodnota omfg", ":omg", "true"};
    2.36 -
    2.37 -		CLIOptions options = parseOptions(args);
    2.38 -	}
    2.39 -
    2.40 -	private static CLIOptions parseOptions(String[] args) {
    2.41 -		CLIOptions options = new CLIOptions();
    2.42 -
    2.43 -		List<Integer> numberedTypes = new ArrayList<>();
    2.44 -		Map<String, Integer> namedTypes = new HashMap<>();
    2.45 -
    2.46 -		for (int i = 0; i < args.length; i++) {
    2.47 -			String arg = args[i];
    2.48 -			switch (arg) {
    2.49 -				case Tokens.TYPES:
    2.50 -					String typesString = args[++i];
    2.51 -
    2.52 -					for (String oneType : typesString.split("\\s*,\\s*")) {
    2.53 -						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    2.54 -						if (sepatratorIndex == -1) {
    2.55 -							numberedTypes.add(getType(oneType));
    2.56 -						} else {
    2.57 -							String namePart = oneType.substring(0, sepatratorIndex);
    2.58 -							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    2.59 -							namedTypes.put(namePart, getType(typePart));
    2.60 -						}
    2.61 -					}
    2.62 -					break;
    2.63 -				case Tokens.NAME_PREFIX:
    2.64 -					options.setNamePrefix(args[++i]);
    2.65 -					break;
    2.66 -				case Tokens.DB:
    2.67 -					options.setDatabaseName(args[++i]);
    2.68 -					break;
    2.69 -				case Tokens.SQL:
    2.70 -					options.setSql(args[++i]);
    2.71 -					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
    2.72 -					break;
    2.73 -				case Tokens.SQL_UPDATE:
    2.74 -				case Tokens.SQL_INSERT:
    2.75 -					options.setSql(args[++i]);
    2.76 -					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
    2.77 -					break;
    2.78 -				case Tokens.BATCH:
    2.79 -					options.setBatch(true);
    2.80 -					break;
    2.81 -				case Tokens.DATA: // --data is the last option
    2.82 -					for (i++; i < args.length; i++) {
    2.83 -						arg = args[i];
    2.84 -
    2.85 -						if (arg.startsWith(options.getNamePrefix())) {
    2.86 -							String paramName = arg.substring(options.getNamePrefix().length());
    2.87 -							String paramValue = args[++i];
    2.88 -							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    2.89 -						} else {
    2.90 -							int paramIndex = options.getNumberedParameters().size();
    2.91 -							int paramType;
    2.92 -							try {
    2.93 -								paramType = numberedTypes.get(paramIndex);
    2.94 -							} catch (IndexOutOfBoundsException e) {
    2.95 -								throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    2.96 -							} catch (NullPointerException e) {
    2.97 -								throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    2.98 -							}
    2.99 -							options.addNumberedParameter(new Parameter(arg, paramType));
   2.100 -						}
   2.101 -					}
   2.102 -					break;
   2.103 -				default:
   2.104 -					throw new IllegalArgumentException("Unknown option: " + arg);
   2.105 -			}
   2.106 -		}
   2.107 -
   2.108 -
   2.109 -
   2.110 -		return options;
   2.111 -
   2.112 -	}
   2.113 -
   2.114 -	public static class Tokens {
   2.115 -
   2.116 -		public static final String DB = "--db";
   2.117 -		public static final String SQL = "--sql";
   2.118 -		public static final String SQL_UPDATE = "--sql-update";
   2.119 -		public static final String SQL_INSERT = "--sql-insert";
   2.120 -		public static final String BATCH = "--batch";
   2.121 -		public static final String DATA = "--data";
   2.122 -		public static final String NAME_PREFIX = "--name-prefix";
   2.123 -		public static final String TYPES = "--types";
   2.124 -
   2.125 -		private Tokens() {
   2.126 -		}
   2.127 -	}
   2.128 -
   2.129 -	private static Integer getType(String typeString) {
   2.130 -		return types.get(typeString);
   2.131 +		CLIParser parser = new CLIParser();
   2.132 +		CLIOptions options = parser.parseOptions(args);
   2.133  	}
   2.134  }