equal
deleted
inserted
replaced
15 * You should have received a copy of the GNU General Public License |
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/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 package info.globalcode.sql.dk; |
18 package info.globalcode.sql.dk; |
19 |
19 |
20 import java.sql.Types; |
|
21 import java.util.ArrayList; |
20 import java.util.ArrayList; |
22 import java.util.Collections; |
|
23 import java.util.HashMap; |
21 import java.util.HashMap; |
24 import java.util.List; |
22 import java.util.List; |
25 import java.util.Map; |
23 import java.util.Map; |
26 |
24 |
27 /** |
25 /** |
29 * @author Ing. František Kučera (frantovo.cz) |
27 * @author Ing. František Kučera (frantovo.cz) |
30 */ |
28 */ |
31 public class CLIParser { |
29 public class CLIParser { |
32 |
30 |
33 public static final String TYPE_NAME_SEPARATOR = ":"; |
31 public static final String TYPE_NAME_SEPARATOR = ":"; |
34 private final Map<String, Integer> types; |
|
35 |
|
36 public CLIParser() { |
|
37 Map<String, Integer> m = new HashMap<>(); |
|
38 m.put("integer", Types.INTEGER); |
|
39 m.put("varchar", Types.VARCHAR); |
|
40 m.put("boolean", Types.BOOLEAN); |
|
41 /** |
|
42 * TODO: more types |
|
43 */ |
|
44 types = Collections.unmodifiableMap(m); |
|
45 } |
|
46 |
32 |
47 public CLIOptions parseOptions(String[] args) throws CLIParserException { |
33 public CLIOptions parseOptions(String[] args) throws CLIParserException { |
48 CLIOptions options = new CLIOptions(); |
34 CLIOptions options = new CLIOptions(); |
49 |
35 |
50 List<Integer> numberedTypes = new ArrayList<>(); |
36 List<SQLType> numberedTypes = new ArrayList<>(); |
51 Map<String, Integer> namedTypes = new HashMap<>(); |
37 Map<String, SQLType> namedTypes = new HashMap<>(); |
52 |
38 |
53 for (int i = 0; i < args.length; i++) { |
39 for (int i = 0; i < args.length; i++) { |
54 String arg = args[i]; |
40 String arg = args[i]; |
55 switch (arg) { |
41 switch (arg) { |
56 case Tokens.TYPES: |
42 case Tokens.TYPES: |
88 Parameter parameter; |
74 Parameter parameter; |
89 if (numberedTypes.isEmpty()) { |
75 if (numberedTypes.isEmpty()) { |
90 parameter = new Parameter(arg, null); |
76 parameter = new Parameter(arg, null); |
91 } else { |
77 } else { |
92 int paramIndex = options.getNumberedParameters().size(); |
78 int paramIndex = options.getNumberedParameters().size(); |
93 int paramType; |
79 SQLType paramType; |
94 try { |
80 try { |
95 paramType = numberedTypes.get(paramIndex); |
81 paramType = numberedTypes.get(paramIndex); |
96 } catch (IndexOutOfBoundsException e) { |
82 } catch (IndexOutOfBoundsException e) { |
97 throw new CLIParserException("Missing type for parameter #" + paramIndex, e); |
83 throw new CLIParserException("Missing type for parameter #" + paramIndex, e); |
98 } catch (NullPointerException e) { |
84 } catch (NullPointerException e) { |
171 |
157 |
172 private Tokens() { |
158 private Tokens() { |
173 } |
159 } |
174 } |
160 } |
175 |
161 |
176 private int getType(String typeString) throws CLIParserException { |
162 private SQLType getType(String typeString) throws CLIParserException { |
177 Integer type = types.get(typeString.trim()); |
163 try { |
178 if (type == null) { |
164 return SQLType.valueOf(typeString.trim()); |
179 throw new CLIParserException("Unsupported type: " + typeString); |
165 } catch (IllegalArgumentException e) { |
180 } else { |
166 throw new CLIParserException("Unsupported type: " + typeString, e); |
181 return type; |
|
182 } |
167 } |
183 } |
168 } |
184 } |
169 } |