java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 11:58:14 +0100
branchv_0
changeset 68 574cd7fbb5b2
parent 62 7a88ac6ba40c
child 69 0befec5034c2
permissions -rw-r--r--
SQLType enum wrapper for java.sql.Types
     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 java.util.ArrayList;
    21 import java.util.HashMap;
    22 import java.util.List;
    23 import java.util.Map;
    24 
    25 /**
    26  *
    27  * @author Ing. František Kučera (frantovo.cz)
    28  */
    29 public class CLIParser {
    30 
    31 	public static final String TYPE_NAME_SEPARATOR = ":";
    32 
    33 	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    34 		CLIOptions options = new CLIOptions();
    35 
    36 		List<SQLType> numberedTypes = new ArrayList<>();
    37 		Map<String, SQLType> namedTypes = new HashMap<>();
    38 
    39 		for (int i = 0; i < args.length; i++) {
    40 			String arg = args[i];
    41 			switch (arg) {
    42 				case Tokens.TYPES:
    43 					String typesString = fetchNext(args, ++i);
    44 
    45 					for (String oneType : typesString.split(",")) {
    46 						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    47 						if (sepatratorIndex == -1) {
    48 							numberedTypes.add(getType(oneType));
    49 						} else {
    50 							String namePart = oneType.substring(0, sepatratorIndex).trim();
    51 							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    52 							namedTypes.put(namePart, getType(typePart));
    53 						}
    54 					}
    55 					break;
    56 				case Tokens.NAME_PREFIX:
    57 					options.setNamePrefix(fetchNext(args, ++i));
    58 					break;
    59 				case Tokens.NAME_SUFFIX:
    60 					options.setNameSuffix(fetchNext(args, ++i));
    61 					break;
    62 				case Tokens.DB:
    63 					options.setDatabaseName(fetchNext(args, ++i));
    64 					break;
    65 				case Tokens.SQL:
    66 					options.setSql(fetchNext(args, ++i));
    67 					break;
    68 				case Tokens.BATCH:
    69 					options.setBatch(true);
    70 					break;
    71 				case Tokens.DATA: // --data is the last option
    72 					for (i++; i < args.length; i++) {
    73 						arg = args[i];
    74 						Parameter parameter;
    75 						if (numberedTypes.isEmpty()) {
    76 							parameter = new Parameter(arg, null);
    77 						} else {
    78 							int paramIndex = options.getNumberedParameters().size();
    79 							SQLType paramType;
    80 							try {
    81 								paramType = numberedTypes.get(paramIndex);
    82 							} catch (IndexOutOfBoundsException e) {
    83 								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
    84 							} catch (NullPointerException e) {
    85 								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
    86 							}
    87 							parameter = new Parameter(arg, paramType);
    88 						}
    89 						options.addNumberedParameter(parameter);
    90 					}
    91 					break;
    92 				case Tokens.DATA_NAMED:
    93 					for (i++; i < args.length; i++) {
    94 						String paramName = args[i];
    95 						String paramValue = fetchNext(args, ++i);
    96 						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    97 					}
    98 					break;
    99 				case Tokens.FORMATTER:
   100 					options.setFormatterName(fetchNext(args, ++i));
   101 					break;
   102 				case Tokens.INFO_HELP:
   103 					options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
   104 					break;
   105 				case Tokens.INFO_FORMATTERS:
   106 					options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
   107 					break;
   108 				case Tokens.INFO_LICENSE:
   109 					options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
   110 					break;
   111 				case Tokens.INFO_TYPES:
   112 					options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
   113 					break;
   114 				case Tokens.INFO_VERSION:
   115 					options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
   116 					break;
   117 				case Tokens.INFO_DATABASES:
   118 					options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
   119 					break;
   120 				case Tokens.INFO_CONNECTION:
   121 					options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
   122 					options.setDatabaseNameToTest(fetchNext(args, ++i));
   123 					break;
   124 				default:
   125 					throw new CLIParserException("Unknown option: " + arg);
   126 			}
   127 		}
   128 		return options;
   129 	}
   130 
   131 	private String fetchNext(String[] args, int index) throws CLIParserException {
   132 		if (index < args.length) {
   133 			return args[index];
   134 		} else {
   135 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
   136 		}
   137 	}
   138 
   139 	public static class Tokens {
   140 
   141 		public static final String DB = "--db";
   142 		public static final String SQL = "--sql";
   143 		public static final String BATCH = "--batch";
   144 		public static final String DATA = "--data";
   145 		public static final String DATA_NAMED = "--data-named";
   146 		public static final String NAME_PREFIX = "--name-prefix";
   147 		public static final String NAME_SUFFIX = "--name-suffix";
   148 		public static final String TYPES = "--types";
   149 		public static final String FORMATTER = "--formatter";
   150 		public static final String INFO_HELP = "--help";
   151 		public static final String INFO_VERSION = "--version";
   152 		public static final String INFO_LICENSE = "--license";
   153 		public static final String INFO_FORMATTERS = "--list-formatters";
   154 		public static final String INFO_TYPES = "--list-types";
   155 		public static final String INFO_DATABASES = "--list-databases";
   156 		public static final String INFO_CONNECTION = "--test-connection";
   157 
   158 		private Tokens() {
   159 		}
   160 	}
   161 
   162 	private SQLType getType(String typeString) throws CLIParserException {
   163 		try {
   164 			return SQLType.valueOf(typeString.trim());
   165 		} catch (IllegalArgumentException e) {
   166 			throw new CLIParserException("Unsupported type: " + typeString, e);
   167 		}
   168 	}
   169 }