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