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

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class CLIParser {

	public static final String TYPE_NAME_SEPARATOR = ":";

	public CLIOptions parseOptions(String[] args) throws CLIParserException {
		CLIOptions options = new CLIOptions();

		List<SQLType> numberedTypes = new ArrayList<>();
		Map<String, SQLType> namedTypes = new HashMap<>();

		for (int i = 0; i < args.length; i++) {
			String arg = args[i];
			switch (arg) {
				case Tokens.TYPES:
					String typesString = fetchNext(args, ++i);

					for (String oneType : typesString.split(",")) {
						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
						if (sepatratorIndex == -1) {
							numberedTypes.add(getType(oneType));
						} else {
							String namePart = oneType.substring(0, sepatratorIndex).trim();
							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
							namedTypes.put(namePart, getType(typePart));
						}
					}
					break;
				case Tokens.NAME_PREFIX:
					options.setNamePrefix(fetchNext(args, ++i));
					break;
				case Tokens.NAME_SUFFIX:
					options.setNameSuffix(fetchNext(args, ++i));
					break;
				case Tokens.DB:
					options.setDatabaseName(fetchNext(args, ++i));
					break;
				case Tokens.SQL:
					options.setSql(fetchNext(args, ++i));
					break;
				case Tokens.BATCH:
					options.setBatch(true);
					break;
				case Tokens.DATA: // --data is the last option
					for (i++; i < args.length; i++) {
						arg = args[i];
						Parameter parameter;
						if (numberedTypes.isEmpty()) {
							parameter = new Parameter(arg, null);
						} else {
							int paramIndex = options.getNumberedParameters().size();
							SQLType paramType;
							try {
								paramType = numberedTypes.get(paramIndex);
							} catch (IndexOutOfBoundsException e) {
								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
							} catch (NullPointerException e) {
								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
							}
							parameter = new Parameter(arg, paramType);
						}
						options.addNumberedParameter(parameter);
					}
					break;
				case Tokens.DATA_NAMED:
					for (i++; i < args.length; i++) {
						String paramName = args[i];
						String paramValue = fetchNext(args, ++i);
						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
					}
					break;
				case Tokens.FORMATTER:
					options.setFormatterName(fetchNext(args, ++i));
					break;
				case Tokens.INFO_HELP:
					options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
					break;
				case Tokens.INFO_FORMATTERS:
					options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
					break;
				case Tokens.INFO_LICENSE:
					options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
					break;
				case Tokens.INFO_TYPES:
					options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
					break;
				case Tokens.INFO_VERSION:
					options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
					break;
				case Tokens.INFO_DATABASES:
					options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
					break;
				case Tokens.INFO_CONNECTION:
					options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
					options.setDatabaseNameToTest(fetchNext(args, ++i));
					break;
				default:
					throw new CLIParserException("Unknown option: " + arg);
			}
		}
		return options;
	}

	private String fetchNext(String[] args, int index) throws CLIParserException {
		if (index < args.length) {
			return args[index];
		} else {
			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
		}
	}

	public static class Tokens {

		public static final String DB = "--db";
		public static final String SQL = "--sql";
		public static final String BATCH = "--batch";
		public static final String DATA = "--data";
		public static final String DATA_NAMED = "--data-named";
		public static final String NAME_PREFIX = "--name-prefix";
		public static final String NAME_SUFFIX = "--name-suffix";
		public static final String TYPES = "--types";
		public static final String FORMATTER = "--formatter";
		public static final String INFO_HELP = "--help";
		public static final String INFO_VERSION = "--version";
		public static final String INFO_LICENSE = "--license";
		public static final String INFO_FORMATTERS = "--list-formatters";
		public static final String INFO_TYPES = "--list-types";
		public static final String INFO_DATABASES = "--list-databases";
		public static final String INFO_CONNECTION = "--test-connection";

		private Tokens() {
		}
	}

	private SQLType getType(String typeString) throws CLIParserException {
		try {
			return SQLType.valueOf(typeString.trim());
		} catch (IllegalArgumentException e) {
			throw new CLIParserException("Unsupported type: " + typeString, e);
		}
	}
}