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