VelocityToc

Last modified by Vincent Massol on 2009/09/08 14:08

HQL Query Examples in Velocity

XWiki allows user to access documents and objects with HQL queries in Velocity scripts.

Public API (searchDocuments)

Information
With this API the query consist in the WHERE condition. Any user with edit rights can use this API (as in write a script using it). Any user with view rights can view the result of such a query.

You can execute queries as follows:

Simple Query

qqq

Ordered Query

qqq

Advanced Query (date & time)

Since there is no standard way to calculate dates interval in HQL those queries are a bit unnatural.

qqq

Other examples, documents modified :

during current day : "where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 1) order by doc.date desc"
during current week : "where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 7) order by doc.date desc"
during current month : "where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc"

Privileged API (search : Documents, Objects, Properties, etc)

Warning
Calls to te privileged API are only executed when the calling page has been saved by an Admin. The reason is that search can be used to send any HQL command like update, delete, etc.

You can execute queries as follows:

Simple Query

qqq

Count Query

qqq


Simple Query with multiple fields

qqq


Getting objects of a specific class

qq


Getting objects’ properties

qqq


Getting documents where objects’ properties equals some value

qqqq

List users currently editing pages

qqq

List attachments of a page

qq

Non-exhaustive list of queryable object fields

XWikiDocument

  • XWikiDocument.fullName : full name, including space and page name. Example of value : Main.WebHome.
  • XWikiDocument.author : last editor. Example of value : XWiki.Admin.
  • XWikiDocument.creator : first editor. Example of value : XWiki.Admin.

BaseObject

  • BaseObject.id : arbitrary unique id of the object. Example of value : 123456789.
  • BaseObject.className : class. Example of value : XWiki.XWikiUsers.

*Property (StringProperty, etc)

  • Property.id.id : unique id of the object the property belongs to. Example of value : 123456789.
  • Property.name : name of the property. Example of value : first_name.
  • Property.value : value. Example of value : John.