more exceptions v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 23:54:51 +0100
branchv_0
changeset 84507cb9a0cf1
parent 7 a7555ec6eea9
child 9 2ec52027b97f
more exceptions
java/sql-dk/nbproject/project.properties
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
     1.1 --- a/java/sql-dk/nbproject/project.properties	Sun Dec 15 23:54:37 2013 +0100
     1.2 +++ b/java/sql-dk/nbproject/project.properties	Sun Dec 15 23:54:51 2013 +0100
     1.3 @@ -52,7 +52,7 @@
     1.4  javadoc.use=true
     1.5  javadoc.version=false
     1.6  javadoc.windowtitle=
     1.7 -main.class=
     1.8 +main.class=info.globalcode.sql.dk.CLIStarter
     1.9  manifest.file=manifest.mf
    1.10  meta.inf.dir=${src.dir}/META-INF
    1.11  mkdist.disabled=false
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Sun Dec 15 23:54:37 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Sun Dec 15 23:54:51 2013 +0100
     2.3 @@ -37,7 +37,7 @@
     2.4  			String arg = args[i];
     2.5  			switch (arg) {
     2.6  				case Tokens.TYPES:
     2.7 -					String typesString = args[++i];
     2.8 +					String typesString = fetchNext(args, ++i);
     2.9  
    2.10  					for (String oneType : typesString.split("\\s*,\\s*")) {
    2.11  						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    2.12 @@ -51,18 +51,18 @@
    2.13  					}
    2.14  					break;
    2.15  				case Tokens.NAME_PREFIX:
    2.16 -					options.setNamePrefix(args[++i]);
    2.17 +					options.setNamePrefix(fetchNext(args, ++i));
    2.18  					break;
    2.19  				case Tokens.DB:
    2.20 -					options.setDatabaseName(args[++i]);
    2.21 +					options.setDatabaseName(fetchNext(args, ++i));
    2.22  					break;
    2.23  				case Tokens.SQL:
    2.24 -					options.setSql(args[++i]);
    2.25 +					options.setSql(fetchNext(args, ++i));
    2.26  					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
    2.27  					break;
    2.28  				case Tokens.SQL_UPDATE:
    2.29  				case Tokens.SQL_INSERT:
    2.30 -					options.setSql(args[++i]);
    2.31 +					options.setSql(fetchNext(args, ++i));
    2.32  					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
    2.33  					break;
    2.34  				case Tokens.BATCH:
    2.35 @@ -72,21 +72,27 @@
    2.36  					for (i++; i < args.length; i++) {
    2.37  						arg = args[i];
    2.38  
    2.39 -						if (arg.startsWith(options.getNamePrefix())) {
    2.40 +						if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
    2.41  							String paramName = arg.substring(options.getNamePrefix().length());
    2.42 -							String paramValue = args[++i];
    2.43 +							String paramValue = fetchNext(args, ++i);
    2.44  							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    2.45 -						} else {
    2.46 -							int paramIndex = options.getNumberedParameters().size();
    2.47 -							int paramType;
    2.48 -							try {
    2.49 -								paramType = numberedTypes.get(paramIndex);
    2.50 -							} catch (IndexOutOfBoundsException e) {
    2.51 -								throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    2.52 -							} catch (NullPointerException e) {
    2.53 -								throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    2.54 +						} else { // Numbered parameters:
    2.55 +							Parameter parameter;
    2.56 +							if (numberedTypes.isEmpty()) {
    2.57 +								parameter = new Parameter(arg, null);
    2.58 +							} else {
    2.59 +								int paramIndex = options.getNumberedParameters().size();
    2.60 +								int paramType;
    2.61 +								try {
    2.62 +									paramType = numberedTypes.get(paramIndex);
    2.63 +								} catch (IndexOutOfBoundsException e) {
    2.64 +									throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    2.65 +								} catch (NullPointerException e) {
    2.66 +									throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    2.67 +								}
    2.68 +								parameter = new Parameter(arg, paramType);
    2.69  							}
    2.70 -							options.addNumberedParameter(new Parameter(arg, paramType));
    2.71 +							options.addNumberedParameter(parameter);
    2.72  						}
    2.73  					}
    2.74  					break;
    2.75 @@ -94,11 +100,15 @@
    2.76  					throw new IllegalArgumentException("Unknown option: " + arg);
    2.77  			}
    2.78  		}
    2.79 +		return options;
    2.80 +	}
    2.81  
    2.82 -
    2.83 -
    2.84 -		return options;
    2.85 -
    2.86 +	private String fetchNext(String[] args, int index) {
    2.87 +		if (index < args.length) {
    2.88 +			return args[index];
    2.89 +		} else {
    2.90 +			throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]);
    2.91 +		}
    2.92  	}
    2.93  
    2.94  	public static class Tokens {
    2.95 @@ -116,7 +126,12 @@
    2.96  		}
    2.97  	}
    2.98  
    2.99 -	private Integer getType(String typeString) {
   2.100 -		return types.get(typeString);
   2.101 +	private int getType(String typeString) {
   2.102 +		Integer type = types.get(typeString);
   2.103 +		if (type == null) {
   2.104 +			throw new IllegalArgumentException("Unsupported type: " + typeString);
   2.105 +		} else {
   2.106 +			return type;
   2.107 +		}
   2.108  	}
   2.109  }