use of javax.jdo.query.Expression in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedQueryImpl method variable.
/* (non-Javadoc)
* @see javax.jdo.JDOQLTypedQuery#variable(java.lang.String, java.lang.Class)
*/
public <V> Expression<V> variable(String name, Class<V> type) {
assertIsOpen();
discardCompiled();
Expression varExpr = null;
if (ec.getApiAdapter().isPersistable(type)) {
// Persistable class
String typeName = type.getName();
int pos = typeName.lastIndexOf('.');
String qName = typeName.substring(0, pos + 1) + getQueryClassNameForClassName(typeName.substring(pos + 1));
try {
Class qClass = ec.getClassLoaderResolver().classForName(qName);
Constructor ctr = qClass.getConstructor(new Class[] { Class.class, String.class, ExpressionType.class });
Object candObj = ctr.newInstance(new Object[] { type, name, ExpressionType.VARIABLE });
varExpr = (Expression) candObj;
} catch (NoSuchMethodException nsme) {
throw new JDOException("Class " + typeName + " has a Query class but has no constructor for variables");
} catch (IllegalAccessException iae) {
throw new JDOException("Class " + typeName + " has a Query class but has no constructor for variables");
} catch (InvocationTargetException ite) {
throw new JDOException("Class " + typeName + " has a Query class but has no constructor for variables");
} catch (InstantiationException ie) {
throw new JDOException("Class " + typeName + " has a Query class but has no constructor for variables");
}
} else if (type == Boolean.class || type == boolean.class) {
varExpr = new BooleanExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Byte.class || type == byte.class) {
varExpr = new ByteExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Character.class || type == char.class) {
varExpr = new CharacterExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Double.class || type == double.class) {
varExpr = new NumericExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Float.class || type == float.class) {
varExpr = new NumericExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Integer.class || type == int.class) {
varExpr = new NumericExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Long.class || type == long.class) {
varExpr = new NumericExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == Short.class || type == short.class) {
varExpr = new NumericExpressionImpl(type, name, ExpressionType.VARIABLE);
} else if (type == String.class) {
varExpr = new StringExpressionImpl((Class<String>) type, name, ExpressionType.VARIABLE);
} else if (Time.class.isAssignableFrom(type)) {
varExpr = new TimeExpressionImpl((Class<Time>) type, name, ExpressionType.VARIABLE);
} else if (Date.class.isAssignableFrom(type)) {
varExpr = new DateExpressionImpl((Class<Date>) type, name, ExpressionType.VARIABLE);
} else if (java.util.Date.class.isAssignableFrom(type)) {
varExpr = new DateTimeExpressionImpl((Class<java.util.Date>) type, name, ExpressionType.VARIABLE);
} else {
varExpr = new ObjectExpressionImpl(type, name, ExpressionType.VARIABLE);
}
return varExpr;
}
use of javax.jdo.query.Expression in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedQueryImpl method setParameters.
/* (non-Javadoc)
* @see javax.jdo.JDOQLTypedQuery#setParameters(java.util.Map)
*/
@Override
public JDOQLTypedQuery<T> setParameters(Map namedParamMap) {
assertIsOpen();
discardCompiled();
if (namedParamMap == null || namedParamMap.isEmpty()) {
parameterValuesByName = null;
return this;
}
if (parameterValuesByName == null) {
parameterValuesByName = new ConcurrentHashMap<>();
}
Iterator<Map.Entry> entryIter = namedParamMap.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = entryIter.next();
Object key = entry.getKey();
Object val = entry.getValue();
if (key instanceof String) {
if (parameterExprByName == null || (parameterExprByName != null && !parameterExprByName.containsKey(key))) {
throw new JDOUserException("Parameter with name " + key + " doesnt exist for this query");
}
parameterValuesByName.put((String) key, val);
} else if (key instanceof Expression) {
ParameterExpression internalParamExpr = (ParameterExpression) ((ExpressionImpl) key).getQueryExpression();
if (parameterExprByName == null || (parameterExprByName != null && !parameterExprByName.containsKey(internalParamExpr.getAlias()))) {
throw new JDOUserException("Parameter with name " + internalParamExpr.getAlias() + " doesnt exist for this query");
}
parameterValuesByName.put(internalParamExpr.getAlias(), val);
}
}
return this;
}
use of javax.jdo.query.Expression in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedSubqueryImpl method internalSelect.
protected Expression internalSelect(Expression expr, Class implClass) {
discardCompiled();
this.result = new ArrayList<ExpressionImpl>();
this.result.add((ExpressionImpl) expr);
VariableExpression varExpr = new VariableExpression(getAlias());
try {
Constructor ctr = implClass.getConstructor(new Class[] { org.datanucleus.query.expression.Expression.class });
return (Expression) ctr.newInstance(new Object[] { varExpr });
} catch (NoSuchMethodException nsme) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " since required constructor doesnt exist");
} catch (InvocationTargetException ite) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
} catch (IllegalAccessException iae) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
} catch (InstantiationException ie) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
}
}
Aggregations