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@29
|
20 |
import info.globalcode.sql.dk.SQLCommand.COMMAND_TYPE;
|
franta-hg@4
|
21 |
import java.sql.Types;
|
franta-hg@4
|
22 |
import java.util.ArrayList;
|
franta-hg@4
|
23 |
import java.util.Collections;
|
franta-hg@4
|
24 |
import java.util.HashMap;
|
franta-hg@4
|
25 |
import java.util.List;
|
franta-hg@4
|
26 |
import java.util.Map;
|
franta-hg@4
|
27 |
|
franta-hg@1
|
28 |
/**
|
franta-hg@1
|
29 |
*
|
franta-hg@1
|
30 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
31 |
*/
|
franta-hg@5
|
32 |
public class CLIParser {
|
franta-hg@1
|
33 |
|
franta-hg@4
|
34 |
public static final String TYPE_NAME_SEPARATOR = ":";
|
franta-hg@5
|
35 |
private final Map<String, Integer> types;
|
franta-hg@4
|
36 |
|
franta-hg@5
|
37 |
public CLIParser() {
|
franta-hg@4
|
38 |
Map<String, Integer> m = new HashMap<>();
|
franta-hg@4
|
39 |
m.put("int", Types.INTEGER);
|
franta-hg@4
|
40 |
m.put("string", Types.VARCHAR);
|
franta-hg@4
|
41 |
m.put("boolean", Types.BOOLEAN);
|
franta-hg@4
|
42 |
/**
|
franta-hg@4
|
43 |
* TODO: more types
|
franta-hg@4
|
44 |
*/
|
franta-hg@4
|
45 |
types = Collections.unmodifiableMap(m);
|
franta-hg@4
|
46 |
}
|
franta-hg@4
|
47 |
|
franta-hg@9
|
48 |
public CLIOptions parseOptions(String[] args) throws CLIParserException {
|
franta-hg@1
|
49 |
CLIOptions options = new CLIOptions();
|
franta-hg@1
|
50 |
|
franta-hg@4
|
51 |
List<Integer> numberedTypes = new ArrayList<>();
|
franta-hg@4
|
52 |
Map<String, Integer> namedTypes = new HashMap<>();
|
franta-hg@2
|
53 |
|
franta-hg@1
|
54 |
for (int i = 0; i < args.length; i++) {
|
franta-hg@1
|
55 |
String arg = args[i];
|
franta-hg@2
|
56 |
switch (arg) {
|
franta-hg@2
|
57 |
case Tokens.TYPES:
|
franta-hg@8
|
58 |
String typesString = fetchNext(args, ++i);
|
franta-hg@4
|
59 |
|
franta-hg@10
|
60 |
for (String oneType : typesString.split(",")) {
|
franta-hg@4
|
61 |
int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
|
franta-hg@4
|
62 |
if (sepatratorIndex == -1) {
|
franta-hg@4
|
63 |
numberedTypes.add(getType(oneType));
|
franta-hg@4
|
64 |
} else {
|
franta-hg@12
|
65 |
String namePart = oneType.substring(0, sepatratorIndex).trim();
|
franta-hg@4
|
66 |
String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
|
franta-hg@4
|
67 |
namedTypes.put(namePart, getType(typePart));
|
franta-hg@4
|
68 |
}
|
franta-hg@4
|
69 |
}
|
franta-hg@2
|
70 |
break;
|
franta-hg@2
|
71 |
case Tokens.NAME_PREFIX:
|
franta-hg@8
|
72 |
options.setNamePrefix(fetchNext(args, ++i));
|
franta-hg@2
|
73 |
break;
|
franta-hg@1
|
74 |
case Tokens.DB:
|
franta-hg@8
|
75 |
options.setDatabaseName(fetchNext(args, ++i));
|
franta-hg@1
|
76 |
break;
|
franta-hg@1
|
77 |
case Tokens.SQL:
|
franta-hg@8
|
78 |
options.setSql(fetchNext(args, ++i));
|
franta-hg@29
|
79 |
options.setCommandType(COMMAND_TYPE.QUERY);
|
franta-hg@1
|
80 |
break;
|
franta-hg@1
|
81 |
case Tokens.SQL_UPDATE:
|
franta-hg@2
|
82 |
case Tokens.SQL_INSERT:
|
franta-hg@8
|
83 |
options.setSql(fetchNext(args, ++i));
|
franta-hg@29
|
84 |
options.setCommandType(COMMAND_TYPE.UPDATE);
|
franta-hg@1
|
85 |
break;
|
franta-hg@1
|
86 |
case Tokens.BATCH:
|
franta-hg@2
|
87 |
options.setBatch(true);
|
franta-hg@1
|
88 |
break;
|
franta-hg@4
|
89 |
case Tokens.DATA: // --data is the last option
|
franta-hg@4
|
90 |
for (i++; i < args.length; i++) {
|
franta-hg@4
|
91 |
arg = args[i];
|
franta-hg@4
|
92 |
|
franta-hg@8
|
93 |
if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
|
franta-hg@4
|
94 |
String paramName = arg.substring(options.getNamePrefix().length());
|
franta-hg@8
|
95 |
String paramValue = fetchNext(args, ++i);
|
franta-hg@4
|
96 |
options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
|
franta-hg@8
|
97 |
} else { // Numbered parameters:
|
franta-hg@8
|
98 |
Parameter parameter;
|
franta-hg@8
|
99 |
if (numberedTypes.isEmpty()) {
|
franta-hg@8
|
100 |
parameter = new Parameter(arg, null);
|
franta-hg@8
|
101 |
} else {
|
franta-hg@8
|
102 |
int paramIndex = options.getNumberedParameters().size();
|
franta-hg@8
|
103 |
int paramType;
|
franta-hg@8
|
104 |
try {
|
franta-hg@8
|
105 |
paramType = numberedTypes.get(paramIndex);
|
franta-hg@8
|
106 |
} catch (IndexOutOfBoundsException e) {
|
franta-hg@9
|
107 |
throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
|
franta-hg@8
|
108 |
} catch (NullPointerException e) {
|
franta-hg@9
|
109 |
throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
|
franta-hg@8
|
110 |
}
|
franta-hg@8
|
111 |
parameter = new Parameter(arg, paramType);
|
franta-hg@4
|
112 |
}
|
franta-hg@8
|
113 |
options.addNumberedParameter(parameter);
|
franta-hg@4
|
114 |
}
|
franta-hg@4
|
115 |
}
|
franta-hg@1
|
116 |
break;
|
franta-hg@14
|
117 |
case Tokens.FORMATTER:
|
franta-hg@14
|
118 |
options.setFormatterName(fetchNext(args, ++i));
|
franta-hg@14
|
119 |
break;
|
franta-hg@14
|
120 |
case Tokens.INFO_HELP:
|
franta-hg@14
|
121 |
options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
|
franta-hg@14
|
122 |
break;
|
franta-hg@14
|
123 |
case Tokens.INFO_FORMATTERS:
|
franta-hg@14
|
124 |
options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
|
franta-hg@14
|
125 |
break;
|
franta-hg@14
|
126 |
case Tokens.INFO_LICENSE:
|
franta-hg@14
|
127 |
options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
|
franta-hg@14
|
128 |
break;
|
franta-hg@14
|
129 |
case Tokens.INFO_TYPES:
|
franta-hg@14
|
130 |
options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
|
franta-hg@14
|
131 |
break;
|
franta-hg@14
|
132 |
case Tokens.INFO_VERSION:
|
franta-hg@14
|
133 |
options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
|
franta-hg@14
|
134 |
break;
|
franta-hg@15
|
135 |
case Tokens.INFO_DATABASES:
|
franta-hg@15
|
136 |
options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
|
franta-hg@15
|
137 |
break;
|
franta-hg@15
|
138 |
case Tokens.INFO_CONNECTION:
|
franta-hg@15
|
139 |
options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
|
franta-hg@15
|
140 |
options.setDatabaseNameToTest(fetchNext(args, ++i));
|
franta-hg@15
|
141 |
break;
|
franta-hg@4
|
142 |
default:
|
franta-hg@9
|
143 |
throw new CLIParserException("Unknown option: " + arg);
|
franta-hg@1
|
144 |
}
|
franta-hg@1
|
145 |
}
|
franta-hg@8
|
146 |
return options;
|
franta-hg@8
|
147 |
}
|
franta-hg@1
|
148 |
|
franta-hg@9
|
149 |
private String fetchNext(String[] args, int index) throws CLIParserException {
|
franta-hg@8
|
150 |
if (index < args.length) {
|
franta-hg@8
|
151 |
return args[index];
|
franta-hg@8
|
152 |
} else {
|
franta-hg@9
|
153 |
throw new CLIParserException("Expecting value for option: " + args[index - 1]);
|
franta-hg@8
|
154 |
}
|
franta-hg@1
|
155 |
}
|
franta-hg@1
|
156 |
|
franta-hg@1
|
157 |
public static class Tokens {
|
franta-hg@1
|
158 |
|
franta-hg@1
|
159 |
public static final String DB = "--db";
|
franta-hg@1
|
160 |
public static final String SQL = "--sql";
|
franta-hg@1
|
161 |
public static final String SQL_UPDATE = "--sql-update";
|
franta-hg@2
|
162 |
public static final String SQL_INSERT = "--sql-insert";
|
franta-hg@1
|
163 |
public static final String BATCH = "--batch";
|
franta-hg@1
|
164 |
public static final String DATA = "--data";
|
franta-hg@2
|
165 |
public static final String NAME_PREFIX = "--name-prefix";
|
franta-hg@1
|
166 |
public static final String TYPES = "--types";
|
franta-hg@14
|
167 |
public static final String FORMATTER = "--formatter";
|
franta-hg@14
|
168 |
public static final String INFO_HELP = "--help";
|
franta-hg@14
|
169 |
public static final String INFO_VERSION = "--version";
|
franta-hg@14
|
170 |
public static final String INFO_LICENSE = "--license";
|
franta-hg@14
|
171 |
public static final String INFO_FORMATTERS = "--list-formatters";
|
franta-hg@14
|
172 |
public static final String INFO_TYPES = "--list-types";
|
franta-hg@15
|
173 |
public static final String INFO_DATABASES = "--list-databases";
|
franta-hg@15
|
174 |
public static final String INFO_CONNECTION = "--test-connection";
|
franta-hg@1
|
175 |
|
franta-hg@1
|
176 |
private Tokens() {
|
franta-hg@1
|
177 |
}
|
franta-hg@1
|
178 |
}
|
franta-hg@4
|
179 |
|
franta-hg@9
|
180 |
private int getType(String typeString) throws CLIParserException {
|
franta-hg@10
|
181 |
Integer type = types.get(typeString.trim());
|
franta-hg@8
|
182 |
if (type == null) {
|
franta-hg@9
|
183 |
throw new CLIParserException("Unsupported type: " + typeString);
|
franta-hg@8
|
184 |
} else {
|
franta-hg@8
|
185 |
return type;
|
franta-hg@8
|
186 |
}
|
franta-hg@4
|
187 |
}
|
franta-hg@1
|
188 |
}
|