SQLType enum wrapper for java.sql.Types v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 11:58:14 +0100
branchv_0
changeset 68574cd7fbb5b2
parent 67 10c9b9e54622
child 69 0befec5034c2
SQLType enum wrapper for java.sql.Types
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java
java/sql-dk/src/info/globalcode/sql/dk/Parameter.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java
java/sql-dk/src/info/globalcode/sql/dk/SQLType.java
java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Thu Dec 26 01:53:15 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Thu Dec 26 11:58:14 2013 +0100
     1.3 @@ -17,9 +17,7 @@
     1.4   */
     1.5  package info.globalcode.sql.dk;
     1.6  
     1.7 -import java.sql.Types;
     1.8  import java.util.ArrayList;
     1.9 -import java.util.Collections;
    1.10  import java.util.HashMap;
    1.11  import java.util.List;
    1.12  import java.util.Map;
    1.13 @@ -31,24 +29,12 @@
    1.14  public class CLIParser {
    1.15  
    1.16  	public static final String TYPE_NAME_SEPARATOR = ":";
    1.17 -	private final Map<String, Integer> types;
    1.18 -
    1.19 -	public CLIParser() {
    1.20 -		Map<String, Integer> m = new HashMap<>();
    1.21 -		m.put("integer", Types.INTEGER);
    1.22 -		m.put("varchar", Types.VARCHAR);
    1.23 -		m.put("boolean", Types.BOOLEAN);
    1.24 -		/**
    1.25 -		 * TODO: more types
    1.26 -		 */
    1.27 -		types = Collections.unmodifiableMap(m);
    1.28 -	}
    1.29  
    1.30  	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    1.31  		CLIOptions options = new CLIOptions();
    1.32  
    1.33 -		List<Integer> numberedTypes = new ArrayList<>();
    1.34 -		Map<String, Integer> namedTypes = new HashMap<>();
    1.35 +		List<SQLType> numberedTypes = new ArrayList<>();
    1.36 +		Map<String, SQLType> namedTypes = new HashMap<>();
    1.37  
    1.38  		for (int i = 0; i < args.length; i++) {
    1.39  			String arg = args[i];
    1.40 @@ -90,7 +76,7 @@
    1.41  							parameter = new Parameter(arg, null);
    1.42  						} else {
    1.43  							int paramIndex = options.getNumberedParameters().size();
    1.44 -							int paramType;
    1.45 +							SQLType paramType;
    1.46  							try {
    1.47  								paramType = numberedTypes.get(paramIndex);
    1.48  							} catch (IndexOutOfBoundsException e) {
    1.49 @@ -173,12 +159,11 @@
    1.50  		}
    1.51  	}
    1.52  
    1.53 -	private int getType(String typeString) throws CLIParserException {
    1.54 -		Integer type = types.get(typeString.trim());
    1.55 -		if (type == null) {
    1.56 -			throw new CLIParserException("Unsupported type: " + typeString);
    1.57 -		} else {
    1.58 -			return type;
    1.59 +	private SQLType getType(String typeString) throws CLIParserException {
    1.60 +		try {
    1.61 +			return SQLType.valueOf(typeString.trim());
    1.62 +		} catch (IllegalArgumentException e) {
    1.63 +			throw new CLIParserException("Unsupported type: " + typeString, e);
    1.64  		}
    1.65  	}
    1.66  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Thu Dec 26 01:53:15 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java	Thu Dec 26 11:58:14 2013 +0100
     2.3 @@ -27,11 +27,12 @@
     2.4  
     2.5  	private String name;
     2.6  
     2.7 -	public NamedParameter(String name, Object value, Integer type) {
     2.8 +	public NamedParameter(String name, Object value, SQLType type) {
     2.9  		super(value, type);
    2.10  		this.name = name;
    2.11  	}
    2.12  
    2.13 +	@Override
    2.14  	public String getName() {
    2.15  		return name;
    2.16  	}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Thu Dec 26 01:53:15 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java	Thu Dec 26 11:58:14 2013 +0100
     3.3 @@ -28,14 +28,14 @@
     3.4  	/**
     3.5  	 * @see Types
     3.6  	 */
     3.7 -	public static final int DEFAULT_TYPE = Types.VARCHAR;
     3.8 +	public static final SQLType DEFAULT_TYPE = SQLType.VARCHAR;
     3.9  	private Object value;
    3.10 -	private int type;
    3.11 +	private SQLType type;
    3.12  
    3.13  	public Parameter() {
    3.14  	}
    3.15  
    3.16 -	public Parameter(Object value, Integer type) {
    3.17 +	public Parameter(Object value, SQLType type) {
    3.18  		this.value = value;
    3.19  		if (type == null) {
    3.20  			this.type = DEFAULT_TYPE;
    3.21 @@ -55,14 +55,14 @@
    3.22  	/**
    3.23  	 * @see java.sql.Types
    3.24  	 */
    3.25 -	public int getType() {
    3.26 +	public SQLType getType() {
    3.27  		return type;
    3.28  	}
    3.29  
    3.30  	/**
    3.31  	 * @see java.sql.Types
    3.32  	 */
    3.33 -	public void setType(int type) {
    3.34 +	public void setType(SQLType type) {
    3.35  		this.type = type;
    3.36  	}
    3.37  }
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Thu Dec 26 01:53:15 2013 +0100
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Thu Dec 26 11:58:14 2013 +0100
     4.3 @@ -68,7 +68,7 @@
     4.4  	public void parametrize(PreparedStatement ps) throws SQLException {
     4.5  		int i = 1;
     4.6  		for (Parameter p : notNull(parametersUsed)) {
     4.7 -			ps.setObject(i++, p.getValue(), p.getType());
     4.8 +			ps.setObject(i++, p.getValue(), p.getType().getCode());
     4.9  		}
    4.10  	}
    4.11  
     5.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java	Thu Dec 26 01:53:15 2013 +0100
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java	Thu Dec 26 11:58:14 2013 +0100
     5.3 @@ -39,7 +39,7 @@
     5.4  	public void parametrize(PreparedStatement ps) throws SQLException {
     5.5  		int i = 1;
     5.6  		for (Parameter p : notNull(parameters)) {
     5.7 -			ps.setObject(i++, p.getValue(), p.getType());
     5.8 +			ps.setObject(i++, p.getValue(), p.getType().getCode());
     5.9  		}
    5.10  	}
    5.11  
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLType.java	Thu Dec 26 11:58:14 2013 +0100
     6.3 @@ -0,0 +1,61 @@
     6.4 +/**
     6.5 + * SQL-DK
     6.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, either version 3 of the License, or
    6.11 + * (at your option) any later version.
    6.12 + *
    6.13 + * This program is distributed in the hope that it will be useful,
    6.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    6.16 + * GNU General Public License for more details.
    6.17 + *
    6.18 + * You should have received a copy of the GNU General Public License
    6.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    6.20 + */
    6.21 +package info.globalcode.sql.dk;
    6.22 +
    6.23 +import java.sql.Types;
    6.24 +
    6.25 +/**
    6.26 + *
    6.27 + * @author Ing. František Kučera (frantovo.cz)
    6.28 + */
    6.29 +public enum SQLType {
    6.30 +
    6.31 +	VARCHAR(Types.VARCHAR),
    6.32 +	BOOLEAN(Types.BOOLEAN),
    6.33 +	INTEGER(Types.INTEGER),
    6.34 +	DECIMAL(Types.DECIMAL);
    6.35 +	/**
    6.36 +	 * TODO: more types
    6.37 +	 */
    6.38 +	private int code;
    6.39 +
    6.40 +	private SQLType(int code) {
    6.41 +		this.code = code;
    6.42 +	}
    6.43 +
    6.44 +	/**
    6.45 +	 * @see java.sql.Types.Types
    6.46 +	 */
    6.47 +	public int getCode() {
    6.48 +		return code;
    6.49 +	}
    6.50 +
    6.51 +	/**
    6.52 +	 * @param code see {@linkplain java.sql.Types.Types}
    6.53 +	 * @return found SQLType
    6.54 +	 * @throws IllegalArgumentException if no data type has given code
    6.55 +	 */
    6.56 +	public SQLType valueOf(int code) {
    6.57 +		for (SQLType t : values()) {
    6.58 +			if (t.code == code) {
    6.59 +				return t;
    6.60 +			}
    6.61 +		}
    6.62 +		throw new IllegalArgumentException("No data type has code: " + code);
    6.63 +	}
    6.64 +}
     7.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Thu Dec 26 01:53:15 2013 +0100
     7.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Thu Dec 26 11:58:14 2013 +0100
     7.3 @@ -19,7 +19,6 @@
     7.4  
     7.5  import info.globalcode.sql.dk.CLIParser.Tokens;
     7.6  import static info.globalcode.sql.dk.CLIParser.TYPE_NAME_SEPARATOR;
     7.7 -import java.sql.Types;
     7.8  import java.util.Collection;
     7.9  import static org.testng.Assert.*;
    7.10  import org.testng.annotations.BeforeMethod;
    7.11 @@ -87,7 +86,7 @@
    7.12  		String[] args = new String[]{
    7.13  			Tokens.DB, DATABASE_NAME_1,
    7.14  			Tokens.SQL, SQL_1,
    7.15 -			Tokens.TYPES, " int,string, boolean",
    7.16 +			Tokens.TYPES, " INTEGER,VARCHAR, BOOLEAN",
    7.17  			Tokens.DATA, DATA_1, DATA_2, DATA_3};
    7.18  		CLIOptions options = parser.parseOptions(args);
    7.19  		options.validate();
    7.20 @@ -99,9 +98,9 @@
    7.21  		assertEquals(options.getNumberedParameters().get(0).getValue(), DATA_1);
    7.22  		assertEquals(options.getNumberedParameters().get(1).getValue(), DATA_2);
    7.23  		assertEquals(options.getNumberedParameters().get(2).getValue(), DATA_3);
    7.24 -		assertEquals(options.getNumberedParameters().get(0).getType(), Types.INTEGER);
    7.25 -		assertEquals(options.getNumberedParameters().get(1).getType(), Types.VARCHAR);
    7.26 -		assertEquals(options.getNumberedParameters().get(2).getType(), Types.BOOLEAN);
    7.27 +		assertEquals(options.getNumberedParameters().get(0).getType(), SQLType.INTEGER);
    7.28 +		assertEquals(options.getNumberedParameters().get(1).getType(), SQLType.VARCHAR);
    7.29 +		assertEquals(options.getNumberedParameters().get(2).getType(), SQLType.BOOLEAN);
    7.30  	}
    7.31  
    7.32  	@Test
    7.33 @@ -128,7 +127,7 @@
    7.34  			Tokens.DB, DATABASE_NAME_1,
    7.35  			Tokens.SQL, SQL_1,
    7.36  			Tokens.NAME_PREFIX, "$",
    7.37 -			Tokens.TYPES, " " + NAME_1 + TYPE_NAME_SEPARATOR + "int" + "," + NAME_3 + TYPE_NAME_SEPARATOR + "boolean",
    7.38 +			Tokens.TYPES, " " + NAME_1 + TYPE_NAME_SEPARATOR + "INTEGER" + "," + NAME_3 + TYPE_NAME_SEPARATOR + "BOOLEAN",
    7.39  			Tokens.DATA_NAMED, NAME_1, DATA_1, NAME_2, DATA_2, NAME_3, DATA_3};
    7.40  		CLIOptions options = parser.parseOptions(args);
    7.41  		options.validate();
    7.42 @@ -137,12 +136,12 @@
    7.43  		assertEquals(options.getSql(), SQL_1);
    7.44  		assertEquals(options.getMode(), CLIOptions.MODE.QUERY_NOW);
    7.45  		assertEquals(options.getNamedParameters().size(), 3);
    7.46 -		assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, Types.INTEGER);
    7.47 +		assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, SQLType.INTEGER);
    7.48  		assertNamedParameter(options.getNamedParameters(), NAME_2, DATA_2, Parameter.DEFAULT_TYPE);
    7.49 -		assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, Types.BOOLEAN);
    7.50 +		assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, SQLType.BOOLEAN);
    7.51  	}
    7.52  
    7.53 -	private void assertNamedParameter(Collection<NamedParameter> params, String name, Object value, int type) {
    7.54 +	private void assertNamedParameter(Collection<NamedParameter> params, String name, Object value, SQLType type) {
    7.55  		for (NamedParameter p : params) {
    7.56  			if (name.equals(p.getName())) {
    7.57  				assertEquals(p.getValue(), value, "value does not match – name: " + name);