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