added valueType checking
This commit is contained in:
parent
6ddd2c969b
commit
ecaaef9227
5
pom.xml
5
pom.xml
|
@ -441,6 +441,11 @@
|
||||||
<tag>LATER</tag>
|
<tag>LATER</tag>
|
||||||
<tag>error</tag>
|
<tag>error</tag>
|
||||||
<tag>ERROR</tag>
|
<tag>ERROR</tag>
|
||||||
|
<tag>temp</tag>
|
||||||
|
<tag>TEMP</tag>
|
||||||
|
<tag>mmm</tag>
|
||||||
|
<tag>MMM</tag>
|
||||||
|
<tag>???</tag>
|
||||||
</tags>
|
</tags>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
|
@ -38,6 +38,7 @@ import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
|
|
||||||
import net.forwardfire.tpquery.config.TPQConfig;
|
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(
|
private static final List<TPQConfigInitializer> DEFAULT_CONFIG_INITIALIZERS = Collections.unmodifiableList(Arrays.asList(
|
||||||
new StatementLanguage(),
|
new StatementLanguage(),
|
||||||
new StatementParameter(),
|
new StatementParameter(),
|
||||||
|
new ParameterValueType(),
|
||||||
new ParameterValueTypeAlias(),
|
new ParameterValueTypeAlias(),
|
||||||
new ParameterValueFunction(),
|
new ParameterValueFunction(),
|
||||||
new TPQConfigInitializer() {
|
new TPQConfigInitializer() {
|
||||||
|
@ -194,7 +196,7 @@ public final class TPQFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class ParameterValueType {
|
public static final class ParameterValueType implements TPQConfigInitializer {
|
||||||
// java type
|
// java type
|
||||||
/** The class name of an Object. */
|
/** The class name of an Object. */
|
||||||
public static final String JAVA_OBJECT = Object.class.getName();
|
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. */
|
/** The class name of an java.sql.Timestamp. */
|
||||||
public static final String SQL_TIMESTAMP = java.sql.Timestamp.class.getName();
|
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 {
|
public static final class ParameterValueTypeAlias implements TPQConfigInitializer {
|
||||||
|
/** Alias for bigdecimal. */
|
||||||
public static final String NUMERIC = "numeric";
|
public static final String NUMERIC = "numeric";
|
||||||
|
|
||||||
|
/** Alias for short. */
|
||||||
public static final String SHORT = "short";
|
public static final String SHORT = "short";
|
||||||
|
/** Alias for short. */
|
||||||
public static final String INT2 = "int2";
|
public static final String INT2 = "int2";
|
||||||
|
/** Alias for short. */
|
||||||
public static final String SMALLINT = "smallint";
|
public static final String SMALLINT = "smallint";
|
||||||
|
|
||||||
|
/** Alias for integer. */
|
||||||
public static final String INTEGER = "integer";
|
public static final String INTEGER = "integer";
|
||||||
|
/** Alias for integer. */
|
||||||
public static final String INT = "int";
|
public static final String INT = "int";
|
||||||
|
/** Alias for integer. */
|
||||||
public static final String INT4 = "int4";
|
public static final String INT4 = "int4";
|
||||||
|
|
||||||
|
/** Alias for long. */
|
||||||
public static final String LONG = "long";
|
public static final String LONG = "long";
|
||||||
|
/** Alias for long. */
|
||||||
public static final String INT8 = "int8";
|
public static final String INT8 = "int8";
|
||||||
|
/** Alias for long. */
|
||||||
public static final String BIGINT = "bigint";
|
public static final String BIGINT = "bigint";
|
||||||
|
|
||||||
|
/** Alias for boolean. */
|
||||||
public static final String BOOLEAN = "boolean";
|
public static final String BOOLEAN = "boolean";
|
||||||
|
/** Alias for boolean. */
|
||||||
public static final String BOOL = "bool";
|
public static final String BOOL = "bool";
|
||||||
|
|
||||||
|
/** Alias for float. */
|
||||||
public static final String FLOAT = "float";
|
public static final String FLOAT = "float";
|
||||||
|
/** Alias for float. */
|
||||||
public static final String FLOAT4 = "float4";
|
public static final String FLOAT4 = "float4";
|
||||||
|
/** Alias for float. */
|
||||||
public static final String REAL = "real";
|
public static final String REAL = "real";
|
||||||
|
|
||||||
|
/** Alias for double. */
|
||||||
public static final String DOUBLE = "double";
|
public static final String DOUBLE = "double";
|
||||||
|
/** Alias for double. */
|
||||||
public static final String FLOAT8 = "float8";
|
public static final String FLOAT8 = "float8";
|
||||||
|
/** Alias for double. */
|
||||||
public static final String DOUBLE_PRECISION = "double precision";
|
public static final String DOUBLE_PRECISION = "double precision";
|
||||||
|
|
||||||
|
/** Alias for string. */
|
||||||
public static final String STRING = "string";
|
public static final String STRING = "string";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String TEXT = "text";
|
public static final String TEXT = "text";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String VARCHAR = "varchar";
|
public static final String VARCHAR = "varchar";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String LONGVARCHAR = "longvarchar";
|
public static final String LONGVARCHAR = "longvarchar";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String CHARACTER_VARYING = "character varying";
|
public static final String CHARACTER_VARYING = "character varying";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String CHAR = "char";
|
public static final String CHAR = "char";
|
||||||
|
/** Alias for string. */
|
||||||
public static final String CHARACTER = "character";
|
public static final String CHARACTER = "character";
|
||||||
|
|
||||||
|
/** Alias for date. */
|
||||||
public static final String DATE = "date";
|
public static final String DATE = "date";
|
||||||
|
/** Alias for time. */
|
||||||
public static final String TIME = "time";
|
public static final String TIME = "time";
|
||||||
|
/** Alias for timestamp. */
|
||||||
public static final String TIMESTAMP = "timestamp";
|
public static final String TIMESTAMP = "timestamp";
|
||||||
|
|
||||||
private ParameterValueTypeAlias() {
|
private ParameterValueTypeAlias() {
|
||||||
|
@ -327,7 +393,7 @@ public final class TPQFactory {
|
||||||
config.addValueTypeAlias(TIME, ParameterValueType.SQL_TIME);
|
config.addValueTypeAlias(TIME, ParameterValueType.SQL_TIME);
|
||||||
config.addValueTypeAlias(TIMESTAMP, ParameterValueType.SQL_TIMESTAMP);
|
config.addValueTypeAlias(TIMESTAMP, ParameterValueType.SQL_TIMESTAMP);
|
||||||
|
|
||||||
// new jdbc names (missing; DISTINCT and REF_CURSOR)
|
// new jdbc names in uppercase; (missing; DISTINCT and REF_CURSOR)
|
||||||
config.addValueTypeAlias(JDBCType.ARRAY.name(), ParameterValueType.SQL_ARRAY);
|
config.addValueTypeAlias(JDBCType.ARRAY.name(), ParameterValueType.SQL_ARRAY);
|
||||||
config.addValueTypeAlias(JDBCType.BIGINT.name(), ParameterValueType.BIGDECIMAL);
|
config.addValueTypeAlias(JDBCType.BIGINT.name(), ParameterValueType.BIGDECIMAL);
|
||||||
config.addValueTypeAlias(JDBCType.BINARY.name(), ParameterValueType.BYTE_DATA);
|
config.addValueTypeAlias(JDBCType.BINARY.name(), ParameterValueType.BYTE_DATA);
|
||||||
|
@ -365,7 +431,6 @@ public final class TPQFactory {
|
||||||
config.addValueTypeAlias(JDBCType.TINYINT.name(), ParameterValueType.SHORT);
|
config.addValueTypeAlias(JDBCType.TINYINT.name(), ParameterValueType.SHORT);
|
||||||
config.addValueTypeAlias(JDBCType.VARBINARY.name(), ParameterValueType.BYTE_DATA);
|
config.addValueTypeAlias(JDBCType.VARBINARY.name(), ParameterValueType.BYTE_DATA);
|
||||||
config.addValueTypeAlias(JDBCType.VARCHAR.name(), ParameterValueType.STRING);
|
config.addValueTypeAlias(JDBCType.VARCHAR.name(), ParameterValueType.STRING);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,9 +54,10 @@ public abstract class AbstractTPQConfig {
|
||||||
protected final Map<String,String> valueTypeAliases;
|
protected final Map<String,String> valueTypeAliases;
|
||||||
protected final List<TPQConfigValidator> configValidators;
|
protected final List<TPQConfigValidator> configValidators;
|
||||||
protected final List<String> parameterValueScripts;
|
protected final List<String> parameterValueScripts;
|
||||||
|
protected final List<Class<?>> valueTypes;
|
||||||
|
|
||||||
protected AbstractTPQConfig() {
|
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(
|
protected AbstractTPQConfig(
|
||||||
|
@ -65,7 +66,8 @@ public abstract class AbstractTPQConfig {
|
||||||
Map<String,TPQStatementLanguage> statementLanguages,
|
Map<String,TPQStatementLanguage> statementLanguages,
|
||||||
Map<String,String> valueTypeAliases,
|
Map<String,String> valueTypeAliases,
|
||||||
List<TPQConfigValidator> configValidators,
|
List<TPQConfigValidator> configValidators,
|
||||||
List<String> parameterValueScripts
|
List<String> parameterValueScripts,
|
||||||
|
List<Class<?>> valueTypes
|
||||||
) {
|
) {
|
||||||
this.querySets=querySets;
|
this.querySets=querySets;
|
||||||
this.statementParameters=statementParameters;
|
this.statementParameters=statementParameters;
|
||||||
|
@ -73,6 +75,7 @@ public abstract class AbstractTPQConfig {
|
||||||
this.valueTypeAliases=valueTypeAliases;
|
this.valueTypeAliases=valueTypeAliases;
|
||||||
this.configValidators=configValidators;
|
this.configValidators=configValidators;
|
||||||
this.parameterValueScripts=parameterValueScripts;
|
this.parameterValueScripts=parameterValueScripts;
|
||||||
|
this.valueTypes=valueTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,4 +161,11 @@ public abstract class AbstractTPQConfig {
|
||||||
public List<String> getParameterValueScripts() {
|
public List<String> getParameterValueScripts() {
|
||||||
return parameterValueScripts;
|
return parameterValueScripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the valueTypes
|
||||||
|
*/
|
||||||
|
public List<Class<?>> getValueTypes() {
|
||||||
|
return valueTypes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -206,4 +206,20 @@ public class TPQConfig extends AbstractTPQConfig {
|
||||||
public void removeParameterValueScript(String script) {
|
public void removeParameterValueScript(String script) {
|
||||||
parameterValueScripts.remove(Validate.notNull(script,"Can't remove null 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."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,12 +69,12 @@ public final class TPQuery extends AbstractTPQueryNode {
|
||||||
public List<TPQStatement> getQueryParts() {
|
public List<TPQStatement> getQueryParts() {
|
||||||
return queryParts;
|
return queryParts;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
public void setQueryParts(List<TPQStatement> parts) {
|
public void setQueryParts(List<TPQStatement> parts) {
|
||||||
Objects.requireNonNull(parts,"Can't set null list");
|
Objects.requireNonNull(parts,"Can't set null list");
|
||||||
queryParts = parts;
|
queryParts = parts;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an query part.
|
* Adds an query part.
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
import org.slf4j.Logger;
|
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.getStatementLanguages())),
|
||||||
Collections.unmodifiableMap(new HashMap<>(config.getValueTypeAliases())),
|
Collections.unmodifiableMap(new HashMap<>(config.getValueTypeAliases())),
|
||||||
Collections.unmodifiableList(new ArrayList<>(config.getConfigValidators())),
|
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();
|
treePathSeperator = config.getTreePathSeperator();
|
||||||
defaultParameterType = config.getDefaultParameterType();
|
defaultParameterType = config.getDefaultParameterType();
|
||||||
|
@ -197,7 +199,18 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
|
||||||
qp.setNullable(getDefaultParameterNullable());
|
qp.setNullable(getDefaultParameterNullable());
|
||||||
LOG.debug("Set nullable on parameter: {} value: {} as default type.",qp.getName(),qp.getNullable());
|
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;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Class<?> loadClass(String valueType) {
|
||||||
|
try {
|
||||||
|
return ClassUtils.getClass(valueType);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,12 +84,13 @@ public final class TPQStoreManagerEntry {
|
||||||
*/
|
*/
|
||||||
public void initEntry() {
|
public void initEntry() {
|
||||||
Map<String,TPQueryParameterMetaData> parameterMetaDataBuild = new HashMap<>();
|
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(),
|
parameterMetaDataBuild.put(p.getName(),
|
||||||
new TPQStoreManagerEntryParameterMetaData(
|
new TPQStoreManagerEntryParameterMetaData(
|
||||||
p.getName(),loadValueType(p.getValueType()),p.getNullable(),defineRequired(p)
|
p.getName(),valueType,p.getNullable(),defineRequired(p)
|
||||||
)
|
));
|
||||||
)
|
}
|
||||||
);
|
);
|
||||||
this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild);
|
this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild);
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,7 +203,7 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
|
||||||
try {
|
try {
|
||||||
result = script.eval();
|
result = script.eval();
|
||||||
} catch (Exception e) {
|
} 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) {
|
if (result instanceof TPQConfigParameterValueHolder) {
|
||||||
TPQConfigParameterValueHolder valueHolderResult = (TPQConfigParameterValueHolder)result;
|
TPQConfigParameterValueHolder valueHolderResult = (TPQConfigParameterValueHolder)result;
|
||||||
|
|
|
@ -35,7 +35,6 @@ public class TPQFactoryTest {
|
||||||
@Test()
|
@Test()
|
||||||
public void testFactoryConstructors() throws Exception {
|
public void testFactoryConstructors() throws Exception {
|
||||||
assertNotNull(new TPQFactory());
|
assertNotNull(new TPQFactory());
|
||||||
assertNotNull(new TPQFactory.ParameterValueType());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,8 +108,8 @@ public class TPQueryParameterDefaultValueTest {
|
||||||
.setLanguage(TPQFactory.StatementLanguage.SQL)
|
.setLanguage(TPQFactory.StatementLanguage.SQL)
|
||||||
.createQuery("test")
|
.createQuery("test")
|
||||||
.parseStatement("select * from foobar where a=$$a$$")
|
.parseStatement("select * from foobar where a=$$a$$")
|
||||||
.createQueryParameter("a", "java.util.Date")
|
.createQueryParameter("a", "java.lang.Integer")
|
||||||
.setDefaultValue("js:createObject(java.util.Date)")
|
.setDefaultValue("js:createObject(java.lang.Integer,'123')")
|
||||||
.build()
|
.build()
|
||||||
.build()
|
.build()
|
||||||
.build()
|
.build()
|
||||||
|
@ -118,7 +118,7 @@ public class TPQueryParameterDefaultValueTest {
|
||||||
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
|
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
|
||||||
assertTrue(!prepared.getParameters().isEmpty());
|
assertTrue(!prepared.getParameters().isEmpty());
|
||||||
assertTrue(prepared.getParameters().get(0) != null);
|
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)
|
@Test(expected=Exception.class)
|
||||||
|
|
Loading…
Reference in a new issue