1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java Mon Mar 04 17:06:42 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,216 +0,0 @@
1.4 -/**
1.5 - * SQL-DK
1.6 - * Copyright © 2013 František Kučera (frantovo.cz)
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License, or
1.11 - * (at your option) any later version.
1.12 - *
1.13 - * This program is distributed in the hope that it will be useful,
1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 - * GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License
1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 - */
1.21 -package info.globalcode.sql.dk;
1.22 -
1.23 -import static info.globalcode.sql.dk.Functions.readString;
1.24 -import info.globalcode.sql.dk.InfoLister.InfoType;
1.25 -import info.globalcode.sql.dk.configuration.Property;
1.26 -import java.io.IOException;
1.27 -import java.io.InputStream;
1.28 -import java.util.ArrayList;
1.29 -import java.util.HashMap;
1.30 -import java.util.List;
1.31 -import java.util.Map;
1.32 -
1.33 -/**
1.34 - * Converts command line arguments from String array to object.
1.35 - * Checks basic constraints (if only supported options are used and if they have correct number of
1.36 - * parameters)
1.37 - *
1.38 - * @author Ing. František Kučera (frantovo.cz)
1.39 - */
1.40 -public class CLIParser {
1.41 -
1.42 - public static final String TYPE_NAME_SEPARATOR = ":";
1.43 -
1.44 - public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
1.45 - CLIOptions options = new CLIOptions();
1.46 -
1.47 - List<SQLType> numberedTypes = new ArrayList<>();
1.48 - Map<String, SQLType> namedTypes = new HashMap<>();
1.49 -
1.50 - for (int i = 0; i < args.length; i++) {
1.51 - String arg = args[i];
1.52 - switch (arg) {
1.53 - case Tokens.TYPES:
1.54 - String typesString = fetchNext(args, ++i);
1.55 -
1.56 - for (String oneType : typesString.split(",")) {
1.57 - int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
1.58 - if (sepatratorIndex == -1) {
1.59 - numberedTypes.add(getType(oneType.toUpperCase()));
1.60 - } else {
1.61 - String namePart = oneType.substring(0, sepatratorIndex).trim();
1.62 - String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
1.63 - namedTypes.put(namePart, getType(typePart.toUpperCase()));
1.64 - }
1.65 - }
1.66 - break;
1.67 - case Tokens.NAME_PREFIX:
1.68 - options.setNamePrefix(fetchNext(args, ++i));
1.69 - break;
1.70 - case Tokens.NAME_SUFFIX:
1.71 - options.setNameSuffix(fetchNext(args, ++i));
1.72 - break;
1.73 - case Tokens.DB:
1.74 - options.setDatabaseName(fetchNext(args, ++i));
1.75 - break;
1.76 - case Tokens.SQL:
1.77 - options.setSql(fetchNext(args, ++i));
1.78 - break;
1.79 - case Tokens.SQL_IN:
1.80 - try {
1.81 - options.setSql(readString(in));
1.82 - } catch (IOException e) {
1.83 - throw new CLIParserException("Unable to read SQL from the input stream", e);
1.84 - }
1.85 - break;
1.86 - case Tokens.BATCH:
1.87 - options.setBatch(true);
1.88 - break;
1.89 - case Tokens.DATA: // --data is the last option
1.90 - for (i++; i < args.length; i++) {
1.91 - arg = args[i];
1.92 - Parameter parameter;
1.93 - if (numberedTypes.isEmpty()) {
1.94 - parameter = new Parameter(arg, null);
1.95 - } else {
1.96 - int paramIndex = options.getNumberedParameters().size();
1.97 - SQLType paramType;
1.98 - try {
1.99 - paramType = numberedTypes.get(paramIndex);
1.100 - } catch (IndexOutOfBoundsException e) {
1.101 - throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
1.102 - } catch (NullPointerException e) {
1.103 - throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
1.104 - }
1.105 - parameter = new Parameter(arg, paramType);
1.106 - }
1.107 - options.addNumberedParameter(parameter);
1.108 - }
1.109 - break;
1.110 - case Tokens.DATA_NAMED:
1.111 - for (i++; i < args.length; i++) {
1.112 - String paramName = args[i];
1.113 - String paramValue = fetchNext(args, ++i);
1.114 - options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
1.115 - }
1.116 - break;
1.117 - case Tokens.FORMATTER:
1.118 - options.setFormatterName(fetchNext(args, ++i));
1.119 - break;
1.120 - case Tokens.DB_PROPERTY:
1.121 - options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
1.122 - break;
1.123 - case Tokens.FORMATTER_PROPERTY:
1.124 - options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
1.125 - break;
1.126 - case Tokens.INFO_HELP:
1.127 - options.addShowInfo(InfoType.HELP);
1.128 - break;
1.129 - case Tokens.INFO_FORMATTERS:
1.130 - options.addShowInfo(InfoType.FORMATTERS);
1.131 - break;
1.132 - case Tokens.INFO_FORMATTER_PROPERTIES:
1.133 - options.addShowInfo(InfoType.FORMATTER_PROPERTIES);
1.134 - options.addFormatterNameToListProperties(fetchNext(args, ++i));
1.135 - break;
1.136 - case Tokens.INFO_LICENSE:
1.137 - options.addShowInfo(InfoType.LICENSE);
1.138 - break;
1.139 - case Tokens.INFO_JAVA_PROPERTIES:
1.140 - options.addShowInfo(InfoType.JAVA_PROPERTIES);
1.141 - break;
1.142 - case Tokens.INFO_ENVIRONMENT_VARIABLES:
1.143 - options.addShowInfo(InfoType.ENVIRONMENT_VARIABLES);
1.144 - break;
1.145 - case Tokens.INFO_TYPES:
1.146 - options.addShowInfo(InfoType.TYPES);
1.147 - break;
1.148 - case Tokens.INFO_VERSION:
1.149 - options.addShowInfo(InfoType.VERSION);
1.150 - break;
1.151 - case Tokens.INFO_JDBC_DRIVERS:
1.152 - options.addShowInfo(InfoType.JDBC_DRIVERS);
1.153 - break;
1.154 - case Tokens.INFO_JDBC_PROPERTIES:
1.155 - options.addShowInfo(InfoType.JDBC_PROPERTIES);
1.156 - options.addDatabaseNameToListProperties(fetchNext(args, ++i));
1.157 - break;
1.158 - case Tokens.INFO_DATABASES:
1.159 - options.addShowInfo(InfoType.DATABASES);
1.160 - break;
1.161 - case Tokens.INFO_CONNECTION:
1.162 - options.addShowInfo(InfoType.CONNECTION);
1.163 - options.addDatabaseNameToTest(fetchNext(args, ++i));
1.164 - break;
1.165 - default:
1.166 - throw new CLIParserException("Unknown option: " + arg);
1.167 - }
1.168 - }
1.169 - return options;
1.170 - }
1.171 -
1.172 - private String fetchNext(String[] args, int index) throws CLIParserException {
1.173 - if (index < args.length) {
1.174 - return args[index];
1.175 - } else {
1.176 - throw new CLIParserException("Expecting value for option: " + args[index - 1]);
1.177 - }
1.178 - }
1.179 -
1.180 - public static class Tokens {
1.181 -
1.182 - // bash-completion:options:
1.183 - public static final String DB = "--db"; // bash-completion:option // help: database name
1.184 - public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
1.185 - public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
1.186 - public static final String SQL_IN = "--sql-in"; // bash-completion:option // help: SQL query/command
1.187 - public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
1.188 - public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
1.189 - public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
1.190 - public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
1.191 - public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
1.192 - public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
1.193 - public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
1.194 - public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
1.195 - public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
1.196 - public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
1.197 - public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
1.198 - public static final String INFO_JAVA_PROPERTIES = "--list-java-properties"; // bash-completion:option // help: list of Java system properties
1.199 - public static final String INFO_ENVIRONMENT_VARIABLES = "--list-environment-variables"; // bash-completion:option // help: list of environment variables
1.200 - public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
1.201 - public static final String INFO_FORMATTER_PROPERTIES = "--list-formatter-properties"; // bash-completion:option // help: print list of available properties for given formatter
1.202 - public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
1.203 - public static final String INFO_JDBC_DRIVERS = "--list-jdbc-drivers"; // bash-completion:option // help: list of available JDBC drivers
1.204 - public static final String INFO_JDBC_PROPERTIES = "--list-jdbc-properties"; // bash-completion:option // help: list of available JDBC properties for given database
1.205 - public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
1.206 - public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
1.207 -
1.208 - private Tokens() {
1.209 - }
1.210 - }
1.211 -
1.212 - private SQLType getType(String typeString) throws CLIParserException {
1.213 - try {
1.214 - return SQLType.valueOf(typeString.trim());
1.215 - } catch (IllegalArgumentException e) {
1.216 - throw new CLIParserException("Unsupported type: " + typeString, e);
1.217 - }
1.218 - }
1.219 -}