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