InterMine Query Package
The InterMine database query system is implemented in the Java package org.intermine.objectstore.query. The main class of the package is Query which represents a query that can be passed to the ORMappingTool. The Java Query maps directly onto IQL. It may be useful to refer to our QueryExamples. In fact, by far the easiest method of creating a Java InterMine Query object is to use the following constructor:
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.iql.IqlQuery;
Query q = new IqlQuery("--- IQL Query text ---", null).toQuery();
However, in a Java program, one will occasionally require more flexibility - the Java interface is a more direct method of inserting parameters into the Query, rather than converting them into text and expecting the parser to convert back.
Structure
The Java Query object contains:
- a From list (which is actually a Set), accessed through Query.addFrom(), which can contain FromElement objects (which includes QueryClass, QueryClassBag or Query).
- a Select list, accessed through Query.addToSelect(), which can contain any QuerySelectable object (which includes QueryClass, QueryExpression, QueryField, QueryFunction, and QueryValue among others).
- an Order By list, accessed through Query.addToOrderBy(), which can contain any QueryOrderable.
- a Group By list (which is actually a Set), accessed through Query.addToGroupBy(), which can contain QueryNode objects. However, it does not make sense to include QueryValue objects directly, or QueryFunction objects at all.
- a reference to a single Constraint object, accessed through Query.setConstraint(), but multiple constraints can be included by encapsulating them in a ConstraintSet object.
An Example
This is an example of how one would build a Java InterMine Query object to represent the IQL query:
SELECT a.field1, a.field2 FROM a WHERE a.field3 = 'Hello';
The Query is built up in stages.
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.QueryField;
import org.intermine.objectstore.query.QueryValue;
import org.intermine.objectstore.query.SimpleConstraint;
Query q = new Query();
QueryClass c = new QueryClass(a.class);
QueryField f1 = new QueryField(c, "field1");
QueryField f2 = new QueryField(c, "field2");
QueryField f3 = new QueryField(c, "field3");
QueryValue v = new QueryValue("Hello");
SimpleConstraint sc = new SimpleConstraint(f3, SimpleConstraint.EQUALS, v);
q.addFrom(c);
q.addToSelect(f1);
q.addToSelect(f2);
q.setConstraint(sc);
q.setDistinct(false);
