java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 23:54:51 +0100
branchv_0
changeset 8 4507cb9a0cf1
parent 5 26223eb63851
child 9 2ec52027b97f
permissions -rw-r--r--
more exceptions
     1 package info.globalcode.sql.dk;
     2 
     3 import java.sql.Types;
     4 import java.util.ArrayList;
     5 import java.util.Collections;
     6 import java.util.HashMap;
     7 import java.util.List;
     8 import java.util.Map;
     9 
    10 /**
    11  *
    12  * @author Ing. František Kučera (frantovo.cz)
    13  */
    14 public class CLIParser {
    15 
    16 	public static final String TYPE_NAME_SEPARATOR = ":";
    17 	private final Map<String, Integer> types;
    18 
    19 	public CLIParser() {
    20 		Map<String, Integer> m = new HashMap<>();
    21 		m.put("int", Types.INTEGER);
    22 		m.put("string", Types.VARCHAR);
    23 		m.put("boolean", Types.BOOLEAN);
    24 		/**
    25 		 * TODO: more types
    26 		 */
    27 		types = Collections.unmodifiableMap(m);
    28 	}
    29 
    30 	public CLIOptions parseOptions(String[] args) {
    31 		CLIOptions options = new CLIOptions();
    32 
    33 		List<Integer> numberedTypes = new ArrayList<>();
    34 		Map<String, Integer> namedTypes = new HashMap<>();
    35 
    36 		for (int i = 0; i < args.length; i++) {
    37 			String arg = args[i];
    38 			switch (arg) {
    39 				case Tokens.TYPES:
    40 					String typesString = fetchNext(args, ++i);
    41 
    42 					for (String oneType : typesString.split("\\s*,\\s*")) {
    43 						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    44 						if (sepatratorIndex == -1) {
    45 							numberedTypes.add(getType(oneType));
    46 						} else {
    47 							String namePart = oneType.substring(0, sepatratorIndex);
    48 							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    49 							namedTypes.put(namePart, getType(typePart));
    50 						}
    51 					}
    52 					break;
    53 				case Tokens.NAME_PREFIX:
    54 					options.setNamePrefix(fetchNext(args, ++i));
    55 					break;
    56 				case Tokens.DB:
    57 					options.setDatabaseName(fetchNext(args, ++i));
    58 					break;
    59 				case Tokens.SQL:
    60 					options.setSql(fetchNext(args, ++i));
    61 					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
    62 					break;
    63 				case Tokens.SQL_UPDATE:
    64 				case Tokens.SQL_INSERT:
    65 					options.setSql(fetchNext(args, ++i));
    66 					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
    67 					break;
    68 				case Tokens.BATCH:
    69 					options.setBatch(true);
    70 					break;
    71 				case Tokens.DATA: // --data is the last option
    72 					for (i++; i < args.length; i++) {
    73 						arg = args[i];
    74 
    75 						if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
    76 							String paramName = arg.substring(options.getNamePrefix().length());
    77 							String paramValue = fetchNext(args, ++i);
    78 							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    79 						} else { // Numbered parameters:
    80 							Parameter parameter;
    81 							if (numberedTypes.isEmpty()) {
    82 								parameter = new Parameter(arg, null);
    83 							} else {
    84 								int paramIndex = options.getNumberedParameters().size();
    85 								int paramType;
    86 								try {
    87 									paramType = numberedTypes.get(paramIndex);
    88 								} catch (IndexOutOfBoundsException e) {
    89 									throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    90 								} catch (NullPointerException e) {
    91 									throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    92 								}
    93 								parameter = new Parameter(arg, paramType);
    94 							}
    95 							options.addNumberedParameter(parameter);
    96 						}
    97 					}
    98 					break;
    99 				default:
   100 					throw new IllegalArgumentException("Unknown option: " + arg);
   101 			}
   102 		}
   103 		return options;
   104 	}
   105 
   106 	private String fetchNext(String[] args, int index) {
   107 		if (index < args.length) {
   108 			return args[index];
   109 		} else {
   110 			throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]);
   111 		}
   112 	}
   113 
   114 	public static class Tokens {
   115 
   116 		public static final String DB = "--db";
   117 		public static final String SQL = "--sql";
   118 		public static final String SQL_UPDATE = "--sql-update";
   119 		public static final String SQL_INSERT = "--sql-insert";
   120 		public static final String BATCH = "--batch";
   121 		public static final String DATA = "--data";
   122 		public static final String NAME_PREFIX = "--name-prefix";
   123 		public static final String TYPES = "--types";
   124 
   125 		private Tokens() {
   126 		}
   127 	}
   128 
   129 	private int getType(String typeString) {
   130 		Integer type = types.get(typeString);
   131 		if (type == null) {
   132 			throw new IllegalArgumentException("Unsupported type: " + typeString);
   133 		} else {
   134 			return type;
   135 		}
   136 	}
   137 }