java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 05 Mar 2019 22:03:02 +0100
branchv_0
changeset 246 277c18b48762
parent 238 4a1864c3e867
child 250 aae5009bd0af
permissions -rw-r--r--
support custom relation names in the XML formatter (added --relation CLI option)
     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 static info.globalcode.sql.dk.Functions.readString;
    21 import info.globalcode.sql.dk.InfoLister.InfoType;
    22 import info.globalcode.sql.dk.configuration.Property;
    23 import java.io.IOException;
    24 import java.io.InputStream;
    25 import java.util.ArrayList;
    26 import java.util.HashMap;
    27 import java.util.List;
    28 import java.util.Map;
    29 
    30 /**
    31  * Converts command line arguments from String array to object. Checks basic constraints (if only
    32  * supported options are used and if they have correct number of parameters)
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class CLIParser {
    37 
    38 	public static final String TYPE_NAME_SEPARATOR = ":";
    39 
    40 	public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
    41 		CLIOptions options = new CLIOptions();
    42 
    43 		List<SQLType> numberedTypes = new ArrayList<>();
    44 		Map<String, SQLType> namedTypes = new HashMap<>();
    45 
    46 		for (int i = 0; i < args.length; i++) {
    47 			String arg = args[i];
    48 			switch (arg) {
    49 				case Tokens.TYPES:
    50 					String typesString = fetchNext(args, ++i);
    51 
    52 					for (String oneType : typesString.split(",")) {
    53 						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    54 						if (sepatratorIndex == -1) {
    55 							numberedTypes.add(getType(oneType.toUpperCase()));
    56 						} else {
    57 							String namePart = oneType.substring(0, sepatratorIndex).trim();
    58 							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    59 							namedTypes.put(namePart, getType(typePart.toUpperCase()));
    60 						}
    61 					}
    62 					break;
    63 				case Tokens.NAME_PREFIX:
    64 					options.setNamePrefix(fetchNext(args, ++i));
    65 					break;
    66 				case Tokens.NAME_SUFFIX:
    67 					options.setNameSuffix(fetchNext(args, ++i));
    68 					break;
    69 				case Tokens.DB:
    70 					options.setDatabaseName(fetchNext(args, ++i));
    71 					break;
    72 				case Tokens.SQL:
    73 					options.setSql(fetchNext(args, ++i));
    74 					break;
    75 				case Tokens.SQL_IN:
    76 					try {
    77 						options.setSql(readString(in));
    78 					} catch (IOException e) {
    79 						throw new CLIParserException("Unable to read SQL from the input stream", e);
    80 					}
    81 					break;
    82 				case Tokens.BATCH:
    83 					options.setBatch(true);
    84 					break;
    85 				case Tokens.DATA: // --data is the last option
    86 					for (i++; i < args.length; i++) {
    87 						arg = args[i];
    88 						Parameter parameter;
    89 						if (numberedTypes.isEmpty()) {
    90 							parameter = new Parameter(arg, null);
    91 						} else {
    92 							int paramIndex = options.getNumberedParameters().size();
    93 							SQLType paramType;
    94 							try {
    95 								paramType = numberedTypes.get(paramIndex);
    96 							} catch (IndexOutOfBoundsException e) {
    97 								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
    98 							} catch (NullPointerException e) {
    99 								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
   100 							}
   101 							parameter = new Parameter(arg, paramType);
   102 						}
   103 						options.addNumberedParameter(parameter);
   104 					}
   105 					break;
   106 				case Tokens.DATA_NAMED:
   107 					for (i++; i < args.length; i++) {
   108 						String paramName = args[i];
   109 						String paramValue = fetchNext(args, ++i);
   110 						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
   111 					}
   112 					break;
   113 				case Tokens.RELATION:
   114 					options.addRelationName(fetchNext(args, ++i));
   115 					break;
   116 				case Tokens.FORMATTER:
   117 					options.setFormatterName(fetchNext(args, ++i));
   118 					break;
   119 				case Tokens.DB_PROPERTY:
   120 					options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
   121 					break;
   122 				case Tokens.FORMATTER_PROPERTY:
   123 					options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
   124 					break;
   125 				case Tokens.INFO_HELP:
   126 					options.addShowInfo(InfoType.HELP);
   127 					break;
   128 				case Tokens.INFO_FORMATTERS:
   129 					options.addShowInfo(InfoType.FORMATTERS);
   130 					break;
   131 				case Tokens.INFO_FORMATTER_PROPERTIES:
   132 					options.addShowInfo(InfoType.FORMATTER_PROPERTIES);
   133 					options.addFormatterNameToListProperties(fetchNext(args, ++i));
   134 					break;
   135 				case Tokens.INFO_LICENSE:
   136 					options.addShowInfo(InfoType.LICENSE);
   137 					break;
   138 				case Tokens.INFO_JAVA_PROPERTIES:
   139 					options.addShowInfo(InfoType.JAVA_PROPERTIES);
   140 					break;
   141 				case Tokens.INFO_ENVIRONMENT_VARIABLES:
   142 					options.addShowInfo(InfoType.ENVIRONMENT_VARIABLES);
   143 					break;
   144 				case Tokens.INFO_TYPES:
   145 					options.addShowInfo(InfoType.TYPES);
   146 					break;
   147 				case Tokens.INFO_VERSION:
   148 					options.addShowInfo(InfoType.VERSION);
   149 					break;
   150 				case Tokens.INFO_JDBC_DRIVERS:
   151 					options.addShowInfo(InfoType.JDBC_DRIVERS);
   152 					break;
   153 				case Tokens.INFO_JDBC_PROPERTIES:
   154 					options.addShowInfo(InfoType.JDBC_PROPERTIES);
   155 					options.addDatabaseNameToListProperties(fetchNext(args, ++i));
   156 					break;
   157 				case Tokens.INFO_DATABASES:
   158 					options.addShowInfo(InfoType.DATABASES);
   159 					break;
   160 				case Tokens.INFO_CONNECTION:
   161 					options.addShowInfo(InfoType.CONNECTION);
   162 					options.addDatabaseNameToTest(fetchNext(args, ++i));
   163 					break;
   164 				default:
   165 					throw new CLIParserException("Unknown option: " + arg);
   166 			}
   167 		}
   168 		return options;
   169 	}
   170 
   171 	private String fetchNext(String[] args, int index) throws CLIParserException {
   172 		if (index < args.length) {
   173 			return args[index];
   174 		} else {
   175 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
   176 		}
   177 	}
   178 
   179 	public static class Tokens {
   180 
   181 		// bash-completion:options:
   182 		public static final String DB = "--db"; // bash-completion:option // help: database name
   183 		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
   184 		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
   185 		public static final String SQL_IN = "--sql-in"; // bash-completion:option // help: SQL query/command
   186 		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
   187 		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
   188 		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
   189 		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
   190 		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
   191 		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
   192 		public static final String RELATION = "--relation"; // bash-completion:option // help: name of the output relation (result set)
   193 		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
   194 		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
   195 		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
   196 		public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
   197 		public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
   198 		public static final String INFO_JAVA_PROPERTIES = "--list-java-properties"; // bash-completion:option // help: list of Java system properties
   199 		public static final String INFO_ENVIRONMENT_VARIABLES = "--list-environment-variables"; // bash-completion:option // help: list of environment variables
   200 		public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
   201 		public static final String INFO_FORMATTER_PROPERTIES = "--list-formatter-properties"; // bash-completion:option // help: print list of available properties for given formatter
   202 		public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
   203 		public static final String INFO_JDBC_DRIVERS = "--list-jdbc-drivers"; // bash-completion:option // help: list of available JDBC drivers
   204 		public static final String INFO_JDBC_PROPERTIES = "--list-jdbc-properties"; // bash-completion:option // help: list of available JDBC properties for given database
   205 		public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
   206 		public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
   207 
   208 		private Tokens() {
   209 		}
   210 	}
   211 
   212 	private SQLType getType(String typeString) throws CLIParserException {
   213 		try {
   214 			return SQLType.valueOf(typeString.trim());
   215 		} catch (IllegalArgumentException e) {
   216 			throw new CLIParserException("Unsupported type: " + typeString, e);
   217 		}
   218 	}
   219 }