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 4 f5c3350f3d78
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
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 20:25:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 22:07:51 2013 +0100
@@ -1,14 +1,35 @@
 package info.globalcode.sql.dk;
 
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  *
  * @author Ing. František Kučera (frantovo.cz)
  */
 public class CLIStarter {
 
+	public static final String TYPE_NAME_SEPARATOR = ":";
+	private static final Map<String, Integer> types;
+
+	static {
+		Map<String, Integer> m = new HashMap<>();
+		m.put("int", Types.INTEGER);
+		m.put("string", Types.VARCHAR);
+		m.put("boolean", Types.BOOLEAN);
+		/**
+		 * TODO: more types
+		 */
+		types = Collections.unmodifiableMap(m);
+	}
+
 	public static void main(String[] args) {
 
-		args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1"};
+		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"};
 
 		CLIOptions options = parseOptions(args);
 	}
@@ -16,28 +37,29 @@
 	private static CLIOptions parseOptions(String[] args) {
 		CLIOptions options = new CLIOptions();
 
-		String typesString = null;
+		List<Integer> numberedTypes = new ArrayList<>();
+		Map<String, Integer> namedTypes = new HashMap<>();
 
 		for (int i = 0; i < args.length; i++) {
 			String arg = args[i];
 			switch (arg) {
 				case Tokens.TYPES:
-					typesString = args[++i];
+					String typesString = args[++i];
+
+					for (String oneType : typesString.split("\\s*,\\s*")) {
+						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
+						if (sepatratorIndex == -1) {
+							numberedTypes.add(getType(oneType));
+						} else {
+							String namePart = oneType.substring(0, sepatratorIndex);
+							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
+							namedTypes.put(namePart, getType(typePart));
+						}
+					}
 					break;
 				case Tokens.NAME_PREFIX:
 					options.setNamePrefix(args[++i]);
 					break;
-			}
-		}
-
-
-		for (int i = 0; i < args.length; i++) {
-			String arg = args[i];
-			switch (arg) {
-				case Tokens.TYPES:
-				case Tokens.NAME_PREFIX:
-					i++;
-					break;
 				case Tokens.DB:
 					options.setDatabaseName(args[++i]);
 					break;
@@ -53,11 +75,35 @@
 				case Tokens.BATCH:
 					options.setBatch(true);
 					break;
-				case Tokens.DATA:
+				case Tokens.DATA: // --data is the last option
+					for (i++; i < args.length; i++) {
+						arg = args[i];
+
+						if (arg.startsWith(options.getNamePrefix())) {
+							String paramName = arg.substring(options.getNamePrefix().length());
+							String paramValue = args[++i];
+							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
+						} else {
+							int paramIndex = options.getNumberedParameters().size();
+							int paramType;
+							try {
+								paramType = numberedTypes.get(paramIndex);
+							} catch (IndexOutOfBoundsException e) {
+								throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
+							} catch (NullPointerException e) {
+								throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
+							}
+							options.addNumberedParameter(new Parameter(arg, paramType));
+						}
+					}
 					break;
+				default:
+					throw new IllegalArgumentException("Unknown option: " + arg);
 			}
 		}
 
+
+
 		return options;
 
 	}
@@ -76,4 +122,8 @@
 		private Tokens() {
 		}
 	}
+
+	private static Integer getType(String typeString) {
+		return types.get(typeString);
+	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Sun Dec 15 20:25:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Sun Dec 15 22:07:51 2013 +0100
@@ -8,6 +8,11 @@
 
 	private String name;
 
+	public NamedParameter(String name, Object value, Integer type) {
+		super(value, type);
+		this.name = name;
+	}
+
 	public String getName() {
 		return name;
 	}
--- a/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Sun Dec 15 20:25:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Sun Dec 15 22:07:51 2013 +0100
@@ -1,14 +1,32 @@
 package info.globalcode.sql.dk;
 
+import java.sql.Types;
+
 /**
  *
  * @author Ing. František Kučera (frantovo.cz)
  */
 public class Parameter {
 
+	/**
+	 * @see Types
+	 */
+	public static final int DEFAULT_TYPE = Types.VARCHAR;
 	private Object value;
 	private int type;
 
+	public Parameter() {
+	}
+
+	public Parameter(Object value, Integer type) {
+		this.value = value;
+		if (type == null) {
+			this.type = DEFAULT_TYPE;
+		} else {
+			this.type = type;
+		}
+	}
+
 	public Object getValue() {
 		return value;
 	}