added valueType checking

This commit is contained in:
Willem 2015-06-20 16:19:57 +02:00
parent 6ddd2c969b
commit ecaaef9227
10 changed files with 172 additions and 63 deletions

View file

@ -441,6 +441,11 @@
<tag>LATER</tag>
<tag>error</tag>
<tag>ERROR</tag>
<tag>temp</tag>
<tag>TEMP</tag>
<tag>mmm</tag>
<tag>MMM</tag>
<tag>???</tag>
</tags>
</configuration>
</plugin>

View file

@ -38,6 +38,7 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.xml.bind.JAXBException;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate;
import net.forwardfire.tpquery.config.TPQConfig;
@ -68,6 +69,7 @@ public final class TPQFactory {
private static final List<TPQConfigInitializer> DEFAULT_CONFIG_INITIALIZERS = Collections.unmodifiableList(Arrays.asList(
new StatementLanguage(),
new StatementParameter(),
new ParameterValueType(),
new ParameterValueTypeAlias(),
new ParameterValueFunction(),
new TPQConfigInitializer() {
@ -194,7 +196,7 @@ public final class TPQFactory {
}
}
public static final class ParameterValueType {
public static final class ParameterValueType implements TPQConfigInitializer {
// java type
/** The class name of an Object. */
public static final String JAVA_OBJECT = Object.class.getName();
@ -243,46 +245,110 @@ public final class TPQFactory {
/** The class name of an java.sql.Timestamp. */
public static final String SQL_TIMESTAMP = java.sql.Timestamp.class.getName();
protected ParameterValueType() {
private ParameterValueType() {
}
@Override
public void initializeConfig(TPQConfig config) {
addValueType(config, JAVA_OBJECT);
addValueType(config, BIGDECIMAL);
addValueType(config, BOOLEAN);
addValueType(config, INTEGER);
addValueType(config, STRING);
addValueType(config, DOUBLE);
addValueType(config, LONG);
addValueType(config, FLOAT);
addValueType(config, SHORT);
addValueType(config, URL);
addValueType(config, BYTE_DATA);
addValueType(config, SQL_ARRAY);
addValueType(config, SQL_BLOB);
addValueType(config, SQL_CLOB);
addValueType(config, SQL_DATE);
addValueType(config, SQL_NCLOB);
addValueType(config, SQL_REF);
addValueType(config, SQL_ROWID);
addValueType(config, SQL_STRUCT);
addValueType(config, SQL_XML);
addValueType(config, SQL_TIME);
addValueType(config, SQL_TIMESTAMP);
}
// load here because in builder we like to use static fields.(as text)
private void addValueType(TPQConfig config,String className) {
try {
config.addValueType(ClassUtils.getClass(className));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e.getMessage(),e);
}
}
}
public static final class ParameterValueTypeAlias implements TPQConfigInitializer {
/** Alias for bigdecimal. */
public static final String NUMERIC = "numeric";
/** Alias for short. */
public static final String SHORT = "short";
/** Alias for short. */
public static final String INT2 = "int2";
/** Alias for short. */
public static final String SMALLINT = "smallint";
/** Alias for integer. */
public static final String INTEGER = "integer";
/** Alias for integer. */
public static final String INT = "int";
/** Alias for integer. */
public static final String INT4 = "int4";
/** Alias for long. */
public static final String LONG = "long";
/** Alias for long. */
public static final String INT8 = "int8";
/** Alias for long. */
public static final String BIGINT = "bigint";
/** Alias for boolean. */
public static final String BOOLEAN = "boolean";
/** Alias for boolean. */
public static final String BOOL = "bool";
/** Alias for float. */
public static final String FLOAT = "float";
/** Alias for float. */
public static final String FLOAT4 = "float4";
/** Alias for float. */
public static final String REAL = "real";
/** Alias for double. */
public static final String DOUBLE = "double";
/** Alias for double. */
public static final String FLOAT8 = "float8";
/** Alias for double. */
public static final String DOUBLE_PRECISION = "double precision";
/** Alias for string. */
public static final String STRING = "string";
/** Alias for string. */
public static final String TEXT = "text";
/** Alias for string. */
public static final String VARCHAR = "varchar";
/** Alias for string. */
public static final String LONGVARCHAR = "longvarchar";
/** Alias for string. */
public static final String CHARACTER_VARYING = "character varying";
/** Alias for string. */
public static final String CHAR = "char";
/** Alias for string. */
public static final String CHARACTER = "character";
/** Alias for date. */
public static final String DATE = "date";
/** Alias for time. */
public static final String TIME = "time";
/** Alias for timestamp. */
public static final String TIMESTAMP = "timestamp";
private ParameterValueTypeAlias() {
@ -290,26 +356,26 @@ public final class TPQFactory {
@Override
public void initializeConfig(TPQConfig config) {
config.addValueTypeAlias(NUMERIC, ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(NUMERIC, ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(SHORT, ParameterValueType.SHORT);
config.addValueTypeAlias(INT2, ParameterValueType.SHORT);
config.addValueTypeAlias(SMALLINT, ParameterValueType.SHORT);
config.addValueTypeAlias(SHORT, ParameterValueType.SHORT);
config.addValueTypeAlias(INT2, ParameterValueType.SHORT);
config.addValueTypeAlias(SMALLINT, ParameterValueType.SHORT);
config.addValueTypeAlias(INTEGER, ParameterValueType.INTEGER);
config.addValueTypeAlias(INT, ParameterValueType.INTEGER);
config.addValueTypeAlias(INT4, ParameterValueType.INTEGER);
config.addValueTypeAlias(INTEGER, ParameterValueType.INTEGER);
config.addValueTypeAlias(INT, ParameterValueType.INTEGER);
config.addValueTypeAlias(INT4, ParameterValueType.INTEGER);
config.addValueTypeAlias(LONG, ParameterValueType.LONG);
config.addValueTypeAlias(INT8, ParameterValueType.LONG);
config.addValueTypeAlias(BIGINT, ParameterValueType.LONG);
config.addValueTypeAlias(LONG, ParameterValueType.LONG);
config.addValueTypeAlias(INT8, ParameterValueType.LONG);
config.addValueTypeAlias(BIGINT, ParameterValueType.LONG);
config.addValueTypeAlias(BOOLEAN, ParameterValueType.BOOLEAN);
config.addValueTypeAlias(BOOL, ParameterValueType.BOOLEAN);
config.addValueTypeAlias(BOOLEAN, ParameterValueType.BOOLEAN);
config.addValueTypeAlias(BOOL, ParameterValueType.BOOLEAN);
config.addValueTypeAlias(FLOAT, ParameterValueType.FLOAT);
config.addValueTypeAlias(FLOAT4, ParameterValueType.FLOAT);
config.addValueTypeAlias(REAL, ParameterValueType.FLOAT);
config.addValueTypeAlias(FLOAT, ParameterValueType.FLOAT);
config.addValueTypeAlias(FLOAT4, ParameterValueType.FLOAT);
config.addValueTypeAlias(REAL, ParameterValueType.FLOAT);
config.addValueTypeAlias(DOUBLE, ParameterValueType.DOUBLE);
config.addValueTypeAlias(DOUBLE_PRECISION, ParameterValueType.DOUBLE);
@ -327,37 +393,37 @@ public final class TPQFactory {
config.addValueTypeAlias(TIME, ParameterValueType.SQL_TIME);
config.addValueTypeAlias(TIMESTAMP, ParameterValueType.SQL_TIMESTAMP);
// new jdbc names (missing; DISTINCT and REF_CURSOR)
config.addValueTypeAlias(JDBCType.ARRAY.name(), ParameterValueType.SQL_ARRAY);
config.addValueTypeAlias(JDBCType.BIGINT.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.BINARY.name(), ParameterValueType.BYTE_DATA);
config.addValueTypeAlias(JDBCType.BIT.name(), ParameterValueType.BOOLEAN);
config.addValueTypeAlias(JDBCType.BLOB.name(), ParameterValueType.SQL_BLOB);
config.addValueTypeAlias(JDBCType.BOOLEAN.name(), ParameterValueType.BOOLEAN);
config.addValueTypeAlias(JDBCType.CHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.CLOB.name(), ParameterValueType.SQL_CLOB);
config.addValueTypeAlias(JDBCType.DATALINK.name(), ParameterValueType.URL);
config.addValueTypeAlias(JDBCType.DATE.name(), ParameterValueType.SQL_DATE);
config.addValueTypeAlias(JDBCType.DECIMAL.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.DOUBLE.name(), ParameterValueType.DOUBLE);
config.addValueTypeAlias(JDBCType.FLOAT.name(), ParameterValueType.FLOAT);
config.addValueTypeAlias(JDBCType.INTEGER.name(), ParameterValueType.INTEGER);
config.addValueTypeAlias(JDBCType.JAVA_OBJECT.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.LONGNVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.LONGVARBINARY.name(), ParameterValueType.BYTE_DATA);
config.addValueTypeAlias(JDBCType.LONGVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.NCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.NCLOB.name(), ParameterValueType.SQL_NCLOB);
config.addValueTypeAlias(JDBCType.NULL.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.NUMERIC.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.NVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.OTHER.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.REAL.name(), ParameterValueType.FLOAT);
config.addValueTypeAlias(JDBCType.REF.name(), ParameterValueType.SQL_REF);
config.addValueTypeAlias(JDBCType.ROWID.name(), ParameterValueType.SQL_ROWID);
config.addValueTypeAlias(JDBCType.SMALLINT.name(), ParameterValueType.INTEGER);
config.addValueTypeAlias(JDBCType.SQLXML.name(), ParameterValueType.SQL_XML);
config.addValueTypeAlias(JDBCType.STRUCT.name(), ParameterValueType.SQL_STRUCT);
// new jdbc names in uppercase; (missing; DISTINCT and REF_CURSOR)
config.addValueTypeAlias(JDBCType.ARRAY.name(), ParameterValueType.SQL_ARRAY);
config.addValueTypeAlias(JDBCType.BIGINT.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.BINARY.name(), ParameterValueType.BYTE_DATA);
config.addValueTypeAlias(JDBCType.BIT.name(), ParameterValueType.BOOLEAN);
config.addValueTypeAlias(JDBCType.BLOB.name(), ParameterValueType.SQL_BLOB);
config.addValueTypeAlias(JDBCType.BOOLEAN.name(), ParameterValueType.BOOLEAN);
config.addValueTypeAlias(JDBCType.CHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.CLOB.name(), ParameterValueType.SQL_CLOB);
config.addValueTypeAlias(JDBCType.DATALINK.name(), ParameterValueType.URL);
config.addValueTypeAlias(JDBCType.DATE.name(), ParameterValueType.SQL_DATE);
config.addValueTypeAlias(JDBCType.DECIMAL.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.DOUBLE.name(), ParameterValueType.DOUBLE);
config.addValueTypeAlias(JDBCType.FLOAT.name(), ParameterValueType.FLOAT);
config.addValueTypeAlias(JDBCType.INTEGER.name(), ParameterValueType.INTEGER);
config.addValueTypeAlias(JDBCType.JAVA_OBJECT.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.LONGNVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.LONGVARBINARY.name(), ParameterValueType.BYTE_DATA);
config.addValueTypeAlias(JDBCType.LONGVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.NCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.NCLOB.name(), ParameterValueType.SQL_NCLOB);
config.addValueTypeAlias(JDBCType.NULL.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.NUMERIC.name(), ParameterValueType.BIGDECIMAL);
config.addValueTypeAlias(JDBCType.NVARCHAR.name(), ParameterValueType.STRING);
config.addValueTypeAlias(JDBCType.OTHER.name(), ParameterValueType.JAVA_OBJECT);
config.addValueTypeAlias(JDBCType.REAL.name(), ParameterValueType.FLOAT);
config.addValueTypeAlias(JDBCType.REF.name(), ParameterValueType.SQL_REF);
config.addValueTypeAlias(JDBCType.ROWID.name(), ParameterValueType.SQL_ROWID);
config.addValueTypeAlias(JDBCType.SMALLINT.name(), ParameterValueType.INTEGER);
config.addValueTypeAlias(JDBCType.SQLXML.name(), ParameterValueType.SQL_XML);
config.addValueTypeAlias(JDBCType.STRUCT.name(), ParameterValueType.SQL_STRUCT);
config.addValueTypeAlias(JDBCType.TIME.name(), ParameterValueType.SQL_TIME);
config.addValueTypeAlias(JDBCType.TIME_WITH_TIMEZONE.name(), ParameterValueType.SQL_TIME);
config.addValueTypeAlias(JDBCType.TIMESTAMP.name(), ParameterValueType.SQL_TIMESTAMP);
@ -365,7 +431,6 @@ public final class TPQFactory {
config.addValueTypeAlias(JDBCType.TINYINT.name(), ParameterValueType.SHORT);
config.addValueTypeAlias(JDBCType.VARBINARY.name(), ParameterValueType.BYTE_DATA);
config.addValueTypeAlias(JDBCType.VARCHAR.name(), ParameterValueType.STRING);
}
}

View file

@ -54,9 +54,10 @@ public abstract class AbstractTPQConfig {
protected final Map<String,String> valueTypeAliases;
protected final List<TPQConfigValidator> configValidators;
protected final List<String> parameterValueScripts;
protected final List<Class<?>> valueTypes;
protected AbstractTPQConfig() {
this(new HashMap<>(),new HashMap<>(),new HashMap<>(),new HashMap<>(),new ArrayList<>(),new ArrayList<>());
this(new HashMap<>(),new HashMap<>(),new HashMap<>(),new HashMap<>(),new ArrayList<>(),new ArrayList<>(),new ArrayList<>());
}
protected AbstractTPQConfig(
@ -65,7 +66,8 @@ public abstract class AbstractTPQConfig {
Map<String,TPQStatementLanguage> statementLanguages,
Map<String,String> valueTypeAliases,
List<TPQConfigValidator> configValidators,
List<String> parameterValueScripts
List<String> parameterValueScripts,
List<Class<?>> valueTypes
) {
this.querySets=querySets;
this.statementParameters=statementParameters;
@ -73,6 +75,7 @@ public abstract class AbstractTPQConfig {
this.valueTypeAliases=valueTypeAliases;
this.configValidators=configValidators;
this.parameterValueScripts=parameterValueScripts;
this.valueTypes=valueTypes;
}
/**
@ -158,4 +161,11 @@ public abstract class AbstractTPQConfig {
public List<String> getParameterValueScripts() {
return parameterValueScripts;
}
/**
* @return the valueTypes
*/
public List<Class<?>> getValueTypes() {
return valueTypes;
}
}

View file

@ -206,4 +206,20 @@ public class TPQConfig extends AbstractTPQConfig {
public void removeParameterValueScript(String script) {
parameterValueScripts.remove(Validate.notNull(script,"Can't remove null script."));
}
/**
* Adds a value type.
* @param valueType The value type to add.
*/
public void addValueType(Class<?> valueType) {
valueTypes.add(Validate.notNull(valueType,"Can't add null valueType."));
}
/**
* Removes a value type.
* @param valueType The value type to remove.
*/
public void removeValueType(Class<?> valueType) {
valueTypes.add(Validate.notNull(valueType,"Can't remove null valueType."));
}
}

View file

@ -69,12 +69,12 @@ public final class TPQuery extends AbstractTPQueryNode {
public List<TPQStatement> getQueryParts() {
return queryParts;
}
/*
public void setQueryParts(List<TPQStatement> parts) {
Objects.requireNonNull(parts,"Can't set null list");
queryParts = parts;
}
*/
/**
* Adds an query part.

View file

@ -8,6 +8,7 @@ import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
@ -47,7 +48,8 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
Collections.unmodifiableMap(new HashMap<>(config.getStatementLanguages())),
Collections.unmodifiableMap(new HashMap<>(config.getValueTypeAliases())),
Collections.unmodifiableList(new ArrayList<>(config.getConfigValidators())),
Collections.unmodifiableList(new ArrayList<>(config.getParameterValueScripts()))
Collections.unmodifiableList(new ArrayList<>(config.getParameterValueScripts())),
Collections.unmodifiableList(new ArrayList<>(config.getValueTypes()))
);
treePathSeperator = config.getTreePathSeperator();
defaultParameterType = config.getDefaultParameterType();
@ -197,7 +199,18 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
qp.setNullable(getDefaultParameterNullable());
LOG.debug("Set nullable on parameter: {} value: {} as default type.",qp.getName(),qp.getNullable());
}
Class<?> clazz = loadClass(qp.getValueType());
Validate.isTrue(getValueTypes().contains(clazz),"Unsupported valueType: %s",clazz.getName());
}
return entry;
}
private Class<?> loadClass(String valueType) {
try {
return ClassUtils.getClass(valueType);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}

View file

@ -84,12 +84,13 @@ public final class TPQStoreManagerEntry {
*/
public void initEntry() {
Map<String,TPQueryParameterMetaData> parameterMetaDataBuild = new HashMap<>();
getQuery().getQueryParameters().forEach(p ->
getQuery().getQueryParameters().forEach(p -> {
Class<?> valueType = loadValueType(p.getValueType()); // check if we van get this from config ?
parameterMetaDataBuild.put(p.getName(),
new TPQStoreManagerEntryParameterMetaData(
p.getName(),loadValueType(p.getValueType()),p.getNullable(),defineRequired(p)
)
)
p.getName(),valueType,p.getNullable(),defineRequired(p)
));
}
);
this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild);
}

View file

@ -203,7 +203,7 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
try {
result = script.eval();
} catch (Exception e) {
throw new TPQueryStoreScriptEngineException("query: "+entry.getQuery().getName()+" param: "+param.getName()+" script: "+param.getDefaultValue(),e);
throw new TPQueryStoreScriptEngineException("query: "+entry.getQuery().getName()+" param: "+param.getName()+" script: "+param.getDefaultValue()+" error: "+e.getMessage(),e);
}
if (result instanceof TPQConfigParameterValueHolder) {
TPQConfigParameterValueHolder valueHolderResult = (TPQConfigParameterValueHolder)result;

View file

@ -35,7 +35,6 @@ public class TPQFactoryTest {
@Test()
public void testFactoryConstructors() throws Exception {
assertNotNull(new TPQFactory());
assertNotNull(new TPQFactory.ParameterValueType());
}
}

View file

@ -108,8 +108,8 @@ public class TPQueryParameterDefaultValueTest {
.setLanguage(TPQFactory.StatementLanguage.SQL)
.createQuery("test")
.parseStatement("select * from foobar where a=$$a$$")
.createQueryParameter("a", "java.util.Date")
.setDefaultValue("js:createObject(java.util.Date)")
.createQueryParameter("a", "java.lang.Integer")
.setDefaultValue("js:createObject(java.lang.Integer,'123')")
.build()
.build()
.build()
@ -118,7 +118,7 @@ public class TPQueryParameterDefaultValueTest {
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
assertTrue(!prepared.getParameters().isEmpty());
assertTrue(prepared.getParameters().get(0) != null);
assertTrue(prepared.getParameters().get(0) instanceof java.util.Date);
assertTrue(prepared.getParameters().get(0) instanceof java.lang.Integer);
}
@Test(expected=Exception.class)