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