import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
def query = midpoint.queryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > '2022-05-10')") (1)
def result = midpoint.searchObjects(query); (2)
Using MidPoint Query Language in Groovy
This page shows you how to work with midPoint Query Language (MQL) in Groovy expressions using the midpoint.queryFor
and midpoint.preparedQueryFor
functions.
Creating and Using Queries
1 | Creates TypedQuery for the specified object type (in this case UserType ). |
2 | Searches for objects using the query. |
Additional Parameters and APIs of Typed Queries
With TypedQuery API, you can specify ordering, paging and other options for the resulting query.
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.prism.path.ItemPath;
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > ?)");
def query = prepared.bind(basic.fromNow("-P1D"));
query.orderBy(ItemPath.create(UserType.F_ACTIVATION, ActivationType.F_ENABLE_TIMESTAMP), OrderDirection.DESCENDING) (1)
query.maxSize(10) (2)
def result = midpoint.searchObjects(query);
1 | Orders the result set by activation/enableTimestamp in descending order (newest first). |
2 | Limits the search results to 10 items. |
Prepared Queries with Placeholders
For a better support of queries in code, you can use placeholders. Placeholders can be bound to variables at a later stage, and midPoint will then be able to provide proper escaping and type checking.
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > ?)"); (1)
def query = prepared.bind(basic.fromNow("-P1D")); (2)
def result = midpoint.searchObjects(query); (3)
1 | Creates PreparedQuery for the specified object type (in this case UserType ). |
2 | Binds positional arguments to PreparedQuery using varargs. |
3 | Executes the search using the query. |
Named Placeholders
Positional, or anonymous, placeholders are suitable for simple queries.
However, for larger queries, you should consider using named placeholders.
Named placeholders are entered in the form of :name
, and their value can be set using the PreparedQuery.set(name, value)
method.
Before you can use your queries for searching, you need to build them using the build()
method.
This converts the prepared queries with placeholders to normal queries.
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = :status and enableTimestamp > :date)"); (1)
prepared.set("status", "enabled"); (2)
prepared.set("date", basic.fromNow("-P1D")); (3)
def query = prepared.build(); (4)
def result = midpoint.searchObjects(UserType.class, query); (5)
1 | Creates PreparedQuery for the specified object type (in this case UserType ). |
2 | Binds the status argument to the "enabled" value. |
3 | Binds the date argument to one day in the past (same time and hour). |
4 | Builds the query. |
5 | Executes the search using the query. |