1 package info.globalcode.sql.dk; |
1 package info.globalcode.sql.dk; |
2 |
|
3 import java.sql.Types; |
|
4 import java.util.ArrayList; |
|
5 import java.util.Collections; |
|
6 import java.util.HashMap; |
|
7 import java.util.List; |
|
8 import java.util.Map; |
|
9 |
2 |
10 /** |
3 /** |
11 * |
4 * |
12 * @author Ing. František Kučera (frantovo.cz) |
5 * @author Ing. František Kučera (frantovo.cz) |
13 */ |
6 */ |
14 public class CLIStarter { |
7 public class CLIStarter { |
15 |
8 |
16 public static final String TYPE_NAME_SEPARATOR = ":"; |
|
17 private static final Map<String, Integer> types; |
|
18 |
|
19 static { |
|
20 Map<String, Integer> m = new HashMap<>(); |
|
21 m.put("int", Types.INTEGER); |
|
22 m.put("string", Types.VARCHAR); |
|
23 m.put("boolean", Types.BOOLEAN); |
|
24 /** |
|
25 * TODO: more types |
|
26 */ |
|
27 types = Collections.unmodifiableMap(m); |
|
28 } |
|
29 |
|
30 public static void main(String[] args) { |
9 public static void main(String[] args) { |
31 |
10 CLIParser parser = new CLIParser(); |
32 args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1", "--types", "int,bbb,omfg:int,omg:boolean", "--data", "xxx", ":omfg", "hodnota omfg", ":omg", "true"}; |
11 CLIOptions options = parser.parseOptions(args); |
33 |
|
34 CLIOptions options = parseOptions(args); |
|
35 } |
|
36 |
|
37 private static CLIOptions parseOptions(String[] args) { |
|
38 CLIOptions options = new CLIOptions(); |
|
39 |
|
40 List<Integer> numberedTypes = new ArrayList<>(); |
|
41 Map<String, Integer> namedTypes = new HashMap<>(); |
|
42 |
|
43 for (int i = 0; i < args.length; i++) { |
|
44 String arg = args[i]; |
|
45 switch (arg) { |
|
46 case Tokens.TYPES: |
|
47 String typesString = args[++i]; |
|
48 |
|
49 for (String oneType : typesString.split("\\s*,\\s*")) { |
|
50 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR); |
|
51 if (sepatratorIndex == -1) { |
|
52 numberedTypes.add(getType(oneType)); |
|
53 } else { |
|
54 String namePart = oneType.substring(0, sepatratorIndex); |
|
55 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length()); |
|
56 namedTypes.put(namePart, getType(typePart)); |
|
57 } |
|
58 } |
|
59 break; |
|
60 case Tokens.NAME_PREFIX: |
|
61 options.setNamePrefix(args[++i]); |
|
62 break; |
|
63 case Tokens.DB: |
|
64 options.setDatabaseName(args[++i]); |
|
65 break; |
|
66 case Tokens.SQL: |
|
67 options.setSql(args[++i]); |
|
68 options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY); |
|
69 break; |
|
70 case Tokens.SQL_UPDATE: |
|
71 case Tokens.SQL_INSERT: |
|
72 options.setSql(args[++i]); |
|
73 options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE); |
|
74 break; |
|
75 case Tokens.BATCH: |
|
76 options.setBatch(true); |
|
77 break; |
|
78 case Tokens.DATA: // --data is the last option |
|
79 for (i++; i < args.length; i++) { |
|
80 arg = args[i]; |
|
81 |
|
82 if (arg.startsWith(options.getNamePrefix())) { |
|
83 String paramName = arg.substring(options.getNamePrefix().length()); |
|
84 String paramValue = args[++i]; |
|
85 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); |
|
86 } else { |
|
87 int paramIndex = options.getNumberedParameters().size(); |
|
88 int paramType; |
|
89 try { |
|
90 paramType = numberedTypes.get(paramIndex); |
|
91 } catch (IndexOutOfBoundsException e) { |
|
92 throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e); |
|
93 } catch (NullPointerException e) { |
|
94 throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e); |
|
95 } |
|
96 options.addNumberedParameter(new Parameter(arg, paramType)); |
|
97 } |
|
98 } |
|
99 break; |
|
100 default: |
|
101 throw new IllegalArgumentException("Unknown option: " + arg); |
|
102 } |
|
103 } |
|
104 |
|
105 |
|
106 |
|
107 return options; |
|
108 |
|
109 } |
|
110 |
|
111 public static class Tokens { |
|
112 |
|
113 public static final String DB = "--db"; |
|
114 public static final String SQL = "--sql"; |
|
115 public static final String SQL_UPDATE = "--sql-update"; |
|
116 public static final String SQL_INSERT = "--sql-insert"; |
|
117 public static final String BATCH = "--batch"; |
|
118 public static final String DATA = "--data"; |
|
119 public static final String NAME_PREFIX = "--name-prefix"; |
|
120 public static final String TYPES = "--types"; |
|
121 |
|
122 private Tokens() { |
|
123 } |
|
124 } |
|
125 |
|
126 private static Integer getType(String typeString) { |
|
127 return types.get(typeString); |
|
128 } |
12 } |
129 } |
13 } |