Extensions Wiki » Extensions » Query Module

Query Module

Last modified by Thomas Mortagne on 2014/04/14 16:57
magnifierAllows querying the XWiki Model using a variety of query languages
TypeJAR
Developed by

XWiki Development Team

LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Enterprise, XWiki Enterprise Manager

Description

All data making up the XWiki application can be queried using one of the supported Query languages. Right now we support 2 languages:

Historically XWiki was only supporting HQL but that lead to complex queries in some instances and thus we've introduced XWQL which is a JPQL superset and which makes it much easier to write queries for the XWiki model, as you can see in the examples below.

The Query Module is only designed for READ-only queries. If you need to write something in the database, you should use the XWiki API.

Query Language Examples

All queries starting with select require programming rights to execute.
Query DescriptionXWQLHQL
Query listing all documents<empty query><empty query>
Query listing all documents created after a given datewhere doc.creationDate > '2008-01-01'where doc.creationDate > '2008-01-01'
Query listing all documents last updated by a given userwhere doc.author = 'XWiki.LudovicDubost'where doc.author = 'XWiki.LudovicDubost'
Query listing all documents containing XWiki Objects (XObject) of a given XWiki Class (XClass)from doc.object(XWiki.XWikiUsers) as user, BaseObject as obj where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers'
Query on XObjects and filtering on XObject property valuewhere doc.author = 'XWiki.LudovicDubost' and doc.object(XWiki.XWikiUsers).email like '%xwiki.com', BaseObject as obj, StringProperty as prop where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers' and obj.id=prop.id.id and prop.id.name='email' and prop.value like '%xwiki.com' and doc.author ='XWiki.LudovicDubost'
Query on 2 XObjectswhere doc.object(XWiki.XWikiUsers).email like '%xwiki.com'  and  doc.object(XWiki.ArticleClass).content like '%ludovic%', BaseObject as obj, StringProperty as prop, BaseObject as obj2, LargeStringProperty as contentprop where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers' and obj.id=prop.id.id and prop.id.name='email' and prop.value like '%xwiki.com' and doc.fullName=obj2.name and obj2.className='XWiki.ArticleClass' and obj2.id=contentprop.id.id and contentprop.id.name='content' and contentprop.value like '%ludovic%'
Search blogs per categorywhere doc.fullName <> 'XWiki.ArticleClassTemplate' and :category member of doc.object(XWiki.ArticleClass).category, BaseObject as obj, DBStringListProperty as prop join prop.list list where obj.name=doc.fullName and obj.className='XWiki.ArticleClass' and obj.name<>'XWiki.ArticleClassTemplate' and obj.id=prop.id.id and prop.id.name='category'  and list='${category}' order by doc.creationDate desc
List all tagsselect distinct obj.tags from Document doc, doc.object(XWiki.TagClass) as objselect distinct tag from BaseObject as obj, DBStringListProperty as prop join prop.list as tag where obj.className='XWiki.TagClass' and obj.id=prop.id.id and prop.id.name='tags'
List all blog posts, published and not hidden (filter by multiple properties of an object)from doc.object(Blog.BlogPostClass) as blog where blog.published = '1' and :category member of blog.category and blog.hidden = '0' and doc.web = 'Blog' order by blog.publishDate desc, BaseObject as obj, IntegerProperty publishedProp, IntegerProperty hiddenProp, DateProperty publishDateProp, DBStringListProperty as categoryProp join categoryProp.list list where obj.name=doc.fullName and obj.className='Blog.BlogPostClass' and doc.space = 'Blog' and obj.id=categoryProp.id.id and categoryProp.id.name='category' and list='$category' and publishedProp.id.id = obj.id and publishedProp.id.name = 'published' and publishedProp.value = '1' and hiddenProp.id.id = obj.id and hiddenProp.id.name='hidden' and hiddenProp.value = '0' and publishDateProp.id.id = obj.id and publishDateProp.id.name='publishDate' order by publishDateProp.value desc
List documents having at least tags tag1 and tag2from doc.object(XWiki.TagClass) as tag where  'tag1' member of tag.tags and  'tag2' member of tag.tagsTODO
Make a join between two objects that are not on the same document and are bound by a propertyfrom doc.object('Space.Class') as obj, Document doc1, doc1.object('Space.Class1') obj1 where obj.prop1 = doc1.fullName and obj1.prop2 like '%text%' order by obj1.prop3 TODO
Note that when using XWQL, it's mandatory to use aliases when filtering for multiple properties of an object; it's not possible to use the abbreviated syntax doc.object(Blog.BlogPostClass).propertyName multiple times in the same query. 

Performing Queries

The principle is to get a reference to the Query Manager, then call a method on it to create a Query for a given Query language. Then on the Query object you can set some behaviors (limit the number of returned result, bind variables, set the offset, set the wiki on which to execute the query) and then execute it.

For the moment, all the Query languages other than XWQL require programming rights to execute queries. 

From Velocity

For XWQL:

  • Return all the results matching the XWQL statement:
    $services.query.xwql("xwqlstatement").execute()
  • Return only 5 results at most:
    $services.query.xwql("xwqlstatement").setLimit(5).execute()
  • Set the starting offset (starts at result number 5 and beyond):
    $services.query.xwql("xwqlstatement").setOffset(5).execute()
  • Set the wiki on which to run the query (requires Programming Rights before XWiki 4.1):
    $services.query.xwql("xwqlstatement").setWiki("mywiki").execute()
  • You can add filters to apply to the query and multiple filters can be added to a query.
    • The hidden filter will exclude documents marked as hidden from the query results, except if the user has chosen to see hidden documents in his profile preferences:
      $services.query.xwql("xwqlstatement").addFilter("hidden").execute()
    • The unique filter will make sure you don't get duplicate results with a short-form query:
      $services.query.xwql("xwqlstatement").addFilter("unique").execute()
    • The count filter allows to transform a short-form query into a count of the distinct results it would return. Unfortunately this doesn't work with XWQL on versions older than 4.4.1:
      $services.query.xwql("xwqlstatement").addFilter("count").execute()
      ## You can also use the following shortcut
      $services.query.xwql("xwqlstatement").count()
    • The language filter will transform the query to also return document languages (the result you'll get is List<Object[]> with Object[0] being the document name and Object[1] being the document language, e.g. en, fr, fr_FR, etc): New in 5.1
      $services.query.xwql("xwqlstatement").addFilter("language").execute()
    • The currentlanguage filter will only return documents in the current language: New in 5.1
      $services.query.xwql("xwqlstatement").addFilter("currentlanguage").execute()
  • Bind a value:
    $services.query.xwql("xwqlstatement containing :var").bindValue("var", "value").execute()

    Specific example that will return all documents that have comments containing the test string:

    $services.query.xwql("select doc.fullName from Document doc, doc.object(XWiki.XWikiComments) com WHERE doc.translation = 0 and com.comment LIKE :value").bindValue("value", "%test%").execute()
  • Behaviors can be chained:
    $services.query.xwql("xwqlstatement").setWiki("mywiki").setOffset(2).setLimit(5).execute()

For HQL:

$services.query.hql("hqlstatement").execute()

The information above is valid starting with XWiki Platform 2.4M2. Before that you had to use the following which is now deprecated:

## For XWQL
$xwiki.queryManager.xwql("xwqlstatement").execute()
## For HQL
$xwiki.queryManager.createQuery("hqlstatement", "hql").execute()

From other Scripting languages

For XWQL (example with Groovy):

services.query.xwql("xwqlstatement").execute()

For HQL:

services.query.hql("hqlstatement").execute()

From Java components

Get a QueryManager injected. For example :

@Inject
private QueryManager queryManager;

Obtain and execute a query :

this.queryManager.createQuery(statement, Query.XWQL).execute();

XWQL Implementation Details

The XWQL grammar is defined here.

Note that currently the XWQL implementation translates queries into HQL which are then executed using Hibernate against the RDBMS.

XWiki-specific extensions in XWQL over JPQL

  • Short form queries:
    • where <expr> means select doc.fullName where <expr>
    • from <fromlist> [where <expr>] means select doc.fullName from Document as doc, <fromlist> [where <expr>]
  • Special syntax for XWiki objects in from and where clauses:
    • from doc.object(Class) as obj
    • where doc.object(Class).prop = 'something'

Note that any JPQL query is a correct XWQL query. This also means almost any HQL query (HQL is a JPQL superset) will work with XWQL (when executed on the XWiki Hibernate store).

Noteworthy differences between XWQL and HQL

DescriptionXWQLHQLDetails
 Like statement can't be transformed in XWQL  where upper(doc.name) like 'WEBHOME'  where upper(doc.name) like upper('webhome')  The string_expression must have a string value. The pattern_value is a string literal or a string-valued input parameter

Using the XWiki object (Deprecated)

This is now deprecated in XWiki Enterprise 2.4 in favor of the Query Manager but it's still supported and XWiki's code itself is still using this manner as we're gradually moving to using the Query Manager.

See the Velocity HQL Guide (till we incorporate it back in this guide).

Tags: development
Created by Vincent Massol on 2010/06/21 11:18

Download XWiki

Get Connected