3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.SQLCommand.COMMAND_TYPE;
21 import java.sql.Types;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class CLIParser {
34 public static final String TYPE_NAME_SEPARATOR = ":";
35 private final Map<String, Integer> types;
38 Map<String, Integer> m = new HashMap<>();
39 m.put("int", Types.INTEGER);
40 m.put("string", Types.VARCHAR);
41 m.put("boolean", Types.BOOLEAN);
45 types = Collections.unmodifiableMap(m);
48 public CLIOptions parseOptions(String[] args) throws CLIParserException {
49 CLIOptions options = new CLIOptions();
51 List<Integer> numberedTypes = new ArrayList<>();
52 Map<String, Integer> namedTypes = new HashMap<>();
54 for (int i = 0; i < args.length; i++) {
58 String typesString = fetchNext(args, ++i);
60 for (String oneType : typesString.split(",")) {
61 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
62 if (sepatratorIndex == -1) {
63 numberedTypes.add(getType(oneType));
65 String namePart = oneType.substring(0, sepatratorIndex).trim();
66 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
67 namedTypes.put(namePart, getType(typePart));
71 case Tokens.NAME_PREFIX:
72 options.setNamePrefix(fetchNext(args, ++i));
75 options.setDatabaseName(fetchNext(args, ++i));
78 options.setSql(fetchNext(args, ++i));
79 options.setCommandType(COMMAND_TYPE.QUERY);
81 case Tokens.SQL_UPDATE:
82 case Tokens.SQL_INSERT:
83 options.setSql(fetchNext(args, ++i));
84 options.setCommandType(COMMAND_TYPE.UPDATE);
87 options.setBatch(true);
89 case Tokens.DATA: // --data is the last option
90 for (i++; i < args.length; i++) {
93 if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
94 String paramName = arg.substring(options.getNamePrefix().length());
95 String paramValue = fetchNext(args, ++i);
96 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
97 } else { // Numbered parameters:
99 if (numberedTypes.isEmpty()) {
100 parameter = new Parameter(arg, null);
102 int paramIndex = options.getNumberedParameters().size();
105 paramType = numberedTypes.get(paramIndex);
106 } catch (IndexOutOfBoundsException e) {
107 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
108 } catch (NullPointerException e) {
109 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
111 parameter = new Parameter(arg, paramType);
113 options.addNumberedParameter(parameter);
117 case Tokens.FORMATTER:
118 options.setFormatterName(fetchNext(args, ++i));
120 case Tokens.INFO_HELP:
121 options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
123 case Tokens.INFO_FORMATTERS:
124 options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
126 case Tokens.INFO_LICENSE:
127 options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
129 case Tokens.INFO_TYPES:
130 options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
132 case Tokens.INFO_VERSION:
133 options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
135 case Tokens.INFO_DATABASES:
136 options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
138 case Tokens.INFO_CONNECTION:
139 options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
140 options.setDatabaseNameToTest(fetchNext(args, ++i));
143 throw new CLIParserException("Unknown option: " + arg);
149 private String fetchNext(String[] args, int index) throws CLIParserException {
150 if (index < args.length) {
153 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
157 public static class Tokens {
159 public static final String DB = "--db";
160 public static final String SQL = "--sql";
161 public static final String SQL_UPDATE = "--sql-update";
162 public static final String SQL_INSERT = "--sql-insert";
163 public static final String BATCH = "--batch";
164 public static final String DATA = "--data";
165 public static final String NAME_PREFIX = "--name-prefix";
166 public static final String TYPES = "--types";
167 public static final String FORMATTER = "--formatter";
168 public static final String INFO_HELP = "--help";
169 public static final String INFO_VERSION = "--version";
170 public static final String INFO_LICENSE = "--license";
171 public static final String INFO_FORMATTERS = "--list-formatters";
172 public static final String INFO_TYPES = "--list-types";
173 public static final String INFO_DATABASES = "--list-databases";
174 public static final String INFO_CONNECTION = "--test-connection";
180 private int getType(String typeString) throws CLIParserException {
181 Integer type = types.get(typeString.trim());
183 throw new CLIParserException("Unsupported type: " + typeString);