1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 15 22:07:51 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 15 22:44:11 2013 +0100
1.3 @@ -1,129 +1,13 @@
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", "--types", "int,bbb,omfg:int,omg:boolean", "--data", "xxx", ":omfg", "hodnota omfg", ":omg", "true"};
1.36 -
1.37 - CLIOptions options = parseOptions(args);
1.38 - }
1.39 -
1.40 - private static CLIOptions parseOptions(String[] args) {
1.41 - CLIOptions options = new CLIOptions();
1.42 -
1.43 - List<Integer> numberedTypes = new ArrayList<>();
1.44 - Map<String, Integer> namedTypes = new HashMap<>();
1.45 -
1.46 - for (int i = 0; i < args.length; i++) {
1.47 - String arg = args[i];
1.48 - switch (arg) {
1.49 - case Tokens.TYPES:
1.50 - String typesString = args[++i];
1.51 -
1.52 - for (String oneType : typesString.split("\\s*,\\s*")) {
1.53 - int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
1.54 - if (sepatratorIndex == -1) {
1.55 - numberedTypes.add(getType(oneType));
1.56 - } else {
1.57 - String namePart = oneType.substring(0, sepatratorIndex);
1.58 - String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
1.59 - namedTypes.put(namePart, getType(typePart));
1.60 - }
1.61 - }
1.62 - break;
1.63 - case Tokens.NAME_PREFIX:
1.64 - options.setNamePrefix(args[++i]);
1.65 - break;
1.66 - case Tokens.DB:
1.67 - options.setDatabaseName(args[++i]);
1.68 - break;
1.69 - case Tokens.SQL:
1.70 - options.setSql(args[++i]);
1.71 - options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
1.72 - break;
1.73 - case Tokens.SQL_UPDATE:
1.74 - case Tokens.SQL_INSERT:
1.75 - options.setSql(args[++i]);
1.76 - options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
1.77 - break;
1.78 - case Tokens.BATCH:
1.79 - options.setBatch(true);
1.80 - break;
1.81 - case Tokens.DATA: // --data is the last option
1.82 - for (i++; i < args.length; i++) {
1.83 - arg = args[i];
1.84 -
1.85 - if (arg.startsWith(options.getNamePrefix())) {
1.86 - String paramName = arg.substring(options.getNamePrefix().length());
1.87 - String paramValue = args[++i];
1.88 - options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
1.89 - } else {
1.90 - int paramIndex = options.getNumberedParameters().size();
1.91 - int paramType;
1.92 - try {
1.93 - paramType = numberedTypes.get(paramIndex);
1.94 - } catch (IndexOutOfBoundsException e) {
1.95 - throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
1.96 - } catch (NullPointerException e) {
1.97 - throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
1.98 - }
1.99 - options.addNumberedParameter(new Parameter(arg, paramType));
1.100 - }
1.101 - }
1.102 - break;
1.103 - default:
1.104 - throw new IllegalArgumentException("Unknown option: " + arg);
1.105 - }
1.106 - }
1.107 -
1.108 -
1.109 -
1.110 - return options;
1.111 -
1.112 - }
1.113 -
1.114 - public static class Tokens {
1.115 -
1.116 - public static final String DB = "--db";
1.117 - public static final String SQL = "--sql";
1.118 - public static final String SQL_UPDATE = "--sql-update";
1.119 - public static final String SQL_INSERT = "--sql-insert";
1.120 - public static final String BATCH = "--batch";
1.121 - public static final String DATA = "--data";
1.122 - public static final String NAME_PREFIX = "--name-prefix";
1.123 - public static final String TYPES = "--types";
1.124 -
1.125 - private Tokens() {
1.126 - }
1.127 - }
1.128 -
1.129 - private static Integer getType(String typeString) {
1.130 - return types.get(typeString);
1.131 + CLIParser parser = new CLIParser();
1.132 + CLIOptions options = parser.parseOptions(args);
1.133 }
1.134 }