data/types CLI options parsing v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 22:07:51 +0100
branchv_0
changeset 4f5c3350f3d78
parent 3 efdf2b886feb
child 5 26223eb63851
data/types CLI options parsing
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java
java/sql-dk/src/info/globalcode/sql/dk/Parameter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 20:25:15 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 22:07:51 2013 +0100
     1.3 @@ -1,14 +1,35 @@
     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 CLIStarter {
    1.18  
    1.19 +	public static final String TYPE_NAME_SEPARATOR = ":";
    1.20 +	private static final Map<String, Integer> types;
    1.21 +
    1.22 +	static {
    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 static void main(String[] args) {
    1.34  
    1.35 -		args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1"};
    1.36 +		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"};
    1.37  
    1.38  		CLIOptions options = parseOptions(args);
    1.39  	}
    1.40 @@ -16,28 +37,29 @@
    1.41  	private static CLIOptions parseOptions(String[] args) {
    1.42  		CLIOptions options = new CLIOptions();
    1.43  
    1.44 -		String typesString = null;
    1.45 +		List<Integer> numberedTypes = new ArrayList<>();
    1.46 +		Map<String, Integer> namedTypes = new HashMap<>();
    1.47  
    1.48  		for (int i = 0; i < args.length; i++) {
    1.49  			String arg = args[i];
    1.50  			switch (arg) {
    1.51  				case Tokens.TYPES:
    1.52 -					typesString = args[++i];
    1.53 +					String typesString = args[++i];
    1.54 +
    1.55 +					for (String oneType : typesString.split("\\s*,\\s*")) {
    1.56 +						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    1.57 +						if (sepatratorIndex == -1) {
    1.58 +							numberedTypes.add(getType(oneType));
    1.59 +						} else {
    1.60 +							String namePart = oneType.substring(0, sepatratorIndex);
    1.61 +							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    1.62 +							namedTypes.put(namePart, getType(typePart));
    1.63 +						}
    1.64 +					}
    1.65  					break;
    1.66  				case Tokens.NAME_PREFIX:
    1.67  					options.setNamePrefix(args[++i]);
    1.68  					break;
    1.69 -			}
    1.70 -		}
    1.71 -
    1.72 -
    1.73 -		for (int i = 0; i < args.length; i++) {
    1.74 -			String arg = args[i];
    1.75 -			switch (arg) {
    1.76 -				case Tokens.TYPES:
    1.77 -				case Tokens.NAME_PREFIX:
    1.78 -					i++;
    1.79 -					break;
    1.80  				case Tokens.DB:
    1.81  					options.setDatabaseName(args[++i]);
    1.82  					break;
    1.83 @@ -53,11 +75,35 @@
    1.84  				case Tokens.BATCH:
    1.85  					options.setBatch(true);
    1.86  					break;
    1.87 -				case Tokens.DATA:
    1.88 +				case Tokens.DATA: // --data is the last option
    1.89 +					for (i++; i < args.length; i++) {
    1.90 +						arg = args[i];
    1.91 +
    1.92 +						if (arg.startsWith(options.getNamePrefix())) {
    1.93 +							String paramName = arg.substring(options.getNamePrefix().length());
    1.94 +							String paramValue = args[++i];
    1.95 +							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    1.96 +						} else {
    1.97 +							int paramIndex = options.getNumberedParameters().size();
    1.98 +							int paramType;
    1.99 +							try {
   1.100 +								paramType = numberedTypes.get(paramIndex);
   1.101 +							} catch (IndexOutOfBoundsException e) {
   1.102 +								throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
   1.103 +							} catch (NullPointerException e) {
   1.104 +								throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
   1.105 +							}
   1.106 +							options.addNumberedParameter(new Parameter(arg, paramType));
   1.107 +						}
   1.108 +					}
   1.109  					break;
   1.110 +				default:
   1.111 +					throw new IllegalArgumentException("Unknown option: " + arg);
   1.112  			}
   1.113  		}
   1.114  
   1.115 +
   1.116 +
   1.117  		return options;
   1.118  
   1.119  	}
   1.120 @@ -76,4 +122,8 @@
   1.121  		private Tokens() {
   1.122  		}
   1.123  	}
   1.124 +
   1.125 +	private static Integer getType(String typeString) {
   1.126 +		return types.get(typeString);
   1.127 +	}
   1.128  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Sun Dec 15 20:25:15 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Sun Dec 15 22:07:51 2013 +0100
     2.3 @@ -8,6 +8,11 @@
     2.4  
     2.5  	private String name;
     2.6  
     2.7 +	public NamedParameter(String name, Object value, Integer type) {
     2.8 +		super(value, type);
     2.9 +		this.name = name;
    2.10 +	}
    2.11 +
    2.12  	public String getName() {
    2.13  		return name;
    2.14  	}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Sun Dec 15 20:25:15 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Sun Dec 15 22:07:51 2013 +0100
     3.3 @@ -1,14 +1,32 @@
     3.4  package info.globalcode.sql.dk;
     3.5  
     3.6 +import java.sql.Types;
     3.7 +
     3.8  /**
     3.9   *
    3.10   * @author Ing. František Kučera (frantovo.cz)
    3.11   */
    3.12  public class Parameter {
    3.13  
    3.14 +	/**
    3.15 +	 * @see Types
    3.16 +	 */
    3.17 +	public static final int DEFAULT_TYPE = Types.VARCHAR;
    3.18  	private Object value;
    3.19  	private int type;
    3.20  
    3.21 +	public Parameter() {
    3.22 +	}
    3.23 +
    3.24 +	public Parameter(Object value, Integer type) {
    3.25 +		this.value = value;
    3.26 +		if (type == null) {
    3.27 +			this.type = DEFAULT_TYPE;
    3.28 +		} else {
    3.29 +			this.type = type;
    3.30 +		}
    3.31 +	}
    3.32 +
    3.33  	public Object getValue() {
    3.34  		return value;
    3.35  	}