more code v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 20:16:27 +0100
branchv_0
changeset 272da10f632b5
parent 1 f32dac78d13a
child 3 efdf2b886feb
more code
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Sun Dec 15 19:20:50 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Sun Dec 15 20:16:27 2013 +0100
     1.3 @@ -14,7 +14,13 @@
     1.4  	private String sql;
     1.5  	private String databaseName;
     1.6  	private boolean batch;
     1.7 -	
     1.8 +
     1.9 +	public enum MODE {
    1.10 +
    1.11 +		QUERY_NOW,
    1.12 +		PREPARE_BATCH,
    1.13 +		EXECUTE_BATCH
    1.14 +	}
    1.15  
    1.16  	public enum COMMAND_TYPE {
    1.17  
    1.18 @@ -30,12 +36,7 @@
    1.19  	public void validate() throws InvalidOptionsException {
    1.20  		InvalidOptionsException e = new InvalidOptionsException();
    1.21  
    1.22 -		if ( //
    1.23 -				(hasDb() ? 1 : 0) + //
    1.24 -				(hasSql() ? 1 : 0) + //
    1.25 -				(hasBatch() ? 1 : 0)
    1.26 -				!= 2) //
    1.27 -		{
    1.28 +		if (getMode() == null) {
    1.29  			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
    1.30  		}
    1.31  
    1.32 @@ -49,15 +50,73 @@
    1.33  		}
    1.34  	}
    1.35  
    1.36 -	public boolean hasSql() {
    1.37 -		return isNotEmpty(sql, true);
    1.38 +	private boolean hasSql() {
    1.39 +		return isNotEmpty(getSql(), true);
    1.40  	}
    1.41  
    1.42 -	public boolean hasDb() {
    1.43 -		return isNotEmpty(databaseName, true);
    1.44 +	private boolean hasDb() {
    1.45 +		return isNotEmpty(getDatabaseName(), true);
    1.46  	}
    1.47  
    1.48 -	public boolean hasBatch() {
    1.49 -		return batch;
    1.50 +	/**
    1.51 +	 * Depends on options: DB, BATCH, SQL
    1.52 +	 *
    1.53 +	 * @return mode | or null if options are not yet initialized or combination of options is
    1.54 +	 * invalid
    1.55 +	 */
    1.56 +	public MODE getMode() {
    1.57 +		if (hasDb() && !batch && hasSql()) {
    1.58 +			return MODE.QUERY_NOW;
    1.59 +		} else if (!hasDb() && batch && hasSql()) {
    1.60 +			return MODE.PREPARE_BATCH;
    1.61 +		} else if (hasDb() && batch && !hasSql()) {
    1.62 +			return MODE.EXECUTE_BATCH;
    1.63 +		} else {
    1.64 +			return null;
    1.65 +		}
    1.66 +	}
    1.67 +
    1.68 +	public String getSql() {
    1.69 +		return sql;
    1.70 +	}
    1.71 +
    1.72 +	public void setSql(String sql) {
    1.73 +		this.sql = sql;
    1.74 +	}
    1.75 +
    1.76 +	public String getDatabaseName() {
    1.77 +		return databaseName;
    1.78 +	}
    1.79 +
    1.80 +	public void setDatabaseName(String databaseName) {
    1.81 +		this.databaseName = databaseName;
    1.82 +	}
    1.83 +
    1.84 +	public void setBatch(boolean batch) {
    1.85 +		this.batch = batch;
    1.86 +	}
    1.87 +
    1.88 +	public COMMAND_TYPE getCommandType() {
    1.89 +		return commandType;
    1.90 +	}
    1.91 +
    1.92 +	public void setCommandType(COMMAND_TYPE commandType) {
    1.93 +		this.commandType = commandType;
    1.94 +	}
    1.95 +
    1.96 +	public Collection<NamedParameter> getNamedParameters() {
    1.97 +		return namedParameters;
    1.98 +	}
    1.99 +
   1.100 +	public List<Parameter> getNumberedParameters() {
   1.101 +		return numberedParameters;
   1.102 +	}
   1.103 +
   1.104 +	public void addNumberedParameter(Parameter p) {
   1.105 +		numberedParameters.add(p);
   1.106 +	}
   1.107 +
   1.108 +	public void addNamedParameter(NamedParameter p) {
   1.109 +		namedParameters.add(p);
   1.110  	}
   1.111  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 19:20:50 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Dec 15 20:16:27 2013 +0100
     2.3 @@ -6,36 +6,59 @@
     2.4   */
     2.5  public class CLIStarter {
     2.6  
     2.7 +	public static final String DEFAULT_NAME_PREFIX = ":";
     2.8 +
     2.9  	public static void main(String[] args) {
    2.10 -		
    2.11 +
    2.12  		args = new String[]{"--sql", "SELECT * FROM tabulka;", "--db", "databáze_1"};
    2.13 -		
    2.14 +
    2.15  		CLIOptions options = parseOptions(args);
    2.16  	}
    2.17  
    2.18  	private static CLIOptions parseOptions(String[] args) {
    2.19  		CLIOptions options = new CLIOptions();
    2.20  
    2.21 +		String typesString = null;
    2.22 +		String namePrefix = DEFAULT_NAME_PREFIX;
    2.23 +
    2.24 +
    2.25  		for (int i = 0; i < args.length; i++) {
    2.26  			String arg = args[i];
    2.27 +			switch (arg) {
    2.28 +				case Tokens.TYPES:
    2.29 +					typesString = args[++i];
    2.30 +					break;
    2.31 +				case Tokens.NAME_PREFIX:
    2.32 +					namePrefix = args[++i];
    2.33 +					break;
    2.34 +			}
    2.35 +		}
    2.36  
    2.37 +
    2.38 +		for (int i = 0; i < args.length; i++) {
    2.39 +			String arg = args[i];
    2.40  			switch (arg) {
    2.41 +				case Tokens.TYPES:
    2.42 +				case Tokens.NAME_PREFIX:
    2.43 +					i++;
    2.44 +					break;
    2.45  				case Tokens.DB:
    2.46 -					String db = args[++i];
    2.47 -					System.out.println("DB: " + db);
    2.48 +					options.setDatabaseName(args[++i]);
    2.49  					break;
    2.50  				case Tokens.SQL:
    2.51 -					String sql = args[++i];
    2.52 -					System.out.println("SQL: " + sql);
    2.53 +					options.setSql(args[++i]);
    2.54 +					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
    2.55  					break;
    2.56  				case Tokens.SQL_UPDATE:
    2.57 +				case Tokens.SQL_INSERT:
    2.58 +					options.setSql(args[++i]);
    2.59 +					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
    2.60  					break;
    2.61  				case Tokens.BATCH:
    2.62 +					options.setBatch(true);
    2.63  					break;
    2.64  				case Tokens.DATA:
    2.65  					break;
    2.66 -				case Tokens.TYPES:
    2.67 -					break;
    2.68  			}
    2.69  		}
    2.70  
    2.71 @@ -48,8 +71,10 @@
    2.72  		public static final String DB = "--db";
    2.73  		public static final String SQL = "--sql";
    2.74  		public static final String SQL_UPDATE = "--sql-update";
    2.75 +		public static final String SQL_INSERT = "--sql-insert";
    2.76  		public static final String BATCH = "--batch";
    2.77  		public static final String DATA = "--data";
    2.78 +		public static final String NAME_PREFIX = "--name-prefix";
    2.79  		public static final String TYPES = "--types";
    2.80  
    2.81  		private Tokens() {