--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java Thu Dec 26 11:58:14 2013 +0100
@@ -17,9 +17,7 @@
*/
package info.globalcode.sql.dk;
-import java.sql.Types;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -31,24 +29,12 @@
public class CLIParser {
public static final String TYPE_NAME_SEPARATOR = ":";
- private final Map<String, Integer> types;
-
- public CLIParser() {
- Map<String, Integer> m = new HashMap<>();
- m.put("integer", Types.INTEGER);
- m.put("varchar", Types.VARCHAR);
- m.put("boolean", Types.BOOLEAN);
- /**
- * TODO: more types
- */
- types = Collections.unmodifiableMap(m);
- }
public CLIOptions parseOptions(String[] args) throws CLIParserException {
CLIOptions options = new CLIOptions();
- List<Integer> numberedTypes = new ArrayList<>();
- Map<String, Integer> namedTypes = new HashMap<>();
+ List<SQLType> numberedTypes = new ArrayList<>();
+ Map<String, SQLType> namedTypes = new HashMap<>();
for (int i = 0; i < args.length; i++) {
String arg = args[i];
@@ -90,7 +76,7 @@
parameter = new Parameter(arg, null);
} else {
int paramIndex = options.getNumberedParameters().size();
- int paramType;
+ SQLType paramType;
try {
paramType = numberedTypes.get(paramIndex);
} catch (IndexOutOfBoundsException e) {
@@ -173,12 +159,11 @@
}
}
- private int getType(String typeString) throws CLIParserException {
- Integer type = types.get(typeString.trim());
- if (type == null) {
- throw new CLIParserException("Unsupported type: " + typeString);
- } else {
- return type;
+ private SQLType getType(String typeString) throws CLIParserException {
+ try {
+ return SQLType.valueOf(typeString.trim());
+ } catch (IllegalArgumentException e) {
+ throw new CLIParserException("Unsupported type: " + typeString, e);
}
}
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/NamedParameter.java Thu Dec 26 11:58:14 2013 +0100
@@ -27,11 +27,12 @@
private String name;
- public NamedParameter(String name, Object value, Integer type) {
+ public NamedParameter(String name, Object value, SQLType type) {
super(value, type);
this.name = name;
}
+ @Override
public String getName() {
return name;
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Parameter.java Thu Dec 26 11:58:14 2013 +0100
@@ -28,14 +28,14 @@
/**
* @see Types
*/
- public static final int DEFAULT_TYPE = Types.VARCHAR;
+ public static final SQLType DEFAULT_TYPE = SQLType.VARCHAR;
private Object value;
- private int type;
+ private SQLType type;
public Parameter() {
}
- public Parameter(Object value, Integer type) {
+ public Parameter(Object value, SQLType type) {
this.value = value;
if (type == null) {
this.type = DEFAULT_TYPE;
@@ -55,14 +55,14 @@
/**
* @see java.sql.Types
*/
- public int getType() {
+ public SQLType getType() {
return type;
}
/**
* @see java.sql.Types
*/
- public void setType(int type) {
+ public void setType(SQLType type) {
this.type = type;
}
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Thu Dec 26 11:58:14 2013 +0100
@@ -68,7 +68,7 @@
public void parametrize(PreparedStatement ps) throws SQLException {
int i = 1;
for (Parameter p : notNull(parametersUsed)) {
- ps.setObject(i++, p.getValue(), p.getType());
+ ps.setObject(i++, p.getValue(), p.getType().getCode());
}
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java Thu Dec 26 11:58:14 2013 +0100
@@ -39,7 +39,7 @@
public void parametrize(PreparedStatement ps) throws SQLException {
int i = 1;
for (Parameter p : notNull(parameters)) {
- ps.setObject(i++, p.getValue(), p.getType());
+ ps.setObject(i++, p.getValue(), p.getType().getCode());
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLType.java Thu Dec 26 11:58:14 2013 +0100
@@ -0,0 +1,61 @@
+/**
+ * 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.sql.Types;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public enum SQLType {
+
+ VARCHAR(Types.VARCHAR),
+ BOOLEAN(Types.BOOLEAN),
+ INTEGER(Types.INTEGER),
+ DECIMAL(Types.DECIMAL);
+ /**
+ * TODO: more types
+ */
+ private int code;
+
+ private SQLType(int code) {
+ this.code = code;
+ }
+
+ /**
+ * @see java.sql.Types.Types
+ */
+ public int getCode() {
+ return code;
+ }
+
+ /**
+ * @param code see {@linkplain java.sql.Types.Types}
+ * @return found SQLType
+ * @throws IllegalArgumentException if no data type has given code
+ */
+ public SQLType valueOf(int code) {
+ for (SQLType t : values()) {
+ if (t.code == code) {
+ return t;
+ }
+ }
+ throw new IllegalArgumentException("No data type has code: " + code);
+ }
+}
--- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java Thu Dec 26 01:53:15 2013 +0100
+++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java Thu Dec 26 11:58:14 2013 +0100
@@ -19,7 +19,6 @@
import info.globalcode.sql.dk.CLIParser.Tokens;
import static info.globalcode.sql.dk.CLIParser.TYPE_NAME_SEPARATOR;
-import java.sql.Types;
import java.util.Collection;
import static org.testng.Assert.*;
import org.testng.annotations.BeforeMethod;
@@ -87,7 +86,7 @@
String[] args = new String[]{
Tokens.DB, DATABASE_NAME_1,
Tokens.SQL, SQL_1,
- Tokens.TYPES, " int,string, boolean",
+ Tokens.TYPES, " INTEGER,VARCHAR, BOOLEAN",
Tokens.DATA, DATA_1, DATA_2, DATA_3};
CLIOptions options = parser.parseOptions(args);
options.validate();
@@ -99,9 +98,9 @@
assertEquals(options.getNumberedParameters().get(0).getValue(), DATA_1);
assertEquals(options.getNumberedParameters().get(1).getValue(), DATA_2);
assertEquals(options.getNumberedParameters().get(2).getValue(), DATA_3);
- assertEquals(options.getNumberedParameters().get(0).getType(), Types.INTEGER);
- assertEquals(options.getNumberedParameters().get(1).getType(), Types.VARCHAR);
- assertEquals(options.getNumberedParameters().get(2).getType(), Types.BOOLEAN);
+ assertEquals(options.getNumberedParameters().get(0).getType(), SQLType.INTEGER);
+ assertEquals(options.getNumberedParameters().get(1).getType(), SQLType.VARCHAR);
+ assertEquals(options.getNumberedParameters().get(2).getType(), SQLType.BOOLEAN);
}
@Test
@@ -128,7 +127,7 @@
Tokens.DB, DATABASE_NAME_1,
Tokens.SQL, SQL_1,
Tokens.NAME_PREFIX, "$",
- Tokens.TYPES, " " + NAME_1 + TYPE_NAME_SEPARATOR + "int" + "," + NAME_3 + TYPE_NAME_SEPARATOR + "boolean",
+ Tokens.TYPES, " " + NAME_1 + TYPE_NAME_SEPARATOR + "INTEGER" + "," + NAME_3 + TYPE_NAME_SEPARATOR + "BOOLEAN",
Tokens.DATA_NAMED, NAME_1, DATA_1, NAME_2, DATA_2, NAME_3, DATA_3};
CLIOptions options = parser.parseOptions(args);
options.validate();
@@ -137,12 +136,12 @@
assertEquals(options.getSql(), SQL_1);
assertEquals(options.getMode(), CLIOptions.MODE.QUERY_NOW);
assertEquals(options.getNamedParameters().size(), 3);
- assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, Types.INTEGER);
+ assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, SQLType.INTEGER);
assertNamedParameter(options.getNamedParameters(), NAME_2, DATA_2, Parameter.DEFAULT_TYPE);
- assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, Types.BOOLEAN);
+ assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, SQLType.BOOLEAN);
}
- private void assertNamedParameter(Collection<NamedParameter> params, String name, Object value, int type) {
+ private void assertNamedParameter(Collection<NamedParameter> params, String name, Object value, SQLType type) {
for (NamedParameter p : params) {
if (name.equals(p.getName())) {
assertEquals(p.getValue(), value, "value does not match – name: " + name);