franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@1
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
19 |
|
franta-hg@4
|
20 |
import java.sql.Types;
|
franta-hg@4
|
21 |
import java.util.ArrayList;
|
franta-hg@4
|
22 |
import java.util.Collections;
|
franta-hg@4
|
23 |
import java.util.HashMap;
|
franta-hg@4
|
24 |
import java.util.List;
|
franta-hg@4
|
25 |
import java.util.Map;
|
franta-hg@4
|
26 |
|
franta-hg@1
|
27 |
/**
|
franta-hg@1
|
28 |
*
|
franta-hg@1
|
29 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
30 |
*/
|
franta-hg@5
|
31 |
public class CLIParser {
|
franta-hg@1
|
32 |
|
franta-hg@4
|
33 |
public static final String TYPE_NAME_SEPARATOR = ":";
|
franta-hg@5
|
34 |
private final Map<String, Integer> types;
|
franta-hg@4
|
35 |
|
franta-hg@5
|
36 |
public CLIParser() {
|
franta-hg@4
|
37 |
Map<String, Integer> m = new HashMap<>();
|
franta-hg@4
|
38 |
m.put("int", Types.INTEGER);
|
franta-hg@4
|
39 |
m.put("string", Types.VARCHAR);
|
franta-hg@4
|
40 |
m.put("boolean", Types.BOOLEAN);
|
franta-hg@4
|
41 |
/**
|
franta-hg@4
|
42 |
* TODO: more types
|
franta-hg@4
|
43 |
*/
|
franta-hg@4
|
44 |
types = Collections.unmodifiableMap(m);
|
franta-hg@4
|
45 |
}
|
franta-hg@4
|
46 |
|
franta-hg@9
|
47 |
public CLIOptions parseOptions(String[] args) throws CLIParserException {
|
franta-hg@1
|
48 |
CLIOptions options = new CLIOptions();
|
franta-hg@1
|
49 |
|
franta-hg@4
|
50 |
List<Integer> numberedTypes = new ArrayList<>();
|
franta-hg@4
|
51 |
Map<String, Integer> namedTypes = new HashMap<>();
|
franta-hg@2
|
52 |
|
franta-hg@1
|
53 |
for (int i = 0; i < args.length; i++) {
|
franta-hg@1
|
54 |
String arg = args[i];
|
franta-hg@2
|
55 |
switch (arg) {
|
franta-hg@2
|
56 |
case Tokens.TYPES:
|
franta-hg@8
|
57 |
String typesString = fetchNext(args, ++i);
|
franta-hg@4
|
58 |
|
franta-hg@10
|
59 |
for (String oneType : typesString.split(",")) {
|
franta-hg@4
|
60 |
int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
|
franta-hg@4
|
61 |
if (sepatratorIndex == -1) {
|
franta-hg@4
|
62 |
numberedTypes.add(getType(oneType));
|
franta-hg@4
|
63 |
} else {
|
franta-hg@12
|
64 |
String namePart = oneType.substring(0, sepatratorIndex).trim();
|
franta-hg@4
|
65 |
String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
|
franta-hg@4
|
66 |
namedTypes.put(namePart, getType(typePart));
|
franta-hg@4
|
67 |
}
|
franta-hg@4
|
68 |
}
|
franta-hg@2
|
69 |
break;
|
franta-hg@2
|
70 |
case Tokens.NAME_PREFIX:
|
franta-hg@8
|
71 |
options.setNamePrefix(fetchNext(args, ++i));
|
franta-hg@2
|
72 |
break;
|
franta-hg@1
|
73 |
case Tokens.DB:
|
franta-hg@8
|
74 |
options.setDatabaseName(fetchNext(args, ++i));
|
franta-hg@1
|
75 |
break;
|
franta-hg@1
|
76 |
case Tokens.SQL:
|
franta-hg@8
|
77 |
options.setSql(fetchNext(args, ++i));
|
franta-hg@1
|
78 |
break;
|
franta-hg@1
|
79 |
case Tokens.BATCH:
|
franta-hg@2
|
80 |
options.setBatch(true);
|
franta-hg@1
|
81 |
break;
|
franta-hg@4
|
82 |
case Tokens.DATA: // --data is the last option
|
franta-hg@4
|
83 |
for (i++; i < args.length; i++) {
|
franta-hg@4
|
84 |
arg = args[i];
|
franta-hg@4
|
85 |
|
franta-hg@8
|
86 |
if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
|
franta-hg@4
|
87 |
String paramName = arg.substring(options.getNamePrefix().length());
|
franta-hg@8
|
88 |
String paramValue = fetchNext(args, ++i);
|
franta-hg@4
|
89 |
options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
|
franta-hg@8
|
90 |
} else { // Numbered parameters:
|
franta-hg@8
|
91 |
Parameter parameter;
|
franta-hg@8
|
92 |
if (numberedTypes.isEmpty()) {
|
franta-hg@8
|
93 |
parameter = new Parameter(arg, null);
|
franta-hg@8
|
94 |
} else {
|
franta-hg@8
|
95 |
int paramIndex = options.getNumberedParameters().size();
|
franta-hg@8
|
96 |
int paramType;
|
franta-hg@8
|
97 |
try {
|
franta-hg@8
|
98 |
paramType = numberedTypes.get(paramIndex);
|
franta-hg@8
|
99 |
} catch (IndexOutOfBoundsException e) {
|
franta-hg@9
|
100 |
throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
|
franta-hg@8
|
101 |
} catch (NullPointerException e) {
|
franta-hg@9
|
102 |
throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
|
franta-hg@8
|
103 |
}
|
franta-hg@8
|
104 |
parameter = new Parameter(arg, paramType);
|
franta-hg@4
|
105 |
}
|
franta-hg@8
|
106 |
options.addNumberedParameter(parameter);
|
franta-hg@4
|
107 |
}
|
franta-hg@4
|
108 |
}
|
franta-hg@1
|
109 |
break;
|
franta-hg@14
|
110 |
case Tokens.FORMATTER:
|
franta-hg@14
|
111 |
options.setFormatterName(fetchNext(args, ++i));
|
franta-hg@14
|
112 |
break;
|
franta-hg@14
|
113 |
case Tokens.INFO_HELP:
|
franta-hg@14
|
114 |
options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
|
franta-hg@14
|
115 |
break;
|
franta-hg@14
|
116 |
case Tokens.INFO_FORMATTERS:
|
franta-hg@14
|
117 |
options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
|
franta-hg@14
|
118 |
break;
|
franta-hg@14
|
119 |
case Tokens.INFO_LICENSE:
|
franta-hg@14
|
120 |
options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
|
franta-hg@14
|
121 |
break;
|
franta-hg@14
|
122 |
case Tokens.INFO_TYPES:
|
franta-hg@14
|
123 |
options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
|
franta-hg@14
|
124 |
break;
|
franta-hg@14
|
125 |
case Tokens.INFO_VERSION:
|
franta-hg@14
|
126 |
options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
|
franta-hg@14
|
127 |
break;
|
franta-hg@15
|
128 |
case Tokens.INFO_DATABASES:
|
franta-hg@15
|
129 |
options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
|
franta-hg@15
|
130 |
break;
|
franta-hg@15
|
131 |
case Tokens.INFO_CONNECTION:
|
franta-hg@15
|
132 |
options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
|
franta-hg@15
|
133 |
options.setDatabaseNameToTest(fetchNext(args, ++i));
|
franta-hg@15
|
134 |
break;
|
franta-hg@4
|
135 |
default:
|
franta-hg@9
|
136 |
throw new CLIParserException("Unknown option: " + arg);
|
franta-hg@1
|
137 |
}
|
franta-hg@1
|
138 |
}
|
franta-hg@8
|
139 |
return options;
|
franta-hg@8
|
140 |
}
|
franta-hg@1
|
141 |
|
franta-hg@9
|
142 |
private String fetchNext(String[] args, int index) throws CLIParserException {
|
franta-hg@8
|
143 |
if (index < args.length) {
|
franta-hg@8
|
144 |
return args[index];
|
franta-hg@8
|
145 |
} else {
|
franta-hg@9
|
146 |
throw new CLIParserException("Expecting value for option: " + args[index - 1]);
|
franta-hg@8
|
147 |
}
|
franta-hg@1
|
148 |
}
|
franta-hg@1
|
149 |
|
franta-hg@1
|
150 |
public static class Tokens {
|
franta-hg@1
|
151 |
|
franta-hg@1
|
152 |
public static final String DB = "--db";
|
franta-hg@1
|
153 |
public static final String SQL = "--sql";
|
franta-hg@1
|
154 |
public static final String BATCH = "--batch";
|
franta-hg@1
|
155 |
public static final String DATA = "--data";
|
franta-hg@2
|
156 |
public static final String NAME_PREFIX = "--name-prefix";
|
franta-hg@1
|
157 |
public static final String TYPES = "--types";
|
franta-hg@14
|
158 |
public static final String FORMATTER = "--formatter";
|
franta-hg@14
|
159 |
public static final String INFO_HELP = "--help";
|
franta-hg@14
|
160 |
public static final String INFO_VERSION = "--version";
|
franta-hg@14
|
161 |
public static final String INFO_LICENSE = "--license";
|
franta-hg@14
|
162 |
public static final String INFO_FORMATTERS = "--list-formatters";
|
franta-hg@14
|
163 |
public static final String INFO_TYPES = "--list-types";
|
franta-hg@15
|
164 |
public static final String INFO_DATABASES = "--list-databases";
|
franta-hg@15
|
165 |
public static final String INFO_CONNECTION = "--test-connection";
|
franta-hg@1
|
166 |
|
franta-hg@1
|
167 |
private Tokens() {
|
franta-hg@1
|
168 |
}
|
franta-hg@1
|
169 |
}
|
franta-hg@4
|
170 |
|
franta-hg@9
|
171 |
private int getType(String typeString) throws CLIParserException {
|
franta-hg@10
|
172 |
Integer type = types.get(typeString.trim());
|
franta-hg@8
|
173 |
if (type == null) {
|
franta-hg@9
|
174 |
throw new CLIParserException("Unsupported type: " + typeString);
|
franta-hg@8
|
175 |
} else {
|
franta-hg@8
|
176 |
return type;
|
franta-hg@8
|
177 |
}
|
franta-hg@4
|
178 |
}
|
franta-hg@1
|
179 |
}
|