use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.
the class Query method getSymbolForParameterInCompilation.
/**
* Convenience method to find a symbol for the specified parameter in the provided compilation.
* @param compilation The compilation
* @param paramKey The parameter name/position
* @return The symbol (if present)
*/
private Symbol getSymbolForParameterInCompilation(QueryCompilation compilation, Object paramKey) {
Symbol sym = null;
if (paramKey instanceof Integer) {
ParameterExpression expr = compilation.getParameterExpressionForPosition((Integer) paramKey);
if (expr != null) {
sym = expr.getSymbol();
}
} else {
String paramName = (String) paramKey;
sym = compilation.getSymbolTable().getSymbol(paramName);
}
return sym;
}
use of org.datanucleus.query.expression.ParameterExpression in project tests by datanucleus.
the class JPQLCompilerTest method testFromMemberOfExpression.
/**
* Tests for from clause with a "MEMBER OF {primary}" expression.
*/
public void testFromMemberOfExpression() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Department.class, null, ":param MEMBER OF projects", null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception during compile", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
// InvokeExpression{[PrimaryExpression{elements}].contains(ParameterExpression{param})}
Expression expr = compilation.getExprFilter();
assertTrue("Invalid type of filter expression", expr instanceof InvokeExpression);
InvokeExpression invokeExpr = (InvokeExpression) expr;
assertEquals("Invoke method is incorrect", "contains", invokeExpr.getOperation());
assertTrue("Invoke left expression is of incorrect type", invokeExpr.getLeft() instanceof PrimaryExpression);
PrimaryExpression leftExpr = (PrimaryExpression) invokeExpr.getLeft();
assertEquals("Invoke left expression id is wrong", "projects", leftExpr.getId());
List args = invokeExpr.getArguments();
assertNotNull("Number of args is null!", args);
assertEquals("Incorrect number of args to invoke", 1, args.size());
assertTrue("Argument is of incorrect type", args.get(0) instanceof ParameterExpression);
ParameterExpression argExpr = (ParameterExpression) args.get(0);
assertEquals("Argument param name is incorrect", "param", argExpr.getId());
}
use of org.datanucleus.query.expression.ParameterExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testExpressionSerializable.
/**
* Test for serialisability of Expressions.
*/
public void testExpressionSerializable() {
// Test use of implicit variable in filter
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == param1", null, null, null, null, null, "java.lang.String param1", null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
// TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr.getRight() instanceof ParameterExpression);
PrimaryExpression left = (PrimaryExpression) dyExpr.getLeft();
assertEquals("Primary expression name is wrong", left.getId(), "name");
ParameterExpression right = (ParameterExpression) dyExpr.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right.getId());
try {
try {
// Serialise the Expression
FileOutputStream fileStream = new FileOutputStream("expr.ser");
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(expr);
os.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in serialise", e);
fail("Failed to serialise " + StringUtils.toJVMIDString(expr));
}
try {
// Deserialise the Expression
FileInputStream fileInputStream = new FileInputStream("expr.ser");
ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
Object obj = oInputStream.readObject();
if (obj instanceof Expression) {
Expression expr1 = (Expression) obj;
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr1 instanceof DyadicExpression);
DyadicExpression dyExpr1 = (DyadicExpression) expr1;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr1.getRight() instanceof ParameterExpression);
PrimaryExpression left1 = (PrimaryExpression) dyExpr1.getLeft();
assertEquals("Primary expression name is wrong", left1.getId(), "name");
ParameterExpression right1 = (ParameterExpression) dyExpr1.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right1.getId());
} else {
fail("Deserialised object is " + obj.getClass().getName() + " not Expression");
}
oInputStream.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in deserialise", e);
fail("Failed to deserialise " + StringUtils.toJVMIDString(expr));
}
} finally {
// Delete the file
File file = new File("expr.ser");
if (file.exists()) {
file.delete();
}
}
}
use of org.datanucleus.query.expression.ParameterExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterExplicitParameter.
/**
* Test for use of an explicit parameter in the filter.
*/
public void testFilterExplicitParameter() {
// Test use of implicit variable in filter
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == param1", null, null, null, null, null, "java.lang.String param1", null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
// TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr.getRight() instanceof ParameterExpression);
PrimaryExpression left = (PrimaryExpression) dyExpr.getLeft();
assertEquals("Primary expression name is wrong", left.getId(), "name");
ParameterExpression right = (ParameterExpression) dyExpr.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right.getId());
}
use of org.datanucleus.query.expression.ParameterExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testQueryCompilationSerializable.
/**
* Test for serialisability of QueryCompilation.
*/
public void testQueryCompilationSerializable() {
// Test use of implicit variable in filter
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == param1", null, null, null, null, null, "java.lang.String param1", null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
// TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
try {
try {
// Serialise the Expression
FileOutputStream fileStream = new FileOutputStream("compilation.ser");
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(compilation);
os.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in serialise", e);
fail("Failed to serialise " + StringUtils.toJVMIDString(compilation));
}
try {
// Deserialise the Expression
FileInputStream fileInputStream = new FileInputStream("compilation.ser");
ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
Object obj = oInputStream.readObject();
if (obj instanceof QueryCompilation) {
QueryCompilation compilation1 = (QueryCompilation) obj;
Expression expr1 = compilation1.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr1 instanceof DyadicExpression);
DyadicExpression dyExpr1 = (DyadicExpression) expr1;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr1.getRight() instanceof ParameterExpression);
PrimaryExpression left1 = (PrimaryExpression) dyExpr1.getLeft();
assertEquals("Primary expression name is wrong", left1.getId(), "name");
ParameterExpression right1 = (ParameterExpression) dyExpr1.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right1.getId());
} else {
fail("Deserialised object is " + obj.getClass().getName() + " not QueryCompilation");
}
oInputStream.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in deserialise", e);
fail("Failed to deserialise " + StringUtils.toJVMIDString(compilation));
}
} finally {
// Delete the file
File file = new File("compilation.ser");
if (file.exists()) {
file.delete();
}
}
}
Aggregations