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