franta-hg@1: package info.globalcode.sql.dk; franta-hg@1: franta-hg@4: import java.sql.Types; franta-hg@4: import java.util.ArrayList; franta-hg@4: import java.util.Collections; franta-hg@4: import java.util.HashMap; franta-hg@4: import java.util.List; franta-hg@4: import java.util.Map; franta-hg@4: franta-hg@1: /** franta-hg@1: * franta-hg@1: * @author Ing. František Kučera (frantovo.cz) franta-hg@1: */ franta-hg@5: public class CLIParser { franta-hg@1: franta-hg@4: public static final String TYPE_NAME_SEPARATOR = ":"; franta-hg@5: private final Map types; franta-hg@4: franta-hg@5: public CLIParser() { franta-hg@4: Map m = new HashMap<>(); franta-hg@4: m.put("int", Types.INTEGER); franta-hg@4: m.put("string", Types.VARCHAR); franta-hg@4: m.put("boolean", Types.BOOLEAN); franta-hg@4: /** franta-hg@4: * TODO: more types franta-hg@4: */ franta-hg@4: types = Collections.unmodifiableMap(m); franta-hg@4: } franta-hg@4: franta-hg@5: public CLIOptions parseOptions(String[] args) { franta-hg@1: CLIOptions options = new CLIOptions(); franta-hg@1: franta-hg@4: List numberedTypes = new ArrayList<>(); franta-hg@4: Map namedTypes = new HashMap<>(); franta-hg@2: franta-hg@1: for (int i = 0; i < args.length; i++) { franta-hg@1: String arg = args[i]; franta-hg@2: switch (arg) { franta-hg@2: case Tokens.TYPES: franta-hg@4: String typesString = args[++i]; franta-hg@4: franta-hg@4: for (String oneType : typesString.split("\\s*,\\s*")) { franta-hg@4: int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR); franta-hg@4: if (sepatratorIndex == -1) { franta-hg@4: numberedTypes.add(getType(oneType)); franta-hg@4: } else { franta-hg@4: String namePart = oneType.substring(0, sepatratorIndex); franta-hg@4: String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length()); franta-hg@4: namedTypes.put(namePart, getType(typePart)); franta-hg@4: } franta-hg@4: } franta-hg@2: break; franta-hg@2: case Tokens.NAME_PREFIX: franta-hg@3: options.setNamePrefix(args[++i]); franta-hg@2: break; franta-hg@1: case Tokens.DB: franta-hg@2: options.setDatabaseName(args[++i]); franta-hg@1: break; franta-hg@1: case Tokens.SQL: franta-hg@2: options.setSql(args[++i]); franta-hg@2: options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY); franta-hg@1: break; franta-hg@1: case Tokens.SQL_UPDATE: franta-hg@2: case Tokens.SQL_INSERT: franta-hg@2: options.setSql(args[++i]); franta-hg@2: options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE); franta-hg@1: break; franta-hg@1: case Tokens.BATCH: franta-hg@2: options.setBatch(true); franta-hg@1: break; franta-hg@4: case Tokens.DATA: // --data is the last option franta-hg@4: for (i++; i < args.length; i++) { franta-hg@4: arg = args[i]; franta-hg@4: franta-hg@4: if (arg.startsWith(options.getNamePrefix())) { franta-hg@4: String paramName = arg.substring(options.getNamePrefix().length()); franta-hg@4: String paramValue = args[++i]; franta-hg@4: options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); franta-hg@4: } else { franta-hg@4: int paramIndex = options.getNumberedParameters().size(); franta-hg@4: int paramType; franta-hg@4: try { franta-hg@4: paramType = numberedTypes.get(paramIndex); franta-hg@4: } catch (IndexOutOfBoundsException e) { franta-hg@4: throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e); franta-hg@4: } catch (NullPointerException e) { franta-hg@4: throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e); franta-hg@4: } franta-hg@4: options.addNumberedParameter(new Parameter(arg, paramType)); franta-hg@4: } franta-hg@4: } franta-hg@1: break; franta-hg@4: default: franta-hg@4: throw new IllegalArgumentException("Unknown option: " + arg); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@4: franta-hg@4: franta-hg@1: return options; franta-hg@1: franta-hg@1: } franta-hg@1: franta-hg@1: public static class Tokens { franta-hg@1: franta-hg@1: public static final String DB = "--db"; franta-hg@1: public static final String SQL = "--sql"; franta-hg@1: public static final String SQL_UPDATE = "--sql-update"; franta-hg@2: public static final String SQL_INSERT = "--sql-insert"; franta-hg@1: public static final String BATCH = "--batch"; franta-hg@1: public static final String DATA = "--data"; franta-hg@2: public static final String NAME_PREFIX = "--name-prefix"; franta-hg@1: public static final String TYPES = "--types"; franta-hg@1: franta-hg@1: private Tokens() { franta-hg@1: } franta-hg@1: } franta-hg@4: franta-hg@5: private Integer getType(String typeString) { franta-hg@4: return types.get(typeString); franta-hg@4: } franta-hg@1: }