use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterMapContainsValueNonPC.
/**
* Test of filter with mapField.containsValue(nonPC).
*/
public void testFilterMapContainsValueNonPC() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<MapHolder> instances = new ArrayList<>();
MapHolder holder1 = new MapHolder("First");
MapHolder holder2 = new MapHolder("Second");
MapHolder holder3 = new MapHolder("Third");
holder1.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Third", "Third Element");
instances.add(holder1);
instances.add(holder2);
instances.add(holder3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(MapHolder.class, "joinMapNonNon.containsValue('Third Element')");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
MapHolder holder = (MapHolder) results.get(0);
assertEquals("Result instance has wrong name", "Third", holder.getName());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterInstanceOf.
/**
* Test of filter with "instanceof".
*/
public void testFilterInstanceOf() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
p1.setAge(34);
Employee p2 = new Employee(102, "Donald", "Duck", "donald.duck@warnerbros.com", 13400.0f, "12345");
p2.setAge(38);
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
p3.setAge(31);
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "this instanceof " + Employee.class.getName());
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
assertEquals("Age of result instance is wrong", 38, p.getAge());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLEvaluatorTest method testImplicitParametersPositional.
/**
* Test use of input params, positional.
*/
public void testImplicitParametersPositional() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName.endsWith(:param)");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(null, null);
// Execute the query
Map<Integer, Object> paramValues = new HashMap<>();
// Simulate what org.datanucleus.store.query.Query does
paramValues.put(new Integer(0), "ald");
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, paramValues, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterStringStartsWith.
/**
* Test of filter with String.startsWith().
*/
public void testFilterStringStartsWith() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName.startsWith('Mi')");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 2, results.size());
Iterator iter = results.iterator();
while (iter.hasNext()) {
Person p = (Person) iter.next();
if (p.getFirstName().equals("Mickey")) {
assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
} else if (p.getFirstName().equals("Minnie")) {
assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
} else {
fail("Incorrect Person found by query " + p.asString());
}
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterAndWithEqualityOperator.
/**
* Test of filter with AND and equality operator.
*/
public void testFilterAndWithEqualityOperator() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName == 'Mickey' && lastName == 'Mouse'");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Mickey", p.getFirstName());
assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations