49 namedTypes.put(namePart, getType(typePart)); |
49 namedTypes.put(namePart, getType(typePart)); |
50 } |
50 } |
51 } |
51 } |
52 break; |
52 break; |
53 case Tokens.NAME_PREFIX: |
53 case Tokens.NAME_PREFIX: |
54 options.setNamePrefix(args[++i]); |
54 options.setNamePrefix(fetchNext(args, ++i)); |
55 break; |
55 break; |
56 case Tokens.DB: |
56 case Tokens.DB: |
57 options.setDatabaseName(args[++i]); |
57 options.setDatabaseName(fetchNext(args, ++i)); |
58 break; |
58 break; |
59 case Tokens.SQL: |
59 case Tokens.SQL: |
60 options.setSql(args[++i]); |
60 options.setSql(fetchNext(args, ++i)); |
61 options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY); |
61 options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY); |
62 break; |
62 break; |
63 case Tokens.SQL_UPDATE: |
63 case Tokens.SQL_UPDATE: |
64 case Tokens.SQL_INSERT: |
64 case Tokens.SQL_INSERT: |
65 options.setSql(args[++i]); |
65 options.setSql(fetchNext(args, ++i)); |
66 options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE); |
66 options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE); |
67 break; |
67 break; |
68 case Tokens.BATCH: |
68 case Tokens.BATCH: |
69 options.setBatch(true); |
69 options.setBatch(true); |
70 break; |
70 break; |
71 case Tokens.DATA: // --data is the last option |
71 case Tokens.DATA: // --data is the last option |
72 for (i++; i < args.length; i++) { |
72 for (i++; i < args.length; i++) { |
73 arg = args[i]; |
73 arg = args[i]; |
74 |
74 |
75 if (arg.startsWith(options.getNamePrefix())) { |
75 if (arg.startsWith(options.getNamePrefix())) { // Named parameters: |
76 String paramName = arg.substring(options.getNamePrefix().length()); |
76 String paramName = arg.substring(options.getNamePrefix().length()); |
77 String paramValue = args[++i]; |
77 String paramValue = fetchNext(args, ++i); |
78 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); |
78 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); |
79 } else { |
79 } else { // Numbered parameters: |
80 int paramIndex = options.getNumberedParameters().size(); |
80 Parameter parameter; |
81 int paramType; |
81 if (numberedTypes.isEmpty()) { |
82 try { |
82 parameter = new Parameter(arg, null); |
83 paramType = numberedTypes.get(paramIndex); |
83 } else { |
84 } catch (IndexOutOfBoundsException e) { |
84 int paramIndex = options.getNumberedParameters().size(); |
85 throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e); |
85 int paramType; |
86 } catch (NullPointerException e) { |
86 try { |
87 throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e); |
87 paramType = numberedTypes.get(paramIndex); |
|
88 } catch (IndexOutOfBoundsException e) { |
|
89 throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e); |
|
90 } catch (NullPointerException e) { |
|
91 throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e); |
|
92 } |
|
93 parameter = new Parameter(arg, paramType); |
88 } |
94 } |
89 options.addNumberedParameter(new Parameter(arg, paramType)); |
95 options.addNumberedParameter(parameter); |
90 } |
96 } |
91 } |
97 } |
92 break; |
98 break; |
93 default: |
99 default: |
94 throw new IllegalArgumentException("Unknown option: " + arg); |
100 throw new IllegalArgumentException("Unknown option: " + arg); |
95 } |
101 } |
96 } |
102 } |
|
103 return options; |
|
104 } |
97 |
105 |
98 |
106 private String fetchNext(String[] args, int index) { |
99 |
107 if (index < args.length) { |
100 return options; |
108 return args[index]; |
101 |
109 } else { |
|
110 throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]); |
|
111 } |
102 } |
112 } |
103 |
113 |
104 public static class Tokens { |
114 public static class Tokens { |
105 |
115 |
106 public static final String DB = "--db"; |
116 public static final String DB = "--db"; |